openwebbeans-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From strub...@apache.org
Subject svn commit: r1195195 - in /openwebbeans/trunk/webbeans-impl/src: main/java/org/apache/webbeans/annotation/ main/java/org/apache/webbeans/component/ main/java/org/apache/webbeans/container/ main/java/org/apache/webbeans/inject/instance/ test/java/org/ap...
Date Sun, 30 Oct 2011 17:32:02 GMT
Author: struberg
Date: Sun Oct 30 17:32:01 2011
New Revision: 1195195

URL: http://svn.apache.org/viewvc?rev=1195195&view=rev
Log:
OWB-627 add dependent beans created via Instance<T>#get() to the CreationalContext

This is needed to properly destroy those contextual instances again. 

Added:
    openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/newtests/managed/instance/beans/DependentBean.java
      - copied, changed from r1195141, openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/newtests/managed/instance/beans/UnparameterizedInstanceBean.java
    openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/newtests/managed/instance/beans/InstanceForDependentBean.java
Modified:
    openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/annotation/AnnotationManager.java
    openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/component/InstanceBean.java
    openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/container/BeanManagerImpl.java
    openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/inject/instance/InstanceFactory.java
    openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/inject/instance/InstanceImpl.java
    openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/newtests/AbstractUnitTest.java
    openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/newtests/managed/instance/InjectedInstanceComponentTest.java

Modified: openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/annotation/AnnotationManager.java
URL: http://svn.apache.org/viewvc/openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/annotation/AnnotationManager.java?rev=1195195&r1=1195194&r2=1195195&view=diff
==============================================================================
--- openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/annotation/AnnotationManager.java
(original)
+++ openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/annotation/AnnotationManager.java
Sun Oct 30 17:32:01 2011
@@ -53,6 +53,7 @@ import java.lang.reflect.Field;
 import java.lang.reflect.Method;
 import java.lang.reflect.Type;
 import java.util.ArrayList;
+import java.util.Arrays;
 import java.util.HashSet;
 import java.util.List;
 import java.util.Set;
@@ -343,7 +344,8 @@ public final class AnnotationManager
         //check for duplicate annotations
         if (qualifierAnnots.length != annSet.size())
         {
-            throw new IllegalArgumentException("Qualifier annotations can not contain duplicate
qualifiers:" + qualifierAnnots);
+            throw new IllegalArgumentException("Qualifier annotations can not contain duplicate
qualifiers:"
+                                               + Arrays.toString(qualifierAnnots));
         }
 
         checkQualifierConditions(annSet);

Modified: openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/component/InstanceBean.java
URL: http://svn.apache.org/viewvc/openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/component/InstanceBean.java?rev=1195195&r1=1195194&r2=1195195&view=diff
==============================================================================
--- openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/component/InstanceBean.java
(original)
+++ openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/component/InstanceBean.java
Sun Oct 30 17:32:01 2011
@@ -20,6 +20,7 @@ package org.apache.webbeans.component;
 
 import java.lang.annotation.Annotation;
 import java.lang.reflect.ParameterizedType;
+import java.lang.reflect.Type;
 import java.util.Set;
 
 import javax.enterprise.context.spi.CreationalContext;
@@ -28,6 +29,7 @@ import javax.enterprise.inject.spi.Injec
 import javax.enterprise.util.TypeLiteral;
 
 import org.apache.webbeans.config.WebBeansContext;
+import org.apache.webbeans.inject.AbstractInjectable;
 import org.apache.webbeans.inject.instance.InstanceFactory;
 
 public class InstanceBean<T> extends AbstractOwbBean<Instance<T>>
@@ -49,10 +51,23 @@ public class InstanceBean<T> extends Abs
         {
             ParameterizedType injectedType = (ParameterizedType)local.get().getType();
             Set<Annotation> qualifiers = local.get().getQualifiers();
-            Instance<T> instance = InstanceFactory.getInstance(injectedType.getActualTypeArguments()[0],

-                    (local.get().getBean() == null) ? null : local.get().getBean().getBeanClass(),
-                    getWebBeansContext(), 
-                            qualifiers.toArray(new Annotation[qualifiers.size()]));
+            Type type = injectedType.getActualTypeArguments()[0];
+            InjectionPoint injectionPoint = local.get();
+            Class injectionPointClass = null;
+
+            if (injectionPoint != null)
+            {
+                if (injectionPoint.getBean() != null)
+                {
+                    injectionPointClass = injectionPoint.getBean().getBeanClass();
+                }
+            }
+
+            Object ownerInstance = AbstractInjectable.instanceUnderInjection.get();
+
+            Instance<T> instance = InstanceFactory.getInstance(type, injectionPointClass,
getWebBeansContext(),
+                                                               creationalContext, ownerInstance,
+                                                               qualifiers.toArray(new Annotation[qualifiers.size()]));
             
             return instance;
         }
@@ -63,16 +78,13 @@ public class InstanceBean<T> extends Abs
         }
     }
 
