Search in sources :

Example 1 with ClassLoaderConfigurer

use of org.apache.openejb.classloader.ClassLoaderConfigurer in project tomee by apache.

the class ClassLoaderUtil method createConfigurer.

private static ClassLoaderConfigurer createConfigurer(final String key, final String impl) {
    try {
        final ObjectRecipe recipe = new ObjectRecipe(impl);
        for (final Map.Entry<Object, Object> entry : SystemInstance.get().getProperties().entrySet()) {
            final String entryKey = entry.getKey().toString();
            if (entryKey.startsWith(key)) {
                final String newKey = entryKey.substring(key.length());
                if (!"clazz".equals(newKey)) {
                    recipe.setProperty(newKey, entry.getValue());
                }
            }
        }
        final Object instance = recipe.create();
        if (instance instanceof ClassLoaderConfigurer) {
            return (ClassLoaderConfigurer) instance;
        } else {
            logger.error(impl + " is not a classlaoder configurer, using default behavior");
        }
    } catch (final Exception e) {
        logger.error("Can't create classloader configurer " + impl + ", using default behavior");
    }
    return null;
}
Also used : ObjectRecipe(org.apache.xbean.recipe.ObjectRecipe) ClassLoaderConfigurer(org.apache.openejb.classloader.ClassLoaderConfigurer) CompositeClassLoaderConfigurer(org.apache.openejb.classloader.CompositeClassLoaderConfigurer) HashMap(java.util.HashMap) Map(java.util.Map) IOException(java.io.IOException) ConcurrentModificationException(java.util.ConcurrentModificationException)

Example 2 with ClassLoaderConfigurer

use of org.apache.openejb.classloader.ClassLoaderConfigurer in project tomee by apache.

the class DeploymentLoader method createConnectorModule.

protected static ConnectorModule createConnectorModule(final String appId, final String rarPath, final ClassLoader parentClassLoader, final String moduleId, final URL raXmlUrl) throws OpenEJBException {
    // unpack the rar file
    final URL baseUrl;
    File rarFile = new File(rarPath);
    if (!rarFile.exists()) {
        LOGGER.warning(rarPath + " doesn't exist, skipping connector");
        return null;
    }
    rarFile = unpack(rarFile);
    baseUrl = getFileUrl(rarFile);
    // read the ra.xml file
    final Map<String, URL> descriptors = getDescriptors(baseUrl);
    Connector connector = null;
    URL rarXmlUrl = descriptors.get("ra.xml");
    if (rarXmlUrl == null && raXmlUrl != null) {
        descriptors.put("ra.xml", raXmlUrl);
        rarXmlUrl = raXmlUrl;
    }
    if (rarXmlUrl != null) {
        connector = ReadDescriptors.readConnector(rarXmlUrl);
    }
    // find the nested jar files
    final HashMap<String, URL> rarLibs = new HashMap<>();
    scanDir(rarFile, rarLibs, "");
    // remove all non jars from the rarLibs
    rarLibs.entrySet().removeIf(fileEntry -> !fileEntry.getKey().endsWith(".jar"));
    // create the class loader
    final List<URL> classPath = new ArrayList<>(rarLibs.values());
    final ClassLoaderConfigurer configurer = QuickJarsTxtParser.parse(new File(rarFile, "META-INF/" + QuickJarsTxtParser.FILE_NAME));
    if (configurer != null) {
        ClassLoaderConfigurer.Helper.configure(classPath, configurer);
    }
    final URL[] urls = classPath.toArray(new URL[classPath.size()]);
    final ClassLoader appClassLoader = ClassLoaderUtil.createTempClassLoader(appId, urls, parentClassLoader);
    // create the Resource Module
    final ConnectorModule connectorModule = new ConnectorModule(connector, appClassLoader, rarPath, moduleId);
    connectorModule.getAltDDs().putAll(descriptors);
    connectorModule.getLibraries().addAll(classPath);
    connectorModule.getWatchedResources().add(rarPath);
    connectorModule.getWatchedResources().add(rarFile.getAbsolutePath());
    if (rarXmlUrl != null && "file".equals(rarXmlUrl.getProtocol())) {
        connectorModule.getWatchedResources().add(URLs.toFilePath(rarXmlUrl));
    }
    return connectorModule;
}
Also used : Connector(org.apache.openejb.jee.Connector) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) ArrayList(java.util.ArrayList) ClassLoaderConfigurer(org.apache.openejb.classloader.ClassLoaderConfigurer) URL(java.net.URL) URLClassLoader(java.net.URLClassLoader) EmptyResourcesClassLoader(org.apache.openejb.core.EmptyResourcesClassLoader) JarFile(java.util.jar.JarFile) File(java.io.File)

Example 3 with ClassLoaderConfigurer

use of org.apache.openejb.classloader.ClassLoaderConfigurer in project tomee by apache.

the class ServerListener method lifecycleEvent.

