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