[seam-commits] Seam SVN: r8803 - branches/Seam_2_0/src/main/org/jboss/seam/core.

seam-commits at lists.jboss.org seam-commits at lists.jboss.org
Tue Aug 26 00:53:56 EDT 2008


Author: shane.bryzak at jboss.com
Date: 2008-08-26 00:53:56 -0400 (Tue, 26 Aug 2008)
New Revision: 8803

Modified:
   branches/Seam_2_0/src/main/org/jboss/seam/core/BijectionInterceptor.java
Log:
JBSEAM-3301

Modified: branches/Seam_2_0/src/main/org/jboss/seam/core/BijectionInterceptor.java
===================================================================
--- branches/Seam_2_0/src/main/org/jboss/seam/core/BijectionInterceptor.java	2008-08-25 19:38:10 UTC (rev 8802)
+++ branches/Seam_2_0/src/main/org/jboss/seam/core/BijectionInterceptor.java	2008-08-26 04:53:56 UTC (rev 8803)
@@ -1,7 +1,10 @@
 //$Id$
 package org.jboss.seam.core;
 
+import java.util.concurrent.locks.ReentrantLock;
+
 import org.jboss.seam.Component;
+import org.jboss.seam.ScopeType;
 import org.jboss.seam.annotations.intercept.AroundInvoke;
 import org.jboss.seam.annotations.intercept.Interceptor;
 import org.jboss.seam.intercept.AbstractInterceptor;
@@ -12,51 +15,103 @@
  * invoking, outject dependencies back into their context.
  * 
  * @author Gavin King
+ * @author Shane Bryzak
  */
 @Interceptor
 public class BijectionInterceptor extends AbstractInterceptor
 {
    private static final long serialVersionUID = 4686458105931528659L;
    
-   private Integer counter = 0;
+   private boolean injected;
+   
+   private boolean canDisinject;
+   
+   private int counter = 0;
+   
+   private ReentrantLock lock = new ReentrantLock();
+   
+   @Override
+   public void setComponent(Component component)
+   {
+      super.setComponent(component);
+            
+      canDisinject = !(component.getScope().equals(ScopeType.SESSION) 
+            || component.getScope().equals(ScopeType.APPLICATION));
+   }
       
    @AroundInvoke
    public Object aroundInvoke(InvocationContext invocation) throws Exception
    {
+      boolean enforceRequired = !component.isLifecycleMethod( invocation.getMethod() );      
+      
       try
-      {
-         synchronized (counter)
+      {    
+         lock.lock();
+         try
          {
-            if (counter == 0)
-            {
-               Component component = getComponent();
-               boolean enforceRequired = !component.isLifecycleMethod( invocation.getMethod() );
+            if (!injected)
+            {              
                component.inject( invocation.getTarget(), enforceRequired );
+               injected = true;
             }
+            
             counter++;
          }
-         
+         finally
+         {
+            lock.unlock();
+         } 
+                           
          Object result = invocation.proceed();
-         
-         if (counter == 1)
+            
+         lock.lock();
+         try
          {
-            Component component = getComponent();
-            boolean enforceRequired = !component.isLifecycleMethod( invocation.getMethod() );
-            component.outject( invocation.getTarget(), enforceRequired );
+            counter--;
+            
+            if (counter == 0)
+            {
+               try
+               {                     
+                  component.outject( invocation.getTarget(), enforceRequired );
+               }
+               finally
+               {
+                  // Avoid an extra lock by disinjecting here instead of the finally block
+                  if (injected && canDisinject)
+                  {
+                     injected = false;
+                     component.disinject( invocation.getTarget() );
+                  }
+               }   
+            }
          }
+         finally
+         {
+            lock.unlock();
+         }
+         
          return result;
       }
       finally
-      {
-         synchronized (counter)
+      {            
+         if (injected && canDisinject)
          {
-            if (counter == 1)
+            lock.lock();
+            try
             {
-               Component component = getComponent();
-               component.disinject( invocation.getTarget() );
+               counter--;
+               
+               if (counter == 0)
+               {
+                  injected = false;
+                  component.disinject( invocation.getTarget() );     
+               }
             }
-            counter--;
-            
+            finally
+            {
+               lock.unlock();
+            }
          }
       }
    }




More information about the seam-commits mailing list