@Override
public void lifecycleEvent(final LifecycleEvent event) {
    if (Lifecycle.BEFORE_INIT_EVENT.equals(event.getType()) && StandardServer.class.isInstance(event.getSource())) {
        installServerInfo();
    }
    synchronized (listenerInstalled) {
        // only install once
        if (listenerInstalled.get() || !Lifecycle.AFTER_INIT_EVENT.equals(event.getType())) {
            return;
        }
        if (!(event.getSource() instanceof StandardServer)) {
            return;
        }
        try {
            final StandardServer server = (StandardServer) event.getSource();
            TomcatHelper.setServer(server);
            final Properties properties = new Properties();
            System.getProperties().setProperty("openejb.embedder.source", getClass().getSimpleName());
            properties.setProperty("openejb.embedder.source", getClass().getSimpleName());
            // if SystemInstance is already initialized, then return
            if (SystemInstance.isInitialized()) {
                return;
            }
            // set the openejb.loader property to tomcat-system
            properties.setProperty("openejb.loader", "tomcat-system");
            // Get the value of catalina.home and set it to openejb.home
            final String catalinaHome = System.getProperty("catalina.home");
            properties.setProperty("openejb.home", catalinaHome);
            // Sets system property for openejb.home
            System.setProperty("openejb.home", catalinaHome);
            // get the value of catalina.base and set it to openejb.base
            final String catalinaBase = System.getProperty("catalina.base");
            properties.setProperty("openejb.base", catalinaBase);
            // Sets system property for openejb.base
            System.setProperty("openejb.base", catalinaBase);
            // System.setProperty("tomcat.version", "x.y.z.w");
            // System.setProperty("tomcat.built", "mmm dd yyyy hh:mm:ss");
            // set the System properties, tomcat.version, tomcat.built
            final ClassLoader classLoader = ServerListener.class.getClassLoader();
            try {
                final Properties tomcatServerInfo = IO.readProperties(classLoader.getResourceAsStream("org/apache/catalina/util/ServerInfo.properties"), new Properties());
                String serverNumber = tomcatServerInfo.getProperty("server.number");
                if (serverNumber == null) {
                    // Tomcat5 only has server.info
                    final String serverInfo = tomcatServerInfo.getProperty("server.info");
                    if (serverInfo != null) {
                        final int slash = serverInfo.indexOf('/');
                        serverNumber = serverInfo.substring(slash + 1);
                    }
                }
                if (serverNumber != null) {
                    System.setProperty("tomcat.version", serverNumber);
                }
                final String serverBuilt = tomcatServerInfo.getProperty("server.built");
                if (serverBuilt != null) {
                    System.setProperty("tomcat.built", serverBuilt);
                }
            } catch (final Throwable e) {
            // no-op
            }
            final TomcatLoader loader = new TomcatLoader();
            loader.initSystemInstance(properties);
            // manage additional libraries
            try {
                final Collection<URL> files = new ArrayList<>();
                for (final File f : ProvisioningUtil.addAdditionalLibraries()) {
                    files.add(f.toURI().toURL());
                }
                final ClassLoaderConfigurer configurer = QuickJarsTxtParser.parse(SystemInstance.get().getConf(QuickJarsTxtParser.FILE_NAME));
                if (configurer != null) {
                    files.addAll(asList(configurer.additionalURLs()));
                }
                if (!files.isEmpty() && URLClassLoader.class.isInstance(classLoader)) {
                    final URLClassLoader ucl = URLClassLoader.class.cast(classLoader);
                    try {
                        final Method addUrl = URLClassLoader.class.getDeclaredMethod("addURL", URL.class);
                        final boolean acc = addUrl.isAccessible();
                        try {
                            for (final URL url : files) {
                                addUrl(ucl, addUrl, url);
                            }
                        } finally {
                            addUrl.setAccessible(acc);
                        }
                    } catch (final Exception e) {
                        LOGGER.log(Level.SEVERE, e.getMessage(), e);
                    }
                }
            } catch (final IOException ex) {
                LOGGER.log(Level.SEVERE, ex.getMessage(), ex);
            }
            loader.initialize(properties);
            listenerInstalled.set(true);
        } catch (final Exception e) {
            LOGGER.log(Level.SEVERE, "TomEE Listener can't start OpenEJB", e);
        // e.printStackTrace(System.err);
        }
    }
}
Also used : ArrayList(java.util.ArrayList) ClassLoaderConfigurer(org.apache.openejb.classloader.ClassLoaderConfigurer) Method(java.lang.reflect.Method) IOException(java.io.IOException) Properties(java.util.Properties) URL(java.net.URL) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException) InvocationTargetException(java.lang.reflect.InvocationTargetException) StandardServer(org.apache.catalina.core.StandardServer) URLClassLoader(java.net.URLClassLoader) URLClassLoader(java.net.URLClassLoader) File(java.io.File)

Example 4 with ClassLoaderConfigurer

use of org.apache.openejb.classloader.ClassLoaderConfigurer in project tomee by apache.

the class TomEEWebappLoader method startInternal.

