You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@openwebbeans.apache.org by ar...@apache.org on 2012/12/28 21:30:44 UTC

svn commit: r1426624 - in /openwebbeans/trunk/webbeans-impl/src: main/java/org/apache/webbeans/container/ main/java/org/apache/webbeans/inject/instance/ test/java/org/apache/webbeans/newtests/concepts/alternatives/common/ test/java/org/apache/webbeans/...

Author: arne
Date: Fri Dec 28 20:30:44 2012
New Revision: 1426624

URL: http://svn.apache.org/viewvc?rev=1426624&view=rev
Log:
OWB-742: Fixed @Alternative resolution for Instance access

Added:
    openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/newtests/concepts/alternatives/common/AlternativeBean.java
    openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/newtests/concepts/alternatives/common/SimpleBean.java
    openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/newtests/concepts/alternatives/common/SimpleInjectionTarget.java
    openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/newtests/concepts/alternatives/common/SimpleInterface.java
    openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/newtests/concepts/alternatives/tests/AlternativeInstanceTest.java
    openwebbeans/trunk/webbeans-impl/src/test/resources/org/apache/webbeans/newtests/concepts/alternatives/tests/simpleAlternative.xml
Modified:
    openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/container/InjectionResolver.java
    openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/container/ResolutionUtil.java
    openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/inject/instance/InstanceImpl.java

Modified: openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/container/InjectionResolver.java
URL: http://svn.apache.org/viewvc/openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/container/InjectionResolver.java?rev=1426624&r1=1426623&r2=1426624&view=diff
==============================================================================
--- openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/container/InjectionResolver.java (original)
+++ openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/container/InjectionResolver.java Fri Dec 28 20:30:44 2012
@@ -641,11 +641,28 @@ public class InjectionResolver
 
     public <X> Bean<? extends X> resolve(Set<Bean<? extends X>> beans)
     {
-        if (beans == null || beans.isEmpty())
+        Set set = resolveAll(beans);
+        
+        if (set.isEmpty())
         {
             return null;
         }
 
+        if(set.size() > 1)
+        {
+            throwAmbiguousResolutionException(set);
+        }
+
+        return (Bean<? extends X>)set.iterator().next();
+    }
+
+    public <X> Set<Bean<? extends X>> resolveAll(Set<Bean<? extends X>> beans)
+    {
+        if (beans == null || beans.isEmpty())
+        {
+            return Collections.emptySet();
+        }
+
         Set set = new HashSet<Bean<Object>>();
         for(Bean<? extends X> obj : beans)
         {
@@ -656,7 +673,7 @@ public class InjectionResolver
 
         if (set == null || set.isEmpty())
         {
-            return null;
+            return Collections.emptySet();
         }
 
         if(set.size() > 1)
@@ -664,15 +681,9 @@ public class InjectionResolver
             set = findBySpecialization(set);
         }
 
-        if(set.size() > 1)
-        {
-            throwAmbiguousResolutionException(set);
-        }
-
-        return (Bean<? extends X>)set.iterator().next();
+        return set;
     }
 
