openwebbeans-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From strub...@apache.org
Subject svn commit: r1678731 - in /openwebbeans/trunk: webbeans-impl/src/main/java/org/apache/webbeans/xml/ webbeans-spi/src/main/java/org/apache/webbeans/spi/ webbeans-web/src/it/webcdiapp/
Date Mon, 11 May 2015 11:46:11 GMT
Author: struberg
Date: Mon May 11 11:46:10 2015
New Revision: 1678731

URL: http://svn.apache.org/r1678731
Log:
OWB-1071 implement beans.xml merging between WEB-INF/beans.xml and WEB-INF/classes/META-INF/beans.xml

Modified:
    openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/xml/DefaultBeanArchiveInformation.java
    openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/xml/DefaultBeanArchiveService.java
    openwebbeans/trunk/webbeans-spi/src/main/java/org/apache/webbeans/spi/BeanArchiveService.java
    openwebbeans/trunk/webbeans-web/src/it/webcdiapp/pom.xml

Modified: openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/xml/DefaultBeanArchiveInformation.java
URL: http://svn.apache.org/viewvc/openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/xml/DefaultBeanArchiveInformation.java?rev=1678731&r1=1678730&r2=1678731&view=diff
==============================================================================
--- openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/xml/DefaultBeanArchiveInformation.java
(original)
+++ openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/xml/DefaultBeanArchiveInformation.java
Mon May 11 11:46:10 2015
@@ -168,4 +168,24 @@ public class DefaultBeanArchiveInformati
 
         excludedPackages.add(packageName);
     }
+
+    public List<String> getExcludedClasses()
+    {
+        return excludedClasses;
+    }
+
+    public void setExcludedClasses(List<String> excludedClasses)
+    {
+        this.excludedClasses = excludedClasses;
+    }
+
+    public List<String> getExcludedPackages()
+    {
+        return excludedPackages;
+    }
+
+    public void setExcludedPackages(List<String> excludedPackages)
+    {
+        this.excludedPackages = excludedPackages;
+    }
 }

Modified: openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/xml/DefaultBeanArchiveService.java
URL: http://svn.apache.org/viewvc/openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/xml/DefaultBeanArchiveService.java?rev=1678731&r1=1678730&r2=1678731&view=diff
==============================================================================
--- openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/xml/DefaultBeanArchiveService.java
(original)
+++ openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/xml/DefaultBeanArchiveService.java
Mon May 11 11:46:10 2015
@@ -24,7 +24,9 @@ import java.io.IOException;
 import java.io.InputStream;
 import java.io.PushbackInputStream;
 import java.net.URL;
+import java.util.ArrayList;
 import java.util.HashMap;
+import java.util.List;
 import java.util.Map;
 import java.util.Set;
 import java.util.logging.Level;
