[webbeans-commits] Webbeans SVN: r1200 - in ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans: context and 3 other directories.

webbeans-commits at lists.jboss.org webbeans-commits at lists.jboss.org
Fri Jan 23 08:33:08 EST 2009


Author: nickarls
Date: 2009-01-23 08:33:08 -0500 (Fri, 23 Jan 2009)
New Revision: 1200

Modified:
   ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/bean/AbstractBean.java
   ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/bean/AbstractClassBean.java
   ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/bean/AbstractProducerBean.java
   ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/bean/EnterpriseBean.java
   ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/bean/SimpleBean.java
   ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/context/ContextualInstance.java
   ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/context/DependentContext.java
   ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/el/WebBeansELResolver.java
   ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/event/ObserverImpl.java
   ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/injection/InjectionPointProvider.java
Log:
Refactoring infra for collecting dependent instances -> DependentContext.

Modified: ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/bean/AbstractBean.java
===================================================================
--- ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/bean/AbstractBean.java	2009-01-23 13:30:20 UTC (rev 1199)
+++ ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/bean/AbstractBean.java	2009-01-23 13:33:08 UTC (rev 1200)
@@ -37,6 +37,7 @@
 import javax.webbeans.manager.Bean;
 
 import org.jboss.webbeans.ManagerImpl;
+import org.jboss.webbeans.context.DependentInstancesStore;
 import org.jboss.webbeans.injection.InjectionPointImpl;
 import org.jboss.webbeans.introspector.AnnotatedItem;
 import org.jboss.webbeans.introspector.AnnotationStore.AnnotationMap;
@@ -62,6 +63,7 @@
    private static Set<Class<?>> STANDARD_WEB_BEAN_CLASSES = new HashSet<Class<?>>(Arrays.asList(Event.class, ManagerImpl.class));
 
    private boolean proxyable;
+   protected DependentInstancesStore dependentInstancesStore;
 
    /**
     * Helper class for getting deployment type
@@ -121,6 +123,7 @@
       super(manager);
       this.manager = manager;
       annotatedInjectionPoints = new HashSet<AnnotatedItem<?, ?>>();
+      dependentInstancesStore = new DependentInstancesStore();
    }
 
    /**
@@ -184,7 +187,7 @@
     * Initializes the deployment types
     */
    protected abstract void initDeploymentType();
-   
+
    protected void initDeploymentTypeFromStereotype()
    {
       AnnotationMap possibleDeploymentTypes = getMergedStereotypes().getPossibleDeploymentTypes();
@@ -229,14 +232,14 @@
          this.name = getSpecializedBean().getName();
          return;
       }
-
+      
       if (beanNameDefaulted || getMergedStereotypes().isBeanNameDefaulted())
       {
          this.name = getDefaultName();
          return;
       }
    }
-   
+
    protected void initProxyable()
    {
       proxyable = Beans.apiTypesAreProxyable(getTypes());
@@ -270,7 +273,7 @@
     * Initializes the scope type
     */
    protected abstract void initScopeType();
-   
+
    protected boolean initScopeTypeFromStereotype()
    {
       Set<Annotation> possibleScopeTypes = getMergedStereotypes().getPossibleScopeTypes();
@@ -309,7 +312,7 @@
          throw new DefinitionException(getAnnotatedItem().getName() + " cannot have deployment type @Standard");
       }
    }
-   
+
    /**
     * Validates that the required types are implemented
     */
@@ -558,7 +561,16 @@
    {
       return proxyable;
    }
