1 9 package org.jboss.remoting.transport.rmi; 10 11 import java.io.IOException ; 12 import java.rmi.Remote ; 13 import java.rmi.RemoteException ; 14 import java.rmi.registry.LocateRegistry ; 15 import java.rmi.registry.Registry ; 16 import java.util.Map ; 17 import org.jboss.remoting.CannotConnectException; 18 import org.jboss.remoting.ConnectionFailedException; 19 import org.jboss.remoting.InvokerLocator; 20 import org.jboss.remoting.RemoteClientInvoker; 21 import org.jboss.remoting.marshal.Marshaller; 22 import org.jboss.remoting.marshal.UnMarshaller; 23 import org.jboss.remoting.marshal.serializable.SerializableMarshaller; 24 25 32 public class RMIClientInvoker extends RemoteClientInvoker 33 { 34 private RMIServerInvokerInf server; 35 36 43 private boolean connected = false; 44 45 public RMIClientInvoker(InvokerLocator locator) 46 { 47 super(locator); 48 } 49 50 private int getRegistryPort(InvokerLocator locator) 51 { 52 int port = RMIServerInvoker.DEFAULT_REGISTRY_PORT; 53 54 Map params = locator.getParameters(); 56 if(params != null) 57 { 58 String value = (String ) params.get(RMIServerInvoker.REGISTRY_PORT_KEY); 59 if(value != null) 60 { 61 try 62 { 63 port = Integer.parseInt(value); 64 log.debug("Using port " + port + " for rmi registry."); 65 } 66 catch(NumberFormatException e) 67 { 68 log.error("Can not set the RMIServerInvoker RMI registry to port " + value + ". This is not a valid port number."); 69 } 70 } 71 } 72 return port; 73 } 74 75 80 public void setServerStub(RMIServerInvokerInf server) 81 { 82 this.server = server; 83 log.trace(this.server); 84 } 85 86 91 public RMIServerInvokerInf getServerStub() 92 { 93 return this.server; 94 } 95 96 104 protected void handleConnect() 105 throws ConnectionFailedException 106 { 107 try 110 { 111 String host = locator.getHost(); 112 int port = getRegistryPort(locator); 113 Registry regsitry = LocateRegistry.getRegistry(host, port); 114 Remote remoteObj = regsitry.lookup("remoting/RMIServerInvoker/" + locator.getPort()); 115 log.debug("Remote RMI Stub: " + remoteObj); 116 setServerStub((RMIServerInvokerInf) remoteObj); 117 connected = true; 118 } 119 catch(Exception e) 120 { 121 connected = false; 122 log.debug("Error connecting RMI invoker client.", e); 123 throw new CannotConnectException("Error connecting RMI invoker client", e); 124 } 125 } 126 127 133 protected void handleDisconnect() 134 { 135 } 136 137 protected String getDefaultDataType() 138 { 139 return SerializableMarshaller.DATATYPE; 140 } 141 142 protected Object transport(String sessionId, Object invocation, Map metadata, Marshaller marshaller, UnMarshaller unmarshaller) 143 throws IOException , ConnectionFailedException 144 { 145 if(this.server == null) 146 { 147 log.error("Server stub has not been set in RMI invoker client. See previous errors for details."); 148 throw new CannotConnectException("Server stub has not been set."); 150 } 151 try 152 { 153 return server.transport(invocation); 154 } 155 catch(RemoteException e) 156 { 157 throw new CannotConnectException("Error making invocation in RMI client invoker.", e); 158 } 159 } 160 } 161 | Popular Tags |