Author: jmarino
Date: Thu Apr 13 18:46:50 2006
New Revision: 393997

URL: http://svn.apache.org/viewcvs?rev=393997&view=rev
Log:
clarify stop vs destroy ordering; remove scope memory leaks

Modified:
    
incubator/tuscany/java/sca/core/src/main/java/org/apache/tuscany/core/context/scope/ModuleScopeContext.java
    
incubator/tuscany/java/sca/core/src/main/java/org/apache/tuscany/core/context/scope/RequestScopeContext.java
    
incubator/tuscany/java/sca/core/src/main/java/org/apache/tuscany/core/context/scope/SessionScopeContext.java
    
incubator/tuscany/java/sca/core/src/main/java/org/apache/tuscany/core/context/scope/StatelessScopeContext.java

Modified: 
incubator/tuscany/java/sca/core/src/main/java/org/apache/tuscany/core/context/scope/ModuleScopeContext.java
URL: 
http://svn.apache.org/viewcvs/incubator/tuscany/java/sca/core/src/main/java/org/apache/tuscany/core/context/scope/ModuleScopeContext.java?rev=393997&r1=393996&r2=393997&view=diff
==============================================================================
--- 
incubator/tuscany/java/sca/core/src/main/java/org/apache/tuscany/core/context/scope/ModuleScopeContext.java
 (original)
+++ 
incubator/tuscany/java/sca/core/src/main/java/org/apache/tuscany/core/context/scope/ModuleScopeContext.java
 Thu Apr 13 18:46:50 2006
@@ -44,10 +44,10 @@
 public class ModuleScopeContext extends AbstractScopeContext {
 
     // Component contexts in this scope keyed by name
-    private Map<String, Context> componentContexts;
+    private Map<String, Context> contexts;
 
     // the queue of contexts to destroy, in the order that their instances 
were created
-    private List<Context> destroyableContexts;
+    private List<Context> destroyQueue;
 
     public ModuleScopeContext(EventContext eventContext) {
         super(eventContext);
@@ -62,10 +62,10 @@
             shutdownContexts();
         } else if (event instanceof InstanceCreated) {
             checkInit();
-            if (event.getSource() instanceof AtomicContext) {
+            if (event.getSource() instanceof Context) {
                 Context context = (Context) event.getSource();
                 // Queue the context to have its implementation instance 
released if destroyable
-                destroyableContexts.add(context);
+                destroyQueue.add(context);
             }
         }
     }
@@ -80,8 +80,8 @@
         if (lifecycleState != RUNNING) {
             throw new IllegalStateException("Scope in wrong state [" + 
lifecycleState + "]");
         }
-        componentContexts = null;
-        destroyableContexts = null;
+        contexts = null;
+        destroyQueue = null;
         lifecycleState = STOPPED;
     }
 
