1 10 11 package org.mule.providers.rmi; 12 13 import org.mule.providers.AbstractMessageReceiver; 14 import org.mule.providers.ConnectException; 15 import org.mule.umo.provider.UMOConnector; 16 import org.mule.umo.provider.UMOMessageAdapter; 17 import org.mule.umo.UMOComponent; 18 import org.mule.umo.MessagingException; 19 import org.mule.umo.UMOException; 20 import org.mule.umo.lifecycle.InitialisationException; 21 import org.mule.umo.endpoint.UMOEndpoint; 22 import org.mule.umo.endpoint.UMOEndpointURI; 23 import org.mule.impl.MuleMessage; 24 25 import javax.naming.Context ; 26 import java.net.InetAddress ; 27 import java.lang.reflect.Method ; 28 29 32 33 public class RmiCallbackMessageReceiver extends AbstractMessageReceiver 34 { 35 39 public static final String PROPERTY_SERVICE_CLASS_NAME = "serviceClassName"; 40 41 protected RmiConnector connector; 42 43 protected RmiAble remoteObject = null; 44 45 private Context jndi = null; 46 47 private String bindName = null; 48 49 public RmiCallbackMessageReceiver(UMOConnector connector, UMOComponent component, UMOEndpoint endpoint) 50 throws InitialisationException 51 { 52 super(connector, component, endpoint); 53 54 this.connector = (RmiConnector)connector; 55 } 56 57 64 private void initialize(UMOEndpoint endpoint) throws InitialisationException 65 { 66 logger.debug("Initializing with endpoint " + endpoint); 67 68 String rmiPolicyPath = connector.getSecurityPolicy(); 69 70 System.setProperty("java.security.policy", rmiPolicyPath); 71 72 UMOEndpointURI endpointUri = endpoint.getEndpointURI(); 73 74 int port = endpointUri.getPort(); 75 76 if (port < 1) 77 { 78 port = RmiConnector.DEFAULT_RMI_REGISTRY_PORT; 79 } 80 81 try 82 { 83 InetAddress inetAddress = InetAddress.getByName(endpointUri.getHost()); 84 85 bindName = endpointUri.getPath(); 86 87 remoteObject = getRmiObject(); 88 89 Method theMethod = remoteObject.getClass().getMethod("setReceiver", 90 new Class []{RmiCallbackMessageReceiver.class}); 91 theMethod.invoke(remoteObject, new Object []{this}); 92 93 jndi = connector.getJndiContext(inetAddress.getHostAddress() + ":" + port); 94 95 jndi.rebind(bindName, remoteObject); 96 } 97 catch (Exception e) 98 { 99 throw new InitialisationException(e, this); 100 } 101 102 logger.debug("Initialized successfully"); 103 } 104 105 110 public void doConnect() throws ConnectException 111 { 112 try 113 { 114 118 if (null == remoteObject) 121 { 122 initialize(getEndpoint()); 123 } 124 } 125 catch (Exception e) 126 { 127 throw new ConnectException(e, this); 128 } 129 } 130 131 134 public void doDisconnect() 135 { 136 logger.debug("Disconnecting..."); 137 138 try 139 { 140 jndi.unbind(bindName); 141 } 142 catch (Exception e) 143 { 144 logger.error(e); 145 } 146 147 logger.debug("Disconnected successfully."); 148 } 149 150 156 private RmiAble getRmiObject() throws InitialisationException 157 { 158 String className = (String )endpoint.getProperty(PROPERTY_SERVICE_CLASS_NAME); 159 160 if (null == className) 161 { 162 throw new InitialisationException(new org.mule.config.i18n.Message("rmi", 163 RmiConnector.NO_RMI_SERVICECLASS_SET), this); 164 } 165 166 RmiAble remote = null; 167 168 try 169 { 170 remote = (RmiAble)Class.forName(className).newInstance(); 171 } 172 catch (Exception e) 173 { 174 throw new InitialisationException(new org.mule.config.i18n.Message("rmi", 175 RmiConnector.RMI_SERVICECLASS_INVOCATION_FAILED), e); 176 } 177 178 return (remote); 179 } 180 181 189 public Object routeMessage(Object message) throws MessagingException, UMOException 190 { 191 UMOMessageAdapter adapter = connector.getMessageAdapter(message); 192 193 return (routeMessage(new MuleMessage(adapter))); 194 } 195 } 196 | Popular Tags |