Search in sources :

Example 1 with DelegatingClassLoader

use of org.glassfish.internal.api.DelegatingClassLoader in project Payara by payara.

the class AppLibClassLoaderServiceImpl method getAppLibClassLoader.

/**
 * @see org.glassfish.internal.api.ClassLoaderHierarchy#getAppLibClassLoader(String, List<URI>)
 */
public ClassLoader getAppLibClassLoader(String application, List<URI> libURIs) throws MalformedURLException {
    ClassLoaderHierarchy clh = habitat.getService(ClassLoaderHierarchy.class);
    DelegatingClassLoader connectorCL = clh.getConnectorClassLoader(application);
    if (libURIs == null || libURIs.isEmpty()) {
        // class loader in the hierarchy? Instead return the parent.
        return connectorCL;
    }
    final ClassLoader commonCL = commonCLS.getCommonClassLoader();
    DelegatingClassLoader applibCL = AccessController.doPrivileged(new PrivilegedAction<DelegatingClassLoader>() {

        public DelegatingClassLoader run() {
            return new DelegatingClassLoader(commonCL);
        }
    });
    // search path
    for (DelegatingClassLoader.ClassFinder cf : connectorCL.getDelegates()) {
        applibCL.addDelegate(cf);
    }
    addDelegates(libURIs, applibCL);
    return applibCL;
}
Also used : DelegatingClassLoader(org.glassfish.internal.api.DelegatingClassLoader) URLClassLoader(java.net.URLClassLoader) DelegatingClassLoader(org.glassfish.internal.api.DelegatingClassLoader) ClassLoaderHierarchy(org.glassfish.internal.api.ClassLoaderHierarchy)

Example 2 with DelegatingClassLoader

use of org.glassfish.internal.api.DelegatingClassLoader in project Payara by payara.

the class EarHandler method getClassLoader.

