1 10 11 package org.mule.providers.rmi; 12 13 import java.io.IOException ; 14 import java.lang.reflect.Method ; 15 import java.net.InetAddress ; 16 import java.net.URL ; 17 import java.rmi.NotBoundException ; 18 import java.rmi.RMISecurityManager ; 19 import java.rmi.Remote ; 20 import java.util.List ; 21 22 import javax.naming.NamingException ; 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 45 public class RmiConnector extends AbstractJndiConnector 46 { 47 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 PROPERTY_RMI_SECURITY_POLICY = "securityPolicy"; 56 57 public static final String PROPERTY_RMI_SERVER_CODEBASE = "serverCodebase"; 58 59 public static final String PROPERTY_SERVER_CLASS_NAME = "serverClassName"; 60 61 68 public static final String PROPERTY_SERVICE_METHOD_PARAM_TYPES = "methodArgumentTypes"; 69 70 74 public static final String PROPERTY_SERVICE_METHOD_PARAMS_LIST = "methodArgumentsList"; 75 76 private String securityPolicy = null; 77 78 private String serverCodebase = null; 79 80 private String serverClassName = null; 81 82 protected long pollingFrequency = 1000L; 83 84 private SecurityManager securityManager = new RMISecurityManager (); 85 86 public String getProtocol() 87 { 88 return "rmi"; 89 } 90 91 94 public String getSecurityPolicy() 95 { 96 return securityPolicy; 97 } 98 99 102 public void setSecurityPolicy(String path) 103 { 104 if (path != null) 106 { 107 URL url = IOUtils.getResourceAsUrl(path, RmiConnector.class); 108 if (url == null) 109 { 110 throw new IllegalArgumentException ( 111 "Error on initialization, RMI security policy does not exist"); 112 } 113 this.securityPolicy = url.toString(); 114 } 115 } 116 117 122 public String getServerCodebase() 123 { 124 return (this.serverCodebase); 125 } 126 127 132 public void setServerCodebase(String serverCodebase) 133 { 134 this.serverCodebase = serverCodebase; 135 } 136 137 142 public String getServerClassName() 143 { 144 return (this.serverClassName); 145 } 146 147 152 public void setServerClassName(String 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 if (securityManager != null) 168 { 169 System.setSecurityManager(securityManager); 170 } 171 initJndiContext(); 172 } 173 174 public SecurityManager getSecurityManager() 175 { 176 return securityManager; 177 } 178 179 public void setSecurityManager(SecurityManager securityManager) 180 { 181 this.securityManager = securityManager; 182 } 183 184 public UMOMessageReceiver createReceiver(UMOComponent component, UMOEndpoint endpoint) throws Exception 185 { 186 final Object [] args = new Object []{new Long (pollingFrequency)}; 187 return getServiceDescriptor().createMessageReceiver(this, component, endpoint, args); 188 } 189 190 201 public Method getMethodObject(Remote remoteObject, UMOEvent event) 202 throws UMOException, NoSuchMethodException , ClassNotFoundException 203 { 204 UMOEndpointURI endpointUri = event.getEndpoint().getEndpointURI(); 205 206 String methodName = MapUtils.getString(endpointUri.getParams(), MuleProperties.MULE_METHOD_PROPERTY, 207 null); 208 209 if (null == methodName) 210 { 211 methodName = (String )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 [] argTypes; 221 222 224 Object args = event.getMessage().getProperty(RmiConnector.PROPERTY_SERVICE_METHOD_PARAM_TYPES); 225 226 String argumentString = null; 227 228 if (args instanceof List ) 229 { 230 List arguments = (List ) args; 231 argumentString = (String ) arguments.get(0); 232 } 233 else if(args instanceof String ) 234 { 235 argumentString = (String )args; 236 } 237 238 if (null != argumentString) 239 { 240 String [] split = argumentString.split(","); 241 242 argTypes = new Class [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 e) 259 { 260 throw new NoSuchMethodException (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 e) 264 { 265 throw e; 266 } 267 } 268 269 protected Object getRemoteRef(UMOImmutableEndpoint endpoint) 270 throws IOException , NotBoundException , NamingException , InitialisationException 271 { 272 273 UMOEndpointURI endpointUri = endpoint.getEndpointURI(); 274 275 String serviceName = endpointUri.getPath(); 276 try 277 { 278 return getJndiContext().lookup(serviceName); 280 } 281 catch (NamingException e) 282 { 283 } 285 286 try 287 { 288 serviceName = serviceName.substring(1); 289 return getJndiContext().lookup(serviceName); 290 } 291 catch (NamingException e) 292 { 293 } 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 inetAddress = InetAddress.getByName(endpointUri.getHost()); 308 309 return getJndiContext(inetAddress.getHostAddress() + ":" + port).lookup(serviceName); 310 } 311 312 public Remote getRemoteObject(UMOImmutableEndpoint endpoint) 313 throws IOException , NotBoundException , NamingException , InitialisationException 314 { 315 return (Remote )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 |