-
     private boolean isAltBeanInInjectionPointBDA(String bdaBeansXMLFilePath, Bean<?> altBean)
     {
 

Modified: openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/container/ResolutionUtil.java
URL: http://svn.apache.org/viewvc/openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/container/ResolutionUtil.java?rev=1426624&r1=1426623&r2=1426624&view=diff
==============================================================================
--- openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/container/ResolutionUtil.java (original)
+++ openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/container/ResolutionUtil.java Fri Dec 28 20:30:44 2012
@@ -18,17 +18,12 @@
  */
 package org.apache.webbeans.container;
 
-import java.lang.annotation.Annotation;
 import java.lang.reflect.Type;
 import java.util.Iterator;
 import java.util.Set;
 
-import javax.enterprise.inject.spi.Bean;
-import javax.enterprise.inject.spi.InjectionPoint;
-
 import org.apache.webbeans.config.WebBeansContext;
 import org.apache.webbeans.util.ClassUtil;
-import org.apache.webbeans.util.InjectionExceptionUtils;
 
 public final class ResolutionUtil
 {
@@ -67,21 +62,4 @@ public final class ResolutionUtil
 
         return false;
     }
-
-    public void checkResolvedBeans(Set<Bean<?>> resolvedSet, Class<?> type, Annotation[] qualifiers, InjectionPoint injectionPoint)
-    {
-        if (resolvedSet.isEmpty())
-        {
-            InjectionExceptionUtils.throwUnsatisfiedResolutionException(type, injectionPoint, qualifiers);
-        }
-
-        if (resolvedSet.size() > 1)
-        {
-            InjectionExceptionUtils.throwAmbiguousResolutionException(resolvedSet, type, injectionPoint, qualifiers);
-        }
-
-        Bean<?> bean = resolvedSet.iterator().next();
-        webBeansContext.getWebBeansUtil().checkUnproxiableApiType(bean, bean.getScope());
-
-    }
 }

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=1426624&r1=1426623&r2=1426624&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 Fri Dec 28 20:30:44 2012
@@ -40,6 +40,7 @@ import org.apache.webbeans.container.Bea
 import org.apache.webbeans.container.InjectionResolver;
 import org.apache.webbeans.context.creational.CreationalContextImpl;
 import org.apache.webbeans.util.ClassUtil;
+import org.apache.webbeans.util.InjectionExceptionUtils;
 import org.apache.webbeans.util.OwbCustomObjectInputStream;
 import org.apache.webbeans.util.WebBeansUtil;
 
@@ -112,10 +113,14 @@ class InstanceImpl<T> implements Instanc
         
             Set<Bean<?>> beans = resolveBeans();
 
-            webBeansContext.getResolutionUtil().checkResolvedBeans(beans, ClassUtil.getClazz(injectionClazz), anns, null);
             BeanManagerImpl beanManager = webBeansContext.getBeanManagerImpl();
 
             Bean<?> bean = beanManager.resolve(beans);
+            
+            if (bean == null)
+            {
+                InjectionExceptionUtils.throwUnsatisfiedResolutionException(ClassUtil.getClazz(injectionClazz), injectionPoint, anns);
+            }
 
             // since Instance<T> is Dependent, we we gonna use the parent CreationalContext by default
             CreationalContext<?> creationalContext = parentCreationalContext;
@@ -163,7 +168,7 @@ class InstanceImpl<T> implements Instanc
             injectionPointClass = injectionPointBean.getBeanClass();
         }
         Set<Bean<?>> beans = resolver.implResolveByType(injectionClazz, injectionPointClass, anns);