public ClassLoader getClassLoader(final ClassLoader parent, DeploymentContext context) {
    final ReadableArchive archive = context.getSource();
    final ApplicationHolder holder = getApplicationHolder(archive, context, true);
    // the ear classloader hierachy will be
    // ear lib classloader <- embedded rar classloader <-
    // ear classloader <- various module classloaders
    final DelegatingClassLoader embeddedConnCl;
    final EarClassLoader cl;
    // Add the libraries packaged in the application library directory
    try {
        String compatProp = context.getAppProps().getProperty(COMPATIBILITY);
        // let's see if it's defined in glassfish-application.xml
        if (compatProp == null) {
            GFApplicationXmlParser gfApplicationXmlParser = new GFApplicationXmlParser(context.getSource());
            compatProp = gfApplicationXmlParser.getCompatibilityValue();
            if (compatProp != null) {
                context.getAppProps().put(COMPATIBILITY, compatProp);
            }
        }
        // let's see if it's defined in sun-application.xml
        if (compatProp == null) {
            SunApplicationXmlParser sunApplicationXmlParser = new SunApplicationXmlParser(context.getSourceDir());
            compatProp = sunApplicationXmlParser.getCompatibilityValue();
            if (compatProp != null) {
                context.getAppProps().put(COMPATIBILITY, compatProp);
            }
        }
        if (getSecurityManager() != null) {
            // Process declared permissions
            earDeclaredPC = getDeclaredPermissions(CommponentType.ear, context);
            // Process EE permissions
            processEEPermissions(context);
        }
        final URL[] earLibURLs = ASClassLoaderUtil.getAppLibDirLibraries(context.getSourceDir(), holder.app.getLibraryDirectory(), compatProp);
        final EarLibClassLoader earLibCl = AccessController.doPrivileged(new PrivilegedAction<EarLibClassLoader>() {

            @Override
            public EarLibClassLoader run() {
                return new EarLibClassLoader(earLibURLs, parent);
            }
        });
        String clDelegate = holder.app.getClassLoadingDelegate();
        // Default to true if null
        if (Boolean.parseBoolean(clDelegate == null ? "true" : clDelegate) == false) {
            earLibCl.enableCurrentBeforeParentUnconditional();
        } else if (clDelegate != null) {
            // otherwise clDelegate == true
            earLibCl.disableCurrentBeforeParent();
        }
        if (System.getSecurityManager() != null) {
            addEEOrDeclaredPermissions(earLibCl, earDeclaredPC, false);
            if (_logger.isLoggable(FINE)) {
                _logger.fine("added declaredPermissions to earlib: " + earDeclaredPC);
            }
            addEEOrDeclaredPermissions(earLibCl, eeGarntsMap.get(CommponentType.ear), true);
            if (_logger.isLoggable(Level.FINE)) {
                _logger.fine("added all ee permissions to earlib: " + eeGarntsMap.get(CommponentType.ear));
            }
        }
        embeddedConnCl = AccessController.doPrivileged(new PrivilegedAction<DelegatingClassLoader>() {

            @Override
            public DelegatingClassLoader run() {
                return new DelegatingClassLoader(earLibCl);
            }
        });
        cl = AccessController.doPrivileged(new PrivilegedAction<EarClassLoader>() {

            @Override
            public EarClassLoader run() {
                return new EarClassLoader(embeddedConnCl, holder.app);
            }
        });
        // add ear lib to module classloader list so we can
        // clean it up later
        cl.addModuleClassLoader(EAR_LIB, earLibCl);
        if (System.getSecurityManager() != null) {
            // push declared permissions to ear classloader
            addEEOrDeclaredPermissions(cl, earDeclaredPC, false);
            if (_logger.isLoggable(Level.FINE))
                _logger.fine("declaredPermissions added: " + earDeclaredPC);
            // push ejb permissions to ear classloader
            addEEOrDeclaredPermissions(cl, eeGarntsMap.get(CommponentType.ejb), true);
            if (_logger.isLoggable(Level.FINE))
                _logger.fine("ee permissions added: " + eeGarntsMap.get(CommponentType.ejb));
        }
    } catch (Exception e) {
        _logger.log(Level.SEVERE, strings.get("errAddLibs"), e);
        throw new RuntimeException(e);
    }
    for (ModuleDescriptor md : holder.app.getModules()) {
        ReadableArchive sub = null;
        String moduleUri = md.getArchiveUri();
        try {
            sub = archive.getSubArchive(moduleUri);
            if (sub instanceof InputJarArchive) {
                throw new IllegalArgumentException(strings.get("wrongArchType", moduleUri));
            }
        } catch (IOException e) {
            _logger.log(Level.FINE, "Sub archive " + moduleUri + " seems unreadable", e);
        }
        if (sub != null) {
            try {
                ArchiveHandler handler = context.getModuleArchiveHandlers().get(moduleUri);
                if (handler == null) {
                    handler = getArchiveHandlerFromModuleType(md.getModuleType());
                    if (handler == null) {
                        handler = deployment.getArchiveHandler(sub);
                    }
                    context.getModuleArchiveHandlers().put(moduleUri, handler);
                }
                if (handler != null) {
                    ActionReport subReport = context.getActionReport().addSubActionsReport();
                    // todo : this is a hack, once again,
                    // the handler is assuming a file:// url
                    ExtendedDeploymentContext subContext = new DeploymentContextImpl(subReport, sub, context.getCommandParameters(DeployCommandParameters.class), env) {

                        @Override
                        public File getScratchDir(String subDirName) {
                            String modulePortion = Util.getURIName(getSource().getURI());
                            return (new File(super.getScratchDir(subDirName), modulePortion));
                        }
                    };
                    // sub context will store the root archive handler also
                    // so we can figure out the enclosing archive type
                    subContext.setArchiveHandler(context.getArchiveHandler());
                    subContext.setParentContext((ExtendedDeploymentContext) context);
                    sub.setParentArchive(context.getSource());
                    ClassLoader subCl = handler.getClassLoader(cl, subContext);
                    if ((System.getSecurityManager() != null) && (subCl instanceof DDPermissionsLoader)) {
                        addEEOrDeclaredPermissions(subCl, earDeclaredPC, false);
                        if (_logger.isLoggable(Level.FINE))
                            _logger.fine("added declared permissions to sub module of " + subCl);
                    }
                    if (md.getModuleType().equals(DOLUtils.ejbType())) {
                        // for ejb module, we just add the ejb urls
                        // to EarClassLoader and use that to load
                        // ejb module
                        URL[] moduleURLs = ((URLClassLoader) subCl).getURLs();
                        for (URL moduleURL : moduleURLs) {
                            cl.addURL(moduleURL);
                        }
                        cl.addModuleClassLoader(moduleUri, cl);
                        PreDestroy.class.cast(subCl).preDestroy();
                    } else if (md.getModuleType().equals(DOLUtils.rarType())) {
                        embeddedConnCl.addDelegate((DelegatingClassLoader.ClassFinder) subCl);
                        cl.addModuleClassLoader(moduleUri, subCl);
                    } else {
                        Boolean isTempClassLoader = context.getTransientAppMetaData(ExtendedDeploymentContext.IS_TEMP_CLASSLOADER, Boolean.class);
                        if (subCl instanceof URLClassLoader && (isTempClassLoader != null) && isTempClassLoader) {
                            // for temp classloader, we add all the module
                            // urls to the top level EarClassLoader
                            URL[] moduleURLs = ((URLClassLoader) subCl).getURLs();
                            for (URL moduleURL : moduleURLs) {
                                cl.addURL(moduleURL);
                            }
                        }
                        cl.addModuleClassLoader(moduleUri, subCl);
                    }
                }
            } catch (IOException e) {
                _logger.log(Level.SEVERE, strings.get("noClassLoader", moduleUri), e);
            }
        }
    }
    return cl;
}
Also used : ArchiveHandler(org.glassfish.api.deployment.archive.ArchiveHandler) AbstractArchiveHandler(com.sun.enterprise.deploy.shared.AbstractArchiveHandler) ActionReport(org.glassfish.api.ActionReport) ExtendedDeploymentContext(org.glassfish.internal.deployment.ExtendedDeploymentContext) URL(java.net.URL) ApplicationHolder(org.glassfish.javaee.core.deployment.ApplicationHolder) PrivilegedAction(java.security.PrivilegedAction) DDPermissionsLoader(com.sun.enterprise.security.integration.DDPermissionsLoader) URLClassLoader(java.net.URLClassLoader) DelegatingClassLoader(org.glassfish.internal.api.DelegatingClassLoader) InputJarArchive(com.sun.enterprise.deployment.deploy.shared.InputJarArchive) IOException(java.io.IOException) DelegatingClassLoader(org.glassfish.internal.api.DelegatingClassLoader) XMLStreamException(javax.xml.stream.XMLStreamException) FileNotFoundException(java.io.FileNotFoundException) PrivilegedActionException(java.security.PrivilegedActionException) IOException(java.io.IOException) SAXParseException(org.xml.sax.SAXParseException) DeploymentContextImpl(org.glassfish.deployment.common.DeploymentContextImpl) DeployCommandParameters(org.glassfish.api.deployment.DeployCommandParameters) ModuleDescriptor(org.glassfish.deployment.common.ModuleDescriptor) URLClassLoader(java.net.URLClassLoader) PreDestroy(org.glassfish.hk2.api.PreDestroy) ReadableArchive(org.glassfish.api.deployment.archive.ReadableArchive) File(java.io.File)

