1
2
3
4
5
6
7
8
9
10
11 package org.mule.transport.rmi;
12
13 import org.mule.DefaultMuleMessage;
14 import org.mule.api.config.MuleProperties;
15 import org.mule.api.endpoint.InboundEndpoint;
16 import org.mule.api.lifecycle.CreateException;
17 import org.mule.api.service.Service;
18 import org.mule.api.transport.Connector;
19 import org.mule.transport.AbstractPollingMessageReceiver;
20 import org.mule.transport.ConnectException;
21 import org.mule.transport.rmi.i18n.RmiMessages;
22 import org.mule.util.ClassUtils;
23
24 import java.lang.reflect.Method;
25 import java.rmi.RMISecurityManager;
26 import java.rmi.Remote;
27 import java.util.List;
28
29 import org.apache.commons.collections.MapUtils;
30
31
32
33
34
35
36
37
38 public class RmiMessageReceiver extends AbstractPollingMessageReceiver
39 {
40 protected RmiConnector connector;
41
42 protected Remote remoteObject;
43
44 protected Method invokeMethod;
45
46 protected Object[] methodArguments = null;
47
48 public RmiMessageReceiver(Connector connector,
49 Service service,
50 InboundEndpoint endpoint,
51 long frequency) throws CreateException
52 {
53 super(connector, service, endpoint);
54 this.setFrequency(frequency);
55 this.connector = (RmiConnector) connector;
56 }
57
58 protected void doDispose()
59 {
60
61 }
62
63 protected void doConnect() throws Exception
64 {
65 System.setProperty("java.security.policy", connector.getSecurityPolicy());
66
67
68 if (System.getSecurityManager() == null)
69 {
70 System.setSecurityManager(new RMISecurityManager());
71 }
72
73 String methodName = MapUtils.getString(endpoint.getEndpointURI().getParams(),
74 MuleProperties.MULE_METHOD_PROPERTY, null);
75
76 if (null == methodName)
77 {
78 methodName = (String) endpoint.getProperty(MuleProperties.MULE_METHOD_PROPERTY);
79
80 if (null == methodName)
81 {
82 throw new ConnectException(RmiMessages.messageParamServiceMethodNotSet(), this);
83 }
84 }
85
86 remoteObject = connector.getRemoteObject(getEndpoint());
87
88 List args = (List) endpoint.getProperty(RmiConnector.PROPERTY_SERVICE_METHOD_PARAMS_LIST);
89
90 Class[] argTypes = new Class[]{};
91
92 if (args == null)
93 {
94 logger.info(RmiConnector.PROPERTY_SERVICE_METHOD_PARAMS_LIST
95 + " not set on endpoint, assuming method call has no arguments");
96 methodArguments = ClassUtils.NO_ARGS;
97 }
98 else
99 {
100 argTypes = ClassUtils.getClassTypes(methodArguments);
101 methodArguments = new Object[args.size()];
102 methodArguments = args.toArray(methodArguments);
103 }
104 invokeMethod = remoteObject.getClass().getMethod(methodName, argTypes);
105 }
106
107 protected void doDisconnect()
108 {
109 invokeMethod = null;
110 remoteObject = null;
111 }
112
113 public void poll()
114 {
115 try
116 {
117 Object result = invokeMethod.invoke(remoteObject, getMethodArguments());
118
119 if (null != result)
120 {
121 final Object payload = connector.getMessageAdapter(result).getPayload();
122 routeMessage(new DefaultMuleMessage(payload), endpoint.isSynchronous());
123 }
124 }
125 catch (Exception e)
126 {
127 handleException(e);
128 }
129 }
130
131
132
133
134
135
136
137 protected Object[] getMethodArguments()
138 {
139 return methodArguments;
140 }
141 }