openwebbeans-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From strub...@apache.org
Subject svn commit: r1595822 - in /openwebbeans/trunk/webbeans-impl/src: main/java/org/apache/webbeans/config/ main/java/org/apache/webbeans/container/ main/java/org/apache/webbeans/inject/ main/java/org/apache/webbeans/util/ test/java/org/apache/webbeans/test...
Date Mon, 19 May 2014 10:34:04 GMT
Author: struberg
Date: Mon May 19 10:34:04 2014
New Revision: 1595822

URL: http://svn.apache.org/r1595822
Log:
OWB-955 streamline Alternatives detection

* remove disabled beans at startup already - no need to check them at runtime

Added:
    openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/util/SingleItemSet.java
    openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/test/concepts/alternatives/common/AlternativeWithEvenHigherPriorityBean.java
      - copied, changed from r1594099, openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/test/concepts/alternatives/common/AlternativeWithHighPriorityBean.java
    openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/test/concepts/alternatives/common/AlternativeWithSamePriorityBean.java
      - copied, changed from r1594099, openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/test/concepts/alternatives/common/AlternativeWithPriorityBean.java
Modified:
    openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/config/BeansDeployer.java
    openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/container/BeanManagerImpl.java
    openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/container/InjectionResolver.java
    openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/inject/AlternativesManager.java
    openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/util/WebBeansUtil.java
    openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/test/concepts/alternatives/tests/AlternativeBeanResolvingTest.java

Modified: openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/config/BeansDeployer.java
URL: http://svn.apache.org/viewvc/openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/config/BeansDeployer.java?rev=1595822&r1=1595821&r2=1595822&view=diff
==============================================================================
--- openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/config/BeansDeployer.java
(original)
+++ openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/config/BeansDeployer.java
Mon May 19 10:34:04 2014
@@ -213,6 +213,8 @@ public class BeansDeployer
 
                 //Check Specialization
                 processSpecializations(scanner);
+
+                removeDisabledBeans();
                 
                 //Fire Event
                 fireAfterBeanDiscoveryEvent();
@@ -258,6 +260,26 @@ public class BeansDeployer
         }
     }
 