-   
+   public DependentInstancesStore getDependentInstancesStore()
+   {
+      return dependentInstancesStore;
+   }
+
+   public boolean isDependent()
+   {
+      return Dependent.class.equals(scopeType);
+   }
+
    public boolean isSpecializing()
    {
       return getAnnotatedItem().isAnnotationPresent(Specializes.class);

Modified: ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/bean/AbstractClassBean.java
===================================================================
--- ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/bean/AbstractClassBean.java	2009-01-23 13:30:20 UTC (rev 1199)
+++ ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/bean/AbstractClassBean.java	2009-01-23 13:33:08 UTC (rev 1200)
@@ -61,7 +61,6 @@
    private Set<AnnotatedField<?>> injectableFields;
    // The initializer methods
    private Set<AnnotatedMethod<?>> initializerMethods;
-   protected DependentInstancesStore dependentInstancesStore;
 
    /**
     * Constructor
@@ -73,7 +72,6 @@
    {
       super(manager);
       this.annotatedItem = type;
-      this.dependentInstancesStore = new DependentInstancesStore();
    }
 
    /**
@@ -269,7 +267,7 @@
          throw new DefinitionException("Web Bean implementation class " + type + " cannot be declared abstract");
       }
    }
-   
+
    @Override
    protected void preCheckSpecialization()
    {
@@ -346,11 +344,6 @@
       return Production.class;
    }
 
-   public DependentInstancesStore getDependentInstancesStore()
-   {
-      return dependentInstancesStore;
-   }
-   
    @Override
    public boolean equals(Object other)
    {

Modified: ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/bean/AbstractProducerBean.java
===================================================================
--- ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/bean/AbstractProducerBean.java	2009-01-23 13:30:20 UTC (rev 1199)
+++ ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/bean/AbstractProducerBean.java	2009-01-23 13:33:08 UTC (rev 1200)
@@ -53,7 +53,7 @@
 {
    // The declaring bean
    protected AbstractClassBean<?> declaringBean;
-   
+
    private static final LogProvider log = Logging.getLogProvider(AbstractProducerBean.class);
 
    /**
@@ -162,8 +162,7 @@
     */
    protected void checkReturnValue(T instance)
    {
-      boolean dependent = Dependent.class.equals(getScopeType());
-      if (instance == null && !dependent)
+      if (instance == null && !isDependent())
       {
          throw new IllegalProductException("Cannot return null from a non-dependent producer method");
       }
@@ -177,7 +176,7 @@
       {
          return;
       }
-      if (dependent && Beans.isPassivatingBean(injectionPoint.getBean()))
+      if (isDependent() && Beans.isPassivatingBean(injectionPoint.getBean()))
       {
          if (injectionPoint.isField())
          {
@@ -208,7 +207,7 @@
          }
       }
    }
-   
+
    @Override
    protected void initScopeType()
    {
@@ -223,16 +222,16 @@
          log.trace("Scope " + scopeType + " specified by annotation");
          return;
       }
-      
+
       initScopeTypeFromStereotype();
-      
+
       if (this.scopeType == null)
       {
          this.scopeType = Dependent.class;
          log.trace("Using default @Dependent scope");
       }
    }
-   
+
    @Override
    protected void initDeploymentType()
    {
@@ -276,8 +275,13 @@
    @Override
    public T create()
    {
+      Object dependentCollector = new Object();
       try
       {
+         if (getDeclaringBean().isDependent())
+         {
+            DependentContext.INSTANCE.setCurrentInjectionInstance(dependentCollector);
+         }
          DependentContext.INSTANCE.setActive(true);
          T instance = produceInstance();
          checkReturnValue(instance);
@@ -285,6 +289,11 @@
       }
       finally
       {
+         if (getDeclaringBean().isDependent())
+         {
+            DependentContext.INSTANCE.clearCurrentInjectionInstance(dependentCollector);
+            dependentInstancesStore.destroyDependentInstances(dependentCollector);
+         }
          DependentContext.INSTANCE.setActive(false);
       }
    }

Modified: ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/bean/EnterpriseBean.java
===================================================================
--- ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/bean/EnterpriseBean.java	2009-01-23 13:30:20 UTC (rev 1199)
+++ ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/bean/EnterpriseBean.java	2009-01-23 13:33:08 UTC (rev 1200)
@@ -309,6 +309,7 @@
       try
       {
          manager.getInjectionPointProvider().pushBean(this);
+         DependentContext.INSTANCE.setCurrentInjectionInstance(instance);
          DependentContext.INSTANCE.setActive(true);
          bindDecorators();
          bindInterceptors();
@@ -318,6 +319,7 @@
       }
       finally
       {
+         DependentContext.INSTANCE.clearCurrentInjectionInstance(instance);
          manager.getInjectionPointProvider().popBean();
          DependentContext.INSTANCE.setActive(false);
       }
@@ -341,7 +343,7 @@
 
    public boolean canCallRemoveMethods()
    {
-      return getEjbDescriptor().isStateful() && Dependent.class.equals(getScopeType());
+      return getEjbDescriptor().isStateful() && isDependent();
    }
    
    @Override

Modified: ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/bean/SimpleBean.java
===================================================================
--- ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/bean/SimpleBean.java	2009-01-23 13:30:20 UTC (rev 1199)
+++ ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/bean/SimpleBean.java	2009-01-23 13:33:08 UTC (rev 1200)
@@ -124,7 +124,7 @@
          try
          {
             instance = constructor.newInstance(manager);
-            injectionPointProvider.setCurrentInjectionInstance(instance);
+            DependentContext.INSTANCE.setCurrentInjectionInstance(instance);
             bindDecorators();
             bindInterceptors();
             injectEjbAndCommonFields(instance);
@@ -134,7 +134,7 @@
          }
          finally
          {
-            injectionPointProvider.clearCurrentInjectionInstance(instance);
+            DependentContext.INSTANCE.clearCurrentInjectionInstance(instance);
             injectionPointProvider.popBean();
          }
          return instance;
@@ -504,8 +504,7 @@
    @Override
    public boolean isSerializable()
    {
-      boolean dependent = Dependent.class.equals(getScopeType());
-      if (dependent)
+      if (isDependent())
       {
          return Reflections.isSerializable(getType());
       }

Modified: ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/context/ContextualInstance.java
===================================================================
--- ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/context/ContextualInstance.java	2009-01-23 13:30:20 UTC (rev 1199)
+++ ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/context/ContextualInstance.java	2009-01-23 13:33:08 UTC (rev 1200)
@@ -1,7 +1,5 @@
 package org.jboss.webbeans.context;
 
-import javax.webbeans.Dependent;
-import javax.webbeans.manager.Bean;
 import javax.webbeans.manager.Contextual;
 
 public class ContextualInstance<T>
@@ -25,13 +23,4 @@
       contextual.destroy(instance);
    }
 
-   public boolean isDependent()
-   {
-      return contextual instanceof Bean && Dependent.class.equals(((Bean<T>)contextual).getScopeType());
-   }
-
-   public Object getInstance()
-   {
-      return instance;
-   }
 }

