You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@openwebbeans.apache.org by st...@apache.org on 2010/06/24 14:23:26 UTC

svn commit: r957523 - in /openwebbeans/trunk: ./ webbeans-impl/src/main/java/org/apache/webbeans/exception/ webbeans-osgi/ webbeans-osgi/src/ webbeans-osgi/src/main/ webbeans-osgi/src/main/java/ webbeans-osgi/src/main/java/org/ webbeans-osgi/src/main/j...

Author: struberg
Date: Thu Jun 24 12:23:26 2010
New Revision: 957523

URL: http://svn.apache.org/viewvc?rev=957523&view=rev
Log:
OWB-400 add ScannerService for OSGi classloader environments like Geronimo3

Added:
    openwebbeans/trunk/webbeans-osgi/   (with props)
    openwebbeans/trunk/webbeans-osgi/pom.xml
    openwebbeans/trunk/webbeans-osgi/src/
    openwebbeans/trunk/webbeans-osgi/src/main/
    openwebbeans/trunk/webbeans-osgi/src/main/java/
    openwebbeans/trunk/webbeans-osgi/src/main/java/org/
    openwebbeans/trunk/webbeans-osgi/src/main/java/org/apache/
    openwebbeans/trunk/webbeans-osgi/src/main/java/org/apache/webbeans/
    openwebbeans/trunk/webbeans-osgi/src/main/java/org/apache/webbeans/osgi/
    openwebbeans/trunk/webbeans-osgi/src/main/java/org/apache/webbeans/osgi/scanner/
    openwebbeans/trunk/webbeans-osgi/src/main/java/org/apache/webbeans/osgi/scanner/OsgiMetaDataScannerService.java
Modified:
    openwebbeans/trunk/pom.xml
    openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/exception/WebBeansDeploymentException.java

Modified: openwebbeans/trunk/pom.xml
URL: http://svn.apache.org/viewvc/openwebbeans/trunk/pom.xml?rev=957523&r1=957522&r2=957523&view=diff
==============================================================================
--- openwebbeans/trunk/pom.xml (original)
+++ openwebbeans/trunk/pom.xml Thu Jun 24 12:23:26 2010
@@ -376,6 +376,7 @@
         <module>webbeans-jsf12</module>
         <module>webbeans-el10</module>
         <module>webbeans-resource</module>
+        <module>webbeans-osgi</module>
         <module>webbeans-porting</module>
         <module>webbeans-test</module>
         <module>samples</module>
@@ -456,6 +457,12 @@
             </dependency>
 
             <dependency>
+                <groupId>org.apache.xbean</groupId>
+                <artifactId>xbean-finder</artifactId>
+                <version>3.7</version>
+            </dependency>
+
+            <dependency>
                 <groupId>net.sf.scannotation</groupId>
                 <artifactId>scannotation</artifactId>
                 <version>${scannatation.version}</version>
@@ -481,6 +488,14 @@
             </dependency>
 
             <dependency>
+                <groupId>org.osgi</groupId>
+                <artifactId>org.osgi.core</artifactId>
+                <version>4.2.0</version>
+                <scope>provided</scope>
+            </dependency>
+            
+
+            <dependency>
                 <groupId>org.apache.geronimo.specs</groupId>
                 <artifactId>geronimo-jpa_2.0_spec</artifactId>
                 <version>${geronimo_jpa.version}</version>

Modified: openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/exception/WebBeansDeploymentException.java
URL: http://svn.apache.org/viewvc/openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/exception/WebBeansDeploymentException.java?rev=957523&r1=957522&r2=957523&view=diff
==============================================================================
--- openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/exception/WebBeansDeploymentException.java (original)
+++ openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/exception/WebBeansDeploymentException.java Thu Jun 24 12:23:26 2010
@@ -40,4 +40,9 @@ public class WebBeansDeploymentException
     {
         super(message);
     }
+
+    public WebBeansDeploymentException(String message, Throwable e)
+    {
+        super(message, e);
+    }
 }

Propchange: openwebbeans/trunk/webbeans-osgi/
------------------------------------------------------------------------------
--- svn:ignore (added)
+++ svn:ignore Thu Jun 24 12:23:26 2010
@@ -0,0 +1,10 @@
+target
+.metadata
+.classpath
+.project
+.settings
+*.iml
+*.ipr
+*.iws
+.git
+.gitignore