+    /**
+     * Remove all beans which are not enabled anymore.
+     * This might e.g. happen because they are 'overridden'
+     * by a @Specialized bean.
+     * We remove those beans now to not having to take care later
+     * during {@link org.apache.webbeans.container.BeanManagerImpl#resolve(java.util.Set)}
+     */
+    private void removeDisabledBeans()
+    {
+        Iterator<Bean<?>> beans = webBeansContext.getBeanManagerImpl().getBeans().iterator();
+        while(beans.hasNext())
+        {
+            Bean<?> bean = beans.next();
+            if (!((OwbBean) bean).isEnabled())
+            {
+                beans.remove();
+            }
+        }
+    }
+
     private void registerAlternativesDecoratorsAndInterceptorsWithPriority(List<AnnotatedType<?>>
annotatedTypes)
     {
         AlternativesManager alternativesManager = webBeansContext.getAlternativesManager();
@@ -410,7 +432,7 @@ public class BeansDeployer
     private void fireAfterTypeDiscoveryEvent()
     {
         AlternativesManager alternativesManager = webBeansContext.getAlternativesManager();
-        List<Class<?>> sortedAlternatives = alternativesManager.getSortedAlternatives();
+        List<Class<?>> sortedAlternatives = alternativesManager.getPrioritizedAlternatives();
 
         BeanManagerImpl manager = webBeansContext.getBeanManagerImpl();
         manager.fireLifecycleEvent(new AfterTypeDiscoveryImpl(webBeansContext, sortedAlternatives));
@@ -590,9 +612,13 @@ public class BeansDeployer
                         Set<Bean<?>> beans = resolver.implResolveByName(beanName);
                         if(beans.size() > 1)
                         {
-                            beans = resolver.findByAlternatives(beans);                 
          
-                            if(beans.size() > 1)
+                            try
+                            {
+                                resolver.resolve(beans);
+                            }
+                            catch(AmbiguousResolutionException are)
                             {
+                                // throw the Exception with even more information
                                 InjectionExceptionUtil.throwAmbiguousResolutionExceptionForBeanName(beans,
beanName);
                             }   
                         }
@@ -989,6 +1015,7 @@ public class BeansDeployer
     }
 
     /**
+     * TODO this has to be changed to use AnnotatedTypes instead of scanner.getBeanClasses()!
      * Checks specialization.
      * @param scanner scanner instance
      */
@@ -1002,7 +1029,7 @@ public class BeansDeployer
             if (beanClasses != null && beanClasses.size() > 0)
             {
                 //superClassList is used to handle the case: Car, CarToyota, Bus, SchoolBus,
CarFord
-                //for which case, the owb should throw exception that both CarToyota and
CarFord are 
+                //for which case OWB should throw exception that both CarToyota and CarFord
are
                 //specialize Car. 
                 Class<?> superClass;
                 ArrayList<Class<?>> superClassList = new ArrayList<Class<?>>();

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=1595822&r1=1595821&r2=1595822&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
Mon May 19 10:34:04 2014
@@ -31,7 +31,6 @@ import java.util.Map;
 import java.util.Set;
 import java.util.concurrent.ConcurrentHashMap;
 import java.util.concurrent.ConcurrentMap;
-import java.util.concurrent.CopyOnWriteArraySet;
 
 import javax.el.ELResolver;
 import javax.el.ExpressionFactory;
@@ -127,7 +126,7 @@ public class BeanManagerImpl extends Abs
     private Map<Class<? extends Annotation>, Context> singleContextMap = new
HashMap<Class<? extends Annotation>, Context>();
 
     /**Deployment archive beans*/
-    private Set<Bean<?>> deploymentBeans = new CopyOnWriteArraySet<Bean<?>>();
+    private Set<Bean<?>> deploymentBeans = new HashSet<Bean<?>>();
 
     /**Normal scoped cache proxies*/
     private Map<Contextual<?>, Object> cacheProxies = new ConcurrentHashMap<Contextual<?>,
Object>();

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=1595822&r1=1595821&r2=1595822&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
Mon May 19 10:34:04 2014
@@ -27,6 +27,7 @@ import java.lang.reflect.WildcardType;
 import java.util.Collections;
 import java.util.HashSet;
 import java.util.Iterator;
+import java.util.List;
 import java.util.Map;
 import java.util.Set;
 import java.util.concurrent.ConcurrentHashMap;
@@ -48,6 +49,7 @@ import org.apache.webbeans.config.WebBea
 import org.apache.webbeans.exception.WebBeansConfigurationException;
 import javax.enterprise.inject.spi.DefinitionException;
 import org.apache.webbeans.exception.inject.NullableDependencyException;
+import org.apache.webbeans.inject.AlternativesManager;
 import org.apache.webbeans.logger.WebBeansLoggerFacade;
 import org.apache.webbeans.spi.BDABeansXmlScanner;
 import org.apache.webbeans.spi.ScannerService;
@@ -56,6 +58,7 @@ import org.apache.webbeans.util.Asserts;
 import org.apache.webbeans.util.ClassUtil;
 import org.apache.webbeans.util.GenericsUtil;
 import org.apache.webbeans.util.InjectionExceptionUtil;
+import org.apache.webbeans.util.SingleItemSet;
 import org.apache.webbeans.util.WebBeansUtil;
 import static org.apache.webbeans.util.InjectionExceptionUtil.throwAmbiguousResolutionException;
 
@@ -78,6 +81,8 @@ public class InjectionResolver
      * Bean Manager
      */
     private WebBeansContext webBeansContext;
+
+    private AlternativesManager alternativesManager;
     
     /**
      * This Map contains all resolved beans via it's type and qualifiers.
@@ -99,7 +104,7 @@ public class InjectionResolver
     public InjectionResolver(WebBeansContext webBeansContext)
     {
         this.webBeansContext = webBeansContext;
-
+        this.alternativesManager = webBeansContext.getAlternativesManager();
     }
 
     /**
@@ -344,20 +349,6 @@ public class InjectionResolver
             }
         }
 
-        //Look for enable/disable
-        resolvedComponents = findByEnabled(resolvedComponents);
-
-        //Still Ambigious, check for specialization
-        if (resolvedComponents.size() > 1)
-        {
-            //Check for specialization
-            Set<Bean<?>> specializedComponents = findSpecializedForNameResolution(resolvedComponents);
-            if (specializedComponents.size() > 0)
-            {
-                resolvedComponents = specializedComponents;
-            }
-        }
-
         if (resolvedComponents.isEmpty())
         {
             // maintain negative cache but use standard empty set so we can garbage collect
@@ -375,51 +366,6 @@ public class InjectionResolver
         return resolvedComponents;
     }
 
-    private Set<Bean<?>> findByEnabled(Set<Bean<?>> resolvedComponents)
-    {
-        Set<Bean<?>> specializedComponents = new HashSet<Bean<?>>();
-        if (resolvedComponents.size() > 0)
-        {
-            for (Bean<?> bean : resolvedComponents)
-            {
-                AbstractOwbBean<?> component = (AbstractOwbBean<?>) bean;
-
-                if (component.isEnabled())
-                {
-                    specializedComponents.add(component);
-                }
-            }
-        }
-
-        return specializedComponents;
-
-    }
-
-
-    /**
-     * Returns filtered set by specialization.
-     *
-     * @param resolvedComponents result beans
-     * @return filtered set by specialization
-     */
-    private Set<Bean<?>> findSpecializedForNameResolution(Set<Bean<?>>
resolvedComponents)
-    {
-        Set<Bean<?>> specializedComponents = new HashSet<Bean<?>>();
-        if (resolvedComponents.size() > 0)
-        {
-            for (Bean<?> bean : resolvedComponents)
-            {
-                AbstractOwbBean<?> component = (AbstractOwbBean<?>) bean;
-
-                if (component.isSpecializedBean())
-                {
-                    specializedComponents.add(component);
-                }
-            }
-        }
-
-        return specializedComponents;
-    }
 
     /**
      * Resolution by type.
@@ -528,16 +474,6 @@ public class InjectionResolver
         // Look for qualifiers
         resolvedComponents = findByQualifier(resolvedComponents, injectionPointType, qualifiers);
 
-        // Ambigious resolution, check for specialization
-        /*
-        // super beans are deactivated so it is useless
-        if (resolvedComponents.size() > 1)
-        {
-            //Look for specialization
-            resolvedComponents = findBySpecialization(resolvedComponents);
-        }
-        */
-
         resolvedBeansByType.put(cacheKey, resolvedComponents);
         if (logger.isLoggable(Level.FINE))
         {
@@ -569,111 +505,53 @@ public class InjectionResolver
     }
 
     /**
-     * Returns specialized beans if exists, otherwise return input result
+     * Gets alternatives from set.
      *
-     * @param result result beans
-     * @return specialized beans if exists, otherwise return input result
+     * @param beans resolved set
+     * @return contains alternatives
      */
-    public Set<Bean<?>> findBySpecialization(Set<Bean<?>> result)
+    public <X> Set<Bean<? extends X>> findByAlternatives(Set<Bean<?
extends X>> beans)
     {
-        Iterator<Bean<?>> it = result.iterator();
-        Set<Bean<?>> res = new HashSet<Bean<?>>();
+        // first check whether we have Alternatives with a Priority annotation
+        List<Class<?>> prioritizedAlternatives = alternativesManager.getPrioritizedAlternatives();
 
-        while (it.hasNext())
+        for (Class<?> alternativeClazz : prioritizedAlternatives)
         {
-            AbstractOwbBean<?> component = (AbstractOwbBean<?>) it.next();
-            if (component.isSpecializedBean() && component.isEnabled())
+            for (Bean<? extends X> bean: beans)
             {
-                res.add(component);
+                if (alternativeClazz.equals(bean.getBeanClass()))
+                {
+                    return new SingleItemSet<Bean<? extends X>>(bean);
+                }
             }
-        }
 
-        if (res.size() > 0)
-        {
-            return res;
         }
 
-        return result;
-    }
 
-    /**
-     * Gets alternatives from set.
-     *
-     * @param result resolved set
-     * @return containes alternatives
-     */
-    public Set<Bean<?>> findByAlternatives(Set<Bean<?>> result)
-    {
-        return findByAlternatives(result, null);
-    }
+        // if none such Alternative got found let's check the 'old' alternatives from beans.xml
+        Set<Bean<? extends X>> alternativeSet = new HashSet<Bean<? extends
X>>();
+        Set<Bean<? extends X>> enableSet = new HashSet<Bean<? extends X>>();
 
-    /**
-     * Gets alternatives from set.
-     *
-     * @param result resolved set
-     * @return containes alternatives
-     */
-    public Set<Bean<?>> findByAlternatives(Set<Bean<?>> result, String
bdaBeansXMLFilePath)
-    {
-        Set<Bean<?>> alternativeSet = new HashSet<Bean<?>>();
-        Set<Bean<?>> enableSet = new HashSet<Bean<?>>();
-        boolean containsAlternative = false;
-
-        if (bdaBeansXMLFilePath != null)
+        for (Bean<? extends X> bean : beans)
         {
-            // per BDA beans.xml
-            for (Bean<?> bean : result)
+            if (bean.isAlternative())
             {
-                if (bean.isAlternative())
-                {
-                    if (isAltBeanInInjectionPointBDA(bdaBeansXMLFilePath, bean))
-                    {
-                        if (!containsAlternative)
-                        {
-                            containsAlternative = true;
-                        }
-                        alternativeSet.add(bean);
-                    }
-                }
-                else
-                {
-                    if (!containsAlternative)
-                    {
-                        // Do not check isEnabled flag to allow beans to be
-                        // added on a per BDA basis when a bean is disabled due
-                        // to specialize alternative defined in a different BDA
-                        enableSet.add(bean);
-                    }
-                }
+                alternativeSet.add(bean);
             }
-        }
-        else
-        {
-            for (Bean<?> bean : result)
+            else
             {
-                if (bean.isAlternative())
-                {
-                    if (!containsAlternative)
-                    {
-                        containsAlternative = true;
-                    }
-                    alternativeSet.add(bean);
-                }
-                else
+                if (alternativeSet.isEmpty())
                 {
-                    if (!containsAlternative)
+                    AbstractOwbBean<?> temp = (AbstractOwbBean<?>) bean;
+                    if (temp.isEnabled())
                     {
-                        AbstractOwbBean<?> temp = (AbstractOwbBean<?>) bean;
-                        if (temp.isEnabled())
-                        {
-                            enableSet.add(bean);
-                        }
+                        enableSet.add(bean);
                     }
                 }
             }
         }
 
-        if (containsAlternative)
+        if (!alternativeSet.isEmpty())
         {
             return alternativeSet;
         }
@@ -681,15 +559,21 @@ public class InjectionResolver
         return enableSet;
     }
 
+    /**
+     * resolve any ambiguity by checking for Alternatives.
+     * If any &#064;Alternative exists, then we pick the one with the
+     * highest priority.
+     */
     public <X> Bean<? extends X> resolve(Set<Bean<? extends X>> beans)
     {
-        if (beans == null)
+        if (beans == null || beans.isEmpty())
         {
             return null;
         }
 
         if (beans.size() == 1)
         {
+            // if there is only one Bean left, then there is for sure no ambiguity.
             return beans.iterator().next();
         }
 
@@ -702,11 +586,7 @@ public class InjectionResolver
 
         if(set.size() > 1)
         {
-            set = findBySpecialization(set);
-            if(set.size() > 1)
-            {
-                throwAmbiguousResolutionException(set);
-            }
+            throwAmbiguousResolutionException(set);
         }
 
         return (Bean<? extends X>)set.iterator().next();
@@ -719,57 +599,16 @@ public class InjectionResolver
             return Collections.emptySet();
         }
 
-        Set set = new HashSet<Bean<Object>>();
-        for(Bean<? extends X> obj : beans)
-        {
-            set.add(obj);
-        }
-
-        set = findByAlternatives(set);
+        Set set = findByAlternatives(beans);
 
         if (set == null || set.isEmpty())
         {
             return Collections.emptySet();
         }
 
-        /*
-        // specialized bean are disabled so no need to refilter
-        if(set.size() > 1)
-        {
-            set = findBySpecialization(set);
-        }
-        */
-
         return set;
     }
 
-    private boolean isAltBeanInInjectionPointBDA(String bdaBeansXMLFilePath, Bean<?>
altBean)
-    {
-
-        ScannerService scannerService = webBeansContext.getScannerService();
-        BDABeansXmlScanner beansXMLScanner = scannerService.getBDABeansXmlScanner();
-
-        Set<Class<?>> definedAlternatives = beansXMLScanner.getAlternatives(bdaBeansXMLFilePath);
-
-        if (definedAlternatives.contains(altBean.getBeanClass()))
-        {
-            return true;
-        }
-
-        Set<Class<? extends Annotation>> definedStereotypes = beansXMLScanner.getStereotypes(bdaBeansXMLFilePath);
-
-        for (Class<? extends Annotation> stereoAnnotations : definedStereotypes)
-        {
-            if (AnnotationUtil.hasClassAnnotation(altBean.getBeanClass(), stereoAnnotations))
-            {
-                return true;
-            }
-        }
-
-        return false;
-    }
-
-
     /**
      * Returns filtered bean set according to the qualifiers.
      *

Modified: openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/inject/AlternativesManager.java
URL: http://svn.apache.org/viewvc/openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/inject/AlternativesManager.java?rev=1595822&r1=1595821&r2=1595822&view=diff
==============================================================================
--- openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/inject/AlternativesManager.java
(original)
+++ openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/inject/AlternativesManager.java
Mon May 19 10:34:04 2014
@@ -75,7 +75,7 @@ public class AlternativesManager
      */
     private final List<PriorityAlternative> priorityAlternatives = new ArrayList<PriorityAlternative>();
 
-    private List<Class<?>> sortedAlternatives = null;
+    private List<Class<?>> prioritizedAlternatives = null;
 
 
 
@@ -84,6 +84,8 @@ public class AlternativesManager
         this.webBeansContext = webBeansContext;
     }
 
