Author: hiranya
Date: Thu Aug  1 23:51:47 2013
New Revision: 1509472

URL: http://svn.apache.org/r1509472
Log:
Refactored SynapseConfiguration so no back to back init/destroy calls can be 
made. See SYNAPSE-698

Modified:
    
synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/config/SynapseConfiguration.java

Modified: 
synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/config/SynapseConfiguration.java
URL: 
http://svn.apache.org/viewvc/synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/config/SynapseConfiguration.java?rev=1509472&r1=1509471&r2=1509472&view=diff
==============================================================================
--- 
synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/config/SynapseConfiguration.java
 (original)
+++ 
synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/config/SynapseConfiguration.java
 Thu Aug  1 23:51:47 2013
@@ -110,7 +110,7 @@ public class SynapseConfiguration implem
     /**
      * This will provide the timer daemon object for the scheduled tasks.
      */
-    private Timer synapseTimer = new Timer(true);
+    private Timer synapseTimer;
 
     /** Hold reference to the Axis2 ConfigurationContext */
     private AxisConfiguration axisConfiguration = null;
@@ -167,14 +167,17 @@ public class SynapseConfiguration implem
     /**
      * Holds synapse Libraries indexed by library qualified name
      */
-    Map<String,Library> synapseLibraries = new 
ConcurrentHashMap<String,Library>();
+    private Map<String,Library> synapseLibraries = new 
ConcurrentHashMap<String,Library>();
 
     /**
      * Holds the library imports  currently being included into Synapse engine
      */
-    Map<String,SynapseImport> synapseImports = new 
ConcurrentHashMap<String,SynapseImport>();
+    private Map<String,SynapseImport> synapseImports = new 
ConcurrentHashMap<String,SynapseImport>();
+
     private boolean allowHotUpdate = true;
 
+    private boolean initialized = false;
+
     /**
      * Add a named sequence into the local registry. If a sequence already 
exists by the specified
      * key a runtime exception is thrown.
@@ -696,7 +699,7 @@ public class SynapseConfiguration implem
                     try {
                         o = registry.getResource(entry, getProperties());
                     } catch (Exception e) {
-                        // Error occured while loading the resource from the 
registry
+                        // Error occurred while loading the resource from the 
registry
                         // Fall back to the cached value - Do not increase the 
expiry time
                         log.warn("Error while loading the resource " + key + " 
from the remote " +
                                 "registry. Previously cached value will be 
used. Check the " +
@@ -1106,6 +1109,10 @@ public class SynapseConfiguration implem
      * @return synapseTimer timer object of the configuration
      */
     public Timer getSynapseTimer() {
+        if (synapseTimer == null) {
+            handleException("Attempted to access the Synapse timer " +
+                    "before initializing SynapseConfiguration");
+        }
         return synapseTimer;
     }
 
@@ -1190,7 +1197,7 @@ public class SynapseConfiguration implem
     }
 
     /**
-     * Sets the properties to configure the Synapse enviornment.
+     * Sets the properties to configure the Synapse environment.
      *
      * @param properties - Properties which needs to be set
      *
@@ -1247,7 +1254,7 @@ public class SynapseConfiguration implem
     }
 
     /**
-     * Gets the propety value if the property specified by the propKey is 
there or null else
+     * Gets the property value if the property specified by the propKey is 
there or null else
      *
      * @param propKey - key for the property lookup
      * @return String representation of the property value if found or null 
else
@@ -1268,89 +1275,50 @@ public class SynapseConfiguration implem
     }
 
     /**
-     * This method will be called on the soft shutdown or destroying the 
configuration
-     * and will destroy all the stateful managed parts of the configuration.
+     * This method will be called in the startup of Synapse or in an initiation
+     * and will initialize all the managed parts of the Synapse Configuration
+     *
+     * @param se
+     *          SynapseEnvironment specifying the env to be initialized
      */
