[seam-commits] Seam SVN: r13493 - in branches/community/Seam_2_2: src/main/org/jboss/seam/servlet and 1 other directory.

seam-commits at lists.jboss.org seam-commits at lists.jboss.org
Fri Jul 23 09:17:21 EDT 2010


Author: manaRH
Date: 2010-07-23 09:17:20 -0400 (Fri, 23 Jul 2010)
New Revision: 13493

Modified:
   branches/community/Seam_2_2/examples/restbay/src/org/jboss/seam/example/restbay/test/SecurityTest.java
   branches/community/Seam_2_2/src/main/org/jboss/seam/servlet/ContextualHttpServletRequest.java
Log:
JBSEAM-4542

Modified: branches/community/Seam_2_2/examples/restbay/src/org/jboss/seam/example/restbay/test/SecurityTest.java
===================================================================
--- branches/community/Seam_2_2/examples/restbay/src/org/jboss/seam/example/restbay/test/SecurityTest.java	2010-07-23 12:52:42 UTC (rev 13492)
+++ branches/community/Seam_2_2/examples/restbay/src/org/jboss/seam/example/restbay/test/SecurityTest.java	2010-07-23 13:17:20 UTC (rev 13493)
@@ -184,7 +184,7 @@
       }.run();
    }
    
-   // @Test
+    @Test
    // JBPAPP-3713
    public void synchronizationsLookup() throws Exception
    {

Modified: branches/community/Seam_2_2/src/main/org/jboss/seam/servlet/ContextualHttpServletRequest.java
===================================================================
--- branches/community/Seam_2_2/src/main/org/jboss/seam/servlet/ContextualHttpServletRequest.java	2010-07-23 12:52:42 UTC (rev 13492)
+++ branches/community/Seam_2_2/src/main/org/jboss/seam/servlet/ContextualHttpServletRequest.java	2010-07-23 13:17:20 UTC (rev 13493)
@@ -1,6 +1,7 @@
 package org.jboss.seam.servlet;
 
 import java.io.IOException;
+import java.util.concurrent.atomic.AtomicInteger;
 
 import javax.servlet.ServletException;
 import javax.servlet.http.HttpServletRequest;
@@ -17,6 +18,7 @@
  * Perform work in a full set of Seam contexts
  * 
  * @author Gavin King
+ * @author Marek Novotny
  *
  */
 public abstract class ContextualHttpServletRequest
@@ -25,51 +27,70 @@
 
    private final HttpServletRequest request;
    
+   private static ThreadLocal<AtomicInteger> count = new ThreadLocal<AtomicInteger>();
+     
    public ContextualHttpServletRequest(HttpServletRequest request)
    {
-      this.request = request;
+      this.request = request;      
    }
    
    public abstract void process() throws Exception;
    
    public void run() throws ServletException, IOException
-   {
-      log.debug("beginning request");
+   {      
+      log.debug("beginning request"); 
+           
       // Force creation of the session
       if (request.getSession(false) == null)
       {
          request.getSession(true);
       }
-      ServletLifecycle.beginRequest(request);
-      ServletContexts.instance().setRequest(request);
-      restoreConversationId();
-      Manager.instance().restoreConversation();
-      ServletLifecycle.resumeConversation(request);
-      handleConversationPropagation();
       
+      // Begin request and Seam life cycle only if it is not nested
+      // ContextualHttpServletRequest
+      if (getCounterValue() == 0)
+      {         
+         ServletLifecycle.beginRequest(request);
+         ServletContexts.instance().setRequest(request);         
+         restoreConversationId();
+         Manager.instance().restoreConversation();
+         ServletLifecycle.resumeConversation(request);
+         handleConversationPropagation();
+      }
       
-      
       try
       {
-         process();
-         //TODO: conversation timeout
-         Manager.instance().endRequest( new ServletRequestSessionMap(request)  );
-         ServletLifecycle.endRequest(request);
+         incrementCounterValue();
+         
+         process();                 
+
+         decrementCounterValue();
+         
+         // End request only if it is not nested ContextualHttpServletRequest
+         if (getCounterValue() == 0)
+         {
+            //TODO: conversation timeout
+            Manager.instance().endRequest( new ServletRequestSessionMap(request)  );
+            ServletLifecycle.endRequest(request);
+         }
       }
       catch (IOException ioe)
       {
+         removeCounter();
          Lifecycle.endRequest();
          log.debug("ended request due to exception");
          throw ioe;
       }
       catch (ServletException se)
       {
+         removeCounter();
          Lifecycle.endRequest();
          log.debug("ended request due to exception");
          throw se;
       }
       catch (Exception e)
       {
+         removeCounter();
          Lifecycle.endRequest();
          log.debug("ended request due to exception");
          throw new ServletException(e);
@@ -90,4 +111,65 @@
       ConversationPropagation.instance().restoreConversationId( request.getParameterMap() );
    }
    
+   /*
+    * Getter for ThreadLocal counter value
+    */
+   private int getCounterValue()
+   {
+      AtomicInteger i = count.get();
+      if (i == null || i.intValue() < 0)
+      {
+         log.trace("Getting 0" );
+         return 0;
+      }
+      else
+      {
+         log.trace("Getting " + i.intValue());
+         return i.intValue();
+      }
+   }
+   
+   /*
+    * Increments ThreadLocal counter value 
+    */
+   private void incrementCounterValue()
+   {
+      AtomicInteger i = count.get();
+      if (i == null || i.intValue() < 0)
+      {
+         i = new AtomicInteger(0);
+         count.set(i);
+      }
+      i.incrementAndGet();
+      log.trace("Incrementing to " + count.get());
+   }
+   
+   /*
+    *  Decrements ThreadLocal counter value
+    */
+   private void decrementCounterValue()
+   {
+      AtomicInteger i = count.get();
+      if (i == null)
+      {
+         log.trace("OOps, something removed counter befor end of request!");
+         // we should never get here...
+         throw new IllegalStateException("Counter for nested ContextualHttpServletRequest was removed before it should be!");
+      }
+      if (i.intValue() > 0)
+      {
+         i.decrementAndGet();
+         log.trace("Decrementing to " + count.get());
+      }
+   }
+   
+   /*
+    * Removes ThreadLocal counter
+    */
+   private void removeCounter() 
+   {
+      log.trace("Removing ThreadLocal counter");
+      count.remove();
+   } 
+   
 }



More information about the seam-commits mailing list