openwebbeans-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From gerdo...@apache.org
Subject svn commit: r911515 - in /openwebbeans/trunk/webbeans-impl/src: main/java/org/apache/webbeans/config/ main/java/org/apache/webbeans/util/ test/java/org/apache/webbeans/test/unittests/specializes/logger/
Date Thu, 18 Feb 2010 18:36:19 GMT
Author: gerdogdu
Date: Thu Feb 18 18:36:19 2010
New Revision: 911515

URL: http://svn.apache.org/viewvc?rev=911515&view=rev
Log:
[OWB-284] thanks to YING WANG

Modified:
    openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/config/BeansDeployer.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/unittests/specializes/logger/LoggerSpecializationTest.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=911515&r1=911514&r2=911515&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
Thu Feb 18 18:36:19 2010
@@ -20,6 +20,7 @@
 import java.util.HashSet;
 import java.util.Iterator;
 import java.util.Set;
+import java.util.ArrayList;
 
 import javax.enterprise.inject.Model;
 import javax.enterprise.inject.Specializes;
@@ -431,32 +432,31 @@
             Set<Class<?>> beanClasses = scanner.getBeanClasses();
             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 
+                //specialize Car. 
                 Class<?> superClass = null;
+                ArrayList<Class<?>> superClassList = new ArrayList<Class<?>>();
+                ArrayList<Class<?>> specialClassList = new ArrayList<Class<?>>();
                 for(Class<?> specialClass : beanClasses)
                 {
                     if(AnnotationUtil.hasClassAnnotation(specialClass, Specializes.class))
                     {
-                        if (superClass == null)
+                        superClass = specialClass.getSuperclass();
+                        if(superClass.equals(Object.class))
                         {
-                            superClass = specialClass.getSuperclass();
-                            
-                            if(superClass.equals(Object.class))
-                            {
-                                throw new WebBeansConfigurationException(logger.getTokenString(OWBLogConst.EXCEPT_0003)
+ specialClass.getName()
-                                                                         + logger.getTokenString(OWBLogConst.EXCEPT_0004));
-                            }
+                            throw new WebBeansConfigurationException(logger.getTokenString(OWBLogConst.EXCEPT_0003)
+ specialClass.getName()
+                                                                     + logger.getTokenString(OWBLogConst.EXCEPT_0004));
                         }
-                        else
+                        if (superClassList.contains(superClass))
                         {
-                            if (superClass.equals(specialClass.getSuperclass()))
-                            {
-                                throw new InconsistentSpecializationException(logger.getTokenString(OWBLogConst.EXCEPT_0005)
+ superClass.getName());
-                            }
+                            throw new InconsistentSpecializationException(logger.getTokenString(OWBLogConst.EXCEPT_0005)
+ superClass.getName());
                         }
-                        
-                        WebBeansUtil.configureSpecializations(specialClass);            
           
+                        superClassList.add(superClass);
+                        specialClassList.add(specialClass);
                     }
                 }
+                WebBeansUtil.configureSpecializations(specialClassList);                
       
             }
 
             // XML Defined Specializations
@@ -483,6 +483,7 @@
         Iterator<Class<?>> it = clazzes.iterator();
         Class<?> superClass = null;
         Class<?> specialClass = null;
+        ArrayList<Class<?>> specialClassList = new ArrayList<Class<?>>();
         while (it.hasNext())
         {
             specialClass = it.next();
@@ -499,10 +500,9 @@
                                                                  + superClass.getName());
                 }
             }
-
-            WebBeansUtil.configureSpecializations(specialClass);
-
+            specialClassList.add(specialClass);
         }
