1 10 11 package org.mule.providers.rmi; 12 13 import java.lang.reflect.Method ; 14 import java.rmi.RMISecurityManager ; 15 import java.rmi.Remote ; 16 import java.util.Collections ; 17 18 import org.apache.commons.logging.Log; 19 import org.apache.commons.logging.LogFactory; 20 import org.mule.impl.MuleMessage; 21 import org.mule.providers.AbstractMessageDispatcher; 22 import org.mule.umo.UMOEvent; 23 import org.mule.umo.UMOException; 24 import org.mule.umo.UMOMessage; 25 import org.mule.umo.endpoint.UMOImmutableEndpoint; 26 import org.mule.umo.provider.UMOConnector; 27 import org.mule.umo.transformer.TransformerException; 28 29 33 public class RmiMessageDispatcher extends AbstractMessageDispatcher 34 { 35 protected static Log logger = LogFactory.getLog(RmiMessageDispatcher.class); 36 37 private final RmiConnector connector; 38 protected volatile Remote remoteObject; 39 protected volatile Method invokedMethod; 40 41 public RmiMessageDispatcher(UMOImmutableEndpoint endpoint) 42 { 43 super(endpoint); 44 this.connector = (RmiConnector)endpoint.getConnector(); 45 } 46 47 protected void doConnect(UMOImmutableEndpoint endpoint) throws Exception 48 { 49 if (remoteObject == null) 50 { 51 String rmiPolicyPath = connector.getSecurityPolicy(); 52 53 System.setProperty("java.security.policy", rmiPolicyPath); 54 55 if (System.getSecurityManager() == null) 57 { 58 System.setSecurityManager(new RMISecurityManager ()); 59 } 60 61 remoteObject = connector.getRemoteObject(endpoint); 62 } 63 } 64 65 protected void doDisconnect() throws Exception 66 { 67 remoteObject = null; 68 invokedMethod = null; 69 } 70 71 private Object [] getArgs(UMOEvent event) throws TransformerException 72 { 73 Object payload = event.getTransformedMessage(); 74 if (payload instanceof Object []) 75 { 76 return (Object [])payload; 77 } 78 else 79 { 80 return new Object []{payload}; 81 } 82 } 83 84 89 protected void doDispatch(UMOEvent event) throws Exception 90 { 91 Object [] arguments = getArgs(event); 92 if (invokedMethod == null) 93 { 94 invokedMethod = connector.getMethodObject(remoteObject, event); 95 } 96 invokedMethod.invoke(remoteObject, arguments); 97 } 98 99 104 public UMOMessage doSend(UMOEvent event) throws Exception 105 { 106 if (invokedMethod == null) 107 { 108 invokedMethod = connector.getMethodObject(remoteObject, event); 109 } 110 111 Object [] arguments = getArgs(event); 112 Object result = invokedMethod.invoke(remoteObject, arguments); 113 114 if (result == null) 115 { 116 return null; 117 } 118 else 119 { 120 return new MuleMessage(connector.getMessageAdapter(result).getPayload(), Collections.EMPTY_MAP); 121 } 122 } 123 124 136 protected UMOMessage doReceive(UMOImmutableEndpoint endpoint, long timeout) throws Exception 137 { 138 throw new UnsupportedOperationException ("doReceive"); 139 } 140 141 147 public Object getDelegateSession() throws UMOException 148 { 149 return null; 150 } 151 152 157 public UMOConnector getConnector() 158 { 159 return connector; 160 } 161 162 protected void doDispose() 163 { 164 } 166 } 167 | Popular Tags |