Author: indika
Date: Tue Dec  2 09:06:15 2008
New Revision: 25544
URL: http://wso2.org/svn/browse/wso2?view=rev&revision=25544

Log:
add functinality to register muliple JNDI datasource in same single RMI 
Registry + if want multiple RMI registry 


Modified:
   
branches/synapse/1.2.wso2v1/modules/core/src/main/java/org/apache/synapse/config/SynapseConfiguration.java
   
branches/synapse/1.2.wso2v1/modules/utils/src/main/java/org/apache/synapse/commons/util/RMIRegistryController.java

Modified: 
branches/synapse/1.2.wso2v1/modules/core/src/main/java/org/apache/synapse/config/SynapseConfiguration.java
URL: 
http://wso2.org/svn/browse/wso2/branches/synapse/1.2.wso2v1/modules/core/src/main/java/org/apache/synapse/config/SynapseConfiguration.java?rev=25544&r1=25543&r2=25544&view=diff
==============================================================================
--- 
branches/synapse/1.2.wso2v1/modules/core/src/main/java/org/apache/synapse/config/SynapseConfiguration.java
  (original)
+++ 
branches/synapse/1.2.wso2v1/modules/core/src/main/java/org/apache/synapse/config/SynapseConfiguration.java
  Tue Dec  2 09:06:15 2008
@@ -25,7 +25,6 @@
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
 import org.apache.synapse.*;
-import org.apache.synapse.eventing.SynapseEventSource;
 import org.apache.synapse.commons.util.RMIRegistryController;
 import org.apache.synapse.commons.util.datasource.InMemoryDataSourceRepository;
 import 
org.apache.synapse.commons.util.datasource.JNDIBasedDataSourceRepository;
@@ -35,6 +34,7 @@
 import org.apache.synapse.core.axis2.ProxyService;
 import org.apache.synapse.endpoints.Endpoint;
 import org.apache.synapse.endpoints.dispatch.SALSessions;
+import org.apache.synapse.eventing.SynapseEventSource;
 import org.apache.synapse.mediators.AbstractMediator;
 import org.apache.synapse.mediators.ListMediator;
 import org.apache.synapse.mediators.base.SequenceMediator;
@@ -817,7 +817,7 @@
 
         // clear session information used for SA load balancing
         try {
-            RMIRegistryController.getInstance().removeLocalRegistry();
+            RMIRegistryController.getInstance().shutDown();
             SALSessions.getInstance().reset();
             InMemoryDataSourceRepository.getInstance().clear();
             JNDIBasedDataSourceRepository registry = 
JNDIBasedDataSourceRepository.getInstance();

Modified: 
branches/synapse/1.2.wso2v1/modules/utils/src/main/java/org/apache/synapse/commons/util/RMIRegistryController.java
URL: 
http://wso2.org/svn/browse/wso2/branches/synapse/1.2.wso2v1/modules/utils/src/main/java/org/apache/synapse/commons/util/RMIRegistryController.java?rev=25544&r1=25543&r2=25544&view=diff
==============================================================================
--- 
branches/synapse/1.2.wso2v1/modules/utils/src/main/java/org/apache/synapse/commons/util/RMIRegistryController.java
  (original)
+++ 
branches/synapse/1.2.wso2v1/modules/utils/src/main/java/org/apache/synapse/commons/util/RMIRegistryController.java
  Tue Dec  2 09:06:15 2008
@@ -11,14 +11,15 @@
 import java.rmi.registry.LocateRegistry;
 import java.rmi.registry.Registry;
 import java.rmi.server.UnicastRemoteObject;
+import java.util.HashMap;
+import java.util.Map;
 
 public class RMIRegistryController {
 
     public static final Log log = 
LogFactory.getLog(RMIRegistryController.class);
 
     private static RMIRegistryController ourInstance = new 
RMIRegistryController();
-    private Registry localRegistry;
-    private boolean weCreatedRMIReg = false;
+    private final Map<String, Registry> registriesCache = new HashMap<String, 
Registry>();
 
     public static RMIRegistryController getInstance() {
         return ourInstance;
@@ -30,11 +31,28 @@
     /**
      * Creates a RMI local registry with given port
      *
-     * @param port The port
+     * @param port The port of the RMI registry to be created
      */
     public void createLocalRegistry(int port) {
+
         try {
-            localRegistry = LocateRegistry.createRegistry(port);
+
+            String key = toKey(port);
+
+            if (registriesCache.containsKey(key)) {
+                if (log.isDebugEnabled()) {
+                    log.debug("There is an RMI registry bound to given port :" 
+ port);
+                }
+                return;
+            }
+
+            Registry locateRegistry = LocateRegistry.createRegistry(port);
+            if (locateRegistry == null) {
+                handleException("Unable to create a RMI registry with port : " 
+ port);
+            }
+
+            registriesCache.put(key, locateRegistry);
+
         } catch (RemoteException e) {
             String msg = "Couldn't create a local registry(RMI) : port " + 
port +
                     " already in use.";
@@ -44,19 +62,56 @@
 
     /**
      * removes if there is a RMI local registry instance
+     *
+     * @param port The port of the RMI registry to be removed
      */
-    public void removeLocalRegistry() {
-        if (localRegistry != null) {
+    public void removeLocalRegistry(int port) {
+
+        String key = toKey(port);
+        if (registriesCache.containsKey(key)) {
+            removeRegistry(key, registriesCache.get(key));
+        } else {
+            if (log.isDebugEnabled()) {
+                log.debug("There is no RMi registry for port : " + port);
+            }
+        }
+
+    }
+
+    public void shutDown() {
+
+        for (String key : registriesCache.keySet()) {
+            removeRegistry(key, registriesCache.get(key));
+        }
+        registriesCache.clear();
+    }
+
+    private static void removeRegistry(String key, Registry registry) {
+
+        if (registry != null) {
             try {
-                log.info("Removing the RMI registy instance from the RMI 
runtime ");
-                UnicastRemoteObject.unexportObject(localRegistry, true);
+                log.info("Removing the RMI registry bound to port : " + key);
+                UnicastRemoteObject.unexportObject(registry, true);
             } catch (NoSuchObjectException e) {
                 String msg = "Error when stopping localregistry(RMI)";
                 handleException(msg, e);
             }
         }
+
     }
 
+    private static String toKey(int port) {
+
+        assertPositive(port);
+        return String.valueOf(port);
+    }
+
+    private static void assertPositive(int port) {
+
+        if (port < 0) {
+            handleException("Invalid port number : " + port);
+        }
+    }
 
     /**
      * Helper methods for handle errors.
@@ -69,4 +124,14 @@
         throw new SynapseUtilException(msg, e);
     }
 
+    /**
+     * Helper methods for handle errors.
+     *
+     * @param msg The error message
+     */
+    private static void handleException(String msg) {
+        log.error(msg);
+        throw new SynapseUtilException(msg);
+    }
+
 }

_______________________________________________
Esb-java-dev mailing list
[email protected]
https://wso2.org/cgi-bin/mailman/listinfo/esb-java-dev

Reply via email to