You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tomee.apache.org by dj...@apache.org on 2011/07/09 08:14:45 UTC

svn commit: r1144602 - /openejb/trunk/openejb3/container/openejb-osgi-core/src/main/java/org/apache/openejb/osgi/core/BundleFinderFactory.java

Author: djencks
Date: Sat Jul  9 06:14:45 2011
New Revision: 1144602

URL: http://svn.apache.org/viewvc?rev=1144602&view=rev
Log:
GERONIMO-6043 filter the annotated classes for owb based on whether they are in a jar with beans.xml (or WEB-INF/beans.xml)

Modified:
    openejb/trunk/openejb3/container/openejb-osgi-core/src/main/java/org/apache/openejb/osgi/core/BundleFinderFactory.java

Modified: openejb/trunk/openejb3/container/openejb-osgi-core/src/main/java/org/apache/openejb/osgi/core/BundleFinderFactory.java
URL: http://svn.apache.org/viewvc/openejb/trunk/openejb3/container/openejb-osgi-core/src/main/java/org/apache/openejb/osgi/core/BundleFinderFactory.java?rev=1144602&r1=1144601&r2=1144602&view=diff
==============================================================================
--- openejb/trunk/openejb3/container/openejb-osgi-core/src/main/java/org/apache/openejb/osgi/core/BundleFinderFactory.java (original)
+++ openejb/trunk/openejb3/container/openejb-osgi-core/src/main/java/org/apache/openejb/osgi/core/BundleFinderFactory.java Sat Jul  9 06:14:45 2011
@@ -16,10 +16,18 @@
  */
 package org.apache.openejb.osgi.core;
 
+import java.io.InputStream;
+import java.net.URL;
+import java.util.Enumeration;
+import java.util.HashSet;
+import java.util.Set;
+import java.util.zip.ZipEntry;
+
 import org.apache.openejb.config.DeploymentModule;
 import org.apache.openejb.config.FinderFactory;
 import org.apache.xbean.finder.IAnnotationFinder;
 import org.apache.xbean.finder.BundleAnnotationFinder;
+import org.apache.xbean.osgi.bundle.util.BundleResourceFinder;
 import org.apache.xbean.osgi.bundle.util.DiscoveryRange;
 import org.apache.xbean.osgi.bundle.util.ResourceDiscoveryFilter;
 import org.osgi.framework.Bundle;
@@ -32,6 +40,8 @@ import org.osgi.service.packageadmin.Pac
  * @version $Rev$ $Date$
  */
 public class BundleFinderFactory extends FinderFactory {
+    private static final String META_INF_BEANS_XML = "META-INF/beans.xml";
+    private static final String WEB_INF_BEANS_XML = "WEB-INF/beans.xml";
 
     @Override
     public IAnnotationFinder create(DeploymentModule module) throws Exception {
@@ -57,6 +67,8 @@ public class BundleFinderFactory extends
             boolean useLocation = location != null
                     && !location.isEmpty()
                     && !module.isStandaloneModule();
+            Set<String> beanArchiveJarNames = findBeansXml(bundle, packageAdmin, useLocation? location : "");
+
             BundleAnnotationFinder bundleAnnotationFinder;
             if (useLocation) {
                 ResourceDiscoveryFilter filter = new ResourceDiscoveryFilter() {
@@ -77,7 +89,7 @@ public class BundleFinderFactory extends
                     }
                 };
 
-                bundleAnnotationFinder = new BundleAnnotationFinder(packageAdmin, bundle, filter);
+                bundleAnnotationFinder = new BundleAnnotationFinder(packageAdmin, bundle, filter, beanArchiveJarNames);
             } else {
                 ResourceDiscoveryFilter filter = new ResourceDiscoveryFilter() {
 
@@ -97,7 +109,7 @@ public class BundleFinderFactory extends
                     }
                 };
 
-                bundleAnnotationFinder = new BundleAnnotationFinder(packageAdmin, bundle, filter);
+                bundleAnnotationFinder = new BundleAnnotationFinder(packageAdmin, bundle, filter, beanArchiveJarNames);
             }
             bundleAnnotationFinder.link();
             return bundleAnnotationFinder;
@@ -106,4 +118,58 @@ public class BundleFinderFactory extends
         throw new IllegalStateException("Module classloader is not a BundleReference. Only use BundleFactoryFinder in an pure osgi environment");
     }
 
+    //TODO consider passing in location?
+    private Set<String> findBeansXml(Bundle mainBundle, PackageAdmin packageAdmin, String location)
+            throws Exception
+    {
+        final Set<String> beanArchiveJarNames = new HashSet<String>();
+        BundleResourceFinder brfXmlJar =  new BundleResourceFinder(packageAdmin, mainBundle, "", META_INF_BEANS_XML);
+
+        BundleResourceFinder.ResourceFinderCallback rfCallback = new BundleResourceFinder.ResourceFinderCallback()
+        {
+
+            public boolean foundInDirectory(Bundle bundle, String basePath, URL url) throws Exception
+            {
+//                logger.info("adding the following beans.xml URL: " + url);
+                beanArchiveJarNames.add(basePath);
+                return true;
+            }
+
+            public boolean foundInJar(Bundle bundle, String jarName, ZipEntry entry, InputStream in) throws Exception
+            {
+                URL jarURL = bundle.getEntry(jarName);
+//                URL beansUrl = new URL("jar:" + jarURL.toString() + "!/" + entry.getName());
+
+//                logger.info("adding the following beans.xml URL: " + beansUrl);
+
+                beanArchiveJarNames.add(jarName);
+                return true;
+            }
+
+        };
+
+        brfXmlJar.find(rfCallback);
+
+// TODO I found no other way to find WEB-INF/beanx.xml directly
+        Enumeration<URL> urls = mainBundle.findEntries(location + "/WEB-INF", "beans.xml", true);
+        boolean webBeansXmlFound = false;
+        while (urls != null && urls.hasMoreElements()) {
+            URL webBeansXml = urls.nextElement();
+            String webBeansXMlString = webBeansXml.toExternalForm();
+            if (!webBeansXMlString.endsWith("/" + WEB_INF_BEANS_XML)) {
+                continue;
+            }
+
+            if (webBeansXmlFound) {
+                throw new IllegalStateException("found more than WEB-INF/beans.xml file!" + webBeansXml);
+            }
+
+            //            logger.info("adding the following WEB-INF/beans.xml URL: " + webBeansXml);
+            beanArchiveJarNames.add(location + (location.isEmpty()? "": "/") + "WEB-INF/classes/");
+            webBeansXmlFound = true;
+
+        }
+        return beanArchiveJarNames;
+    }
+
 }