cocoon-cvs mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From vgritse...@apache.org
Subject svn commit: r265589 - /cocoon/branches/BRANCH_2_1_X/src/java/org/apache/cocoon/components/CocoonComponentManager.java
Date Wed, 31 Aug 2005 21:45:19 GMT
Author: vgritsenko
Date: Wed Aug 31 14:45:13 2005
New Revision: 265589

URL: http://svn.apache.org/viewcvs?rev=265589&view=rev
Log:
Fix memory leak: Setting ThreadLocal to null allows it to be garbage collected

Modified:
    cocoon/branches/BRANCH_2_1_X/src/java/org/apache/cocoon/components/CocoonComponentManager.java

Modified: cocoon/branches/BRANCH_2_1_X/src/java/org/apache/cocoon/components/CocoonComponentManager.java
URL: http://svn.apache.org/viewcvs/cocoon/branches/BRANCH_2_1_X/src/java/org/apache/cocoon/components/CocoonComponentManager.java?rev=265589&r1=265588&r2=265589&view=diff
==============================================================================
--- cocoon/branches/BRANCH_2_1_X/src/java/org/apache/cocoon/components/CocoonComponentManager.java
(original)
+++ cocoon/branches/BRANCH_2_1_X/src/java/org/apache/cocoon/components/CocoonComponentManager.java
Wed Aug 31 14:45:13 2005
@@ -58,7 +58,7 @@
 
     /** The key used to store the current process environment */
     private static final String PROCESS_KEY = CocoonComponentManager.class.getName();
-    
+
     /** The environment attribute used to keep track of the actual environment in which the
pipeline was built. */
     private static final String PROCESSOR_ATTR = "CocoonComponentManager.processor";
 
@@ -81,25 +81,26 @@
     /** has this been disposed? */
     private boolean wasDisposed;
 
+
     /** Create the ComponentManager */
     public CocoonComponentManager() {
-        super( null, Thread.currentThread().getContextClassLoader() );
+        super(null, Thread.currentThread().getContextClassLoader());
     }
 
     /** Create the ComponentManager with a Classloader */
     public CocoonComponentManager(final ClassLoader loader) {
-        super( null, loader );
+        super(null, loader);
     }
 
     /** Create the ComponentManager with a Classloader and parent ComponentManager */
     public CocoonComponentManager(final ComponentManager manager, final ClassLoader loader)
{
-        super( manager, loader );
+        super(manager, loader);
         this.parentManager = manager;
     }
 
     /** Create the ComponentManager with a parent ComponentManager */
     public CocoonComponentManager(final ComponentManager manager) {
-        super( manager);
+        super(manager);
         this.parentManager = manager;
     }
 
@@ -111,21 +112,22 @@
     public static void enterEnvironment(Environment      env,
                                         ComponentManager manager,
                                         Processor        processor) {
-        if ( null == env || null == manager || null == processor) {
-            throw new RuntimeException("CocoonComponentManager.enterEnvironment: all parameters
must be set: " + env + " - " + manager + " - " + processor);
+        if (null == env || null == manager || null == processor) {
+            throw new RuntimeException("CocoonComponentManager.enterEnvironment: " +
+                                       "All parameters must be set: " + env + " - " + manager
+ " - " + processor);
         }
 
         EnvironmentStack stack = (EnvironmentStack)environmentStack.get();
-		if (stack == null) {
+        if (stack == null) {
             stack = new EnvironmentStack();
-			environmentStack.set(stack);
-		}
-		stack.push(new EnvironmentStack.Item(env, processor, manager, stack.getOffset()));
+            environmentStack.set(stack);
+        }
+        stack.push(new EnvironmentStack.Item(env, processor, manager, stack.getOffset()));
         stack.setOffset(stack.size()-1);
 
         env.setAttribute(PROCESSOR_ATTR, processor);
     }
-    
+
     /**
      * This hook must be called by the sitemap each time a sitemap is left.
      * It's the counterpart to {@link #enterEnvironment(Environment, ComponentManager, Processor)}.
@@ -138,7 +140,7 @@
     /**
      * This hook must be called by the sitemap each time a sitemap is left.
      * It's the counterpart to {@link #enterEnvironment(Environment, ComponentManager, Processor)}.
-     * 
+     *
      * @param success indicates if the request was successfully handled by the environment
that's being left
      */
     public static void leaveEnvironment(boolean success) {
@@ -146,19 +148,22 @@
         final EnvironmentStack.Item objs = (EnvironmentStack.Item)stack.pop();
         stack.setOffset(objs.offset);
 
-        if ( stack.isEmpty() ) {
+        if (stack.isEmpty()) {
             final Environment env = objs.env;
             final Map globalComponents = (Map)env.getAttribute(GlobalRequestLifecycleComponent.class.getName());
-            if ( globalComponents != null) {
+            if (globalComponents != null) {
 
                 final Iterator iter = globalComponents.values().iterator();
-                while ( iter.hasNext() ) {
+                while (iter.hasNext()) {
                     final Object[] o = (Object[])iter.next();
                     final Component c = (Component)o[0];
                     ((CocoonComponentManager)o[1]).releaseRLComponent( c );
                 }
             }
             env.removeAttribute(GlobalRequestLifecycleComponent.class.getName());
+
+            // Setting this ThreadLocal to null allows it to be garbage collected
+            CocoonComponentManager.environmentStack.set(null);
         } else {
             if (!success) {
                 // Restore the current processor as being the active one
@@ -216,12 +221,12 @@
      * @return A unique key within this thread.
      */
     public static Object startProcessing(Environment env) {
-		if (null == env) {
-			throw new RuntimeException("CocoonComponentManager.startProcessing: environment must be
set.");
-		}
+        if (null == env) {
+            throw new RuntimeException("CocoonComponentManager.startProcessing: environment
must be set.");
+        }
         final EnvironmentDescription desc = new EnvironmentDescription(env);
         env.getObjectModel().put(PROCESS_KEY, desc);
-		env.startingProcessing();
+        env.startingProcessing();
         return desc;
     }
 
@@ -582,7 +587,7 @@
         }
         parentAwareComponents = null;  // null to save memory, and catch logic bugs.
     }
-    
+
     /**
      * A runnable wrapper that inherits the environment stack of the thread it is
      * created in.
@@ -619,7 +624,7 @@
             // A CocoonThread is meant to start and die within the execution period of the
parent request,
             // and it is an error if it lives longer as the parent environment is no more
valid.
         }
-        
+
         abstract protected void doRun();
     }
 }



Mime
View raw message