1 9 package org.jboss.remoting.transport.rmi; 10 11 import java.io.IOException ; 12 import java.rmi.RemoteException ; 13 import java.rmi.registry.LocateRegistry ; 14 import java.rmi.registry.Registry ; 15 import java.rmi.server.ExportException ; 16 import java.rmi.server.RemoteStub ; 17 import java.rmi.server.UnicastRemoteObject ; 18 import java.util.Map ; 19 import org.jboss.remoting.InvokerLocator; 20 import org.jboss.remoting.ServerInvoker; 21 import org.jboss.remoting.marshal.serializable.SerializableMarshaller; 22 23 30 public class RMIServerInvoker extends ServerInvoker implements RMIServerInvokerInf 31 { 32 private RemoteStub stub; 33 34 public static final int DEFAULT_REGISTRY_PORT = 3455; 35 public static final String REGISTRY_PORT_KEY = "registryPort"; 36 37 public RMIServerInvoker(InvokerLocator locator) 38 { 39 super(locator); 40 } 41 42 protected void setup() throws Exception 43 { 44 super.setup(); 45 46 int bindPort = getServerBindPort(); 47 Registry registry = null; 48 registry = getRegistry(); 49 stub = (RemoteStub ) UnicastRemoteObject.exportObject(this, bindPort); 50 51 log.debug("Binding registry to " + "remoting/RMIServerInvoker/" + bindPort); 52 53 registry.rebind("remoting/RMIServerInvoker/" + bindPort, this); 54 } 55 56 public RMIServerInvoker(InvokerLocator locator, Map configuration) 57 { 58 super(locator, configuration); 59 } 60 61 private Registry getRegistry() throws Exception 62 { 63 Registry registry = null; 64 65 int port = DEFAULT_REGISTRY_PORT; 66 67 Map params = getConfiguration(); 69 if(params != null) 70 { 71 String value = (String ) params.get(REGISTRY_PORT_KEY); 72 if(value != null) 73 { 74 try 75 { 76 port = Integer.parseInt(value); 77 log.debug("Using port " + port + " for rmi registry."); 78 } 79 catch(NumberFormatException e) 80 { 81 throw new Exception ("Can not set the RMIServerInvoker RMI registry to port " + value + ". This is not a valid port number."); 82 } 83 } 84 } 85 86 try 87 { 88 log.debug("Creating registry for " + port); 89 90 registry = LocateRegistry.createRegistry(port); 91 } 92 catch(ExportException exportEx) 93 { 94 log.debug("Locating registry for " + port); 95 96 registry = LocateRegistry.getRegistry(port); 98 } 99 if(log.isTraceEnabled()) 100 { 101 log.trace("Got registry: " + registry); 102 } 103 return registry; 104 } 105 106 protected String getDefaultDataType() 107 { 108 return SerializableMarshaller.DATATYPE; 109 } 110 111 114 public void destroy() 115 { 116 super.destroy(); 117 try 118 { 119 try 120 { 121 log.debug("unbinding " + "remoting/RMIServerInvoker/" + locator.getPort() + " from registry running on port " + (locator.getPort() + 1)); 122 Registry registry = getRegistry(); 123 registry.unbind("remoting/RMIServerInvoker/" + locator.getPort()); 124 } 125 catch(Exception e) 126 { 127 log.error("Error unbinding RMIServerInvoker from RMI registry.", e); 128 } 129 130 UnicastRemoteObject.unexportObject(this, true); 131 132 } 133 catch(java.rmi.NoSuchObjectException e) 134 { 135 136 } 137 } 138 139 protected void finalize() throws Throwable 140 { 141 destroy(); 142 super.finalize(); 143 } 144 145 152 public boolean isTransportBiDirectional() 153 { 154 return true; 155 } 156 157 public final RemoteStub getStub() 158 { 159 return stub; 160 } 161 162 public Object transport(Object invocation) 163 throws RemoteException , IOException 164 { 165 return invoke(invocation); 166 } 167 } 168 | Popular Tags |