+
+
     /**
      * This methods gets called while scanning the various beans.xml files.
      * It registers a &lt;stereotype&gt; alternative.
@@ -145,37 +147,27 @@ public class AlternativesManager
      * the alternatives added via XML get added.
      * @return the list of sorted alternatives
      */
-    public List<Class<?>> getSortedAlternatives()
+    public List<Class<?>> getPrioritizedAlternatives()
     {
-        if (sortedAlternatives == null)
+        if (prioritizedAlternatives == null)
         {
             Collections.sort(priorityAlternatives);
 
-            sortedAlternatives = new ArrayList<Class<?>>(priorityAlternatives.size());
+            prioritizedAlternatives = new ArrayList<Class<?>>(priorityAlternatives.size());
 
             for (PriorityAlternative priorityAlternative : priorityAlternatives)
             {
                 // add in reverse order
-                sortedAlternatives.add(0, priorityAlternative.clazz);
+                prioritizedAlternatives.add(priorityAlternative.clazz);
             }
         }
 
-        return sortedAlternatives;
-    }
-
-
-    /**
-     * @deprecated this is not enough since CDI-1.1. We now need to handle a weighted list
of enabled alternatives
-     */
-    public boolean isClassAlternative(Class<?> clazz)
-    {
-        return configuredAlternatives.contains(clazz);
+        return prioritizedAlternatives;
     }
 
 
     /**
      * @return <code>true</code> if the given bean is a configured alternative
-     * @deprecated this is not enough since CDI-1.1. We now need to handle a weighted list
of enabled alternatives
      */
     public boolean isAlternative(Bean<?> bean)
     {
@@ -188,7 +180,7 @@ public class AlternativesManager
     public boolean isAlternative(Class<?> beanType, Set<Class<? extends Annotation>>
stereotypes)
     {
         if(configuredAlternatives.contains(beanType) ||
-           sortedAlternatives.contains(beanType))
+           prioritizedAlternatives.contains(beanType))
         {
             return true;
         }
@@ -211,7 +203,7 @@ public class AlternativesManager
         configuredStereotypeAlternatives.clear();
         priorityAlternatives.clear();
 
-        sortedAlternatives = null;
+        prioritizedAlternatives = null;
     }
 
     private static class PriorityAlternative implements Comparable<PriorityAlternative>