+        WebBeansUtil.configureSpecializations(specialClassList);
     }
 
     /**

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=911515&r1=911514&r2=911515&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
Thu Feb 18 18:36:19 2010
@@ -1516,6 +1516,52 @@
         }
     }
     
+    /**
+     * Return true if a list of beans are directly specialized/extended each other.
+     * 
+     * @param beans, a set of specialized beans.
+     * 
+     * @return
+     */
+    protected static boolean isDirectlySpecializedBeanSet(Set<Bean<?>> beans)
{
+    	
+    	ArrayList<AbstractOwbBean<?>> beanList = new ArrayList<AbstractOwbBean<?>>();
+
+    	for(Bean<?> bb : beans) 
+    	{
+    		AbstractOwbBean<?>bean = (AbstractOwbBean<?>)bb;
+    		beanList.add(bean);
+    	}
+    	
+    	java.util.Collections.sort(beanList, new java.util.Comparator() 
+    	{
+    		public int compare(Object o1, Object o2) 
+    		{
+    			AbstractOwbBean<?> b1 = (AbstractOwbBean<?>)o1;
+    			AbstractOwbBean<?> b2 = (AbstractOwbBean<?>)o2;
+    			Class c1 = b1.getReturnType();
+    			Class c2 = b2.getReturnType();
+    			if (c2.isAssignableFrom(c1)) return 1;
+    			if (c1.isAssignableFrom(c2)) return -1;
+    			throw new InconsistentSpecializationException(c1 + " and " + c2 + "are not assignable
to each other." );
+    		}
+    	});
+
+    	for(int i=0; i<beanList.size() - 1; i++) 
+    	{
+    		if (!beanList.get(i).getReturnType().equals(beanList.get(i+1).getReturnType().getSuperclass()))
+    				return false;
+    	}
+    	return true;
+    }
+
+    public static void configureSpecializations(List<Class<?>> beanClasses)
+    {
+    	for(Class<?> clazz : beanClasses) 
+    	{
+    		configureSpecializations(clazz, beanClasses);
+    	}
+	}
 
     /**
      * Configures the bean specializations.
@@ -1530,29 +1576,40 @@
      * @throws InconsistentSpecializationException related with priority
      * @throws WebBeansConfigurationException any other exception
      */
-    public static void configureSpecializations(Class<?> specializedClass)
+    protected static void configureSpecializations(Class<?> specializedClass, List<Class<?>>
beanClasses)
     {
         Asserts.nullCheckForClass(specializedClass);
 
         Bean<?> superBean = null;
         Bean<?> specialized = null;
         Set<Bean<?>> resolvers = null;
+        AlternativesManager altManager = AlternativesManager.getInstance();
         
-        if ((resolvers = isConfiguredWebBeans(specializedClass,true)) != null)
+        if ((resolvers = isConfiguredWebBeans(specializedClass, true)) != null)
         {            
             if(resolvers.isEmpty())
             {
                 throw new InconsistentSpecializationException("Specialized bean for class
: " + specializedClass + " is not enabled in the deployment.");
             }
             
+            specialized = resolvers.iterator().next();
+            
             if(resolvers.size() > 1)
             {
-                throw new InconsistentSpecializationException("More than one specialized
bean for class : " + specializedClass + " is enabled in the deployment.");
+            	if (!isDirectlySpecializedBeanSet(resolvers)) 
+            	{
+            		throw new InconsistentSpecializationException("More than one specialized bean
for class : " + specializedClass + " is enabled in the deployment.");
+            	}
+            	// find the widest bean which satisfies the specializedClass
+                for( Bean<?> sp : resolvers) {
+                	if (sp == specialized) continue;
+                	if (((AbstractOwbBean<?>)sp).getReturnType().isAssignableFrom(((AbstractOwbBean<?>)specialized).getReturnType()))

+                	{
+                		specialized = sp;
+                	}
+                }
             }
             
-                                   
-            specialized = resolvers.iterator().next();
-            
             Class<?> superClass = specializedClass.getSuperclass();
             
             resolvers = isConfiguredWebBeans(superClass,false);
@@ -1573,7 +1630,23 @@
                         
             if (superBean != null)
             {
-                ((AbstractOwbBean<?>)superBean).setEnabled(false);
+            	// Recursively configure super class first if super class is also a special
bean.
+            	// So the name and bean meta data could be populated to this beanclass.  
+            	if (beanClasses.contains(superClass) && ((AbstractOwbBean<?>)superBean).isEnabled())

+            	{
+            		configureSpecializations(superClass, beanClasses);
+            	}
+            	
+            	if (!AnnotationUtil.hasClassAnnotation(specializedClass, Alternative.class))

+            	{
+            		//disable superbean if the current bean is not an alternative 
+            		((AbstractOwbBean<?>)superBean).setEnabled(false);
+            	} 
+            	else if(altManager.isClassAlternative(specializedClass)) 
+            	{
+            		//disable superbean if the current bean is an enabled alternative 
+            		((AbstractOwbBean<?>)superBean).setEnabled(false);
+            	}
                                 
                 AbstractOwbBean<?> comp = (AbstractOwbBean<?>)specialized;
 

Modified: openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/test/unittests/specializes/logger/LoggerSpecializationTest.java
URL: http://svn.apache.org/viewvc/openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/test/unittests/specializes/logger/LoggerSpecializationTest.java?rev=911515&r1=911514&r2=911515&view=diff
==============================================================================
--- openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/test/unittests/specializes/logger/LoggerSpecializationTest.java
(original)
+++ openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/test/unittests/specializes/logger/LoggerSpecializationTest.java
Thu Feb 18 18:36:19 2010
@@ -14,7 +14,7 @@
 package org.apache.webbeans.test.unittests.specializes.logger;
 
 import java.io.InputStream;
-
+import java.util.ArrayList;
 import javax.enterprise.inject.spi.Bean;
 
 import junit.framework.Assert;
@@ -91,7 +91,9 @@
         defineManagedBean(SystemLogger.class);
         defineManagedBean(MockSpecializedLogger.class);
         
-        WebBeansUtil.configureSpecializations(MockSpecializedLogger.class);
+        ArrayList<Class<?>> specialClassList = new ArrayList<Class<?>>();
+        specialClassList.add(MockSpecializedLogger.class);
+        WebBeansUtil.configureSpecializations(specialClassList);
         
         Bean<SpecializedInjector> bean = defineManagedBean(SpecializedInjector.class);
         Object instance = getManager().getReference(bean, SpecializedInjector.class, getManager().createCreationalContext(bean));



Mime
View raw message