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 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 private String securityPolicy = null;
74
75 private String serverCodebase = null;
76
77 private String serverClassName = null;
78
79 protected long pollingFrequency = 1000L;
80
81 private SecurityManager securityManager = new RMISecurityManager();
82
83 protected void doInitialise() throws InitialisationException
84 {
85
86 if (securityPolicy != null)
87 {
88 System.setProperty("java.security.policy", securityPolicy);
89 }
90
91
92 if (securityManager != null)
93 {
94 System.setSecurityManager(securityManager);
95 }
96
97 initJndiContext();
98 }
99
100 protected void doDispose()
101 {
102
103 }
104
105 protected void doConnect() throws Exception
106 {
107
108 }
109
110 protected void doDisconnect() throws Exception
111 {
112
113 }
114
115 protected void doStart() throws MuleException
116 {
117
118 }
119
120 protected void doStop() throws MuleException
121 {
122
123 }
124
125 public String getProtocol()
126 {
127 return RMI;
128 }
129
130
131
132
133 public String getSecurityPolicy()
134 {
135 return securityPolicy;
136 }
137
138
139
140
141 public void setSecurityPolicy(String path)
142 {
143
144 if (path != null)
145 {
146 URL url = IOUtils.getResourceAsUrl(path, RmiConnector.class);
147 if (url == null)
148 {
149 throw new IllegalArgumentException(
150 "Error on initialization, RMI security policy does not exist");
151 }
152 this.securityPolicy = url.toString();
153 }
154 }
155
156
157
158
159
160
161 public String getServerCodebase()
162 {
163 return (this.serverCodebase);
164 }
165
166
167
168
169
170
171 public void setServerCodebase(String serverCodebase)
172 {
173 this.serverCodebase = serverCodebase;
174 }
175
176
177
178
179
180
181 public String getServerClassName()
182 {
183 return (this.serverClassName);
184 }
185
186
187
188
189
190
191 public void setServerClassName(String serverClassName)
192 {
193 this.serverClassName = serverClassName;
194 }
195
196 public SecurityManager getSecurityManager()
197 {
198 return securityManager;
199 }
200
201 public void setSecurityManager(SecurityManager securityManager)
202 {
203 this.securityManager = securityManager;
204 }
205
206 public MessageReceiver createReceiver(Service service, InboundEndpoint endpoint) throws Exception
207 {
208 final Object[] args = new Object[]{new Long(pollingFrequency)};
209 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 EndpointURI endpointUri = event.getEndpoint().getEndpointURI();
227
228 String methodName = MapUtils.getString(endpointUri.getParams(), MuleProperties.MULE_METHOD_PROPERTY,
229 null);
230
231 if (null == methodName)
232 {
233 methodName = (String)event.getMessage().removeProperty(MuleProperties.MULE_METHOD_PROPERTY);
234
235 if (null == methodName)
236 {
237 throw new DispatchException(
238 RmiMessages.messageParamServiceMethodNotSet(),
239 event.getMessage(), event.getEndpoint());
240 }
241 }
242
243 Class[] argTypes;
244
245
246 Object args = event.getMessage().getProperty(RmiConnector.PROPERTY_SERVICE_METHOD_PARAM_TYPES);
247 if (args instanceof List)
248 {
249
250
251 argTypes = stringsToClasses((List) args);
252 }
253 else if (args instanceof String)
254 {
255 argTypes = stringsToClasses(Arrays.asList(((String) args).split(",")));
256 }
257 else
258 {
259 argTypes = ClassUtils.getClassTypes(event.transformMessage());
260 }
261
262 try
263 {
264 return remoteObject.getClass().getMethod(methodName, argTypes);
265 }
266 catch (NoSuchMethodException e)
267 {
268 throw new NoSuchMethodException(
269 CoreMessages.methodWithParamsNotFoundOnObject(methodName, ArrayUtils.toString(argTypes),
270 remoteObject.getClass()).toString());
271 }
272 catch (SecurityException e)
273 {
274 throw e;
275 }
276 }
277
278 protected Class[] stringsToClasses(Collection strings) throws ClassNotFoundException
279 {
280 Class[] classes = new Class[strings.size()];
281 int index = 0;
282 Iterator string = strings.iterator();
283 while (string.hasNext())
284 {
285 classes[index++] = ClassUtils.loadClass((String) string.next(), getClass());
286 }
287 return classes;
288 }
289
290 protected Object getRemoteRef(ImmutableEndpoint endpoint)
291 throws IOException, NotBoundException, NamingException, InitialisationException
292 {
293
294 EndpointURI endpointUri = endpoint.getEndpointURI();
295
296 String serviceName = endpointUri.getPath();
297 try
298 {
299
300 return getJndiContext().lookup(serviceName);
301 }
302 catch (NamingException e)
303 {
304
305 }
306
307 try
308 {
309 serviceName = serviceName.substring(1);
310 return getJndiContext().lookup(serviceName);
311 }
312 catch (NamingException e)
313 {
314
315 }
316
317 int port = endpointUri.getPort();
318 if (port < 1)
319 {
320 if (logger.isWarnEnabled())
321 {
322 logger.warn("RMI port not set on URI: " + endpointUri + ". Using default port: "
323 + RmiConnector.DEFAULT_RMI_muleRegistry_PORT);
324 }
325 port = RmiConnector.DEFAULT_RMI_muleRegistry_PORT;
326 }
327
328 InetAddress inetAddress = InetAddress.getByName(endpointUri.getHost());
329
330 return getJndiContext(inetAddress.getHostAddress() + ":" + port).lookup(serviceName);
331 }
332
333 public Remote getRemoteObject(ImmutableEndpoint endpoint)
334 throws IOException, NotBoundException, NamingException, InitialisationException
335 {
336 return (Remote)getRemoteRef(endpoint);
337 }
338
339 public long getPollingFrequency()
340 {
341 return pollingFrequency;
342 }
343
344 public void setPollingFrequency(long pollingFrequency)
345 {
346 this.pollingFrequency = pollingFrequency;
347 }
348
349 }