1 16 17 package org.springframework.remoting.rmi; 18 19 import java.rmi.NoSuchObjectException ; 20 import java.rmi.NotBoundException ; 21 import java.rmi.Remote ; 22 import java.rmi.RemoteException ; 23 import java.rmi.registry.LocateRegistry ; 24 import java.rmi.registry.Registry ; 25 import java.rmi.server.RMIClientSocketFactory ; 26 import java.rmi.server.RMIServerSocketFactory ; 27 import java.rmi.server.UnicastRemoteObject ; 28 29 import org.springframework.beans.factory.DisposableBean; 30 import org.springframework.beans.factory.InitializingBean; 31 32 68 public class RmiServiceExporter extends RmiBasedExporter implements InitializingBean, DisposableBean { 69 70 private String serviceName; 71 72 private int servicePort = 0; 74 private RMIClientSocketFactory clientSocketFactory; 75 76 private RMIServerSocketFactory serverSocketFactory; 77 78 private Registry registry; 79 80 private String registryHost; 81 82 private int registryPort = Registry.REGISTRY_PORT; 83 84 private RMIClientSocketFactory registryClientSocketFactory; 85 86 private RMIServerSocketFactory registryServerSocketFactory; 87 88 private boolean alwaysCreateRegistry = false; 89 90 private Remote exportedObject; 91 92 93 97 public void setServiceName(String serviceName) { 98 this.serviceName = serviceName; 99 } 100 101 105 public void setServicePort(int servicePort) { 106 this.servicePort = servicePort; 107 } 108 109 118 public void setClientSocketFactory(RMIClientSocketFactory clientSocketFactory) { 119 this.clientSocketFactory = clientSocketFactory; 120 } 121 122 131 public void setServerSocketFactory(RMIServerSocketFactory serverSocketFactory) { 132 this.serverSocketFactory = serverSocketFactory; 133 } 134 135 149 public void setRegistry(Registry registry) { 150 this.registry = registry; 151 } 152 153 158 public void setRegistryHost(String registryHost) { 159 this.registryHost = registryHost; 160 } 161 162 168 public void setRegistryPort(int registryPort) { 169 this.registryPort = registryPort; 170 } 171 172 181 public void setRegistryClientSocketFactory(RMIClientSocketFactory registryClientSocketFactory) { 182 this.registryClientSocketFactory = registryClientSocketFactory; 183 } 184 185 194 public void setRegistryServerSocketFactory(RMIServerSocketFactory registryServerSocketFactory) { 195 this.registryServerSocketFactory = registryServerSocketFactory; 196 } 197 198 205 public void setAlwaysCreateRegistry(boolean alwaysCreateRegistry) { 206 this.alwaysCreateRegistry = alwaysCreateRegistry; 207 } 208 209 210 public void afterPropertiesSet() throws RemoteException { 211 prepare(); 212 } 213 214 219 public void prepare() throws RemoteException { 220 checkService(); 221 222 if (this.serviceName == null) { 223 throw new IllegalArgumentException ("Property 'serviceName' is required"); 224 } 225 226 if (this.clientSocketFactory instanceof RMIServerSocketFactory ) { 228 this.serverSocketFactory = (RMIServerSocketFactory ) this.clientSocketFactory; 229 } 230 if ((this.clientSocketFactory != null && this.serverSocketFactory == null) || 231 (this.clientSocketFactory == null && this.serverSocketFactory != null)) { 232 throw new IllegalArgumentException ( 233 "Both RMIClientSocketFactory and RMIServerSocketFactory or none required"); 234 } 235 236 if (this.registryClientSocketFactory instanceof RMIServerSocketFactory ) { 238 this.registryServerSocketFactory = (RMIServerSocketFactory ) this.registryClientSocketFactory; 239 } 240 if (this.registryClientSocketFactory == null && this.registryServerSocketFactory != null) { 241 throw new IllegalArgumentException ( 242 "RMIServerSocketFactory without RMIClientSocketFactory for registry not supported"); 243 } 244 245 if (this.registry == null) { 247 this.registry = getRegistry(this.registryHost, this.registryPort, 248 this.registryClientSocketFactory, this.registryServerSocketFactory); 249 } 250 251 this.exportedObject = getObjectToExport(); 253 254 if (logger.isInfoEnabled()) { 255 logger.info("Binding service '" + this.serviceName + "' to RMI registry: " + this.registry); 256 } 257 258 if (this.clientSocketFactory != null) { 260 UnicastRemoteObject.exportObject( 261 this.exportedObject, this.servicePort, this.clientSocketFactory, this.serverSocketFactory); 262 } 263 else { 264 UnicastRemoteObject.exportObject(this.exportedObject, this.servicePort); 265 } 266 267 try { 269 this.registry.rebind(this.serviceName, this.exportedObject); 270 } 271 catch (RemoteException ex) { 272 unexportObjectSilently(); 274 throw ex; 275 } 276 } 277 278 279 289 protected Registry getRegistry(String registryHost, int registryPort, 290 RMIClientSocketFactory clientSocketFactory, RMIServerSocketFactory serverSocketFactory) 291 throws RemoteException { 292 293 if (registryHost != null) { 294 if (logger.isInfoEnabled()) { 296 logger.info("Looking for RMI registry at port '" + registryPort + "' of host [" + registryHost + "]"); 297 } 298 Registry reg = LocateRegistry.getRegistry(registryHost, registryPort, clientSocketFactory); 299 testRegistry(reg); 300 return reg; 301 } 302 303 else { 304 return getRegistry(registryPort, clientSocketFactory, serverSocketFactory); 305 } 306 } 307 308 316 protected Registry getRegistry( 317 int registryPort, RMIClientSocketFactory clientSocketFactory, RMIServerSocketFactory serverSocketFactory) 318 throws RemoteException { 319 320 if (clientSocketFactory != null) { 321 if (this.alwaysCreateRegistry) { 322 logger.info("Creating new RMI registry"); 323 return LocateRegistry.createRegistry(registryPort, clientSocketFactory, serverSocketFactory); 324 } 325 if (logger.isInfoEnabled()) { 326 logger.info("Looking for RMI registry at port '" + registryPort + "', using custom socket factory"); 327 } 328 try { 329 Registry reg = LocateRegistry.getRegistry(null, registryPort, clientSocketFactory); 331 testRegistry(reg); 332 return reg; 333 } 334 catch (RemoteException ex) { 335 logger.debug("RMI registry access threw exception", ex); 336 logger.info("Could not detect RMI registry - creating new one"); 337 return LocateRegistry.createRegistry(registryPort, clientSocketFactory, serverSocketFactory); 339 } 340 } 341 342 else { 343 return getRegistry(registryPort); 344 } 345 } 346 347 353 protected Registry getRegistry(int registryPort) throws RemoteException { 354 if (this.alwaysCreateRegistry) { 355 logger.info("Creating new RMI registry"); 356 return LocateRegistry.createRegistry(registryPort); 357 } 358 if (logger.isInfoEnabled()) { 359 logger.info("Looking for RMI registry at port '" + registryPort + "'"); 360 } 361 try { 362 Registry reg = LocateRegistry.getRegistry(registryPort); 364 testRegistry(reg); 365 return reg; 366 } 367 catch (RemoteException ex) { 368 logger.debug("RMI registry access threw exception", ex); 369 logger.info("Could not detect RMI registry - creating new one"); 370 return LocateRegistry.createRegistry(registryPort); 372 } 373 } 374 375 383 protected void testRegistry(Registry registry) throws RemoteException { 384 registry.list(); 385 } 386 387 388 391 public void destroy() throws RemoteException { 392 if (logger.isInfoEnabled()) { 393 logger.info("Unbinding RMI service '" + this.serviceName + 394 "' from registry at port '" + this.registryPort + "'"); 395 } 396 try { 397 this.registry.unbind(this.serviceName); 398 } 399 catch (NotBoundException ex) { 400 if (logger.isWarnEnabled()) { 401 logger.warn("RMI service '" + this.serviceName + "' is not bound to registry at port '" + 402 this.registryPort + "' anymore", ex); 403 } 404 } 405 finally { 406 unexportObjectSilently(); 407 } 408 } 409 410 413 private void unexportObjectSilently() { 414 try { 415 UnicastRemoteObject.unexportObject(this.exportedObject, true); 416 } 417 catch (NoSuchObjectException ex) { 418 if (logger.isWarnEnabled()) { 419 logger.warn("RMI object for service '" + this.serviceName + "' isn't exported anymore", ex); 420 } 421 } 422 } 423 424 } 425 | Popular Tags |