-    public synchronized void destroy() {
-        
-        if (log.isDebugEnabled()) {
-            log.debug("Destroying the Synapse Configuration");
-        }
-
-        // clear the timer tasks of Synapse
-        synapseTimer.cancel();
-        synapseTimer = null;
-
-        // stop and shutdown all the proxy services
-        for (ProxyService p : getProxyServices()) {
-
-            if (p.getTargetInLineInSequence() != null) {
-                p.getTargetInLineInSequence().destroy();
-            }
-
-            if (p.getTargetInLineOutSequence() != null) {
-                p.getTargetInLineOutSequence().destroy();
-            }
-        }
-
-        // destroy the managed mediators
-        for (ManagedLifecycle seq : getDefinedSequences().values()) {
-            seq.destroy();
-        }
-
-        //destroy sequence templates
-        for (TemplateMediator seqTemplate : getSequenceTemplates().values()) {
-            seqTemplate.destroy();
-        }
-
-        // destroy the managed endpoints
-        for (Endpoint endpoint : getDefinedEndpoints().values()) {
-            endpoint.destroy();
+    public synchronized void init(SynapseEnvironment se) {
+        if (initialized) {
+            log.warn("Attempted to re-initialize SynapseConfiguration");
+            return;
         }
 
-        // destroy the startups
-        for (ManagedLifecycle stp : startups.values()) {
-            stp.destroy();
+        if (log.isDebugEnabled()) {
+            log.debug("Initializing the Synapse Configuration using the 
SynapseEnvironment");
         }
-        
-        // clear session information used for SA load balancing
         try {
-            SALSessions.getInstance().reset();
-            
DataSourceRepositoryHolder.getInstance().getDataSourceRepositoryManager().clear();
-        } catch (Throwable ignored) {}
-
-        // destroy the priority executors. 
-        for(PriorityExecutor pe : executors.values()) {
-            pe.destroy();
+            doInit(se);
+        } finally {
+            initialized = true;
         }
+    }
 
-        // destroy the Message Stores
-        for(MessageStore ms : messageStores.values()) {
-            ms.destroy();
+    /**
+     * This method will be called on the soft shutdown or destroying the 
configuration
+     * and will destroy all the stateful managed parts of the configuration.
+     */
+    public synchronized void destroy() {
+        if (!initialized) {
+            log.warn("Attempted to destroy uninitialized 
SynapseConfiguration");
+            return;
         }
 
-        // destroy the Message processors
-        for(MessageProcessor mp : messageProcessors.values()) {
-            mp.destroy();
+        if (log.isDebugEnabled()) {
+            log.debug("Destroying the Synapse Configuration");
         }
-
-        for (API api : apiTable.values()) {
-            api.destroy();
+        try {
+            doDestroy();
+        } finally {
+            initialized = false;
         }
     }
 
-    /**
-     * This method will be called in the startup of Synapse or in an initiation
-     * and will initialize all the managed parts of the Synapse Configuration
-     *
-     * @param se
-     *          SynapseEnvironment specifying the env to be initialized
-     */
-    public synchronized void init(SynapseEnvironment se) {
-        
-        if (log.isDebugEnabled()) {
-            log.debug("Initializing the Synapse Configuration using the 
SynapseEnvironment");
-        }
+    private void doInit(SynapseEnvironment se) {
+        synapseTimer = new Timer(true);
 
         // initialize registry
         if (registry != null && registry instanceof ManagedLifecycle) {
@@ -1367,7 +1335,7 @@ public class SynapseConfiguration implem
             seqTemplate.init(se);
         }
 
-         // initialize managed mediators
+        // initialize managed mediators
         for (ManagedLifecycle seq : getDefinedSequences().values()) {
             if (seq != null) {
                 seq.init(se);
@@ -1407,12 +1375,12 @@ public class SynapseConfiguration implem
         }
 
         //initialize message stores
-        for(MessageStore messageStore : messageStores.values()) {
+        for (MessageStore messageStore : messageStores.values()) {
             messageStore.init(se);
         }
 
         // initialize message processors
-        for(MessageProcessor messageProcessor : messageProcessors.values()) {
+        for (MessageProcessor messageProcessor : messageProcessors.values()) {
             messageProcessor.init(se);
         }
 
@@ -1421,6 +1389,69 @@ public class SynapseConfiguration implem
         }
     }
 
+    private void doDestroy() {
+        // clear the timer tasks of Synapse
+        synapseTimer.cancel();
+        synapseTimer = null;
+
+        // stop and shutdown all the proxy services
+        for (ProxyService p : getProxyServices()) {
+
+            if (p.getTargetInLineInSequence() != null) {
+                p.getTargetInLineInSequence().destroy();
+            }
+
+            if (p.getTargetInLineOutSequence() != null) {
+                p.getTargetInLineOutSequence().destroy();
+            }
+        }
+
+        // destroy the managed mediators
+        for (ManagedLifecycle seq : getDefinedSequences().values()) {
+            seq.destroy();
+        }
+
+        //destroy sequence templates
+        for (TemplateMediator seqTemplate : getSequenceTemplates().values()) {
+            seqTemplate.destroy();
+        }
+
+        // destroy the managed endpoints
+        for (Endpoint endpoint : getDefinedEndpoints().values()) {
+            endpoint.destroy();
+        }
+
+        // destroy the startups
+        for (ManagedLifecycle stp : startups.values()) {
+            stp.destroy();
+        }
+
+        // clear session information used for SA load balancing
+        try {
+            SALSessions.getInstance().reset();
+            
DataSourceRepositoryHolder.getInstance().getDataSourceRepositoryManager().clear();
+        } catch (Throwable ignored) {}
+
+        // destroy the priority executors.
+        for (PriorityExecutor pe : executors.values()) {
+            pe.destroy();
+        }
+
+        // destroy the Message Stores
+        for (MessageStore ms : messageStores.values()) {
+            ms.destroy();
+        }
+
+        // destroy the Message processors
+        for (MessageProcessor mp : messageProcessors.values()) {
+            mp.destroy();
+        }
+
+        for (API api : apiTable.values()) {
+            api.destroy();
+        }
+    }
+
     private void handleException(String msg) {
         log.error(msg);
         throw new SynapseException(msg);
@@ -1570,7 +1601,7 @@ public class SynapseConfiguration implem
     }
 
     /**
-     * Get Message sotres defined
+     * Get Message stores defined
      * @return  message store map stored as name of the message store and 
message store
      */
     public Map<String, MessageStore> getMessageStores() {


Reply via email to