@Override
protected void startInternal() throws LifecycleException {
    if (getClassLoader() != null) {
        final TomEEWebappClassLoader webappClassLoader = TomEEWebappClassLoader.class.cast(getClassLoader());
        if (webappClassLoader.isStopped()) {
            webappClassLoader.internalStop();
        }
    }
    final Context context = getContext();
    ClassLoaderConfigurer configurer = ClassLoaderUtil.configurer(context.getName());
    // WEB-INF/jars.xml
    final File war = Contexts.warPath(Context.class.cast(context));
    final File jarsXml = new File(war, "WEB-INF/" + QuickJarsTxtParser.FILE_NAME);
    final ClassLoaderConfigurer configurerTxt = QuickJarsTxtParser.parse(jarsXml);
    if (configurerTxt != null) {
        configurer = new CompositeClassLoaderConfigurer(configurer, configurerTxt);
    }
    TomEEWebappClassLoader.initContext(configurer);
    TomEEWebappClassLoader.initContext(context);
    try {
        super.startInternal();
    } finally {
        TomEEWebappClassLoader.cleanContext();
    }
    if (FORCE_SKIP != null && TomEEWebappClassLoader.class.isInstance(getClassLoader())) {
        TomEEWebappClassLoader.class.cast(getClassLoader()).setForceSkip(FORCE_SKIP.split(" *, *"));
    }
}
Also used : Context(org.apache.catalina.Context) CompositeClassLoaderConfigurer(org.apache.openejb.classloader.CompositeClassLoaderConfigurer) ClassLoaderConfigurer(org.apache.openejb.classloader.ClassLoaderConfigurer) CompositeClassLoaderConfigurer(org.apache.openejb.classloader.CompositeClassLoaderConfigurer) File(java.io.File)

Example 5 with ClassLoaderConfigurer

use of org.apache.openejb.classloader.ClassLoaderConfigurer in project tomee by apache.

the class TomEEWebappClassLoader method start.

// embeddeding implementation of sthg (JPA, JSF) can lead to classloading issues if we don't enrich the webapp
// with our integration jars
// typically the class will try to be loaded by the common classloader
// but the interface implemented or the parent class
// will be in the webapp
@Override
public void start() throws LifecycleException {
    // do it first otherwise we can't use this as classloader
    super.start();
    // mainly for tomee-maven-plugin
    initAdditionalRepos();
    if (additionalRepos != null && !additionalRepos.isEmpty()) {
        for (final File f : additionalRepos) {
            final DirResourceSet webResourceSet = new PremptiveDirResourceSet(resources, "/", f.getAbsolutePath(), "/");
            resources.addPreResources(webResourceSet);
        }
        resources.setCachingAllowed(false);
    }
    // add configurer enrichments
    if (configurer != null) {
        // add now we removed all we wanted
        final URL[] enrichment = configurer.additionalURLs();
        for (final URL url : enrichment) {
            super.addURL(url);
        }
    }
    // add internal enrichments
    for (final URL url : SystemInstance.get().getComponent(WebAppEnricher.class).enrichment(this)) {
        super.addURL(url);
    }
    // WEB-INF/jars.xml
    final File war = Contexts.warPath(CONTEXT.get());
    final File jarsXml = new File(war, "WEB-INF/" + QuickJarsTxtParser.FILE_NAME);
    final ClassLoaderConfigurer configurerTxt = QuickJarsTxtParser.parse(jarsXml);
    if (configurerTxt != null) {
        configurer = new CompositeClassLoaderConfigurer(configurer, configurerTxt);
    }
    stopped = false;
}
Also used : DirResourceSet(org.apache.catalina.webresources.DirResourceSet) CompositeClassLoaderConfigurer(org.apache.openejb.classloader.CompositeClassLoaderConfigurer) ClassLoaderConfigurer(org.apache.openejb.classloader.ClassLoaderConfigurer) CompositeClassLoaderConfigurer(org.apache.openejb.classloader.CompositeClassLoaderConfigurer) WebAppEnricher(org.apache.openejb.classloader.WebAppEnricher) File(java.io.File) URL(java.net.URL)

Aggregations

ClassLoaderConfigurer (org.apache.openejb.classloader.ClassLoaderConfigurer)9 File (java.io.File)8 URL (java.net.URL)7 IOException (java.io.IOException)5 ArrayList (java.util.ArrayList)5 CompositeClassLoaderConfigurer (org.apache.openejb.classloader.CompositeClassLoaderConfigurer)5 MalformedURLException (java.net.MalformedURLException)4 URLClassLoader (java.net.URLClassLoader)4 JarFile (java.util.jar.JarFile)4 HashMap (java.util.HashMap)3 OpenEJBException (org.apache.openejb.OpenEJBException)3 EmptyResourcesClassLoader (org.apache.openejb.core.EmptyResourcesClassLoader)3 LinkedHashMap (java.util.LinkedHashMap)2 Map (java.util.Map)2 Properties (java.util.Properties)2 BeforeDeploymentEvent (org.apache.openejb.config.event.BeforeDeploymentEvent)2 InvalidObjectException (java.io.InvalidObjectException)1 ObjectStreamException (java.io.ObjectStreamException)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1