Modified: ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/context/DependentContext.java
===================================================================
--- ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/context/DependentContext.java	2009-01-23 13:30:20 UTC (rev 1199)
+++ ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/context/DependentContext.java	2009-01-23 13:33:08 UTC (rev 1200)
@@ -23,9 +23,7 @@
 import javax.webbeans.Dependent;
 import javax.webbeans.manager.Contextual;
 
-import org.jboss.webbeans.CurrentManager;
 import org.jboss.webbeans.bean.AbstractClassBean;
-import org.jboss.webbeans.injection.InjectionPointProvider;
 
 /**
  * The dependent context
@@ -38,7 +36,9 @@
    public static DependentContext INSTANCE = new DependentContext();
 
    private ThreadLocal<AtomicInteger> reentrantActiveCount;
+   private ThreadLocal<Object> currentInjectionInstance;
 
+
    /**
     * Constructor
     */
@@ -54,6 +54,7 @@
             return new AtomicInteger(0);
          }
       };
+      this.currentInjectionInstance = new ThreadLocal<Object>();
    }
 
    /**
@@ -69,11 +70,10 @@
          throw new ContextNotActiveException();
       }
       T instance = create == false ? null : bean.create();
-      InjectionPointProvider injectionPointProvider = CurrentManager.rootManager().getInjectionPointProvider();
-      if (bean instanceof AbstractClassBean && injectionPointProvider.isInjecting())
+      if (bean instanceof AbstractClassBean && (currentInjectionInstance.get() != null))
       {
          DependentInstancesStore dependentInstancesStore = ((AbstractClassBean<?>) bean).getDependentInstancesStore();
-         dependentInstancesStore.addDependentInstance(injectionPointProvider.getCurrentInjectionInstance(), ContextualInstance.of(bean, instance));
+         dependentInstancesStore.addDependentInstance(currentInjectionInstance.get(), ContextualInstance.of(bean, instance));
       }
       return instance;
    }
@@ -103,5 +103,21 @@
          }
       }
    }
+   
+   public void setCurrentInjectionInstance(Object currentInjectionInstance)
+   {
+      if (this.currentInjectionInstance.get() == null)
+      {
+         this.currentInjectionInstance.set(currentInjectionInstance);
+      }
+   }
 
+   public void clearCurrentInjectionInstance(Object instance)
+   {
+      if (this.currentInjectionInstance.get() == instance)
+      {
+         this.currentInjectionInstance.set(null);
+      }
+   }
+   
 }

Modified: ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/el/WebBeansELResolver.java
===================================================================
--- ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/el/WebBeansELResolver.java	2009-01-23 13:30:20 UTC (rev 1199)
+++ ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/el/WebBeansELResolver.java	2009-01-23 13:33:08 UTC (rev 1200)
@@ -25,7 +25,6 @@
 
 import org.jboss.webbeans.CurrentManager;
 import org.jboss.webbeans.context.DependentContext;
-import org.jboss.webbeans.injection.InjectionPointProvider;
 
 public class WebBeansELResolver extends ELResolver
 {
@@ -67,11 +66,10 @@
       {
          // TODO Any other way than creating a dummy parent to collect the created dependent objects under?
          Object dependentsCollector = new Object();
-         InjectionPointProvider injectionPointProvider = CurrentManager.rootManager().getInjectionPointProvider();
          try
          {
             DependentContext.INSTANCE.setActive(true);
-            injectionPointProvider.setCurrentInjectionInstance(dependentsCollector);
+            DependentContext.INSTANCE.setCurrentInjectionInstance(dependentsCollector);
             Object value = CurrentManager.rootManager().getInstanceByName(property.toString());
             if (value != null)
             {
@@ -81,7 +79,7 @@
          }
          finally
          {
-            injectionPointProvider.clearCurrentInjectionInstance(dependentsCollector);
+            DependentContext.INSTANCE.clearCurrentInjectionInstance(dependentsCollector);
             DependentContext.INSTANCE.setActive(false);
          }
       }

Modified: ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/event/ObserverImpl.java
===================================================================
--- ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/event/ObserverImpl.java	2009-01-23 13:30:20 UTC (rev 1199)
+++ ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/event/ObserverImpl.java	2009-01-23 13:33:08 UTC (rev 1200)
@@ -29,6 +29,7 @@
 import javax.webbeans.AfterTransactionSuccess;
 import javax.webbeans.BeforeTransactionCompletion;
 import javax.webbeans.DefinitionException;
+import javax.webbeans.Dependent;
 import javax.webbeans.Disposes;
 import javax.webbeans.ExecutionException;
 import javax.webbeans.IfExists;
@@ -38,9 +39,13 @@
 import javax.webbeans.Observes;
 import javax.webbeans.Produces;
 import javax.webbeans.manager.Bean;
+import javax.webbeans.manager.Contextual;
 
 import org.jboss.webbeans.ManagerImpl;
+import org.jboss.webbeans.bean.AbstractBean;
 import org.jboss.webbeans.bean.AbstractClassBean;
+import org.jboss.webbeans.context.ContextualInstance;
+import org.jboss.webbeans.context.DependentContext;
 import org.jboss.webbeans.introspector.AnnotatedMethod;
 import org.jboss.webbeans.introspector.AnnotatedParameter;
 import org.jboss.webbeans.transaction.UserTransaction;
@@ -62,12 +67,12 @@
    /**
     * The known transactional phases a transactional event observer can be
     * interested in
-    */ 
+    */
    protected enum TransactionObservationPhase
    {
       NONE, BEFORE_COMPLETION, AFTER_COMPLETION, AFTER_FAILURE, AFTER_SUCCESS
-   }   
-   
+   }
+
    private final Bean<?> observerBean;
    private final AnnotatedMethod<?> observerMethod;
    private TransactionObservationPhase transactionObservationPhase;
