Revision: 869
          http://stripes.svn.sourceforge.net/stripes/?rev=869&view=rev
Author:   tfenne
Date:     2008-03-01 06:23:10 -0800 (Sat, 01 Mar 2008)

Log Message:
-----------
Change to keep track of all Configuration objects instantiated by the 
StripesFilter, and to use that list to return the single Configuration object 
when possible and the ThreadLocal configurationStash is blank.

Modified Paths:
--------------
    trunk/stripes/src/net/sourceforge/stripes/controller/StripesFilter.java

Modified: 
trunk/stripes/src/net/sourceforge/stripes/controller/StripesFilter.java
===================================================================
--- trunk/stripes/src/net/sourceforge/stripes/controller/StripesFilter.java     
2008-02-27 14:40:42 UTC (rev 868)
+++ trunk/stripes/src/net/sourceforge/stripes/controller/StripesFilter.java     
2008-03-01 14:23:10 UTC (rev 869)
@@ -33,10 +33,14 @@
 import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletRequestWrapper;
 import javax.servlet.http.HttpServletResponse;
+import java.beans.Introspector;
 import java.io.IOException;
+import java.lang.ref.WeakReference;
+import java.util.HashSet;
+import java.util.Iterator;
 import java.util.Locale;
 import java.util.Map;
-import java.beans.Introspector;
+import java.util.Set;
 
 /**
  * The Stripes filter is used to ensure that all requests coming to a Stripes 
application
@@ -69,6 +73,15 @@
     private static final ThreadLocal<Configuration> configurationStash = new 
ThreadLocal<Configuration>();
 
     /**
+     * A set of weak references to all the Configuration objects that this 
class has ever
+     * seen. Uses weak references to allow garbage collection to reap these 
objects if this
+     * is the only reference left.  Used to determine if there is only one 
active Configuration
+     * for the VM, and if so return it even when the Configuration isn't set 
in the thread local.
+     */
+    private static final Set<WeakReference<Configuration>> configurations =
+            new HashSet<WeakReference<Configuration>>(); 
+
+    /**
      * Some operations should only be done if the current invocation of
      * [EMAIL PROTECTED] #doFilter(ServletRequest, ServletResponse, 
FilterChain)} is the
      * first in the filter chain. This [EMAIL PROTECTED] ThreadLocal} keeps 
track of
@@ -112,6 +125,7 @@
 
         this.configuration.setBootstrapPropertyResolver(bootstrap);
         this.configuration.init();
+        StripesFilter.configurations.add(new 
WeakReference<Configuration>(this.configuration));
 
         this.servletContext = filterConfig.getServletContext();
         this.servletContext.setAttribute(StripesFilter.class.getName(), this);
@@ -127,7 +141,25 @@
     public static Configuration getConfiguration() {
         Configuration configuration = StripesFilter.configurationStash.get();
 
+        // If the configuration wasn't available in thread local, check to see 
if we only
+        // know about one configuration in total, and if so use that one
         if (configuration == null) {
+            synchronized (StripesFilter.configurations) {
+                // Remove any references from the set that have been cleared
+                Iterator<WeakReference<Configuration>> iterator = 
StripesFilter.configurations.iterator();
+                while (iterator.hasNext()) {
+                    WeakReference<Configuration> ref = iterator.next();
+                    if (ref.get() == null) iterator.remove();
+                }
+
+                // If there is one and only one Configuration active, take it
+                if (StripesFilter.configurations.size() == 1) {
+                    configuration = 
StripesFilter.configurations.iterator().next().get();
+                }
+            }
+        }
+
+        if (configuration == null) {
             StripesRuntimeException sre = new StripesRuntimeException(
                     "Something is trying to access the current Stripes 
configuration but the " +
                     "current request was never routed through the 
StripesFilter! As a result " +


This was sent by the SourceForge.net collaborative development platform, the 
world's largest Open Source development site.

-------------------------------------------------------------------------
This SF.net email is sponsored by: Microsoft
Defy all challenges. Microsoft(R) Visual Studio 2008.
http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/
_______________________________________________
Stripes-development mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/stripes-development

Reply via email to