KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mule > providers > rmi > RmiConnector


1 /*
2  * $Id: RmiConnector.java 3798 2006-11-04 04:07:14Z aperepel $
3  * --------------------------------------------------------------------------------------
4  * Copyright (c) MuleSource, Inc. All rights reserved. http://www.mulesource.com
5  *
6  * The software in this package is published under the terms of the MuleSource MPL
7  * license, a copy of which has been included with this distribution in the
8  * LICENSE.txt file.
9  */

10
11 package org.mule.providers.rmi;
12
13 import java.io.IOException JavaDoc;
14 import java.lang.reflect.Method JavaDoc;
15 import java.net.InetAddress JavaDoc;
16 import java.net.URL JavaDoc;
17 import java.rmi.NotBoundException JavaDoc;
18 import java.rmi.RMISecurityManager JavaDoc;
19 import java.rmi.Remote JavaDoc;
20 import java.util.List JavaDoc;
21
22 import javax.naming.NamingException JavaDoc;
23
24 import org.apache.commons.collections.MapUtils;
25 import org.mule.config.MuleProperties;
26 import org.mule.config.i18n.Message;
27 import org.mule.config.i18n.Messages;
28 import org.mule.providers.AbstractJndiConnector;
29 import org.mule.umo.UMOComponent;
30 import org.mule.umo.UMOEvent;
31 import org.mule.umo.UMOException;
32 import org.mule.umo.endpoint.UMOEndpoint;
33 import org.mule.umo.endpoint.UMOEndpointURI;
34 import org.mule.umo.endpoint.UMOImmutableEndpoint;
35 import org.mule.umo.lifecycle.InitialisationException;
36 import org.mule.umo.provider.DispatchException;
37 import org.mule.umo.provider.UMOMessageReceiver;
38 import org.mule.util.ArrayUtils;
39 import org.mule.util.ClassUtils;
40 import org.mule.util.IOUtils;
41
42 /**
43  * <code>RmiConnector</code> can bind or send to a given RMI port on a given host.
44  */