-
     /* (non-Javadoc)
-     * @see org.apache.webbeans.component.AbstractOwbBean#isPassivationCapable()
-     */
+    * @see org.apache.webbeans.component.AbstractOwbBean#isPassivationCapable()
+    */
     @Override
     public boolean isPassivationCapable()
     {
         return true;
     }
-    
-    
-    
+
 }

Modified: openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/container/BeanManagerImpl.java
URL: http://svn.apache.org/viewvc/openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/container/BeanManagerImpl.java?rev=1195195&r1=1195194&r2=1195195&view=diff
==============================================================================
--- openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/container/BeanManagerImpl.java
(original)
+++ openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/container/BeanManagerImpl.java
Sun Oct 30 17:32:01 2011
@@ -826,19 +826,20 @@ public class BeanManagerImpl implements 
             // producers
             InjectionResolver.injectionPoints.set(injectionPoint);
         }
+
         if(WebBeansUtil.isDependent(injectedBean))
         {        
             //Using owner creational context
             //Dependents use parent creational context
             instance = getReference(injectedBean, injectionPoint.getType(), ownerCreationalContext);
         }
-        
         else
         {   
             //New creational context for normal scoped beans
             CreationalContextImpl<Object> injectedCreational = (CreationalContextImpl<Object>)createCreationalContext(injectedBean);
           
             instance = getReference(injectedBean, injectionPoint.getType(), injectedCreational);
         }
+
         if(isSetIPForProducers)
         {
             //remove reference immediate after instance is retrieved

Modified: openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/inject/instance/InstanceFactory.java
URL: http://svn.apache.org/viewvc/openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/inject/instance/InstanceFactory.java?rev=1195195&r1=1195194&r2=1195195&view=diff
==============================================================================
--- openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/inject/instance/InstanceFactory.java
(original)
+++ openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/inject/instance/InstanceFactory.java
Sun Oct 30 17:32:01 2011
@@ -21,6 +21,7 @@ package org.apache.webbeans.inject.insta
 import java.lang.annotation.Annotation;
 import java.lang.reflect.Type;
 
+import javax.enterprise.context.spi.CreationalContext;
 import javax.enterprise.inject.Instance;
 
 import org.apache.webbeans.config.WebBeansContext;
@@ -33,18 +34,22 @@ public final class InstanceFactory
     }
     
     /**
-     * 
+     *
      * @param injectedType injection class type
      * @param injectionPointClass null or the class of the injection point
      * @param webBeansContext
+     * @param creationalContext will get used for creating &#064;Dependent beans
+     * @param ownerInstance the object the current Instance got injected into
      * @param annotations qualifier annotations
-     * @return
+     * @return the {@link Instance<T>} for the given type.
      */
     public static <T> Instance<T> getInstance(Type injectedType, Class<?>
injectionPointClass,
-                                              WebBeansContext webBeansContext, Annotation...
annotations)
+                                              WebBeansContext webBeansContext, CreationalContext<?>
creationalContext,
+                                              Object ownerInstance, Annotation... annotations)
     {
-        InstanceImpl<T> instance = new InstanceImpl<T>(injectedType,injectionPointClass,
webBeansContext, annotations);
-        
+        InstanceImpl<T> instance = new InstanceImpl<T>(injectedType,injectionPointClass,
webBeansContext,
+                                                       creationalContext, ownerInstance,
annotations);
+
         return instance;
     }
 

