[seam-commits] Seam SVN: r8895 - in branches/Seam_2_0/src: main/org/jboss/seam/core and 2 other directories.

seam-commits at lists.jboss.org seam-commits at lists.jboss.org
Thu Sep 4 00:52:51 EDT 2008


Author: matt.drees
Date: 2008-09-04 00:52:51 -0400 (Thu, 04 Sep 2008)
New Revision: 8895

Removed:
   branches/Seam_2_0/src/main/org/jboss/seam/CyclicDependencyException.java
Modified:
   branches/Seam_2_0/src/main/org/jboss/seam/core/BijectionInterceptor.java
   branches/Seam_2_0/src/main/org/jboss/seam/util/Reflections.java
   branches/Seam_2_0/src/test/unit/org/jboss/seam/test/unit/InterceptorTest.java
Log:
Remove CyclicDependencyException from 2.0 branch, and replace with warning in log

Deleted: branches/Seam_2_0/src/main/org/jboss/seam/CyclicDependencyException.java
===================================================================
--- branches/Seam_2_0/src/main/org/jboss/seam/CyclicDependencyException.java	2008-09-04 04:46:09 UTC (rev 8894)
+++ branches/Seam_2_0/src/main/org/jboss/seam/CyclicDependencyException.java	2008-09-04 04:52:51 UTC (rev 8895)
@@ -1,98 +0,0 @@
-package org.jboss.seam;
-
-import java.lang.reflect.Method;
-import java.util.ArrayList;
-import java.util.List;
-
-import org.jboss.seam.core.BijectionInterceptor;
-
-/**
- * An exception that is thrown when {@link BijectionInterceptor} detects that a
- * component's dependencies cannot be injected due to a cyclic dependency. As
- * the exception is passed up the stack, the call sequence is recorded so that a
- * useful exception message can be constructed.
- * 
- * @author Matt Drees
- * 
- */
-public class CyclicDependencyException extends IllegalStateException
-{
-
-   /**
-    * stores the invocations in reverse call order
-    */
-   private final List<String> invocations = new ArrayList<String>();
-   private String tailComponentName;
-   private boolean cycleComplete;
-
-   /**
-    * Records this invocation's component name and method to be displayed in
-    * {@link #getMessage()}, unless this invocation is not part of the detected
-    * cycle. This method will be successively called as the exception is
-    * propagated up the stack.
-    * 
-    * @param componentName
-    * @param method
-    */
-   public void addInvocation(String componentName, Method method)
-   {
-      if (cycleComplete)
-      {
-         return;
-      }
-
-      if (invocations.isEmpty())
-      {
-         tailComponentName = componentName;
-      }
-      else
-      {
-         if (tailComponentName.equals(componentName))
-         {
-            cycleComplete = true;
-         }
-      }
-      invocations.add(createInvocationLabel(componentName, method));
-   }
-
-   /**
-    * returns e.g. "foo.doSomething()"
-    */
-   private String createInvocationLabel(String componentName, Method method)
-   {
-      String invocationLabel = componentName + "." + method.getName() + "(";
-      int i = 1;
-      for (Class<?> parameterType : method.getParameterTypes())
-      {
-         invocationLabel += parameterType.getSimpleName();
-         if (i < method.getParameterTypes().length)
-         {
-            invocationLabel += ", ";
-         }
-         i++;
-      }
-      invocationLabel += ")";
-      return invocationLabel;
-   }
-
-   @Override
-   public String getMessage()
-   {
-      if (!cycleComplete)
-      {
-         return "Cyclic dependency found";
-      }
-      else
-      {
-         String message = "Injection into " + tailComponentName + " resulted in a dependency cycle, requiring the invocation of " + invocations.get(0) + ".  The complete cycle: ";
-         for (int i = invocations.size() - 1; i >= 0; i--)
-         {
-            message += invocations.get(i);
-            if (i != 0)
-               message += " -> ";
-         }
-         return message;
-      }
-   }
-
-}

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-09-04 04:46:09 UTC (rev 8894)
+++ branches/Seam_2_0/src/main/org/jboss/seam/core/BijectionInterceptor.java	2008-09-04 04:52:51 UTC (rev 8895)
@@ -1,15 +1,17 @@
 //$Id$
 package org.jboss.seam.core;
 
+import java.lang.reflect.Method;
 import java.util.concurrent.locks.ReentrantLock;
 
 import org.jboss.seam.Component;
-import org.jboss.seam.CyclicDependencyException;
 import org.jboss.seam.annotations.intercept.AroundInvoke;
 import org.jboss.seam.annotations.intercept.Interceptor;
 import org.jboss.seam.intercept.AbstractInterceptor;
 import org.jboss.seam.intercept.InvocationContext;