@@ -230,9 +222,13 @@ public class AlternativesManager
         {
             if (priority != o.priority)
             {
-                return Integer.compare(priority, o.priority);
+                // sort descending
+                return (priority > o.priority) ? -1 : ((priority == o.priority) ? 0 :
1);
             }
 
+            // we additionally sort according to the class name to at least
+            // prevent randomness if 2 classes have the same ordinal.
+            // see CDI-437 for more info about why it's broken in CDI-1.1.
             return clazz.getName().compareTo(o.clazz.getName());
         }
     }

Added: openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/util/SingleItemSet.java
URL: http://svn.apache.org/viewvc/openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/util/SingleItemSet.java?rev=1595822&view=auto
==============================================================================
--- openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/util/SingleItemSet.java
(added)
+++ openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/util/SingleItemSet.java
Mon May 19 10:34:04 2014
@@ -0,0 +1,155 @@
+/*
+ * 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.util;
+
+import java.util.Collection;
+import java.util.Iterator;
+import java.util.Set;
+
+/**
+ * This is a special 'Set' which actually is no Collection but
+ * only contains a very single item.
+ * This way we keep mem low and also are pretty fast ;)
+ */
+public class SingleItemSet<T> implements Set<T>
+{
+
+    private T instance;
+
+
+    public SingleItemSet(T instance)
+    {
+        this.instance = instance;
+    }
+
+    @Override
+    public int size()
+    {
+        return 1;
+    }
+
+    @Override
+    public boolean isEmpty()
+    {
+        return false;
+    }
+
+    @Override
+    public boolean contains(Object o)
+    {
+        return instance.equals(o);
+    }
+
+    @Override
+    public Iterator<T> iterator()
+    {
+        return new SingleItemIterator(instance);
+    }
+
+    @Override
+    public Object[] toArray()
+    {
+        Object[] array = new Object[1];
+        array[0] = instance;
+        return array;
+    }
+
+    @Override
+    public <T1> T1[] toArray(T1[] a)
+    {
+        if (a.length > 0)
+        {
+            a[0] = (T1) instance;
+            return a;
+        }
+        return (T1[]) toArray();
+    }
+
+    @Override
+    public boolean add(T t)
+    {
+        throw new IllegalArgumentException("not supported operation");
+    }
+
+    @Override
+    public boolean remove(Object o)
+    {
+        throw new IllegalArgumentException("not supported operation");
+    }
+
+    @Override
+    public boolean containsAll(Collection<?> c)
+    {
+        return false;
+    }
+
+    @Override
+    public boolean addAll(Collection<? extends T> c)
+    {
+        throw new IllegalArgumentException("not supported operation");
+    }
+
+    @Override
+    public boolean retainAll(Collection<?> c)
+    {
+        throw new IllegalArgumentException("not supported operation");
+    }
+
+    @Override
+    public boolean removeAll(Collection<?> c)
+    {
+        return false;
+    }
+
+    @Override
+    public void clear()
+    {
+        instance = null;
+    }
+
+    public class SingleItemIterator implements Iterator<T>
+    {
+        private T instance;
+
+        public SingleItemIterator(T instance)
+        {
+            this.instance = instance;
+        }
+
+        @Override
+        public boolean hasNext()
+        {
+            return instance != null;
+        }
+
+        @Override
+        public T next()
+        {
+            T oldInstance = instance;
+            this.instance = null;
+            return oldInstance;
+        }
+
+        @Override
+        public void remove()
+        {
+            // do nothing...
+        }
+    }
+}