Example 3 with DelegatingClassLoader

use of org.glassfish.internal.api.DelegatingClassLoader in project Payara by payara.

the class EarClassLoader method preDestroy.

@Override
public void preDestroy() {
    if (isPreDestroyCalled) {
        return;
    }
    try {
        for (ClassLoaderHolder clh : moduleClassLoaders) {
            // destroy all the module classloaders
            if (!(clh.loader instanceof EarLibClassLoader) && !(clh.loader instanceof EarClassLoader) && !isRARCL(clh.loader)) {
                try {
                    PreDestroy.class.cast(clh.loader).preDestroy();
                } catch (Exception e) {
                // ignore, the class loader does not need to be
                // explicitly stopped.
                }
            }
        }
        // destroy itself
        super.preDestroy();
        // now destroy embedded Connector CLs
        DelegatingClassLoader dcl = (DelegatingClassLoader) this.getParent();
        for (DelegatingClassLoader.ClassFinder cf : dcl.getDelegates()) {
            try {
                PreDestroy.class.cast(cf).preDestroy();
            } catch (Exception e) {
            // ignore, the class loader does not need to be
            // explicitly stopped.
            }
        }
        // now destroy the EarLibClassLoader
        PreDestroy.class.cast(this.getParent().getParent()).preDestroy();
        moduleClassLoaders = null;
    } catch (Exception e) {
    // ignore, the class loader does not need to be explicitely stopped.
    }
    isPreDestroyCalled = true;
}
Also used : PreDestroy(org.glassfish.hk2.api.PreDestroy) DelegatingClassLoader(org.glassfish.internal.api.DelegatingClassLoader)

