Author: rmannibucau
Date: Fri Oct 12 14:24:34 2012
New Revision: 1397578

URL: http://svn.apache.org/viewvc?rev=1397578&view=rev
Log:
OPENEJB-1922 osgi dep + cdi management - first test

Modified:
    
openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/config/FinderFactory.java
    openejb/trunk/openejb/osgi/openejb-core-osgi/pom.xml
    
openejb/trunk/openejb/osgi/openejb-core-osgi/src/main/java/org/apache/openejb/core/osgi/impl/Activator.java
    
openejb/trunk/openejb/osgi/openejb-core-osgi/src/main/java/org/apache/openejb/core/osgi/impl/Deployer.java

Modified: 
openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/config/FinderFactory.java
URL: 
http://svn.apache.org/viewvc/openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/config/FinderFactory.java?rev=1397578&r1=1397577&r2=1397578&view=diff
==============================================================================
--- 
openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/config/FinderFactory.java
 (original)
+++ 
openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/config/FinderFactory.java
 Fri Oct 12 14:24:34 2012
@@ -34,6 +34,7 @@ import java.lang.reflect.Field;
 import java.lang.reflect.Method;
 import java.net.URL;
 import java.util.ArrayList;
+import java.util.Collection;
 import java.util.Iterator;
 import java.util.List;
 
@@ -97,7 +98,7 @@ public class FinderFactory {
         return new ModuleLimitedFinder(finder);
     }
 
