[seam-commits] Seam SVN: r8823 - trunk/src/main/org/jboss/seam/core.

seam-commits at lists.jboss.org seam-commits at lists.jboss.org
Tue Aug 26 19:10:54 EDT 2008


Author: shane.bryzak at jboss.com
Date: 2008-08-26 19:10:53 -0400 (Tue, 26 Aug 2008)
New Revision: 8823

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

Modified: trunk/src/main/org/jboss/seam/core/BijectionInterceptor.java
===================================================================
--- trunk/src/main/org/jboss/seam/core/BijectionInterceptor.java	2008-08-26 23:04:38 UTC (rev 8822)
+++ trunk/src/main/org/jboss/seam/core/BijectionInterceptor.java	2008-08-26 23:10:53 UTC (rev 8823)
@@ -1,6 +1,8 @@
 //$Id$
 package org.jboss.seam.core;
 
+import java.util.concurrent.locks.ReentrantLock;
+
 import org.jboss.seam.Component;
 import org.jboss.seam.annotations.intercept.AroundInvoke;
 import org.jboss.seam.annotations.intercept.Interceptor;
@@ -12,51 +14,99 @@
  * 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 int counter = 0;
+   
+   private ReentrantLock lock = new ReentrantLock();
+   
+   @Override
+   public void setComponent(Component component)
+   {
+      super.setComponent(component);
+   }
       
    @AroundInvoke
    public Object aroundInvoke(InvocationContext invocation) throws Exception
    {
+      Component component = getComponent();
+      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)
+                  {
+                     injected = false;
+                     component.disinject( invocation.getTarget() );
+                  }
+               }   
+            }
          }
+         finally
+         {
+            lock.unlock();
+         }
+         
          return result;
       }
       finally
-      {
-         synchronized (counter)
+      {            
+         if (injected)
          {
-            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