Added: openwebbeans/trunk/webbeans-osgi/pom.xml
URL: http://svn.apache.org/viewvc/openwebbeans/trunk/webbeans-osgi/pom.xml?rev=957523&view=auto
==============================================================================
--- openwebbeans/trunk/webbeans-osgi/pom.xml (added)
+++ openwebbeans/trunk/webbeans-osgi/pom.xml Thu Jun 24 12:23:26 2010
@@ -0,0 +1,76 @@
+<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
+<!--
+    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.
+-->
+    <modelVersion>4.0.0</modelVersion>
+    <parent>
+        <groupId>org.apache.openwebbeans</groupId>
+        <artifactId>openwebbeans</artifactId>
+        <version>1.0.0-SNAPSHOT</version>
+    </parent>
+
+    <artifactId>openwebbeans-osgi</artifactId>
+    <name>Apache OpenWebBeans :: OSGi ClassLoader support</name>
+    <description>Apache OpenWebBeans OSGi ClassLoader support</description>
+
+    <dependencies>
+        <dependency>
+            <groupId>org.apache.geronimo.specs</groupId>
+            <artifactId>geronimo-servlet_2.5_spec</artifactId>
+        </dependency>
+        
+        <dependency>
+            <groupId>org.apache.openwebbeans</groupId>
+            <artifactId>openwebbeans-spi</artifactId>
+            <version>${project.version}</version>
+            <scope>provided</scope>
+        </dependency>
+
+        <dependency>
+            <groupId>org.apache.openwebbeans</groupId>
+            <artifactId>openwebbeans-impl</artifactId>
+        </dependency>
+
+        <dependency>
+            <groupId>org.osgi</groupId>
+            <artifactId>org.osgi.core</artifactId>
+            <scope>provided</scope>
+        </dependency>
+        
+        <dependency>
+            <groupId>org.apache.geronimo.specs</groupId>
+            <artifactId>geronimo-jcdi_1.0_spec</artifactId>
+            <scope>provided</scope>
+        </dependency>
+
+        <dependency>
+            <groupId>org.apache.geronimo.specs</groupId>
+            <artifactId>geronimo-atinject_1.0_spec</artifactId>
+            <scope>provided</scope>
+        </dependency>
+
+        <dependency>
+            <groupId>org.apache.xbean</groupId>
+            <artifactId>xbean-finder</artifactId>
+            <version>3.7</version>
+            <scope>provided</scope>
+        </dependency>
+
+    </dependencies>
+    
+</project>

