From 6ca253e3e697fd12b7c9720aeff0ab27eb75c61b Mon Sep 17 00:00:00 2001 From: Juergen Hoeller Date: Tue, 24 Mar 2009 00:05:21 +0000 Subject: [PATCH] RmiServiceExporter and RmiRegistryFactoryBean synchronize registry lookup/creation --- .../remoting/rmi/RmiRegistryFactoryBean.java | 52 ++++++++++--------- .../remoting/rmi/RmiServiceExporter.java | 50 ++++++++++-------- 2 files changed, 55 insertions(+), 47 deletions(-) diff --git a/org.springframework.context/src/main/java/org/springframework/remoting/rmi/RmiRegistryFactoryBean.java b/org.springframework.context/src/main/java/org/springframework/remoting/rmi/RmiRegistryFactoryBean.java index bb551a908b5..1d400adffb1 100644 --- a/org.springframework.context/src/main/java/org/springframework/remoting/rmi/RmiRegistryFactoryBean.java +++ b/org.springframework.context/src/main/java/org/springframework/remoting/rmi/RmiRegistryFactoryBean.java @@ -215,18 +215,20 @@ public class RmiRegistryFactoryBean implements FactoryBean, Initializi if (logger.isInfoEnabled()) { logger.info("Looking for RMI registry at port '" + registryPort + "', using custom socket factory"); } - try { - // Retrieve existing registry. - Registry reg = LocateRegistry.getRegistry(null, registryPort, clientSocketFactory); - testRegistry(reg); - return reg; - } - catch (RemoteException ex) { - logger.debug("RMI registry access threw exception", ex); - logger.info("Could not detect RMI registry - creating new one"); - // Assume no registry found -> create new one. - this.created = true; - return LocateRegistry.createRegistry(registryPort, clientSocketFactory, serverSocketFactory); + synchronized (LocateRegistry.class) { + try { + // Retrieve existing registry. + Registry reg = LocateRegistry.getRegistry(null, registryPort, clientSocketFactory); + testRegistry(reg); + return reg; + } + catch (RemoteException ex) { + logger.debug("RMI registry access threw exception", ex); + logger.info("Could not detect RMI registry - creating new one"); + // Assume no registry found -> create new one. + this.created = true; + return LocateRegistry.createRegistry(registryPort, clientSocketFactory, serverSocketFactory); + } } } @@ -250,18 +252,20 @@ public class RmiRegistryFactoryBean implements FactoryBean, Initializi if (logger.isInfoEnabled()) { logger.info("Looking for RMI registry at port '" + registryPort + "'"); } - try { - // Retrieve existing registry. - Registry reg = LocateRegistry.getRegistry(registryPort); - testRegistry(reg); - return reg; - } - catch (RemoteException ex) { - logger.debug("RMI registry access threw exception", ex); - logger.info("Could not detect RMI registry - creating new one"); - // Assume no registry found -> create new one. - this.created = true; - return LocateRegistry.createRegistry(registryPort); + synchronized (LocateRegistry.class) { + try { + // Retrieve existing registry. + Registry reg = LocateRegistry.getRegistry(registryPort); + testRegistry(reg); + return reg; + } + catch (RemoteException ex) { + logger.debug("RMI registry access threw exception", ex); + logger.info("Could not detect RMI registry - creating new one"); + // Assume no registry found -> create new one. + this.created = true; + return LocateRegistry.createRegistry(registryPort); + } } } diff --git a/org.springframework.context/src/main/java/org/springframework/remoting/rmi/RmiServiceExporter.java b/org.springframework.context/src/main/java/org/springframework/remoting/rmi/RmiServiceExporter.java index ae6e9ba42ad..f276352aea4 100644 --- a/org.springframework.context/src/main/java/org/springframework/remoting/rmi/RmiServiceExporter.java +++ b/org.springframework.context/src/main/java/org/springframework/remoting/rmi/RmiServiceExporter.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2007 the original author or authors. + * Copyright 2002-2009 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -352,17 +352,19 @@ public class RmiServiceExporter extends RmiBasedExporter implements Initializing if (logger.isInfoEnabled()) { logger.info("Looking for RMI registry at port '" + registryPort + "', using custom socket factory"); } - try { - // Retrieve existing registry. - Registry reg = LocateRegistry.getRegistry(null, registryPort, clientSocketFactory); - testRegistry(reg); - return reg; - } - catch (RemoteException ex) { - logger.debug("RMI registry access threw exception", ex); - logger.info("Could not detect RMI registry - creating new one"); - // Assume no registry found -> create new one. - return LocateRegistry.createRegistry(registryPort, clientSocketFactory, serverSocketFactory); + synchronized (LocateRegistry.class) { + try { + // Retrieve existing registry. + Registry reg = LocateRegistry.getRegistry(null, registryPort, clientSocketFactory); + testRegistry(reg); + return reg; + } + catch (RemoteException ex) { + logger.debug("RMI registry access threw exception", ex); + logger.info("Could not detect RMI registry - creating new one"); + // Assume no registry found -> create new one. + return LocateRegistry.createRegistry(registryPort, clientSocketFactory, serverSocketFactory); + } } } @@ -385,17 +387,19 @@ public class RmiServiceExporter extends RmiBasedExporter implements Initializing if (logger.isInfoEnabled()) { logger.info("Looking for RMI registry at port '" + registryPort + "'"); } - try { - // Retrieve existing registry. - Registry reg = LocateRegistry.getRegistry(registryPort); - testRegistry(reg); - return reg; - } - catch (RemoteException ex) { - logger.debug("RMI registry access threw exception", ex); - logger.info("Could not detect RMI registry - creating new one"); - // Assume no registry found -> create new one. - return LocateRegistry.createRegistry(registryPort); + synchronized (LocateRegistry.class) { + try { + // Retrieve existing registry. + Registry reg = LocateRegistry.getRegistry(registryPort); + testRegistry(reg); + return reg; + } + catch (RemoteException ex) { + logger.debug("RMI registry access threw exception", ex); + logger.info("Could not detect RMI registry - creating new one"); + // Assume no registry found -> create new one. + return LocateRegistry.createRegistry(registryPort); + } } }