Modified: openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/util/WebBeansUtil.java
URL: http://svn.apache.org/viewvc/openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/util/WebBeansUtil.java?rev=1595822&r1=1595821&r2=1595822&view=diff
==============================================================================
--- openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/util/WebBeansUtil.java
(original)
+++ openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/util/WebBeansUtil.java
Mon May 19 10:34:04 2014
@@ -751,7 +751,7 @@ public final class WebBeansUtil
                     //disable superbean if the current bean is not an alternative
                     ((AbstractOwbBean<?>)superBean).setEnabled(false);
                 }
-                else if(altManager.isClassAlternative(specializedClass))
+                else if(altManager.isAlternative(specialized))
                 {
                     //disable superbean if the current bean is an enabled alternative
                     ((AbstractOwbBean<?>)superBean).setEnabled(false);

Copied: openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/test/concepts/alternatives/common/AlternativeWithEvenHigherPriorityBean.java
(from r1594099, openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/test/concepts/alternatives/common/AlternativeWithHighPriorityBean.java)
URL: http://svn.apache.org/viewvc/openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/test/concepts/alternatives/common/AlternativeWithEvenHigherPriorityBean.java?p2=openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/test/concepts/alternatives/common/AlternativeWithEvenHigherPriorityBean.java&p1=openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/test/concepts/alternatives/common/AlternativeWithHighPriorityBean.java&r1=1594099&r2=1595822&rev=1595822&view=diff
==============================================================================
--- openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/test/concepts/alternatives/common/AlternativeWithHighPriorityBean.java
(original)
+++ openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/test/concepts/alternatives/common/AlternativeWithEvenHigherPriorityBean.java
Mon May 19 10:34:04 2014
@@ -22,13 +22,13 @@ import javax.annotation.Priority;
 import javax.enterprise.inject.Alternative;
 
 @Alternative
-@Priority(100)
-public class AlternativeWithHighPriorityBean implements SimpleInterface
+@Priority(500)
+public class AlternativeWithEvenHigherPriorityBean implements SimpleInterface
 {
 
     @Override
     public Class<?> getImplementationType()
     {
-        return AlternativeWithHighPriorityBean.class;
+        return AlternativeWithEvenHigherPriorityBean.class;
     }
 }

Copied: openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/test/concepts/alternatives/common/AlternativeWithSamePriorityBean.java
(from r1594099, openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/test/concepts/alternatives/common/AlternativeWithPriorityBean.java)
URL: http://svn.apache.org/viewvc/openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/test/concepts/alternatives/common/AlternativeWithSamePriorityBean.java?p2=openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/test/concepts/alternatives/common/AlternativeWithSamePriorityBean.java&p1=openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/test/concepts/alternatives/common/AlternativeWithPriorityBean.java&r1=1594099&r2=1595822&rev=1595822&view=diff
==============================================================================
--- openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/test/concepts/alternatives/common/AlternativeWithPriorityBean.java
(original)
+++ openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/test/concepts/alternatives/common/AlternativeWithSamePriorityBean.java
Mon May 19 10:34:04 2014
@@ -23,12 +23,12 @@ import javax.enterprise.inject.Alternati
 
 @Alternative
 @Priority(5)
-public class AlternativeWithPriorityBean implements SimpleInterface
+public class AlternativeWithSamePriorityBean implements SimpleInterface
 {
 
     @Override
     public Class<?> getImplementationType()
     {
-        return AlternativeWithPriorityBean.class;
+        return AlternativeWithSamePriorityBean.class;
     }
 }

Modified: openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/test/concepts/alternatives/tests/AlternativeBeanResolvingTest.java
URL: http://svn.apache.org/viewvc/openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/test/concepts/alternatives/tests/AlternativeBeanResolvingTest.java?rev=1595822&r1=1595821&r2=1595822&view=diff
==============================================================================
--- openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/test/concepts/alternatives/tests/AlternativeBeanResolvingTest.java
(original)
+++ openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/test/concepts/alternatives/tests/AlternativeBeanResolvingTest.java
Mon May 19 10:34:04 2014
@@ -29,6 +29,8 @@ import org.apache.webbeans.test.Abstract
 import org.apache.webbeans.test.concepts.alternatives.common.AlternativeBean;
 import org.apache.webbeans.test.concepts.alternatives.common.AlternativeOnClassAndProducerMethodBean;
 import org.apache.webbeans.test.concepts.alternatives.common.AlternativeOnClassOnlyBean;
+import org.apache.webbeans.test.concepts.alternatives.common.AlternativeWithEvenHigherPriorityBean;
+import org.apache.webbeans.test.concepts.alternatives.common.AlternativeWithHighPriorityBean;
 import org.apache.webbeans.test.concepts.alternatives.common.AlternativeWithPriorityBean;
 import org.apache.webbeans.test.concepts.alternatives.common.DefaultBeanProducerWithoutDisposes;
 import org.apache.webbeans.test.concepts.alternatives.common.Pen;
@@ -130,4 +132,20 @@ public class AlternativeBeanResolvingTes
         Assert.assertEquals(AlternativeWithPriorityBean.class, simpleInterface.getImplementationType());
     }
 
+    /**
+     * Test alternatives which are NOT in any beans.xml but activated
+     * by having a Priority annotation.
+     */
+    @Test
+    public void testMultiplePriorityAlternative() throws Exception
+    {
+        startContainer(AlternativeWithPriorityBean.class, AlternativeWithEvenHigherPriorityBean.class,
AlternativeWithHighPriorityBean.class, SimpleBean.class);
+
+        SimpleInterface simpleInterface = getInstance(SimpleInterface.class);
+        Assert.assertNotNull(simpleInterface);
+        Assert.assertEquals(AlternativeWithEvenHigherPriorityBean.class, simpleInterface.getImplementationType());
+    }
+
+
+
 }



Mime
View raw message