You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tomee.apache.org by rm...@apache.org on 2011/12/07 19:06:11 UTC

svn commit: r1211576 - in /openejb/trunk/openejb/container: openejb-core/src/main/java/org/apache/openejb/config/ openejb-osgi/src/main/java/org/apache/openejb/core/osgi/impl/

Author: rmannibucau
Date: Wed Dec  7 18:06:10 2011
New Revision: 1211576

URL: http://svn.apache.org/viewvc?rev=1211576&view=rev
Log:
trying to use a prebuilt classloader to deploy a bundle

Modified:
    openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/config/DeploymentLoader.java
    openejb/trunk/openejb/container/openejb-osgi/src/main/java/org/apache/openejb/core/osgi/impl/Activator.java
    openejb/trunk/openejb/container/openejb-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/DeploymentLoader.java
URL: http://svn.apache.org/viewvc/openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/config/DeploymentLoader.java?rev=1211576&r1=1211575&r2=1211576&view=diff
==============================================================================
--- openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/config/DeploymentLoader.java (original)
+++ openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/config/DeploymentLoader.java Wed Dec  7 18:06:10 2011
@@ -50,7 +50,6 @@ import org.apache.xbean.finder.IAnnotati
 import org.apache.xbean.finder.ResourceFinder;
 import org.apache.xbean.finder.UrlSet;
 import org.apache.xbean.finder.archive.Archive;
-import org.apache.xbean.finder.archive.CompositeArchive;
 import org.apache.xbean.finder.archive.JarArchive;
 import org.xml.sax.SAXException;
 
@@ -93,6 +92,35 @@ public class DeploymentLoader implements
     private static final String ddDir = "META-INF/";
     private boolean scanManagedBeans = true;
 