@@ -47,8 +49,8 @@ import org.w3c.dom.NodeList;
  */
 public class DefaultBeanArchiveService implements BeanArchiveService
 {
-    public static final String WEB_INF_BEANS_XML = "WEB-INF/beans.xml";
-    public static final String WEB_INF_CLASSES = "WEB-INF/classes";
+    private static final String WEB_INF_BEANS_XML = "WEB-INF/beans.xml";
+    private static final String WEB_INF_CLASSES = "WEB-INF/classes/";
 
     private static final Logger logger = WebBeansLoggerFacade.getLogger(BeanArchiveService.class);
 
@@ -73,13 +75,6 @@ public class DefaultBeanArchiveService i
 
             String strippedBeanArchiveUrl = stripProtocol(beanArchiveLocation);
 
-            if (strippedBeanArchiveUrl.contains(WEB_INF_BEANS_XML))
-            {
-                // this is a very special case for beans.xml in a WAR file
-                // in this case we are looking for the WEB-INF/classes URL
-                strippedBeanArchiveUrl = strippedBeanArchiveUrl.replace(WEB_INF_BEANS_XML,
WEB_INF_CLASSES);
-            }
-
             for (Map.Entry<String, BeanArchiveInformation> entry : beanArchiveInformations.entrySet())
             {
                 if (stripProtocol(entry.getKey()).startsWith(strippedBeanArchiveUrl))
@@ -91,6 +86,21 @@ public class DefaultBeanArchiveService i
 
         }
 
+        if (bdaInfo == null && beanArchiveLocation.contains(WEB_INF_CLASSES))
+        {
+            // this is a very special case for beans.xml in a WAR file
+            // in this case we need to merge the 2 BDAs from WEB-INF/classes/META-INF/beans.xml
and WEB-INF/beans.xml
+            // this requires the WEB-INF/beans.xml being parsed first (which is usually the
case)
+
+            // first we read the BeanArchiveInformation from the WEB-INF/classes directory.
+            bdaInfo = readBeansXml(beanArchiveUrl, beanArchiveLocation);
+
+            // next we merge in the BDAInfo from WEB-INF/beans.xml
+            bdaInfo = mergeWithWebInfBeansXml(bdaInfo);
+            beanArchiveInformations.put(beanArchiveLocation, bdaInfo);
+            registeredBeanArchives.add(beanArchiveUrl);
+        }
+
         if (bdaInfo == null)
         {
             // if we still did not find anything, then this is a 'new' bean archive
@@ -102,6 +112,70 @@ public class DefaultBeanArchiveService i
         return bdaInfo;
     }
 
