Search in sources :

Example 1 with CompositeClassLoaderConfigurer

use of org.apache.openejb.classloader.CompositeClassLoaderConfigurer 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 (forceSkip != null && WebAppFirstEarClassLoader.class.isInstance(getClassLoader())) {
        WebAppFirstEarClassLoader.class.cast(getClassLoader()).setForceSkip(forceSkip.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 2 with CompositeClassLoaderConfigurer

use of org.apache.openejb.classloader.CompositeClassLoaderConfigurer 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)

Example 3 with CompositeClassLoaderConfigurer

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

the class Assembler method createAppClassLoader.

public ClassLoader createAppClassLoader(final AppInfo appInfo) throws OpenEJBException, IOException {
    if ("openejb".equals(appInfo.appId)) {
        return ParentClassLoaderFinder.Helper.get();
    }
    final Set<URL> jars = new HashSet<>();
    for (final EjbJarInfo info : appInfo.ejbJars) {
        if (info.path != null) {
            jars.add(toUrl(info.path));
        }
    }
    for (final ClientInfo info : appInfo.clients) {
        if (info.path != null) {
            jars.add(toUrl(info.path));
        }
    }
    for (final ConnectorInfo info : appInfo.connectors) {
        for (final String jarPath : info.libs) {
            jars.add(toUrl(jarPath));
        }
    }
    for (final String jarPath : appInfo.libs) {
        jars.add(toUrl(jarPath));
    }
    // add openejb-jpa-integration if the jpa provider is in lib/
    if (appInfo.libs.size() > 0) {
        // the test could be enhanced
        try {
            final File jpaIntegrationFile = JarLocation.jarLocation(MakeTxLookup.class);
            final URL url = jpaIntegrationFile.toURI().toURL();
            if (!jars.contains(url)) {
                // could have been done before (webapp enrichment or manually for instance)
                jars.add(url);
            }
        } catch (final RuntimeException re) {
            logger.warning("Unable to find the open-jpa-integration jar");
        }
    }
    final ClassLoaderEnricher component = SystemInstance.get().getComponent(ClassLoaderEnricher.class);
    if (component != null) {
        jars.addAll(Arrays.asList(component.applicationEnrichment()));
    } else {
        logger.warning("Unable to find open-jpa-integration jar");
    }
    // Create the class loader
    final ClassLoader parent = ParentClassLoaderFinder.Helper.get();
    final String prefix;
    if (appInfo.webAppAlone) {
        prefix = "WEB-INF/";
    } else {
        prefix = "META-INF/";
    }
    final ClassLoaderConfigurer configurer1 = QuickJarsTxtParser.parse(new File(appInfo.path, prefix + QuickJarsTxtParser.FILE_NAME));
    final ClassLoaderConfigurer configurer2 = ClassLoaderUtil.configurer(appInfo.appId);
    if (configurer1 != null || configurer2 != null) {
        final ClassLoaderConfigurer configurer = new CompositeClassLoaderConfigurer(configurer1, configurer2);
        ClassLoaderConfigurer.Helper.configure(jars, configurer);
    }
    final URL[] filtered = jars.toArray(new URL[jars.size()]);
    // since we don't really need to create a classloader here when starting from classpath just let skip this step
    if (skipLoaderIfPossible) {
        // TODO: maybe use a boolean to know if all urls comes from the classpath to avoid this validation
        if ("classpath.ear".equals(appInfo.appId)) {
            return parent;
        }
        final Collection<File> urls = new HashSet<>();
        for (final URL url : ClassLoaders.findUrls(parent)) {
            // need to convert it to file since urls can be file:/xxx or jar:file:///xxx
            try {
                urls.add(URLs.toFile(url).getCanonicalFile());
            } catch (final Exception error) {
                if (logger.isDebugEnabled()) {
                    logger.debug("Can't determine url for: " + url.toExternalForm(), error);
                }
            }
        }
        boolean allIsIntheClasspath = true;
        for (final URL url : filtered) {
            try {
                if (!urls.contains(URLs.toFile(url).getCanonicalFile())) {
                    allIsIntheClasspath = false;
                    if (logger.isDebugEnabled()) {
                        logger.debug(url.toExternalForm() + " (" + URLs.toFile(url) + ") is not in the classloader so we'll create a dedicated classloader for this app");
                    }
                    break;
                }
            } catch (final Exception ignored) {
                allIsIntheClasspath = false;
                if (logger.isDebugEnabled()) {
                    logger.debug(url.toExternalForm() + " (" + URLs.toFile(url) + ") is not in the classloader", ignored);
                }
                break;
            }
        }
        if (allIsIntheClasspath) {
            logger.info("Not creating another application classloader for " + appInfo.appId);
            return parent;
        } else if (logger.isDebugEnabled()) {
            logger.debug("Logging all urls from the app since we don't skip the app classloader creation:");
            for (final URL url : filtered) {
                logger.debug(" -> " + url.toExternalForm());
            }
            logger.debug("Logging all urls from the classloader since we don't skip the app classloader creation:");
            for (final File url : urls) {
                logger.debug(" -> " + url.getAbsolutePath());
            }
        }
    }
    logger.info("Creating dedicated application classloader for " + appInfo.appId);
    if (!appInfo.delegateFirst) {
        return ClassLoaderUtil.createClassLoader(appInfo.path, filtered, parent);
    }
    return ClassLoaderUtil.createClassLoaderFirst(appInfo.path, filtered, parent);
}
Also used : CompositeClassLoaderConfigurer(org.apache.openejb.classloader.CompositeClassLoaderConfigurer) ClassLoaderEnricher(org.apache.openejb.component.ClassLoaderEnricher) ClassLoaderConfigurer(org.apache.openejb.classloader.ClassLoaderConfigurer) CompositeClassLoaderConfigurer(org.apache.openejb.classloader.CompositeClassLoaderConfigurer) URL(java.net.URL) InvalidObjectException(java.io.InvalidObjectException) NameAlreadyBoundException(javax.naming.NameAlreadyBoundException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) ObjectStreamException(java.io.ObjectStreamException) ResourceAdapterInternalException(javax.resource.spi.ResourceAdapterInternalException) URISyntaxException(java.net.URISyntaxException) UndeployException(org.apache.openejb.UndeployException) DefinitionException(javax.enterprise.inject.spi.DefinitionException) ConstructionException(org.apache.xbean.recipe.ConstructionException) MBeanRegistrationException(javax.management.MBeanRegistrationException) InstanceNotFoundException(javax.management.InstanceNotFoundException) ValidationException(javax.validation.ValidationException) MalformedObjectNameException(javax.management.MalformedObjectNameException) DuplicateDeploymentIdException(org.apache.openejb.DuplicateDeploymentIdException) TimeoutException(java.util.concurrent.TimeoutException) NamingException(javax.naming.NamingException) OpenEJBException(org.apache.openejb.OpenEJBException) DeploymentException(javax.enterprise.inject.spi.DeploymentException) NoSuchApplicationException(org.apache.openejb.NoSuchApplicationException) MalformedURLException(java.net.MalformedURLException) OpenEJBRuntimeException(org.apache.openejb.OpenEJBRuntimeException) OpenEJBRuntimeException(org.apache.openejb.OpenEJBRuntimeException) File(java.io.File) HashSet(java.util.HashSet)

Example 4 with CompositeClassLoaderConfigurer

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

the class ClassLoaderUtil method createTempClassLoader.

public static URLClassLoader createTempClassLoader(final String appId, final URL[] rawUrls, final ClassLoader parent) {
    String updatedAppId = appId;
    if (appId != null) {
        // here we often get the full path of the app as id where later it is simply the name of the file/dir
        final File file = new File(appId);
        if (file.exists()) {
            updatedAppId = file.getName();
            if (updatedAppId.endsWith(".war") || updatedAppId.endsWith(".ear")) {
                updatedAppId = updatedAppId.substring(0, updatedAppId.length() - ".war".length());
            }
        }
    }
    // from the app
    final ClassLoaderConfigurer configurer1 = QuickJarsTxtParser.parse(new File(appId, "META-INF/" + QuickJarsTxtParser.FILE_NAME));
    final ClassLoaderConfigurer configurer2 = QuickJarsTxtParser.parse(new File(appId, "WEB-INF/" + QuickJarsTxtParser.FILE_NAME));
    // external config
    ClassLoaderConfigurer configurer3 = ClassLoaderUtil.configurer(updatedAppId);
    if (configurer3 == null) {
        // try the complete path
        configurer3 = ClassLoaderUtil.configurer(appId);
    }
    final URL[] urls;
    if (configurer1 == null && configurer2 == null && configurer3 == null) {
        urls = rawUrls;
    } else {
        final CompositeClassLoaderConfigurer configurer = new CompositeClassLoaderConfigurer(configurer1, configurer2, configurer3);
        final Collection<URL> list = new ArrayList<URL>();
        list.addAll(Arrays.asList(rawUrls));
        ClassLoaderConfigurer.Helper.configure(list, configurer);
        urls = list.toArray(new URL[list.size()]);
    }
    return new TempClassLoader(createClassLoader(appId, urls, parent));
}
Also used : TempClassLoader(org.apache.openejb.core.TempClassLoader) CompositeClassLoaderConfigurer(org.apache.openejb.classloader.CompositeClassLoaderConfigurer) ArrayList(java.util.ArrayList) ClassLoaderConfigurer(org.apache.openejb.classloader.ClassLoaderConfigurer) CompositeClassLoaderConfigurer(org.apache.openejb.classloader.CompositeClassLoaderConfigurer) JarFile(java.util.jar.JarFile) ZipFile(java.util.zip.ZipFile) File(java.io.File) URL(java.net.URL)

Aggregations

File (java.io.File)4 ClassLoaderConfigurer (org.apache.openejb.classloader.ClassLoaderConfigurer)4 CompositeClassLoaderConfigurer (org.apache.openejb.classloader.CompositeClassLoaderConfigurer)4 URL (java.net.URL)3 IOException (java.io.IOException)1 InvalidObjectException (java.io.InvalidObjectException)1 ObjectStreamException (java.io.ObjectStreamException)1 MalformedURLException (java.net.MalformedURLException)1 URISyntaxException (java.net.URISyntaxException)1 ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1 ExecutionException (java.util.concurrent.ExecutionException)1 TimeoutException (java.util.concurrent.TimeoutException)1 JarFile (java.util.jar.JarFile)1 ZipFile (java.util.zip.ZipFile)1 DefinitionException (javax.enterprise.inject.spi.DefinitionException)1 DeploymentException (javax.enterprise.inject.spi.DeploymentException)1 InstanceNotFoundException (javax.management.InstanceNotFoundException)1 MBeanRegistrationException (javax.management.MBeanRegistrationException)1 MalformedObjectNameException (javax.management.MalformedObjectNameException)1