Modified: openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/inject/instance/InstanceImpl.java
URL: http://svn.apache.org/viewvc/openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/inject/instance/InstanceImpl.java?rev=1195195&r1=1195194&r2=1195195&view=diff
==============================================================================
--- openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/inject/instance/InstanceImpl.java
(original)
+++ openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/inject/instance/InstanceImpl.java
Sun Oct 30 17:32:01 2011
@@ -28,12 +28,15 @@ import java.util.HashSet;
 import java.util.Iterator;
 import java.util.Set;
 
+import javax.enterprise.context.spi.CreationalContext;
 import javax.enterprise.inject.Instance;
 import javax.enterprise.inject.spi.Bean;
 import javax.enterprise.util.TypeLiteral;
 
 import org.apache.webbeans.config.WebBeansContext;
+import org.apache.webbeans.container.BeanManagerImpl;
 import org.apache.webbeans.container.InjectionResolver;
+import org.apache.webbeans.context.creational.CreationalContextImpl;
 import org.apache.webbeans.util.ClassUtil;
 import org.apache.webbeans.util.OwbCustomObjectInputStream;
 import org.apache.webbeans.util.WebBeansUtil;
@@ -60,18 +63,27 @@ class InstanceImpl<T> implements Instanc
 
     private WebBeansContext webBeansContext;
 
+    private CreationalContext<?> parentCreationalContext;
+
+    private Object ownerInstance;
+
     /**
      * Creates new instance.
      * 
      * @param injectionClazz injection class type
      * @param injectionPointClazz null or class of injection point
      * @param webBeansContext
+     * @param creationalContext will get used for creating &#064;Dependent beans
+     * @param ownerInstance the object the current Instance got injected into
      * @param annotations qualifier annotations
      */
-    InstanceImpl(Type injectionClazz, Class<?> injectionPointClazz, WebBeansContext
webBeansContext, Annotation... annotations)
+    InstanceImpl(Type injectionClazz, Class<?> injectionPointClazz, WebBeansContext
webBeansContext,
+                 CreationalContext<?> creationalContext, Object ownerInstance, Annotation...
annotations)
     {
         this.injectionClazz = injectionClazz;
         this.injectionPointClazz=injectionPointClazz;
+        this.parentCreationalContext = creationalContext;
+        this.ownerInstance = ownerInstance;
 
         for (Annotation ann : annotations)
         {
@@ -83,13 +95,12 @@ class InstanceImpl<T> implements Instanc
     /**
      * Returns the bean instance with given qualifier annotations.
      * 
-     * @param annotations qualifier annotations
      * @return bean instance
      */
     @SuppressWarnings("unchecked")
     public T get()
     {
-        T instance = null;
+        T instance;
 
         Annotation[] anns = new Annotation[qualifierAnnotations.size()];
         anns = qualifierAnnotations.toArray(anns);
@@ -97,9 +108,27 @@ class InstanceImpl<T> implements Instanc
         Set<Bean<?>> beans = resolveBeans();
 
         webBeansContext.getResolutionUtil().checkResolvedBeans(beans, ClassUtil.getClazz(injectionClazz),
anns);
+        BeanManagerImpl beanManager = webBeansContext.getBeanManagerImpl();
+
+        Bean<?> bean = beanManager.resolve(beans);
+
+        // since Instance<T> is Dependent, we we gonna use the parent CreationalContext
by default
+        CreationalContext<?> creationalContext = parentCreationalContext;
+
+        boolean isDependentBean = WebBeansUtil.isDependent(bean);
 
-        Bean<?> bean = beans.iterator().next();
-        instance = (T) webBeansContext.getBeanManagerImpl().getInstance(bean, null);
+        if (!isDependentBean)
+        {
+            // but for all NormalScoped beans we will need to create a fresh CreationalContext
+            creationalContext = beanManager.createCreationalContext(bean);
+        }
+
+        instance = (T) beanManager.getReference(bean, null, creationalContext);
+
+        if (isDependentBean && ownerInstance != null && creationalContext
instanceof CreationalContextImpl)
+        {
+            ((CreationalContextImpl<?>) creationalContext).addDependent(ownerInstance,
bean, instance);
+        }
 
         return instance;
     }
@@ -117,8 +146,7 @@ class InstanceImpl<T> implements Instanc
         InjectionResolver injectionResolver = webBeansContext.getBeanManagerImpl().getInjectionResolver();
 
         InjectionResolver resolver = injectionResolver;
-        Set<Bean<?>> beans = resolver.implResolveByType(
-                injectionClazz, injectionPointClazz, anns);
+        Set<Bean<?>> beans = resolver.implResolveByType(injectionClazz, injectionPointClazz,
anns);
         return beans;
     }
     
@@ -149,7 +177,8 @@ class InstanceImpl<T> implements Instanc
     {
         Annotation[] newQualifiersArray = getAdditionalQualifiers(qualifiers);
         InstanceImpl<T> newInstance = new InstanceImpl<T>(injectionClazz, injectionPointClazz,
-                                                          webBeansContext, newQualifiersArray);
+                                                          webBeansContext, parentCreationalContext,
+                                                          ownerInstance, newQualifiersArray);
 
         return newInstance;
     }
