1 16 17 package org.springframework.remoting.rmi; 18 19 import java.rmi.RemoteException ; 20 import java.rmi.registry.LocateRegistry ; 21 import java.rmi.registry.Registry ; 22 import java.rmi.server.RMIClientSocketFactory ; 23 import java.rmi.server.RMIServerSocketFactory ; 24 import java.rmi.server.UnicastRemoteObject ; 25 26 import org.apache.commons.logging.Log; 27 import org.apache.commons.logging.LogFactory; 28 29 import org.springframework.beans.factory.DisposableBean; 30 import org.springframework.beans.factory.FactoryBean; 31 import org.springframework.beans.factory.InitializingBean; 32 33 63 public class RmiRegistryFactoryBean implements FactoryBean, InitializingBean, DisposableBean { 64 65 protected final Log logger = LogFactory.getLog(getClass()); 66 67 private String host; 68 69 private int port = Registry.REGISTRY_PORT; 70 71 private RMIClientSocketFactory clientSocketFactory; 72 73 private RMIServerSocketFactory serverSocketFactory; 74 75 private Registry registry; 76 77 private boolean alwaysCreate = false; 78 79 private boolean created = false; 80 81 82 87 public void setHost(String host) { 88 this.host = host; 89 } 90 91 94 public String getHost() { 95 return this.host; 96 } 97 98 103 public void setPort(int port) { 104 this.port = port; 105 } 106 107 110 public int getPort() { 111 return this.port; 112 } 113 114 123 public void setClientSocketFactory(RMIClientSocketFactory clientSocketFactory) { 124 this.clientSocketFactory = clientSocketFactory; 125 } 126 127 136 public void setServerSocketFactory(RMIServerSocketFactory serverSocketFactory) { 137 this.serverSocketFactory = serverSocketFactory; 138 } 139 140 147 public void setAlwaysCreate(boolean alwaysCreate) { 148 this.alwaysCreate = alwaysCreate; 149 } 150 151 152 public void afterPropertiesSet() throws Exception { 153 if (this.clientSocketFactory instanceof RMIServerSocketFactory ) { 155 this.serverSocketFactory = (RMIServerSocketFactory ) this.clientSocketFactory; 156 } 157 if ((this.clientSocketFactory != null && this.serverSocketFactory == null) || 158 (this.clientSocketFactory == null && this.serverSocketFactory != null)) { 159 throw new IllegalArgumentException ( 160 "Both RMIClientSocketFactory and RMIServerSocketFactory or none required"); 161 } 162 163 this.registry = getRegistry(this.host, this.port, this.clientSocketFactory, this.serverSocketFactory); 165 } 166 167 168 178 protected Registry getRegistry(String registryHost, int registryPort, 179 RMIClientSocketFactory clientSocketFactory, RMIServerSocketFactory serverSocketFactory) 180 throws RemoteException { 181 182 if (registryHost != null) { 183 if (logger.isInfoEnabled()) { 185 logger.info("Looking for RMI registry at port '" + registryPort + "' of host [" + registryHost + "]"); 186 } 187 Registry reg = LocateRegistry.getRegistry(registryHost, registryPort, clientSocketFactory); 188 testRegistry(reg); 189 return reg; 190 } 191 192 else { 193 return getRegistry(registryPort, clientSocketFactory, serverSocketFactory); 194 } 195 } 196 197 205 protected Registry getRegistry( 206 int registryPort, RMIClientSocketFactory clientSocketFactory, RMIServerSocketFactory serverSocketFactory) 207 throws RemoteException { 208 209 if (clientSocketFactory != null) { 210 if (this.alwaysCreate) { 211 logger.info("Creating new RMI registry"); 212 this.created = true; 213 return LocateRegistry.createRegistry(registryPort, clientSocketFactory, serverSocketFactory); 214 } 215 if (logger.isInfoEnabled()) { 216 logger.info("Looking for RMI registry at port '" + registryPort + "', using custom socket factory"); 217 } 218 try { 219 Registry reg = LocateRegistry.getRegistry(null, registryPort, clientSocketFactory); 221 testRegistry(reg); 222 return reg; 223 } 224 catch (RemoteException ex) { 225 logger.debug("RMI registry access threw exception", ex); 226 logger.info("Could not detect RMI registry - creating new one"); 227 this.created = true; 229 return LocateRegistry.createRegistry(registryPort, clientSocketFactory, serverSocketFactory); 230 } 231 } 232 233 else { 234 return getRegistry(registryPort); 235 } 236 } 237 238 244 protected Registry getRegistry(int registryPort) throws RemoteException { 245 if (this.alwaysCreate) { 246 logger.info("Creating new RMI registry"); 247 this.created = true; 248 return LocateRegistry.createRegistry(registryPort); 249 } 250 if (logger.isInfoEnabled()) { 251 logger.info("Looking for RMI registry at port '" + registryPort + "'"); 252 } 253 try { 254 Registry reg = LocateRegistry.getRegistry(registryPort); 256 testRegistry(reg); 257 return reg; 258 } 259 catch (RemoteException ex) { 260 logger.debug("RMI registry access threw exception", ex); 261 logger.info("Could not detect RMI registry - creating new one"); 262 this.created = true; 264 return LocateRegistry.createRegistry(registryPort); 265 } 266 } 267 268 276 protected void testRegistry(Registry registry) throws RemoteException { 277 registry.list(); 278 } 279 280 281 public Object getObject() throws Exception { 282 return this.registry; 283 } 284 285 public Class getObjectType() { 286 return (this.registry != null ? this.registry.getClass() : Registry .class); 287 } 288 289 public boolean isSingleton() { 290 return true; 291 } 292 293 294 298 public void destroy() throws RemoteException { 299 if (this.created) { 300 logger.info("Unexporting RMI registry"); 301 UnicastRemoteObject.unexportObject(this.registry, true); 302 } 303 } 304 305 } 306 | Popular Tags |