1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
package org.mule.transport.rmi; |
12 | |
|
13 | |
import org.mule.api.MuleEvent; |
14 | |
import org.mule.api.MuleException; |
15 | |
import org.mule.api.config.MuleProperties; |
16 | |
import org.mule.api.endpoint.EndpointURI; |
17 | |
import org.mule.api.endpoint.ImmutableEndpoint; |
18 | |
import org.mule.api.endpoint.InboundEndpoint; |
19 | |
import org.mule.api.lifecycle.InitialisationException; |
20 | |
import org.mule.api.service.Service; |
21 | |
import org.mule.api.transport.DispatchException; |
22 | |
import org.mule.api.transport.MessageReceiver; |
23 | |
import org.mule.config.i18n.CoreMessages; |
24 | |
import org.mule.transport.AbstractJndiConnector; |
25 | |
import org.mule.transport.rmi.i18n.RmiMessages; |
26 | |
import org.mule.util.ArrayUtils; |
27 | |
import org.mule.util.ClassUtils; |
28 | |
import org.mule.util.IOUtils; |
29 | |
|
30 | |
import java.io.IOException; |
31 | |
import java.lang.reflect.Method; |
32 | |
import java.net.InetAddress; |
33 | |
import java.net.URL; |
34 | |
import java.rmi.NotBoundException; |
35 | |
import java.rmi.RMISecurityManager; |
36 | |
import java.rmi.Remote; |
37 | |
import java.util.Arrays; |
38 | |
import java.util.Collection; |
39 | |
import java.util.Iterator; |
40 | |
import java.util.List; |
41 | |
|
42 | |
import javax.naming.NamingException; |
43 | |
|
44 | |
import org.apache.commons.collections.MapUtils; |
45 | |
|
46 | |
|
47 | |
|
48 | |
|
49 | 88 | public class RmiConnector extends AbstractJndiConnector |
50 | |
{ |
51 | |
|
52 | |
public static final String RMI = "rmi"; |
53 | |
public static final int DEFAULT_RMI_muleRegistry_PORT = 1099; |
54 | |
public static final String PROPERTY_RMI_SECURITY_POLICY = "securityPolicy"; |
55 | |
public static final String PROPERTY_RMI_SERVER_CODEBASE = "serverCodebase"; |
56 | |
public static final String PROPERTY_SERVER_CLASS_NAME = "serverClassName"; |
57 | |
|
58 | |
|
59 | |
|
60 | |
|
61 | |
|
62 | |
|
63 | |
|
64 | |
|
65 | |
public static final String PROPERTY_SERVICE_METHOD_PARAM_TYPES = "methodArgumentTypes"; |
66 | |
|
67 | |
|
68 | |
|
69 | |
|
70 | |
|
71 | |
public static final String PROPERTY_SERVICE_METHOD_PARAMS_LIST = "methodArgumentsList"; |
72 | |
|
73 | 88 | private String securityPolicy = null; |
74 | |
|
75 | 88 | private String serverCodebase = null; |
76 | |
|
77 | 88 | private String serverClassName = null; |
78 | |
|
79 | 88 | protected long pollingFrequency = 1000L; |
80 | |
|
81 | 88 | private SecurityManager securityManager = new RMISecurityManager(); |
82 | |
|
83 | |
protected void doInitialise() throws InitialisationException |
84 | |
{ |
85 | |
|
86 | 88 | if (securityPolicy != null) |
87 | |
{ |
88 | 50 | System.setProperty("java.security.policy", securityPolicy); |
89 | |
} |
90 | |
|
91 | |
|
92 | 88 | if (securityManager != null) |
93 | |
{ |
94 | 50 | System.setSecurityManager(securityManager); |
95 | |
} |
96 | |
|
97 | 88 | initJndiContext(); |
98 | 88 | } |
99 | |
|
100 | |
protected void doDispose() |
101 | |
{ |
102 | |
|
103 | 122 | } |
104 | |
|
105 | |
protected void doConnect() throws Exception |
106 | |
{ |
107 | |
|
108 | 88 | } |
109 | |
|
110 | |
protected void doDisconnect() throws Exception |
111 | |
{ |
112 | |
|
113 | 88 | } |
114 | |
|
115 | |
protected void doStart() throws MuleException |
116 | |
{ |
117 | |
|
118 | 88 | } |
119 | |
|
120 | |
protected void doStop() throws MuleException |
121 | |
{ |
122 | |
|
123 | 88 | } |
124 | |
|
125 | |
public String getProtocol() |
126 | |
{ |
127 | 156 | return RMI; |
128 | |
} |
129 | |
|
130 | |
|
131 | |
|
132 | |
|
133 | |
public String getSecurityPolicy() |
134 | |
{ |
135 | 54 | return securityPolicy; |
136 | |
} |
137 | |
|
138 | |
|
139 | |
|
140 | |
|
141 | |
public void setSecurityPolicy(String path) |
142 | |
{ |
143 | |
|
144 | 52 | if (path != null) |
145 | |
{ |
146 | 52 | URL url = IOUtils.getResourceAsUrl(path, RmiConnector.class); |
147 | 52 | if (url == null) |
148 | |
{ |
149 | 0 | throw new IllegalArgumentException( |
150 | |
"Error on initialization, RMI security policy does not exist"); |
151 | |
} |
152 | 52 | this.securityPolicy = url.toString(); |
153 | |
} |
154 | 52 | } |
155 | |
|
156 | |
|
157 | |
|
158 | |
|
159 | |
|
160 | |
|
161 | |
public String getServerCodebase() |
162 | |
{ |
163 | 6 | return (this.serverCodebase); |
164 | |
} |
165 | |
|
166 | |
|
167 | |
|
168 | |
|
169 | |
|
170 | |
|
171 | |
public void setServerCodebase(String serverCodebase) |
172 | |
{ |
173 | 10 | this.serverCodebase = serverCodebase; |
174 | 10 | } |
175 | |
|
176 | |
|
177 | |
|
178 | |
|
179 | |
|
180 | |
|
181 | |
public String getServerClassName() |
182 | |
{ |
183 | 4 | return (this.serverClassName); |
184 | |
} |
185 | |
|
186 | |
|
187 | |
|
188 | |
|
189 | |
|
190 | |
|
191 | |
public void setServerClassName(String serverClassName) |
192 | |
{ |
193 | 8 | this.serverClassName = serverClassName; |
194 | 8 | } |
195 | |
|
196 | |
public SecurityManager getSecurityManager() |
197 | |
{ |
198 | 4 | return securityManager; |
199 | |
} |
200 | |
|
201 | |
public void setSecurityManager(SecurityManager securityManager) |
202 | |
{ |
203 | 46 | this.securityManager = securityManager; |
204 | 46 | } |
205 | |
|
206 | |
public MessageReceiver createReceiver(Service service, InboundEndpoint endpoint) throws Exception |
207 | |
{ |
208 | 4 | final Object[] args = new Object[]{new Long(pollingFrequency)}; |
209 | 4 | return getServiceDescriptor().createMessageReceiver(this, service, endpoint, args); |
210 | |
} |
211 | |
|
212 | |
|
213 | |
|
214 | |
|
215 | |
|
216 | |
|
217 | |
|
218 | |
|
219 | |
|
220 | |
|
221 | |
|
222 | |
|
223 | |
public Method getMethodObject(Remote remoteObject, MuleEvent event) |
224 | |
throws MuleException, NoSuchMethodException, ClassNotFoundException |
225 | |
{ |
226 | 48 | EndpointURI endpointUri = event.getEndpoint().getEndpointURI(); |
227 | |
|
228 | 48 | String methodName = MapUtils.getString(endpointUri.getParams(), MuleProperties.MULE_METHOD_PROPERTY, |
229 | |
null); |
230 | |
|
231 | 48 | if (null == methodName) |
232 | |
{ |
233 | 22 | methodName = (String)event.getMessage().removeProperty(MuleProperties.MULE_METHOD_PROPERTY); |
234 | |
|
235 | 22 | if (null == methodName) |
236 | |
{ |
237 | 6 | throw new DispatchException( |
238 | |
RmiMessages.messageParamServiceMethodNotSet(), |
239 | |
event.getMessage(), event.getEndpoint()); |
240 | |
} |
241 | |
} |
242 | |
|
243 | |
Class[] argTypes; |
244 | |
|
245 | |
|
246 | 42 | Object args = event.getMessage().getProperty(RmiConnector.PROPERTY_SERVICE_METHOD_PARAM_TYPES); |
247 | 42 | if (args instanceof List) |
248 | |
{ |
249 | |
|
250 | |
|
251 | 0 | argTypes = stringsToClasses((List) args); |
252 | |
} |
253 | 42 | else if (args instanceof String) |
254 | |
{ |
255 | 12 | argTypes = stringsToClasses(Arrays.asList(((String) args).split(","))); |
256 | |
} |
257 | |
else |
258 | |
{ |
259 | 30 | argTypes = ClassUtils.getClassTypes(event.transformMessage()); |
260 | |
} |
261 | |
|
262 | |
try |
263 | |
{ |
264 | 42 | return remoteObject.getClass().getMethod(methodName, argTypes); |
265 | |
} |
266 | 12 | catch (NoSuchMethodException e) |
267 | |
{ |
268 | 12 | throw new NoSuchMethodException( |
269 | |
CoreMessages.methodWithParamsNotFoundOnObject(methodName, ArrayUtils.toString(argTypes), |
270 | |
remoteObject.getClass()).toString()); |
271 | |
} |
272 | 0 | catch (SecurityException e) |
273 | |
{ |
274 | 0 | throw e; |
275 | |
} |
276 | |
} |
277 | |
|
278 | |
protected Class[] stringsToClasses(Collection strings) throws ClassNotFoundException |
279 | |
{ |
280 | 12 | Class[] classes = new Class[strings.size()]; |
281 | 12 | int index = 0; |
282 | 12 | Iterator string = strings.iterator(); |
283 | 24 | while (string.hasNext()) |
284 | |
{ |
285 | 12 | classes[index++] = ClassUtils.loadClass((String) string.next(), getClass()); |
286 | |
} |
287 | 12 | return classes; |
288 | |
} |
289 | |
|
290 | |
protected Object getRemoteRef(ImmutableEndpoint endpoint) |
291 | |
throws IOException, NotBoundException, NamingException, InitialisationException |
292 | |
{ |
293 | |
|
294 | 48 | EndpointURI endpointUri = endpoint.getEndpointURI(); |
295 | |
|
296 | 48 | String serviceName = endpointUri.getPath(); |
297 | |
try |
298 | |
{ |
299 | |
|
300 | 48 | return getJndiContext().lookup(serviceName); |
301 | |
} |
302 | 48 | catch (NamingException e) |
303 | |
{ |
304 | |
|
305 | |
} |
306 | |
|
307 | |
try |
308 | |
{ |
309 | 48 | serviceName = serviceName.substring(1); |
310 | 48 | return getJndiContext().lookup(serviceName); |
311 | |
} |
312 | 0 | catch (NamingException e) |
313 | |
{ |
314 | |
|
315 | |
} |
316 | |
|
317 | 0 | int port = endpointUri.getPort(); |
318 | 0 | if (port < 1) |
319 | |
{ |
320 | 0 | if (logger.isWarnEnabled()) |
321 | |
{ |
322 | 0 | logger.warn("RMI port not set on URI: " + endpointUri + ". Using default port: " |
323 | |
+ RmiConnector.DEFAULT_RMI_muleRegistry_PORT); |
324 | |
} |
325 | 0 | port = RmiConnector.DEFAULT_RMI_muleRegistry_PORT; |
326 | |
} |
327 | |
|
328 | 0 | InetAddress inetAddress = InetAddress.getByName(endpointUri.getHost()); |
329 | |
|
330 | 0 | return getJndiContext(inetAddress.getHostAddress() + ":" + port).lookup(serviceName); |
331 | |
} |
332 | |
|
333 | |
public Remote getRemoteObject(ImmutableEndpoint endpoint) |
334 | |
throws IOException, NotBoundException, NamingException, InitialisationException |
335 | |
{ |
336 | 48 | return (Remote)getRemoteRef(endpoint); |
337 | |
} |
338 | |
|
339 | |
public long getPollingFrequency() |
340 | |
{ |
341 | 4 | return pollingFrequency; |
342 | |
} |
343 | |
|
344 | |
public void setPollingFrequency(long pollingFrequency) |
345 | |
{ |
346 | 8 | this.pollingFrequency = pollingFrequency; |
347 | 8 | } |
348 | |
|
349 | |
} |