-import org.jboss.seam.util.EJB;
+import org.jboss.seam.log.LogProvider;
+import org.jboss.seam.log.Logging;
+import org.jboss.seam.util.Reflections;
 
 /**
  * Before invoking the component, inject all dependencies. After
@@ -22,6 +24,8 @@
 public class BijectionInterceptor extends AbstractInterceptor
 {
    private static final long serialVersionUID = 4686458105931528659L;
+
+   private static final LogProvider log = Logging.getLogProvider(BijectionInterceptor.class);
    
    private boolean injected;
    
@@ -31,11 +35,14 @@
    
    private ReentrantLock lock = new ReentrantLock();
          
+   private String initialMethod;
+   
    @AroundInvoke
    public Object aroundInvoke(InvocationContext invocation) throws Exception
    {
       Component component = getComponent();    
-      boolean enforceRequired = !component.isLifecycleMethod( invocation.getMethod() );      
+      Method method = invocation.getMethod();
+      boolean enforceRequired = !component.isLifecycleMethod( method );      
       
       try
       {    
@@ -44,20 +51,26 @@
          {
             if (!injected)
             {              
-               if (injecting == true)
+               if (injecting)
                {
-                  throw new CyclicDependencyException();
+                  log.warn("Injecting dependencies into " + component.getName() + " for the invocation of " 
+                        + initialMethod + " caused the invocation of a reentrant method: " + Reflections.toString(method) 
+                        + ".  Some injected dependencies may not be available for the duration of this method invocation.");
                }
-
-               injecting = true;
-               try
+               else
                {
-                  component.inject(invocation.getTarget(), enforceRequired);
+                  injecting = true;
+                  try
+                  {
+                     initialMethod = Reflections.toString(method);
+                     component.inject(invocation.getTarget(), enforceRequired);
+                  }
+                  finally
+                  {
+                     injecting = false;
+                     initialMethod = null;
+                  }
                }
-               finally
-               {
-                  injecting = false;
-               }
                injected = true;
             }
             
@@ -99,20 +112,6 @@
          
          return result;
       }
-      catch (Exception e)
-      {
-         Exception root = e;
-         while (EJB.getCause(root) != null)
-         {
-            root = EJB.getCause(root);
-         }
-         if (root instanceof CyclicDependencyException)
-         {
-            CyclicDependencyException cyclicDependencyException = (CyclicDependencyException) root;
-            cyclicDependencyException.addInvocation(getComponent().getName(), invocation.getMethod());
-         }
-         throw e;
-      }
       finally
       {            
          if (injected)

Modified: branches/Seam_2_0/src/main/org/jboss/seam/util/Reflections.java
===================================================================
--- branches/Seam_2_0/src/main/org/jboss/seam/util/Reflections.java	2008-09-04 04:46:09 UTC (rev 8894)
+++ branches/Seam_2_0/src/main/org/jboss/seam/util/Reflections.java	2008-09-04 04:52:51 UTC (rev 8895)
@@ -137,7 +137,7 @@
       }
    }
    
-   private static String toString(Method method)
+   public static String toString(Method method)
    {
       return Strings.unqualify( method.getDeclaringClass().getName() ) + 
             '.' + 

Modified: branches/Seam_2_0/src/test/unit/org/jboss/seam/test/unit/InterceptorTest.java
===================================================================
--- branches/Seam_2_0/src/test/unit/org/jboss/seam/test/unit/InterceptorTest.java	2008-09-04 04:46:09 UTC (rev 8894)
+++ branches/Seam_2_0/src/test/unit/org/jboss/seam/test/unit/InterceptorTest.java	2008-09-04 04:52:51 UTC (rev 8895)
@@ -7,7 +7,6 @@
 import javax.faces.event.PhaseId;
 
 import org.jboss.seam.Component;
-import org.jboss.seam.CyclicDependencyException;
 import org.jboss.seam.NoConversationException;
 import org.jboss.seam.RequiredException;
 import org.jboss.seam.Seam;
@@ -79,7 +78,14 @@
             return bar;
          }
 
+         Method method = Foo.class.getMethod("foo");
          @Override
+         public Method getMethod()
+         {
+            return method;
+         }
+         
+         @Override
          public Object proceed() throws Exception
          {
             assert bar.otherFoo==foo;
@@ -236,7 +242,7 @@
    }
    
    @Test
-   public void testCyclicDependencyThowsException() throws Exception
+   public void testCyclicDependencyDoesNotStackOverflow() throws Exception
    {
       MockServletContext servletContext = new MockServletContext();
       ServletLifecycle.beginApplication(servletContext);
@@ -357,12 +363,7 @@
       appContext.set("cyclicFoo", cyclicFooProxy);
       appContext.set("cyclicBar", cyclicBarProxy);
       
-      try
-      {
-         cyclicFooProxy.getFooBar();
-         assert false : "cyclic dependency not detected";
-      }
-      catch (CyclicDependencyException e) {}
+      cyclicFooProxy.getFooBar();
       
    }
 




More information about the seam-commits mailing list