+    /**
+     * Merge the BDA info from webInfClasses with the one from WEB-INF/beans.xml
+     */
+    private BeanArchiveInformation mergeWithWebInfBeansXml(BeanArchiveInformation bdaWebClasses)
+    {
+        BeanArchiveInformation bdaWebInf = null;
+        for (Map.Entry<String, BeanArchiveInformation> entry : beanArchiveInformations.entrySet())
+        {
+            if (entry.getKey().endsWith("WEB-INF/beans.xml"))
+            {
+                bdaWebInf = entry.getValue();
+                break;
+            }
+        }
+
+        if (bdaWebInf == null)
+        {
+            // no merge needed
+            return bdaWebClasses;
+        }
+
+        // means we need to merge them
+        DefaultBeanArchiveInformation mergedBdaInfo = new DefaultBeanArchiveInformation();
+
+        mergedBdaInfo.setBeanDiscoveryMode(BeanDiscoveryMode.max(bdaWebClasses.getBeanDiscoveryMode(),
bdaWebInf.getBeanDiscoveryMode()));
+
+        mergedBdaInfo.setVersion(bdaWebClasses.getVersion() != null ? bdaWebClasses.getVersion()
: bdaWebInf.getVersion());
+
+        mergedBdaInfo.setExcludedClasses(mergeLists(bdaWebClasses.getExcludedClasses(), bdaWebInf.getExcludedClasses()));
+        mergedBdaInfo.setExcludedPackages(mergeLists(bdaWebClasses.getExcludedPackages(),
bdaWebInf.getExcludedPackages()));
+
+        mergedBdaInfo.setInterceptors(mergeLists(bdaWebClasses.getInterceptors(), bdaWebInf.getInterceptors()));
+        mergedBdaInfo.setDecorators(mergeLists(bdaWebClasses.getDecorators(), bdaWebInf.getDecorators()));
+        mergedBdaInfo.getAlternativeClasses().addAll(mergeLists(bdaWebClasses.getAlternativeClasses(),
bdaWebInf.getAlternativeClasses()));
+        mergedBdaInfo.getAlternativeStereotypes().addAll(mergeLists(bdaWebClasses.getAlternativeStereotypes(),
bdaWebInf.getAlternativeStereotypes()));
+
+        return mergedBdaInfo;
+    }
+
+    private List<String> mergeLists(List<String> list1, List<String> list2)
+    {
+        if (list1 == null || list1.isEmpty())
+        {
+            return list2;
+        }
+
+        if (list2 == null)
+        {
+            return null;
+        }
+
+        List<String> mergedList = new ArrayList<String>(list1);
+
+        for (String val : list2)
+        {
+            if (!mergedList.contains(val))
+            {
+                mergedList.add(val);
+            }
+        }
+
+        return mergedList;
+    }
+
     @Override
     public Set<URL> getRegisteredBeanArchives()
     {

Modified: openwebbeans/trunk/webbeans-spi/src/main/java/org/apache/webbeans/spi/BeanArchiveService.java
URL: http://svn.apache.org/viewvc/openwebbeans/trunk/webbeans-spi/src/main/java/org/apache/webbeans/spi/BeanArchiveService.java?rev=1678731&r1=1678730&r2=1678731&view=diff
==============================================================================
--- openwebbeans/trunk/webbeans-spi/src/main/java/org/apache/webbeans/spi/BeanArchiveService.java
(original)
+++ openwebbeans/trunk/webbeans-spi/src/main/java/org/apache/webbeans/spi/BeanArchiveService.java
Mon May 11 11:46:10 2015
@@ -45,7 +45,7 @@ public interface BeanArchiveService
          * will get picked up as &#064;Dependent scoped beans.
          * This is basically the backward compatible mode to CDI-1.0.
          */
-        ALL,
+        ALL(10),
 
         /**
          * Only classes with a 'bean defining annotation' will get
@@ -53,12 +53,53 @@ public interface BeanArchiveService
          * A 'bean defining annotation' is any CDI or atinject Scope annotation
          * as well as Stereotypes (the later only since CDI-1.2)
          */
-        ANNOTATED,
+        ANNOTATED(5),
 
         /**
          * Ignore all classes in this BDA when it comes to beans scanning.
          */
-        NONE
+        NONE(2);
+
+        /**
+         * used for internal sorting. higher ordinal means more scanning
+         */
+        private final int ordinal;
+
+        BeanDiscoveryMode(int ordinal)
+        {
+            this.ordinal = ordinal;
+        }
+
+        public int getOrdinal()
+        {
+            return ordinal;
+        }
+
+        public static BeanDiscoveryMode getByOrdinal(int ordinal)
+        {
+            for (BeanDiscoveryMode beanDiscoveryMode : values())
+            {
+                if (beanDiscoveryMode.getOrdinal() == ordinal)
+                {
+                    return beanDiscoveryMode;
+                }
+            }
+            return null;
+        }
+
+        public static BeanDiscoveryMode max(BeanDiscoveryMode bdmA, BeanDiscoveryMode bdmB)
+        {
+            if (bdmA == null)
+            {
+                return bdmB;
+            }
+            if (bdmB == null)
+            {
+                return bdmA;
+            }
+
+            return getByOrdinal(Math.max(bdmA.getOrdinal(), bdmB.getOrdinal()));
+        }
     }
 
     /**
@@ -109,6 +150,10 @@ public interface BeanArchiveService
          *          in a &lt;alternatives&gt;&lt;stereotype&gt; section or
an empty List.
          */
         List<String> getAlternativeStereotypes();
+
+
+        List<String> getExcludedClasses();
+        List<String> getExcludedPackages();
     }
 
 

Modified: openwebbeans/trunk/webbeans-web/src/it/webcdiapp/pom.xml
URL: http://svn.apache.org/viewvc/openwebbeans/trunk/webbeans-web/src/it/webcdiapp/pom.xml?rev=1678731&r1=1678730&r2=1678731&view=diff
==============================================================================
--- openwebbeans/trunk/webbeans-web/src/it/webcdiapp/pom.xml (original)
+++ openwebbeans/trunk/webbeans-web/src/it/webcdiapp/pom.xml Mon May 11 11:46:10 2015
@@ -242,7 +242,7 @@
                         <id>start-tomcat</id>
                         <phase>pre-integration-test</phase>
                         <goals>
-                            <goal>run</goal>
+                            <goal>run-war-only</goal>
                         </goals>
                     </execution>
                     <execution>



Mime
View raw message