@@ -200,7 +229,9 @@ class InstanceImpl<T> implements Instanc
         
         Annotation[] newQualifiers = getAdditionalQualifiers(qualifiers);
         
-        InstanceImpl<U> newInstance = new InstanceImpl<U>(sub, injectionPointClazz,
webBeansContext, newQualifiers);
+        InstanceImpl<U> newInstance = new InstanceImpl(sub, injectionPointClazz, webBeansContext,
+                                                       parentCreationalContext, ownerInstance,
+                                                       newQualifiers);
                     
         return newInstance;
     }

Modified: openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/newtests/AbstractUnitTest.java
URL: http://svn.apache.org/viewvc/openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/newtests/AbstractUnitTest.java?rev=1195195&r1=1195194&r2=1195195&view=diff
==============================================================================
--- openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/newtests/AbstractUnitTest.java
(original)
+++ openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/newtests/AbstractUnitTest.java
Sun Oct 30 17:32:01 2011
@@ -104,7 +104,7 @@ public abstract class AbstractUnitTest
     {
         return this.webBeansContext.getBeanManagerImpl();
     }
-    
+
     @SuppressWarnings("unchecked")
     protected <T> T getInstance(Class<T> type, Annotation... qualifiers)
     {

Modified: openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/newtests/managed/instance/InjectedInstanceComponentTest.java
URL: http://svn.apache.org/viewvc/openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/newtests/managed/instance/InjectedInstanceComponentTest.java?rev=1195195&r1=1195194&r2=1195195&view=diff
==============================================================================
--- openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/newtests/managed/instance/InjectedInstanceComponentTest.java
(original)
+++ openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/newtests/managed/instance/InjectedInstanceComponentTest.java
Sun Oct 30 17:32:01 2011
@@ -21,22 +21,18 @@ package org.apache.webbeans.newtests.man
 
 import junit.framework.Assert;
 import org.apache.webbeans.newtests.AbstractUnitTest;
+import org.apache.webbeans.newtests.managed.instance.beans.DependentBean;
+import org.apache.webbeans.newtests.managed.instance.beans.InstanceForDependentBean;
 import org.apache.webbeans.newtests.managed.instance.beans.InstanceInjectedComponent;
 import org.apache.webbeans.test.component.CheckWithCheckPayment;
 import org.apache.webbeans.test.component.CheckWithMoneyPayment;
 import org.apache.webbeans.test.component.IPayment;
 import org.apache.webbeans.test.component.PaymentProcessorComponent;
 import org.junit.Test;
-import org.junit.Ignore;
 
-import javax.enterprise.context.spi.CreationalContext;
 import javax.enterprise.inject.Instance;
-import javax.enterprise.inject.spi.Bean;
-import javax.enterprise.util.TypeLiteral;
-import java.lang.reflect.Type;
 import java.util.ArrayList;
 import java.util.Collection;
-import java.util.Set;
 
 public class InjectedInstanceComponentTest extends AbstractUnitTest
 {
@@ -72,28 +68,25 @@ public class InjectedInstanceComponentTe
         shutDownContainer();
     }
 
-    @Ignore
     @Test
-    public void testManualInstanceBeanResolving()
+    public void testInstanceDestroyal()
     {
         Collection<Class<?>> beanClasses = new ArrayList<Class<?>>();
 
+        beanClasses.add(InstanceForDependentBean.class);
+        beanClasses.add(DependentBean.class);
         startContainer(beanClasses, null);
 
-        //X TODO doesn't work Set<Bean<?>> beans = getBeanManager().getBeans(Instance.class);
-        Type instanceType = new TypeLiteral<Instance<Object>>(){}.getType();
-        Set<Bean<?>> beans = getBeanManager().getBeans(instanceType);
-        Assert.assertNotNull(beans);
-        Assert.assertTrue(beans.size() > 0);
-
-        Bean<Instance> bean = (Bean<Instance>) getBeanManager().resolve(beans);
-        Assert.assertNotNull(bean);
-        
-        CreationalContext<Instance> ctx = getBeanManager().createCreationalContext(bean);
-        
-        Object reference = getBeanManager().getReference(bean, instanceType, ctx);
-        Assert.assertNotNull(reference);
-        
+        InstanceForDependentBean holder = getInstance(InstanceForDependentBean.class);
+        Assert.assertNotNull(holder);
+
+        Assert.assertEquals(42, holder.getMeaningOfLife());
+
+        DependentBean.properlyDestroyed = false;
+
         shutDownContainer();
+
+        Assert.assertTrue(DependentBean.properlyDestroyed);
     }
+
 }