@@ -92,30 +92,30 @@
     public void registerFactory(ContextFactory<Context> configuration) {
         contextFactories.put(configuration.getName(), configuration);
         if (lifecycleState == RUNNING) {
-            componentContexts.put(configuration.getName(), 
configuration.createContext());
+            contexts.put(configuration.getName(), 
configuration.createContext());
         }
     }
 
     public Context getContext(String ctxName) {
         checkInit();
         initComponentContexts();
-        return componentContexts.get(ctxName);
+        return contexts.get(ctxName);
     }
 
     public Context getContextByKey(String ctxName, Object key) {
         checkInit();
         initComponentContexts();
-        return componentContexts.get(ctxName);
+        return contexts.get(ctxName);
     }
 
     public void removeContext(String ctxName) {
         checkInit();
-        if (componentContexts == null){
+        if (contexts == null){
             return;
         }
-        Context context = componentContexts.remove(ctxName);
+        Context context = contexts.remove(ctxName);
         if (context != null) {
-            destroyableContexts.remove(context);
+            destroyQueue.remove(context);
         }
     }
 
@@ -127,28 +127,37 @@
      * Notifies contexts of a shutdown in reverse order to which they were 
started
      */
     private synchronized void shutdownContexts() {
-        if (destroyableContexts == null || destroyableContexts.size() == 0) {
+        if (destroyQueue == null || destroyQueue.size() == 0) {
             return;
         }
         // shutdown destroyable instances in reverse instantiation order
-        ListIterator<Context> iter = 
destroyableContexts.listIterator(destroyableContexts.size());
+        ListIterator<Context> iter = 
destroyQueue.listIterator(destroyQueue.size());
         while(iter.hasPrevious()){
             Context context = iter.previous();
             if (context.getLifecycleState() == RUNNING) {
-                synchronized (context) {
-                    try {
-                        if (context instanceof AtomicContext){
-                            ((AtomicContext)context).destroy();
-                        }
-                        context.stop();
-                    } catch (TargetException e) {
-                        // TODO send a monitoring event
+                try {
+                    if (context instanceof AtomicContext){
+                        ((AtomicContext)context).destroy();
                     }
+                } catch (TargetException e) {
+                    // TODO send a monitoring event
                 }
             }
         }
-        componentContexts = null;
-        destroyableContexts = null;
+        if (contexts == null){
+            return;
+        }
+        for(Context context: contexts.values()) {
+            try {
+                if (context.getLifecycleState() == RUNNING) {
+                    context.stop();
+                }
+            } catch (CoreRuntimeException e){
+                // TODO send monitoring event
+            }
+        }
+        contexts = null;
+        destroyQueue = null;
      }
 
     /**
@@ -158,23 +167,23 @@
      * @throws CoreRuntimeException
      */
     private synchronized void initComponentContexts() throws 
CoreRuntimeException {
-        if (componentContexts == null) {
-            componentContexts = new ConcurrentHashMap<String, Context>();
-            destroyableContexts = new ArrayList<Context>();
+        if (contexts == null) {
+            contexts = new ConcurrentHashMap<String, Context>();
+            destroyQueue = new ArrayList<Context>();
             for (ContextFactory<Context> config : contextFactories.values()) {
                 Context context = config.createContext();
                 context.start();
-                componentContexts.put(context.getName(), context);
+                contexts.put(context.getName(), context);
             }
             // Initialize eager contexts. Note this cannot be done when we 
initially create each context since a component may
             // contain a forward reference to a component which has not been 
instantiated
-            for (Context context : componentContexts.values()) {
+            for (Context context : contexts.values()) {
                 if (context instanceof AtomicContext) {
                     AtomicContext atomic = (AtomicContext) context;
                     if (atomic.isEagerInit()) {
                         // perform silent creation and manual shutdown 
registration
                         atomic.init();
-                        destroyableContexts.add(context);
+                        destroyQueue.add(context);
                     }
                 }
                 context.addListener(this);

Modified: 
incubator/tuscany/java/sca/core/src/main/java/org/apache/tuscany/core/context/scope/RequestScopeContext.java
URL: 
http://svn.apache.org/viewcvs/incubator/tuscany/java/sca/core/src/main/java/org/apache/tuscany/core/context/scope/RequestScopeContext.java?rev=393997&r1=393996&r2=393997&view=diff
==============================================================================
--- 
incubator/tuscany/java/sca/core/src/main/java/org/apache/tuscany/core/context/scope/RequestScopeContext.java
 (original)
+++ 
incubator/tuscany/java/sca/core/src/main/java/org/apache/tuscany/core/context/scope/RequestScopeContext.java
 Thu Apr 13 18:46:50 2006
@@ -172,18 +172,17 @@
         return contexts;
     }
 
-    private synchronized void shutdownContexts() {
-          List<Context> contexts = destroyQueues.get(Thread.currentThread());
-          if (contexts == null || contexts.size() == 0) {
+    private void shutdownContexts() {
+          List<Context> destroyQueue = 
destroyQueues.remove(Thread.currentThread());
+          if (destroyQueue == null || destroyQueue.size() == 0) {
              return;
           }
-          // shutdown destroyable instances in reverse instantiation order
-          ListIterator<Context> iter = contexts.listIterator(contexts.size());
-          while(iter.hasPrevious()){
-              Context context = iter.previous();
-              if (context.getLifecycleState() == RUNNING) {
-                  synchronized (context) {
-                      //removeContextByKey(context.getName(), key);
+          synchronized(destroyQueue){
+              // shutdown destroyable instances in reverse instantiation order
+              ListIterator<Context> iter = 
destroyQueue.listIterator(destroyQueue.size());
+              while(iter.hasPrevious()){
+                  Context context = iter.previous();
+                  if (context.getLifecycleState() == RUNNING) {
                       try {
                           if (context instanceof AtomicContext){
                               ((AtomicContext)context).destroy();
@@ -191,11 +190,21 @@
                           context.stop();
                       } catch (TargetException e) {
                           // TODO send a monitoring event
-                          // log.error("Error releasing instance [" + 
context.getName() + "]",e);
                       }
                   }
               }
           }
+          // shutdown contexts
+          Map<String,Context> currentContexts = 
contexts.remove(Thread.currentThread());
+          if (currentContexts == null){
+             return;
+          }
+          for (Context context: currentContexts.values()){
+              if (context.getLifecycleState() == RUNNING) {
+                  context.stop();
+              }
+          }
+
       }
 
 }

Modified: 
incubator/tuscany/java/sca/core/src/main/java/org/apache/tuscany/core/context/scope/SessionScopeContext.java
URL: 
http://svn.apache.org/viewcvs/incubator/tuscany/java/sca/core/src/main/java/org/apache/tuscany/core/context/scope/SessionScopeContext.java?rev=393997&r1=393996&r2=393997&view=diff
==============================================================================
--- 
incubator/tuscany/java/sca/core/src/main/java/org/apache/tuscany/core/context/scope/SessionScopeContext.java
 (original)
+++ 
incubator/tuscany/java/sca/core/src/main/java/org/apache/tuscany/core/context/scope/SessionScopeContext.java
 Thu Apr 13 18:46:50 2006
@@ -206,24 +206,34 @@
 
 
     private synchronized void shutdownContexts(Object key) {
-        List<Context> contexts = destroyQueues.get(key);
-        if (contexts == null || contexts.size() == 0) {
+        List<Context> destroyQueue = destroyQueues.remove(key);
+        if (destroyQueue == null || destroyQueue.size() == 0) {
              return;
         }
         // shutdown destroyable instances in reverse instantiation order
-        ListIterator<Context> iter = contexts.listIterator(contexts.size());
-        while(iter.hasPrevious()){
-            Context context = iter.previous();
-            if (context.getLifecycleState() == RUNNING) {
-                //removeContextByKey(context.getName(), key);
-                try {
-                    if (context instanceof AtomicContext){
-                        ((AtomicContext)context).destroy();
+        ListIterator<Context> iter = 
destroyQueue.listIterator(destroyQueue.size());
+        synchronized(destroyQueue){
+            while(iter.hasPrevious()){
+                Context context = iter.previous();
+                if (context.getLifecycleState() == RUNNING) {
+                    try {
+                        if (context instanceof AtomicContext){
+                            ((AtomicContext)context).destroy();
+                        }
+                    } catch (TargetException e) {
+                        // TODO send a monitoring event
                     }
-                    context.stop();
-                } catch (TargetException e) {
-                    // TODO send a monitoring event
                 }
+            }
+        }
+        // shutdown contexts
+        Map<String,Context> currentContexts = 
contexts.remove(Thread.currentThread());
+        if (currentContexts == null){
+            return;
+        }
+        for (Context context: currentContexts.values()){
+            if (context.getLifecycleState() == RUNNING) {
+                context.stop();
             }
         }
     }

Modified: 
incubator/tuscany/java/sca/core/src/main/java/org/apache/tuscany/core/context/scope/StatelessScopeContext.java
URL: 
http://svn.apache.org/viewcvs/incubator/tuscany/java/sca/core/src/main/java/org/apache/tuscany/core/context/scope/StatelessScopeContext.java?rev=393997&r1=393996&r2=393997&view=diff
==============================================================================
--- 
incubator/tuscany/java/sca/core/src/main/java/org/apache/tuscany/core/context/scope/StatelessScopeContext.java
 (original)
+++ 
incubator/tuscany/java/sca/core/src/main/java/org/apache/tuscany/core/context/scope/StatelessScopeContext.java
 Thu Apr 13 18:46:50 2006
@@ -21,6 +21,7 @@
 import org.apache.tuscany.core.context.Context;
 import org.apache.tuscany.core.context.CoreRuntimeException;
 import org.apache.tuscany.core.context.event.Event;
+import org.apache.tuscany.core.context.event.ModuleStop;
 
 import java.util.Map;
 import java.util.concurrent.ConcurrentHashMap;
@@ -63,7 +64,9 @@
     }
 
     public void onEvent(Event event){
-        // do nothing
+        if (event instanceof ModuleStop) {
+            shutdownContexts();
+        }
     }
 
     public boolean isCacheable() {
@@ -106,5 +109,23 @@
             }
         }
     }
+
+    private void shutdownContexts(){
+        if (contexts == null){
+            return;
+        }
+        for(Context context: contexts.values()) {
+            try {
+                if (context.getLifecycleState() == RUNNING) {
+                    context.stop();
+                }
+            } catch (CoreRuntimeException e){
+                // TODO send monitoring event
+            }
+
+        }
+        contexts = null;
+    }
+
 
 }


Reply via email to