-    private static class DebugArchive implements Archive {
+    public static class DebugArchive implements Archive {
         private final Archive archive;
 
         private DebugArchive(Archive archive) {
@@ -124,7 +125,8 @@ public class FinderFactory {
             }
         }
     }
-    private AnnotationFinder enableFinderOptions(AnnotationFinder 
annotationFinder) {
+
+    public static AnnotationFinder enableFinderOptions(AnnotationFinder 
annotationFinder) {
         if (annotationFinder.hasMetaAnnotations()) {
             annotationFinder.enableMetaAnnotations();
         }
@@ -135,7 +137,7 @@ public class FinderFactory {
         return annotationFinder;
     }
 
-    private static boolean enableFindSubclasses() {
+    public static boolean enableFindSubclasses() {
         return isJaxRsInstalled() && 
SystemInstance.get().getOptions().get(TOMEE_JAXRS_DEPLOY_UNDECLARED_PROP, 
false);
     }
 

Modified: openejb/trunk/openejb/osgi/openejb-core-osgi/pom.xml
URL: 
http://svn.apache.org/viewvc/openejb/trunk/openejb/osgi/openejb-core-osgi/pom.xml?rev=1397578&r1=1397577&r2=1397578&view=diff
==============================================================================
--- openejb/trunk/openejb/osgi/openejb-core-osgi/pom.xml (original)
+++ openejb/trunk/openejb/osgi/openejb-core-osgi/pom.xml Fri Oct 12 14:24:34 
2012
@@ -54,6 +54,12 @@
       <artifactId>geronimo-transaction</artifactId>
       <optional>true</optional>
     </dependency>
+    <dependency>
+      <groupId>org.apache.xbean</groupId>
+      <artifactId>xbean-bundleutils</artifactId>
+      <version>${xbeanVersion}</version>
+      <optional>true</optional>
+    </dependency>
   </dependencies>
   <build>
     <plugins>

Modified: 
openejb/trunk/openejb/osgi/openejb-core-osgi/src/main/java/org/apache/openejb/core/osgi/impl/Activator.java
URL: 
http://svn.apache.org/viewvc/openejb/trunk/openejb/osgi/openejb-core-osgi/src/main/java/org/apache/openejb/core/osgi/impl/Activator.java?rev=1397578&r1=1397577&r2=1397578&view=diff
==============================================================================
--- 
openejb/trunk/openejb/osgi/openejb-core-osgi/src/main/java/org/apache/openejb/core/osgi/impl/Activator.java
 (original)
+++ 
openejb/trunk/openejb/osgi/openejb-core-osgi/src/main/java/org/apache/openejb/core/osgi/impl/Activator.java
 Fri Oct 12 14:24:34 2012
@@ -19,6 +19,7 @@ package org.apache.openejb.core.osgi.imp
 import org.apache.openejb.OpenEJB;
 import org.apache.openejb.OpenEJBException;
 import org.apache.openejb.cdi.CdiScanner;
+import org.apache.openejb.cdi.OptimizedLoaderService;
 import org.apache.openejb.loader.OpenEJBInstance;
 import org.apache.openejb.loader.SystemInstance;
 import org.osgi.framework.BundleActivator;

Modified: 
openejb/trunk/openejb/osgi/openejb-core-osgi/src/main/java/org/apache/openejb/core/osgi/impl/Deployer.java
URL: 
http://svn.apache.org/viewvc/openejb/trunk/openejb/osgi/openejb-core-osgi/src/main/java/org/apache/openejb/core/osgi/impl/Deployer.java?rev=1397578&r1=1397577&r2=1397578&view=diff
==============================================================================
--- 
openejb/trunk/openejb/osgi/openejb-core-osgi/src/main/java/org/apache/openejb/core/osgi/impl/Deployer.java
 (original)
+++ 
openejb/trunk/openejb/osgi/openejb-core-osgi/src/main/java/org/apache/openejb/core/osgi/impl/Deployer.java
 Fri Oct 12 14:24:34 2012
@@ -24,13 +24,20 @@ import org.apache.openejb.UndeployExcept
 import org.apache.openejb.assembler.classic.AppInfo;
 import org.apache.openejb.assembler.classic.Assembler;
 import org.apache.openejb.config.AppModule;
+import org.apache.openejb.config.ConfigurableClasspathArchive;
 import org.apache.openejb.config.ConfigurationFactory;
 import org.apache.openejb.config.DeploymentLoader;
+import org.apache.openejb.config.EjbModule;
+import org.apache.openejb.config.FinderFactory;
+import org.apache.openejb.config.NewLoaderLogic;
 import org.apache.openejb.config.UnknownModuleTypeException;
 import org.apache.openejb.core.ivm.IntraVmProxy;
 import org.apache.openejb.loader.SystemInstance;
 import org.apache.openejb.util.ArrayEnumeration;
 import org.apache.openejb.util.proxy.ProxyEJB;
+import org.apache.xbean.finder.AnnotationFinder;
+import org.apache.xbean.finder.filter.Filter;
+import org.apache.xbean.osgi.bundle.util.BundleUtils;
 import org.osgi.framework.Bundle;
 import org.osgi.framework.BundleContext;
 import org.osgi.framework.BundleEvent;
@@ -41,6 +48,7 @@ import org.slf4j.LoggerFactory;
 
 import java.io.File;
 import java.io.IOException;
+import java.net.MalformedURLException;
 import java.net.URL;
 import java.util.ArrayList;
 import java.util.Arrays;
@@ -62,6 +70,7 @@ public class Deployer implements BundleL
 
     private final Map<Bundle, List<ServiceRegistration>> registrations = new 
ConcurrentHashMap<Bundle, List<ServiceRegistration>>();
     private final Map<Bundle, String> paths = new ConcurrentHashMap<Bundle, 
String>();
+    private final Map<Bundle, Set<URL>> libByBundle = new 
ConcurrentHashMap<Bundle, Set<URL>>();
 
     private final Activator openejbActivator;
 
@@ -111,18 +120,37 @@ public class Deployer implements BundleL
 
         Thread.currentThread().setContextClassLoader(osgiCl);
 
+        final Set<Bundle> wiredBundles = BundleUtils.getWiredBundles(bundle);
+        final Set<URL> libs = new HashSet<URL>();
+        final Filter filter = new 
OSGiPrefixFilter(NewLoaderLogic.getExclusions());
+        for (Bundle b : wiredBundles) {
+            final String location = b.getLocation();
+            if (location == null || !filter.accept(location)) {
+                continue;
+            }
+
+            final File file = findBundleFile(b);
+            if (file == null) {
+                continue;
+            }
+
+            try {
+                libs.add(file.toURI().toURL());
+            } catch (MalformedURLException e) {
+                // no-op
+            }
+
+            final Set<URL> others = libByBundle.get(b);
+            if (others != null) {
+                libs.addAll(others);
+            }
+        }
+
         try {
             try {
                 try {
-                    // equinox? found in aries
-                    File bundleDump = 
bundle.getBundleContext().getDataFile(bundle.getSymbolicName() + "/" + 
bundle.getVersion() + "/");
-                    // TODO: what should happen if there is multiple versions?
-                    if (!bundleDump.exists() && 
bundle.getBundleContext().getDataFile("") != null) { // felix. TODO: maybe find 
something better
-                        bundleDump = findFelixJar(bundle.getBundleContext());
-                    }
-                    if (bundleDump == null || !bundleDump.exists()) {
-                        bundleDump = findEquinoxJar(bundle.getBundleContext());
-                    }
+                    File bundleDump = findBundleFile(bundle);
+                    libs.remove(bundleDump.toURI().toURL()); // remove this 
bundle from libs
 
                     if (bundleDump == null || !bundleDump.exists()) {
                         LOGGER.warn("can't find bundle {}", 
bundle.getBundleId());
@@ -131,10 +159,21 @@ public class Deployer implements BundleL
 
                     LOGGER.info("looking bundle {} in {}", 
bundle.getBundleId(), bundleDump);
                     final AppModule appModule = new 
OSGiDeploymentLoader(bundle).load(bundleDump);
+                    if (libs.size() > 0) {
+                        for (EjbModule ejbModule : appModule.getEjbModules()) {
+                            if (ejbModule.getJarLocation() != null) {
+                                final Set<URL> cp = new HashSet<URL>(libs);
+                                cp.add(new 
File(ejbModule.getJarLocation()).toURI().toURL());
+
+                                final AnnotationFinder annotationFinder = new 
AnnotationFinder(new ConfigurableClasspathArchive(ejbModule, cp));
+                                
FinderFactory.enableFinderOptions(annotationFinder);
+                                ejbModule.setFinder(annotationFinder.link());
+                            }
+                        }
+                        libByBundle.put(bundle, libs);
+                    }
                     LOGGER.info("deploying bundle #" + bundle.getBundleId() + 
" as an EJBModule");
 
-                    RegisterOSGIServicesExtension.current = null;
-
                     final ConfigurationFactory configurationFactory = new 
ConfigurationFactory();
                     final AppInfo appInfo = 
configurationFactory.configureApplication(appModule);
                     appInfo.appId = "bundle_" + bundle.getBundleId();
@@ -163,6 +202,18 @@ public class Deployer implements BundleL
         }
     }
 
+    private static File findBundleFile(final Bundle bundle) {
+        // equinox? found in aries
+        File bundleDump = 
bundle.getBundleContext().getDataFile(bundle.getSymbolicName() + "/" + 
bundle.getVersion() + "/");
+        if (!bundleDump.exists() && bundle.getBundleContext().getDataFile("") 
!= null) {
+            bundleDump = findFelixJar(bundle.getBundleContext());
+        }
+        if (bundleDump == null || !bundleDump.exists()) {
+            bundleDump = findEquinoxJar(bundle.getBundleContext());
+        }
+        return bundleDump;
+    }
+
     private static File findEquinoxJar(final BundleContext bundleContext) {
         final File root = bundleContext.getDataFile("").getParentFile();
         int idx = 0;
@@ -221,6 +272,7 @@ public class Deployer implements BundleL
     }
 
     private void undeploy(final Bundle bundle) {
+        libByBundle.remove(bundle);
         if (registrations.containsKey(bundle)) {
             for (final ServiceRegistration registration : 
registrations.get(bundle)) {
                 try {
@@ -434,5 +486,34 @@ public class Deployer implements BundleL
             return new OSGIClassLoader(bundle, 
OpenEJBBundleContextHolder.get().getBundle());
         }
     }
+
+    private static class OSGiPrefixFilter implements Filter {
+        private final String[] exclusions;
+
+        public OSGiPrefixFilter(final String[] exclusions) {
+            this.exclusions = exclusions;
+            for (int i = 0; i < exclusions.length; i++) {
+                if (exclusions[i].endsWith("-")) {
+                    this.exclusions[i] = exclusions[i].substring(0, 
exclusions[i].length() - 1);
+                }
+            }
+        }
+
+        @Override
+        public boolean accept(final String s) {
+            for (String e : exclusions) {
+                if (s.contains(e)) {
+                    return false;
+                }
+            }
+            return !s.contains("org.apache.geronimo.specs")
+                    && !s.contains("org.apache.felix.framework")
+                    && !s.equals("System Bundle")
+                    && !s.startsWith("mvn:javax.")
+                    && !s.contains("org.apache.bval")
+                    && !s.contains("org.apache.aries")
+                    && !s.contains("org.apache.karaf");
+        }
+    }
 }
 


Reply via email to