Copied: openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/newtests/managed/instance/beans/DependentBean.java
(from r1195141, openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/newtests/managed/instance/beans/UnparameterizedInstanceBean.java)
URL: http://svn.apache.org/viewvc/openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/newtests/managed/instance/beans/DependentBean.java?p2=openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/newtests/managed/instance/beans/DependentBean.java&p1=openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/newtests/managed/instance/beans/UnparameterizedInstanceBean.java&r1=1195141&r2=1195195&rev=1195195&view=diff
==============================================================================
--- openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/newtests/managed/instance/beans/UnparameterizedInstanceBean.java
(original)
+++ openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/newtests/managed/instance/beans/DependentBean.java
Sun Oct 30 17:32:01 2011
@@ -18,18 +18,26 @@
  */
 package org.apache.webbeans.newtests.managed.instance.beans;
 
-import javax.enterprise.inject.Instance;
-import javax.inject.Inject;
+import javax.annotation.PreDestroy;
+import javax.enterprise.context.Dependent;
 
 /**
- * Test for injecting a Instance without any generic type.
+ * Test for properly destroying dependent contextual instances
  */
-public class UnparameterizedInstanceBean
+@Dependent
+public class DependentBean
 {
-    private @Inject Instance instance;
 
-    public Instance getInstanceHook()
+    public static boolean properlyDestroyed = false;
+
+    public int getMeaningOfLife()
+    {
+        return 42;
+    }
+
+    @PreDestroy
+    protected void destroy()
     {
-        return instance;
+        properlyDestroyed = true;
     }
 }

Added: openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/newtests/managed/instance/beans/InstanceForDependentBean.java
URL: http://svn.apache.org/viewvc/openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/newtests/managed/instance/beans/InstanceForDependentBean.java?rev=1195195&view=auto
==============================================================================
--- openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/newtests/managed/instance/beans/InstanceForDependentBean.java
(added)
+++ openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/newtests/managed/instance/beans/InstanceForDependentBean.java
Sun Oct 30 17:32:01 2011
@@ -0,0 +1,40 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.webbeans.newtests.managed.instance.beans;
+
+import javax.enterprise.context.RequestScoped;
+import javax.enterprise.inject.Instance;
+import javax.inject.Inject;
+
+/**
+ * Test for properly destroying dependent contextual instances
+ */
+@RequestScoped
+public class InstanceForDependentBean
+{
+
+    @Inject
+    private Instance<DependentBean> instance;
+
+
+    public int getMeaningOfLife()
+    {
+        return instance.get().getMeaningOfLife();
+    }
+}



Mime
View raw message