@@ -103,12 +108,12 @@
       this.observerBean = observerBean;
       this.observerMethod = observer;
       checkObserverMethod();
-      
+
       @SuppressWarnings("unchecked")
       Class<T> c = (Class<T>) observerMethod.getAnnotatedParameters(Observes.class).get(0).getType();
       this.eventType = c;
-      
-      this.bindings = observerMethod.getAnnotatedParameters(Observes.class).get(0).getBindingTypesAsArray(); 
+
+      this.bindings = observerMethod.getAnnotatedParameters(Observes.class).get(0).getBindingTypesAsArray();
       initTransactionObservationPhase();
       this.conditional = !observerMethod.getAnnotatedParameters(IfExists.class).isEmpty();
    }
@@ -147,7 +152,8 @@
    }
 
    /**
-    * Performs validation of the observer method for compliance with the specifications.
+    * Performs validation of the observer method for compliance with the
+    * specifications.
     */
    private void checkObserverMethod()
    {
@@ -175,7 +181,7 @@
       }
       // Check annotations on the method to make sure this is not a producer
       // method, initializer method, or destructor method.
-      if ( this.observerMethod.isAnnotationPresent(Produces.class) )
+      if (this.observerMethod.isAnnotationPresent(Produces.class))
       {
          throw new DefinitionException(this + " cannot be annotated with @Produces");
       }