45 public class RmiConnector extends AbstractJndiConnector
46 {
47     // Messages
48
public static final int MSG_PARAM_SERVICE_METHOD_NOT_SET = 1;
49     public static final int MSG_PROPERTY_SERVICE_METHOD_PARAM_TYPES_NOT_SET = 2;
50     public static final int NO_RMI_SERVICECLASS_SET = 10;
51     public static final int RMI_SERVICECLASS_INVOCATION_FAILED = 11;
52
53     public static final int DEFAULT_RMI_REGISTRY_PORT = 1099;
54
55     public static final String JavaDoc PROPERTY_RMI_SECURITY_POLICY = "securityPolicy";
56
57     public static final String JavaDoc PROPERTY_RMI_SERVER_CODEBASE = "serverCodebase";
58
59     public static final String JavaDoc PROPERTY_SERVER_CLASS_NAME = "serverClassName";
60
61     /**
62      * The property name that explicitly defines which argument types should be
63      * passed to a remote object method invocation. This is a comma-separate list for
64      * fully qualified classnames. If this property is not set on an outbound
65      * endpoint, the argument types will be determined automatically from the payload
66      * of the current message
67      */

68     public static final String JavaDoc PROPERTY_SERVICE_METHOD_PARAM_TYPES = "methodArgumentTypes";
69
70     /**
71      * The property name for a list of objects used to call a Remote object via an
72      * RMI or EJB MessageReceiver
73      */

74     public static final String JavaDoc PROPERTY_SERVICE_METHOD_PARAMS_LIST = "methodArgumentsList";
75
76     private String JavaDoc securityPolicy = null;
77
78     private String JavaDoc serverCodebase = null;
79
80     private String JavaDoc serverClassName = null;
81
82     protected long pollingFrequency = 1000L;
83
84     private SecurityManager JavaDoc securityManager = new RMISecurityManager JavaDoc();
85
86     public String JavaDoc getProtocol()
87     {
88         return "rmi";
89     }
90
91     /**
92      * @return Returns the securityPolicy.
93      */

94     public String JavaDoc getSecurityPolicy()
95     {
96         return securityPolicy;
97     }
98
99     /**
100      * @param path The securityPolicy to set.
101      */

102     public void setSecurityPolicy(String JavaDoc path)
103     {
104         // verify securityPolicy existence
105
if (path != null)
106         {
107             URL JavaDoc url = IOUtils.getResourceAsUrl(path, RmiConnector.class);
108             if (url == null)
109             {
110                 throw new IllegalArgumentException JavaDoc(
111                     "Error on initialization, RMI security policy does not exist");
112             }
113             this.securityPolicy = url.toString();
114         }
115     }
116
117     /**
118      * Method getServerCodebase
119      *
120      * @return
121      */

122     public String JavaDoc getServerCodebase()
123     {
124         return (this.serverCodebase);
125     }
126
127     /**
128      * Method setServerCodebase
129      *
130      * @param serverCodebase
131      */

132     public void setServerCodebase(String JavaDoc serverCodebase)
133     {
134         this.serverCodebase = serverCodebase;
135     }
136
137     /**
138      * Method getServerClassName
139      *
140      * @return
141      */

142     public String JavaDoc getServerClassName()
143     {
144         return (this.serverClassName);
145     }
146
147     /**
148      * Method setServerClassName
149      *
150      * @param serverClassName
151      */

152     public void setServerClassName(String JavaDoc serverClassName)
153     {
154         this.serverClassName = serverClassName;
155     }
156
157     public void doInitialise() throws InitialisationException
158     {
159         super.doInitialise();
160
161         if (securityPolicy != null)
162         {
163             System.setProperty("java.security.policy", securityPolicy);
164         }
165
166         // Set security manager
167
if (securityManager != null)
168         {
169             System.setSecurityManager(securityManager);
170         }
171         initJndiContext();
172     }
173
174     public SecurityManager JavaDoc getSecurityManager()
175     {
176         return securityManager;
177     }
178
179     public void setSecurityManager(SecurityManager JavaDoc securityManager)
180     {
181         this.securityManager = securityManager;
182     }
183
184     public UMOMessageReceiver createReceiver(UMOComponent component, UMOEndpoint endpoint) throws Exception JavaDoc
185     {
186         final Object JavaDoc[] args = new Object JavaDoc[]{new Long JavaDoc(pollingFrequency)};
187         return getServiceDescriptor().createMessageReceiver(this, component, endpoint, args);
188     }
189
190     /**
191      * Helper method for Dispatchers and Receives to extract the correct method from
192      * a Remote object
193      *
194      * @param remoteObject The remote object on which to invoke the method
195      * @param event The current event being processed
196      * @return
197      * @throws org.mule.umo.UMOException
198      * @throws NoSuchMethodException
199      * @throws ClassNotFoundException
200      */

201     public Method JavaDoc getMethodObject(Remote JavaDoc remoteObject, UMOEvent event)
202         throws UMOException, NoSuchMethodException JavaDoc, ClassNotFoundException JavaDoc
203     {
204         UMOEndpointURI endpointUri = event.getEndpoint().getEndpointURI();
205
206         String JavaDoc methodName = MapUtils.getString(endpointUri.getParams(), MuleProperties.MULE_METHOD_PROPERTY,
207             null);
208
209         if (null == methodName)
210         {
211             methodName = (String JavaDoc)event.getMessage().removeProperty(MuleProperties.MULE_METHOD_PROPERTY);
212
213             if (null == methodName)
214             {
215                 throw new DispatchException(new org.mule.config.i18n.Message("rmi",
216                     RmiConnector.MSG_PARAM_SERVICE_METHOD_NOT_SET), event.getMessage(), event.getEndpoint());
217             }
218         }
219
220         Class JavaDoc[] argTypes;
221
222         // Parse method args
223

224         Object JavaDoc args = event.getMessage().getProperty(RmiConnector.PROPERTY_SERVICE_METHOD_PARAM_TYPES);
225
226         String JavaDoc argumentString = null;
227
228         if (args instanceof List JavaDoc)
229         {
230             List JavaDoc arguments = (List JavaDoc) args;
231             argumentString = (String JavaDoc) arguments.get(0);
232         }
233         else if(args instanceof String JavaDoc)
234         {
235             argumentString = (String JavaDoc)args;
236         }
237
238         if (null != argumentString)
239         {
240             String JavaDoc[] split = argumentString.split(",");
241
242             argTypes = new Class JavaDoc[split.length];
243             for (int i = 0; i < split.length; i++)
244             {
245                 argTypes[i] = ClassUtils.loadClass(split[i].trim(), getClass());
246
247             }
248         }
249         else
250         {
251             argTypes = ClassUtils.getClassTypes(event.getTransformedMessage());
252         }
253
254         try
255         {
256             return remoteObject.getClass().getMethod(methodName, argTypes);
257         }
258         catch (NoSuchMethodException JavaDoc e)
259         {
260             throw new NoSuchMethodException JavaDoc(new Message(Messages.METHOD_X_WITH_PARAMS_X_NOT_FOUND_ON_X,
261                 methodName, ArrayUtils.toString(argTypes), remoteObject.getClass().getName()).toString());
262         }
263         catch (SecurityException JavaDoc e)
264         {
265             throw e;
266         }
267     }
268
269     protected Object JavaDoc getRemoteRef(UMOImmutableEndpoint endpoint)
270         throws IOException JavaDoc, NotBoundException JavaDoc, NamingException JavaDoc, InitialisationException
271     {
272
273         UMOEndpointURI endpointUri = endpoint.getEndpointURI();
274
275         String JavaDoc serviceName = endpointUri.getPath();
276         try
277         {
278             // Test if we can find the object locally
279
return getJndiContext().lookup(serviceName);
280         }
281         catch (NamingException JavaDoc e)
282         {
283             // Strip path seperator
284
}
285
286         try
287         {
288             serviceName = serviceName.substring(1);
289             return getJndiContext().lookup(serviceName);
290         }
291         catch (NamingException JavaDoc e)
292         {
293             // Try with full host and path
294
}
295
296         int port = endpointUri.getPort();
297         if (port < 1)
298         {
299             if (logger.isWarnEnabled())
300             {
301                 logger.warn("RMI port not set on URI: " + endpointUri + ". Using default port: "
302                             + RmiConnector.DEFAULT_RMI_REGISTRY_PORT);
303             }
304             port = RmiConnector.DEFAULT_RMI_REGISTRY_PORT;
305         }
306
307         InetAddress JavaDoc inetAddress = InetAddress.getByName(endpointUri.getHost());
308
309         return getJndiContext(inetAddress.getHostAddress() + ":" + port).lookup(serviceName);
310     }
311
312     public Remote JavaDoc getRemoteObject(UMOImmutableEndpoint endpoint)
313         throws IOException JavaDoc, NotBoundException JavaDoc, NamingException JavaDoc, InitialisationException
314     {
315         return (Remote JavaDoc)getRemoteRef(endpoint);
316     }
317
318     public long getPollingFrequency()
319     {
320         return pollingFrequency;
321     }
322
323     public void setPollingFrequency(long pollingFrequency)
324     {
325         this.pollingFrequency = pollingFrequency;
326     }
327
328 }
329
Popular Tags