+    public AppModule load(ClassLoader classLoader) throws OpenEJBException {
+        final Class<? extends DeploymentModule> moduleClass;
+        ClassLoader tempClassLoader = null;
+        try {
+            try {
+                moduleClass = discoverModuleType(null, ClassLoaderUtil.createTempClassLoader(classLoader), true);
+            } catch (IOException e) {
+                throw new UnknownModuleTypeException("Unable to determine module type for bundle loaded from a built classloader", e);
+            }
+
+            if (EjbModule.class.equals(moduleClass)) {
+                tempClassLoader = ClassLoaderUtil.createTempClassLoader(classLoader);
+                Class<? extends DeploymentModule> o = EjbModule.class;
+                EjbModule ejbModule = createEjbModule(null, null, tempClassLoader, null);
+                AppModule appModule = new AppModule(ejbModule);
+                addPersistenceUnits(appModule);
+                return appModule;
+            }
+        } finally {
+            if (null != tempClassLoader) {
+                ClassLoaderUtil.destroyClassLoader(tempClassLoader);
+                tempClassLoader = null;
+                System.gc();
+            }
+        }
+
+        throw new UnsupportedModuleTypeException("Unsupported module type (deployement from classloader): " + moduleClass.getSimpleName());
+    }
+
     public AppModule load(File jarFile) throws OpenEJBException {
         // verify we have a valid file
         String jarPath;
@@ -583,7 +611,9 @@ public class DeploymentLoader implements
         ClientModule clientModule = new ClientModule(applicationClient, appClassLoader, absolutePath, mainClass, moduleName);
 
         clientModule.getAltDDs().putAll(descriptors);
-        clientModule.getWatchedResources().add(absolutePath);
+        if (absolutePath != null) {
+            clientModule.getWatchedResources().add(absolutePath);
+        }
         if (clientXmlUrl != null && "file".equals(clientXmlUrl.getProtocol())) {
             clientModule.getWatchedResources().add(URLs.toFilePath(clientXmlUrl));
         }
@@ -592,7 +622,16 @@ public class DeploymentLoader implements
 
     protected EjbModule createEjbModule(URL baseUrl, String jarPath, ClassLoader classLoader, String moduleId) throws OpenEJBException {
         // read the ejb-jar.xml file
-        Map<String, URL> descriptors = getDescriptors(baseUrl);
+        Map<String, URL> descriptors;
+        if (baseUrl != null) {
+            descriptors = getDescriptors(baseUrl);
+        } else {
+            try {
+                descriptors = getDescriptors(classLoader, null);
+            } catch (IOException e) {
+                descriptors = new HashMap<String, URL>();
+            }
+        }
 
         EjbJar ejbJar = null;
         URL ejbJarXmlUrl = descriptors.get("ejb-jar.xml");
@@ -603,7 +642,9 @@ public class DeploymentLoader implements
         // create the EJB Module
         EjbModule ejbModule = new EjbModule(classLoader, moduleId, jarPath, ejbJar, null);
         ejbModule.getAltDDs().putAll(descriptors);
-        ejbModule.getWatchedResources().add(jarPath);
+        if (jarPath != null) {
+            ejbModule.getWatchedResources().add(jarPath);
+        }
         if (ejbJarXmlUrl != null && "file".equals(ejbJarXmlUrl.getProtocol())) {
             ejbModule.getWatchedResources().add(URLs.toFilePath(ejbJarXmlUrl));
         }
@@ -1431,20 +1472,27 @@ public class DeploymentLoader implements
         final boolean scanPotentialClientModules = !requireDescriptor.contains(RequireDescriptors.CLIENT);
 
 
-        URL pathToScanDescriptors=baseUrl;
-        String baseURLString=baseUrl.toString();
-
-        if (baseUrl.getProtocol().equals("file") && baseURLString.endsWith("WEB-INF/classes/")) {
-            //EJB found in WAR/WEB-INF/classes, scan WAR for ejb-jar.xml
-
-            pathToScanDescriptors=new URL(baseURLString.substring(0,baseURLString.lastIndexOf("WEB-INF/classes/")));
-
+        URL pathToScanDescriptors = baseUrl;
+        if (baseUrl != null) {
+            final String baseURLString=baseUrl.toString();
+            if (baseUrl.getProtocol().equals("file") && baseURLString.endsWith("WEB-INF/classes/")) {
+                //EJB found in WAR/WEB-INF/classes, scan WAR for ejb-jar.xml
+                pathToScanDescriptors = new URL(baseURLString.substring(0,baseURLString.lastIndexOf("WEB-INF/classes/")));
+            }
         }
 
         Map<String, URL> descriptors = getDescriptors(classLoader, pathToScanDescriptors);
 
-        String path = baseUrl.getPath();
-        if (path.endsWith("/")) path = path.substring(0, path.length() - 1);
+        String path;
+        if (baseUrl != null) {
+            path = baseUrl.getPath();
+        } else {
+            path = "";
+        }
+
+        if (path.endsWith("/")) {
+            path = path.substring(0, path.length() - 1);
+        }
 
         if (descriptors.containsKey("application.xml") || path.endsWith(".ear")) {
             return AppModule.class;
@@ -1458,10 +1506,12 @@ public class DeploymentLoader implements
             return ConnectorModule.class;
         }
 
-        Map<String, URL> webDescriptors = getWebDescriptors(getFile(baseUrl));
-        if (webDescriptors.containsKey("web.xml") || webDescriptors.containsKey("web-fragment.xml") // descriptor
-            || path.endsWith(".war") || new File(path, "WEB-INF").exists()) { // webapp specific files
-            return WebModule.class;
+        if (baseUrl != null) {
+            Map<String, URL> webDescriptors = getWebDescriptors(getFile(baseUrl));
+            if (webDescriptors.containsKey("web.xml") || webDescriptors.containsKey("web-fragment.xml") // descriptor
+                || path.endsWith(".war") || new File(path, "WEB-INF").exists()) { // webapp specific files
+                return WebModule.class;
+            }
         }
 
         if (descriptors.containsKey("ejb-jar.xml") || descriptors.containsKey("beans.xml")) {
@@ -1486,7 +1536,7 @@ public class DeploymentLoader implements
             return PersistenceModule.class;
         }
 
-        throw new UnknownModuleTypeException("Unknown module type: url=" + baseUrl.toExternalForm());
+        throw new UnknownModuleTypeException("Unknown module type: url=" + path); // baseUrl can be null
     }
 
     private Map<String, URL> getDescriptors(ClassLoader classLoader, URL pathToScanDescriptors)

Modified: openejb/trunk/openejb/container/openejb-osgi/src/main/java/org/apache/openejb/core/osgi/impl/Activator.java
URL: http://svn.apache.org/viewvc/openejb/trunk/openejb/container/openejb-osgi/src/main/java/org/apache/openejb/core/osgi/impl/Activator.java?rev=1211576&r1=1211575&r2=1211576&view=diff
==============================================================================
--- openejb/trunk/openejb/container/openejb-osgi/src/main/java/org/apache/openejb/core/osgi/impl/Activator.java (original)
+++ openejb/trunk/openejb/container/openejb-osgi/src/main/java/org/apache/openejb/core/osgi/impl/Activator.java Wed Dec  7 18:06:10 2011
@@ -44,9 +44,9 @@ public class Activator implements Bundle
     private Object serviceManager;
 
     public void start(BundleContext context) throws Exception {
-        LOGGER.info("Starting OpenEJB");
+        LOGGER.info("Starting OpenEJB for bundle #{}", context.getBundle().getBundleId());
 
-        Thread.currentThread().setContextClassLoader(this.getClass().getClassLoader());
+        Thread.currentThread().setContextClassLoader(getClass().getClassLoader());
 
         openejb = new OpenEJBInstance();
 
@@ -54,14 +54,11 @@ public class Activator implements Bundle
         // env.setProperty("openejb.embedded", "true");
         // default, but to remember that the setting exists
         env.setProperty("openejb.loader", "context");
-        // NPE
         env.setProperty("openejb.deployments.classpath", "false");
-        env.setProperty("openejb.log.factory", "org.apache.openejb.util.JuliLogStreamFactory");
 
         SystemInstance.init(env);
         // OptionsLog.install();
 
-        // OpenEJB.init(env);
         try {
             openejb.init(env);
         } catch (Exception e) {

Modified: openejb/trunk/openejb/container/openejb-osgi/src/main/java/org/apache/openejb/core/osgi/impl/Deployer.java
URL: http://svn.apache.org/viewvc/openejb/trunk/openejb/container/openejb-osgi/src/main/java/org/apache/openejb/core/osgi/impl/Deployer.java?rev=1211576&r1=1211575&r2=1211576&view=diff
==============================================================================
--- openejb/trunk/openejb/container/openejb-osgi/src/main/java/org/apache/openejb/core/osgi/impl/Deployer.java (original)
+++ openejb/trunk/openejb/container/openejb-osgi/src/main/java/org/apache/openejb/core/osgi/impl/Deployer.java Wed Dec  7 18:06:10 2011
@@ -25,6 +25,7 @@ import org.apache.openejb.assembler.clas
 import org.apache.openejb.config.AppModule;
 import org.apache.openejb.config.ConfigurationFactory;
 import org.apache.openejb.config.DeploymentLoader;
+import org.apache.openejb.config.UnknownModuleTypeException;
 import org.apache.openejb.loader.SystemInstance;
 import org.osgi.framework.Bundle;
 import org.osgi.framework.BundleContext;
@@ -34,14 +35,12 @@ import org.osgi.framework.ServiceRegistr
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
-import java.io.File;
 import java.io.IOException;
 import java.lang.reflect.InvocationHandler;
 import java.lang.reflect.Method;
 import java.lang.reflect.Proxy;
 import java.net.URL;
 import java.util.ArrayList;
-import java.util.Arrays;
 import java.util.Enumeration;
 import java.util.List;
 import java.util.Map;
@@ -70,48 +69,34 @@ public class Deployer implements BundleL
 
     private void deploy(Bundle bundle) {
         final ClassLoader oldCl = Thread.currentThread().getContextClassLoader();
-        Thread.currentThread().setContextClassLoader(new OSGIClassLoader(bundle));
+        final ClassLoader osgiCl = new OSGIClassLoader(bundle);
+        Thread.currentThread().setContextClassLoader(osgiCl);
 
-        LOGGER.info(String.format("[Deployer] Bundle %s has been started", bundle.getSymbolicName()));
-
-        LOGGER.info(String.format("[Deployer] Checking whether it's an EJB module"));
         try {
-            Enumeration<URL> e = bundle.findEntries("META-INF", "ejb-jar.xml", false);
-            if (e != null && e.hasMoreElements()) {
-                URL ejbJarUrl = e.nextElement();
-
-                LOGGER.info("[Deployer] It's an EJB module: " + ejbJarUrl);
-
-                LOGGER.info("[Deployer] Deploying onto OpenEJB");
-
-                String location = bundle.getLocation();
-                LOGGER.info("[Deployer] bundle location: " + location);
+            try {
                 try {
-                    File file = new File(new URL(location).getFile());
-                    try {
-                        DeploymentLoader deploymentLoader = new DeploymentLoader();
-                        AppModule appModule = deploymentLoader.load(file);
-
-                        ConfigurationFactory configurationFactory = new ConfigurationFactory();
-                        AppInfo appInfo = configurationFactory.configureApplication(appModule);
-
-                        Assembler assembler = SystemInstance.get().getComponent(Assembler.class);
-                        LOGGER.debug("Assembler : " + assembler);
-                        LOGGER.debug("AppInfo id: " + appInfo.appId);
-                        AppContext appContext = assembler.createApplication(appInfo);
-                        appContexts.put(bundle, appContext);
-
-                        LOGGER.info("[Deployer] Application deployed: " + appInfo.path);
-
-                        registrations.put(bundle, new ArrayList<ServiceRegistration>());
-                        registerService(bundle, appContext);
-
-                    } catch (Exception ex) {
-                        LOGGER.error("can't deploy " + ejbJarUrl.toExternalForm(), ex);
-                    }
-                } catch (Exception ex1) {
-                    LOGGER.error("can't deploy " + ejbJarUrl.toExternalForm(), ex1);
+                    DeploymentLoader deploymentLoader = new DeploymentLoader();
+                    AppModule appModule = deploymentLoader.load(osgiCl); // here file doesn't mean anything
+                    LOGGER.info("deploying bundle #" + bundle.getBundleId() + " as an EJBModule");
+
+                    ConfigurationFactory configurationFactory = new ConfigurationFactory();
+                    AppInfo appInfo = configurationFactory.configureApplication(appModule);
+
+                    Assembler assembler = SystemInstance.get().getComponent(Assembler.class);
+                    AppContext appContext = assembler.createApplication(appInfo);
+                    appContexts.put(bundle, appContext);
+                    LOGGER.info("Application deployed: " + appInfo.path);
+
+                    registrations.put(bundle, new ArrayList<ServiceRegistration>());
+                    registerService(bundle, appContext);
+
+                } catch (UnknownModuleTypeException unknowException) {
+                    LOGGER.info("bundle #" + bundle.getBundleId() + " is not an EJBModule");
+                } catch(Exception ex) {
+                    LOGGER.error("can't deploy bundle #" + bundle.getBundleId(), ex);
                 }
+            } catch (Exception ex1) {
+                LOGGER.error("can't deploy bundle #" + bundle.getBundleId(), ex1);
             }
         } finally {
             Thread.currentThread().setContextClassLoader(oldCl);
@@ -139,22 +124,25 @@ public class Deployer implements BundleL
     /**
      * Register OSGi Service for EJB so calling the service will actually call the EJB
      *
-     * @param bundle
-     * @param appContext
+     * @param bundle the deployed bundle
+     * @param appContext the appcontext to search EJBs
      */
     private void registerService(Bundle bundle, AppContext appContext) {
-        LOGGER.info("[Deployer] Registering a service for the EJB");
+        LOGGER.info("Registering remote EJBs as OSGi services");
         BundleContext context = bundle.getBundleContext();
         for (BeanContext beanContext : appContext.getBeanContexts()) {
             try {
-                if (beanContext.getBusinessLocalInterface() != null) {
-                    registerService(beanContext, context, beanContext.getBusinessLocalInterfaces());
-                }
                 if (beanContext.getBusinessRemoteInterface() != null) {
+                    LOGGER.error(String.format("registering: %s", beanContext.getEjbName()));
                     registerService(beanContext, context, beanContext.getBusinessRemoteInterfaces());
                 }
+                if (beanContext.getBusinessLocalInterface() != null) {
+                    // don't register it since it should only be used in the bundle itself
+                    // registerService(beanContext, context, beanContext.getBusinessLocalInterfaces());
+                }
                 if (beanContext.isLocalbean()) {
-                    registerService(beanContext, context, Arrays.asList(beanContext.getBusinessLocalBeanInterface()));
+                    // don't register it since it should only be used in the bundle itself
+                    // registerService(beanContext, context, Arrays.asList(beanContext.getBusinessLocalBeanInterface()));
                 }
             } catch (Exception e) {
                 LOGGER.error(String.format("[Deployer] can't register: %s", beanContext.getEjbName()));
@@ -168,9 +156,9 @@ public class Deployer implements BundleL
             try {
                 Object service = Proxy.newProxyInstance(itfs[0].getClassLoader(), itfs, new Handler(beanContext));
                 registrations.get(context.getBundle()).add(context.registerService(str(itfs), service, new Properties()));
-                LOGGER.info(String.format("[Deployer] EJB registered: %s for interfaces %s", beanContext.getEjbName(), interfaces));
+                LOGGER.info(String.format("EJB registered: %s for interfaces %s", beanContext.getEjbName(), interfaces));
             } catch (IllegalArgumentException iae) {
-                LOGGER.error(String.format("[Deployer] can't register: %s for interfaces %s", beanContext.getEjbName(), interfaces));
+                LOGGER.error(String.format("can't register: %s for interfaces %s", beanContext.getEjbName(), interfaces));
             }
         }
     }