@@ -187,35 +193,50 @@
 
    public void notify(final T event)
    {
-      // Get the most specialized instance of the component
-      Object instance = manager.getInstance(observerBean, !isConditional());
-      if (instance != null)
+      Object instance = null;
+      Object dependentsCollector = new Object();
+      try
       {
-         try
+         if (Dependent.class.equals(observerBean.getScopeType()) && observerBean instanceof AbstractBean)
          {
-            if ( isTransactional() && isTransactionActive() )
+            DependentContext.INSTANCE.setCurrentInjectionInstance(dependentsCollector);
+         }
+         // Get the most specialized instance of the component
+         instance = manager.getInstance(observerBean, !isConditional());
+         if (instance == null)
+         {
+            return;
+         }
+         if (isTransactional() && isTransactionActive())
+         {
+            deferEvent(event);
+         }
+         else
+         {
+            observerMethod.invokeWithSpecialValue(instance, Observes.class, event, manager);
+         }
+      }
+      catch (ExecutionException e)
+      {
+         if ((e.getCause() != null) && (e.getCause() instanceof InvocationTargetException))
+         {
+            InvocationTargetException wrappedException = (InvocationTargetException) e.getCause();
+            if ((wrappedException.getCause() != null) && (RuntimeException.class.isAssignableFrom(wrappedException.getCause().getClass())))
             {
-               deferEvent(event);
+               throw (RuntimeException) wrappedException.getCause();
             }
             else
             {
-               observerMethod.invokeWithSpecialValue(instance, Observes.class, event, manager);
+               throw new ObserverException(wrappedException.getCause().getMessage(), wrappedException.getCause());
             }
          }
-         catch (ExecutionException e)
+      }
+      finally
+      {
+         if (Dependent.class.equals(observerBean.getScopeType()))
          {
-            if ((e.getCause() != null) && (e.getCause() instanceof InvocationTargetException))
-            {
-               InvocationTargetException wrappedException = (InvocationTargetException) e.getCause();
-               if ((wrappedException.getCause() != null) && (RuntimeException.class.isAssignableFrom(wrappedException.getCause().getClass())))
-               {
-                  throw (RuntimeException) wrappedException.getCause();
-               }
-               else
-               {
-                  throw new ObserverException(wrappedException.getCause().getMessage(), wrappedException.getCause());
-               }
-            }
+            ((AbstractBean<?, ?>) observerBean).getDependentInstancesStore().destroyDependentInstances(dependentsCollector);
+            DependentContext.INSTANCE.clearCurrentInjectionInstance(instance);
          }
       }
    }
@@ -230,7 +251,7 @@
       UserTransaction userTransaction = manager.getInstanceByType(UserTransaction.class);
       try
       {
-         return userTransaction!=null && userTransaction.getStatus() == Status.STATUS_ACTIVE;
+         return userTransaction != null && userTransaction.getStatus() == Status.STATUS_ACTIVE;
       }
       catch (SystemException e)
       {
@@ -239,7 +260,8 @@
    }
 
    /**
-    * Defers an event for processing in a later phase of the current transaction.
+    * Defers an event for processing in a later phase of the current
+    * transaction.
     * 
     * Gets the transaction listener, creates a deferred event representation and
     * registers the deferred event.
@@ -283,7 +305,7 @@
    {
       return transactionObservationPhase.equals(currentPhase);
    }
-   
+
    @Override
    public String toString()
    {

Modified: ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/injection/InjectionPointProvider.java
===================================================================
--- ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/injection/InjectionPointProvider.java	2009-01-23 13:30:20 UTC (rev 1199)
+++ ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/injection/InjectionPointProvider.java	2009-01-23 13:33:08 UTC (rev 1200)
@@ -36,29 +36,8 @@
    private final Stack<Bean<?>> beans = new Stack<Bean<?>>();
    // The stack of injection points
    private final Stack<InjectionPoint> injectionPoints = new Stack<InjectionPoint>();
-   private Object currentInjectionInstance;
 
-   public void setCurrentInjectionInstance(Object currentInjectionInstance)
-   {
-      if (this.currentInjectionInstance == null)
-      {
-         this.currentInjectionInstance = currentInjectionInstance;
-      }
-   }
-
-   public void clearCurrentInjectionInstance(Object instance)
-   {
-      if (this.currentInjectionInstance == instance)
-      {
-         this.currentInjectionInstance = null;
-      }
-   }
-
-   public Object getCurrentInjectionInstance()
-   {
-      return currentInjectionInstance;
-   }
-
+   
    /**
     * Pushes a bean to the stack
     * 
@@ -153,9 +132,4 @@
       return "InjectionPointProvider: Bean stack = " + beans.toString() + " InjectionPoint stack = " + injectionPoints.toString();
    }
 
-   public boolean isInjecting()
-   {
-      return currentInjectionInstance != null;
-   }
-
 }




More information about the weld-commits mailing list