-        return beans;
+        return resolver.resolveAll(beans);
     }
     
     /**

Added: openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/newtests/concepts/alternatives/common/AlternativeBean.java
URL: http://svn.apache.org/viewvc/openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/newtests/concepts/alternatives/common/AlternativeBean.java?rev=1426624&view=auto
==============================================================================
--- openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/newtests/concepts/alternatives/common/AlternativeBean.java (added)
+++ openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/newtests/concepts/alternatives/common/AlternativeBean.java Fri Dec 28 20:30:44 2012
@@ -0,0 +1,31 @@
+/*
+ * 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.concepts.alternatives.common;
+
+import javax.enterprise.inject.Alternative;
+
+@Alternative
+public class AlternativeBean implements SimpleInterface
+{
+
+    public Class<?> getImplementationType()
+    {
+        return AlternativeBean.class;
+    }
+}

Added: openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/newtests/concepts/alternatives/common/SimpleBean.java
URL: http://svn.apache.org/viewvc/openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/newtests/concepts/alternatives/common/SimpleBean.java?rev=1426624&view=auto
==============================================================================
--- openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/newtests/concepts/alternatives/common/SimpleBean.java (added)
+++ openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/newtests/concepts/alternatives/common/SimpleBean.java Fri Dec 28 20:30:44 2012
@@ -0,0 +1,28 @@
+/*
+ * 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.concepts.alternatives.common;
+
+public class SimpleBean implements SimpleInterface
+{
+
+    public Class<?> getImplementationType()
+    {
+        return SimpleBean.class;
+    }
+}

Added: openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/newtests/concepts/alternatives/common/SimpleInjectionTarget.java
URL: http://svn.apache.org/viewvc/openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/newtests/concepts/alternatives/common/SimpleInjectionTarget.java?rev=1426624&view=auto
==============================================================================
--- openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/newtests/concepts/alternatives/common/SimpleInjectionTarget.java (added)
+++ openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/newtests/concepts/alternatives/common/SimpleInjectionTarget.java Fri Dec 28 20:30:44 2012
@@ -0,0 +1,61 @@
+/*
+ * 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.concepts.alternatives.common;
+
+import java.util.Iterator;
+
+import javax.annotation.PostConstruct;
+import javax.enterprise.inject.Instance;
+import javax.inject.Inject;
+
+public class SimpleInjectionTarget
+{
+
+    @Inject
+    private SimpleInterface simpleInterface1;
+
+    @Inject
+    private Instance<SimpleInterface> simpleInterface2Instance;
+
+    private SimpleInterface simpleInterface2;
+    
+    @PostConstruct
+    public void initialize()
+    {
+        simpleInterface2 = simpleInterface2Instance.get();
+    }
+    
+    public SimpleInterface getSimpleInterface1()
+    {
+        return simpleInterface1;
+    }
+
+    public SimpleInterface getSimpleInterface2()
+    {
+        return simpleInterface2;
+    }
+
+    public boolean isSimpleInterfaceAmbiguous() {
+        return simpleInterface2Instance.isAmbiguous();
+    }
+
+    public Iterator<SimpleInterface> getSimpleInterfaceInstances() {
+        return simpleInterface2Instance.iterator();
+    }
+}

Added: openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/newtests/concepts/alternatives/common/SimpleInterface.java
URL: http://svn.apache.org/viewvc/openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/newtests/concepts/alternatives/common/SimpleInterface.java?rev=1426624&view=auto
==============================================================================
--- openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/newtests/concepts/alternatives/common/SimpleInterface.java (added)
+++ openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/newtests/concepts/alternatives/common/SimpleInterface.java Fri Dec 28 20:30:44 2012
@@ -0,0 +1,25 @@
+/*
+ * 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.concepts.alternatives.common;
+
+public interface SimpleInterface
+{
+
+    Class<?> getImplementationType();
+}

Added: openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/newtests/concepts/alternatives/tests/AlternativeInstanceTest.java
URL: http://svn.apache.org/viewvc/openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/newtests/concepts/alternatives/tests/AlternativeInstanceTest.java?rev=1426624&view=auto
==============================================================================
--- openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/newtests/concepts/alternatives/tests/AlternativeInstanceTest.java (added)
+++ openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/newtests/concepts/alternatives/tests/AlternativeInstanceTest.java Fri Dec 28 20:30:44 2012
@@ -0,0 +1,97 @@
+/*
+ * 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.concepts.alternatives.tests;
+
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Iterator;
+
+import javax.enterprise.context.spi.CreationalContext;
+import javax.enterprise.inject.spi.Bean;
+
+import org.apache.webbeans.newtests.AbstractUnitTest;
+import org.apache.webbeans.newtests.concepts.alternatives.common.AlternativeBean;
+import org.apache.webbeans.newtests.concepts.alternatives.common.SimpleBean;
+import org.apache.webbeans.newtests.concepts.alternatives.common.SimpleInjectionTarget;
+import org.apache.webbeans.newtests.concepts.alternatives.common.SimpleInterface;
+import org.junit.Assert;
+import org.junit.Test;
+
+/**
+ * see OWB-742
+ */
+public class AlternativeInstanceTest extends AbstractUnitTest
+{
+    private static final String PACKAGE_NAME = AlternativeProducerMethodTest.class.getPackage().getName();
+
+    @Test
+    public void testAlternativeDisabled()
+    {
+        Collection<String> beanXmls = new ArrayList<String>();
+        Collection<Class<?>> beanClasses = new ArrayList<Class<?>>();
+        beanClasses.add(SimpleBean.class);
+        beanClasses.add(AlternativeBean.class);
+        beanClasses.add(SimpleInjectionTarget.class);
+
+        startContainer(beanClasses, beanXmls);
+
+        Bean<?> simpleInjectionTargetBean = getBeanManager().getBeans(SimpleInjectionTarget.class).iterator().next();
+        CreationalContext<?> context = getBeanManager().createCreationalContext(simpleInjectionTargetBean);
+        SimpleInjectionTarget target = (SimpleInjectionTarget) getBeanManager().getReference(simpleInjectionTargetBean, SimpleInjectionTarget.class, context);
+        
+        Assert.assertFalse(target.isSimpleInterfaceAmbiguous());
+        Iterator<SimpleInterface> simpleInterfaceInstances = target.getSimpleInterfaceInstances();
+        Assert.assertTrue(simpleInterfaceInstances.hasNext());
+        Assert.assertEquals(SimpleBean.class, simpleInterfaceInstances.next().getImplementationType());
+        Assert.assertFalse(simpleInterfaceInstances.hasNext());
+        Assert.assertEquals(SimpleBean.class, target.getSimpleInterface1().getImplementationType());
+        Assert.assertEquals(SimpleBean.class, target.getSimpleInterface2().getImplementationType());
+        
+        shutDownContainer();
+    }
+
+    @Test
+    public void testAlternativeEnabled()
+    {
+        Collection<String> beanXmls = new ArrayList<String>();
+        beanXmls.add(getXmlPath(PACKAGE_NAME, "simpleAlternative"));
+
+        Collection<Class<?>> beanClasses = new ArrayList<Class<?>>();
+        beanClasses.add(SimpleBean.class);
+        beanClasses.add(AlternativeBean.class);
+        beanClasses.add(SimpleInjectionTarget.class);
+
+        startContainer(beanClasses, beanXmls);
+
+        Bean<?> simpleInjectionTargetBean = getBeanManager().getBeans(SimpleInjectionTarget.class).iterator().next();
+        CreationalContext<?> context = getBeanManager().createCreationalContext(simpleInjectionTargetBean);
+        SimpleInjectionTarget target = (SimpleInjectionTarget) getBeanManager().getReference(simpleInjectionTargetBean, SimpleInjectionTarget.class, context);
+        
+        Assert.assertFalse(target.isSimpleInterfaceAmbiguous());
+        Iterator<SimpleInterface> simpleInterfaceInstances = target.getSimpleInterfaceInstances();
+        Assert.assertTrue(simpleInterfaceInstances.hasNext());
+        Assert.assertEquals(AlternativeBean.class, simpleInterfaceInstances.next().getImplementationType());
+        Assert.assertFalse(simpleInterfaceInstances.hasNext());
+        Assert.assertEquals(AlternativeBean.class, target.getSimpleInterface1().getImplementationType());
+        Assert.assertEquals(AlternativeBean.class, target.getSimpleInterface2().getImplementationType());
+        
+        shutDownContainer();
+    }
+
+}

Added: openwebbeans/trunk/webbeans-impl/src/test/resources/org/apache/webbeans/newtests/concepts/alternatives/tests/simpleAlternative.xml
URL: http://svn.apache.org/viewvc/openwebbeans/trunk/webbeans-impl/src/test/resources/org/apache/webbeans/newtests/concepts/alternatives/tests/simpleAlternative.xml?rev=1426624&view=auto
==============================================================================
--- openwebbeans/trunk/webbeans-impl/src/test/resources/org/apache/webbeans/newtests/concepts/alternatives/tests/simpleAlternative.xml (added)
+++ openwebbeans/trunk/webbeans-impl/src/test/resources/org/apache/webbeans/newtests/concepts/alternatives/tests/simpleAlternative.xml Fri Dec 28 20:30:44 2012
@@ -0,0 +1,24 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+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.
+-->
+<beans>
+    <alternatives>
+        <class>org.apache.webbeans.newtests.concepts.alternatives.common.AlternativeBean</class>
+    </alternatives>
+</beans>