Added: openwebbeans/trunk/webbeans-osgi/src/main/java/org/apache/webbeans/osgi/scanner/OsgiMetaDataScannerService.java
URL: http://svn.apache.org/viewvc/openwebbeans/trunk/webbeans-osgi/src/main/java/org/apache/webbeans/osgi/scanner/OsgiMetaDataScannerService.java?rev=957523&view=auto
==============================================================================
--- openwebbeans/trunk/webbeans-osgi/src/main/java/org/apache/webbeans/osgi/scanner/OsgiMetaDataScannerService.java (added)
+++ openwebbeans/trunk/webbeans-osgi/src/main/java/org/apache/webbeans/osgi/scanner/OsgiMetaDataScannerService.java Thu Jun 24 12:23:26 2010
@@ -0,0 +1,199 @@
+/*
+ * 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.osgi.scanner;
+
+import org.apache.webbeans.exception.WebBeansDeploymentException;
+import org.apache.webbeans.logger.WebBeansLogger;
+import org.apache.webbeans.spi.ScannerService;
+
+import org.apache.xbean.finder.BundleAssignableClassFinder;
+import org.apache.xbean.osgi.bundle.util.BundleClassFinder;
+import org.apache.xbean.osgi.bundle.util.BundleResourceFinder;
+import org.apache.xbean.osgi.bundle.util.BundleUtils;
+import org.apache.xbean.osgi.bundle.util.ClassDiscoveryFilter;
+import org.apache.xbean.osgi.bundle.util.DiscoveryRange;
+import org.osgi.framework.Bundle;
+import org.osgi.framework.ServiceReference;
+import org.osgi.service.packageadmin.PackageAdmin;
+
+import javax.servlet.ServletContext;
+import java.io.InputStream;
+import java.net.URL;
+import java.util.HashSet;
+import java.util.Set;
+import java.util.zip.ZipEntry;
+
+/**
+ * In an OSGi environment, resources will not be delivered in
+ * jars or file URLs, but as 'bundle://'.
+ * This {@link org.apache.webbeans.spi.ScannerService} parses for all classes
+ * in such a bundle.
+ */
+public class OsgiMetaDataScannerService implements ScannerService
+{
+    private WebBeansLogger logger = WebBeansLogger.getLogger(OsgiMetaDataScannerService.class);
+
+    private boolean configured = false;
+
+    protected ServletContext servletContext = null;
+    private static final String META_INF_BEANS_XML = "META-INF/beans.xml";
+    private static final String WEB_INF_BEANS_XML = "WEB-INF/beans.xml";
+
+    /** All classes which have to be scanned for Bean information */
+    private Set<Class<?>> beanClasses = new HashSet<Class<?>>();
+
+    /** the URLs of all META-INF/beans.xml files */
+    private Set<URL> beanXMLs = new HashSet<URL>();
+
+    /**contains all the JARs we found with valid beans.xml in it */
+    private Set<String> beanArchiveJarNames = new HashSet<String>();
+
+    @Override
+    public void init(Object object)
+    {
+        if (object instanceof ServletContext)
+        {
+            servletContext = (ServletContext) object;
+        }
+    }
+
+    @Override
+    public void scan() throws WebBeansDeploymentException
+    {
+        logger.info("Using OsgiMetaDataScannerService!");
+        Bundle mainBundle = BundleUtils.getContextBundle(true);
+
+
+        ServiceReference reference = mainBundle.getBundleContext().getServiceReference(PackageAdmin.class.getName());
+        try
+        {
+            PackageAdmin packageAdmin = (PackageAdmin) mainBundle.getBundleContext().getService(reference);
+
+            // search for all META-INF/beans.xml files
+            findBeansXml(mainBundle, packageAdmin);
+
+            // search for all classes
+            findBeanClasses(mainBundle, packageAdmin);
+        }
+        catch(Exception e)
+        {
+            throw new WebBeansDeploymentException("problem while scanning OSGi bundle", e);
+        }
+        finally
+        {
+            mainBundle.getBundleContext().ungetService(reference);
+        }
+
+    }
+
+    private void findBeanClasses(Bundle mainBundle, PackageAdmin packageAdmin)
+    {
+        BundleClassFinder bundleClassFinder =
+                new BundleAssignableClassFinder(packageAdmin, mainBundle,
+                                                new Class<?>[]{Object.class},
+                                                new ClassDiscoveryFilter()
+         {
+
+            @Override
+            public boolean directoryDiscoveryRequired(String directory)
+            {
+                return true;
+            }
+
+            @Override
+            public boolean jarFileDiscoveryRequired(String jarUrl)
+            {
+                boolean isValidBeanArchive = beanArchiveJarNames.contains(jarUrl);
+                return isValidBeanArchive;
+            }
+
+            @Override
+            public boolean packageDiscoveryRequired(String packageName)
+            {
+                return true;
+            }
+
+            @Override
+            public boolean rangeDiscoveryRequired(DiscoveryRange discoveryRange)
+            {
+                return discoveryRange.equals(DiscoveryRange.BUNDLE_CLASSPATH);
+            }
+        });
+
+        Set<String> acceptedClassNames = bundleClassFinder.find();
+        for (String clsName : acceptedClassNames)
+        {
+            try
+            {
+                Class<?> cls = mainBundle.loadClass(clsName);
+                beanClasses.add(cls);
+            }
+            catch(Exception e)
+            {
+                logger.info("cannot load class from bundle: " + clsName);
+            }
+        }
+    }
+
+    private void findBeansXml(Bundle mainBundle, PackageAdmin packageAdmin)
+            throws Exception
+    {
+        BundleResourceFinder brfXmlJar =  new BundleResourceFinder(packageAdmin, mainBundle, "", META_INF_BEANS_XML);
+
+        BundleResourceFinder.ResourceFinderCallback rfCallback = new BundleResourceFinder.ResourceFinderCallback()
+        {
+
+            public void foundInDirectory(Bundle bundle, String basePath, URL url) throws Exception
+            {
+                logger.info("adding the following beans.xml URL: " + url);
+                beanXMLs.add(url);
+            }
+
+            public void 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);
+
+                beanXMLs.add(beansUrl);
+                beanArchiveJarNames.add(jarName);
+            }
+
+        };
+
+        brfXmlJar.find(rfCallback);
+
+        BundleResourceFinder brfXmlWar =  new BundleResourceFinder(packageAdmin, mainBundle, "", WEB_INF_BEANS_XML);
+
+        brfXmlWar.find(rfCallback);
+    }
+
+    @Override
+    public Set<URL> getBeanXmls()
+    {
+        return beanXMLs;
+    }
+
+    @Override
+    public Set<Class<?>> getBeanClasses()
+    {
+        return beanClasses;
+    }
+}