Example 4 with DelegatingClassLoader

use of org.glassfish.internal.api.DelegatingClassLoader in project Payara by payara.

the class AppLibClassLoaderServiceImpl method addDelegates.

private void addDelegates(Collection<URI> libURIs, DelegatingClassLoader holder) throws MalformedURLException {
    ClassLoader commonCL = commonCLS.getCommonClassLoader();
    for (URI libURI : libURIs) {
        synchronized (this) {
            DelegatingClassLoader.ClassFinder libCF = classFinderRegistry.get(libURI);
            if (libCF == null) {
                libCF = new URLClassFinder(new URL[] { libURI.toURL() }, commonCL);
                classFinderRegistry.put(libURI, libCF);
            }
            holder.addDelegate(libCF);
        }
    }
}
Also used : DelegatingClassLoader(org.glassfish.internal.api.DelegatingClassLoader) URLClassLoader(java.net.URLClassLoader) URI(java.net.URI) DelegatingClassLoader(org.glassfish.internal.api.DelegatingClassLoader) URL(java.net.URL)

Example 5 with DelegatingClassLoader

use of org.glassfish.internal.api.DelegatingClassLoader in project Payara by payara.

the class AppLibClassLoaderServiceImpl method getAppLibClassFinder.

/**
 * @see org.glassfish.internal.api.ClassLoaderHierarchy#getAppLibClassFinder(List<URI>)
 */
public DelegatingClassLoader.ClassFinder getAppLibClassFinder(Collection<URI> libURIs) throws MalformedURLException {
    final ClassLoader commonCL = commonCLS.getCommonClassLoader();
    DelegatingClassLoader appLibClassFinder = AccessController.doPrivileged(new PrivilegedAction<DelegatingClassLoader>() {

        public DelegatingClassLoader run() {
            return new AppLibClassFinder(commonCL);
        }
    });
    addDelegates(libURIs, appLibClassFinder);
    return (DelegatingClassLoader.ClassFinder) appLibClassFinder;
}
Also used : DelegatingClassLoader(org.glassfish.internal.api.DelegatingClassLoader) URLClassLoader(java.net.URLClassLoader) DelegatingClassLoader(org.glassfish.internal.api.DelegatingClassLoader)

Aggregations

DelegatingClassLoader (org.glassfish.internal.api.DelegatingClassLoader)5 URLClassLoader (java.net.URLClassLoader)4 URL (java.net.URL)2 PreDestroy (org.glassfish.hk2.api.PreDestroy)2 AbstractArchiveHandler (com.sun.enterprise.deploy.shared.AbstractArchiveHandler)1 InputJarArchive (com.sun.enterprise.deployment.deploy.shared.InputJarArchive)1 DDPermissionsLoader (com.sun.enterprise.security.integration.DDPermissionsLoader)1 File (java.io.File)1 FileNotFoundException (java.io.FileNotFoundException)1 IOException (java.io.IOException)1 URI (java.net.URI)1 PrivilegedAction (java.security.PrivilegedAction)1 PrivilegedActionException (java.security.PrivilegedActionException)1 XMLStreamException (javax.xml.stream.XMLStreamException)1 ActionReport (org.glassfish.api.ActionReport)1 DeployCommandParameters (org.glassfish.api.deployment.DeployCommandParameters)1 ArchiveHandler (org.glassfish.api.deployment.archive.ArchiveHandler)1 ReadableArchive (org.glassfish.api.deployment.archive.ReadableArchive)1 DeploymentContextImpl (org.glassfish.deployment.common.DeploymentContextImpl)1 ModuleDescriptor (org.glassfish.deployment.common.ModuleDescriptor)1