Search in sources :

Example 1 with ClassLoaderHierarchy

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

the class Realm method doInstantiate.

/**
 * Instantiates a Realm class of the given type and invokes its init()
 */
private static synchronized Realm doInstantiate(String name, String className, Properties props) throws BadRealmException {
    ServiceLocator habitat = Globals.getDefaultHabitat();
    RealmsManager mgr = null;
    try {
        mgr = getRealmsManager();
        Class realmClass = null;
        // try a HK2 route first
        Realm r = habitat.getService(Realm.class, name);
        if (r == null) {
            try {
                // TODO: workaround here. Once fixed in V3 we should be able to use
                // Context ClassLoader instead.
                ClassLoaderHierarchy hierarchy = habitat.getService(ClassLoaderHierarchy.class);
                realmClass = hierarchy.getCommonClassLoader().loadClass(className);
                Object obj = realmClass.newInstance();
                r = (Realm) obj;
            } catch (ClassNotFoundException ex) {
                realmClass = Class.forName(className);
                Object obj = realmClass.newInstance();
                r = (Realm) obj;
            }
        }
        r.setName(name);
        r.init(props);
        if (mgr == null) {
            throw new BadRealmException("Unable to locate RealmsManager Service");
        }
        _logger.log(Level.FINER, SecurityLoggerInfo.realmCreated, new Object[] { name, className });
        return r;
    } catch (NoSuchRealmException | InstantiationException | IllegalAccessException | ClassNotFoundException ex) {
        throw new BadRealmException(ex);
    }
}
Also used : ServiceLocator(org.glassfish.hk2.api.ServiceLocator) ClassLoaderHierarchy(org.glassfish.internal.api.ClassLoaderHierarchy)

Example 2 with ClassLoaderHierarchy

use of org.glassfish.internal.api.ClassLoaderHierarchy 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 3 with ClassLoaderHierarchy

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

the class ASClassLoaderUtil method getModuleClassPath.

/**
 * Gets the classpath associated with a module, suffixing libraries
 * defined [if any] for the application
 *
 * @param habitat the habitat the application resides in.
 * @param moduleId Module id of the module
 * @param deploymentLibs libraries option passed through deployment
 * @return A <code>File.pathSeparator</code> separated list of classpaths
 *         for the passed in module, including the module specified
 *         "libraries" defined for the module.
 */
public static String getModuleClassPath(ServiceLocator habitat, String moduleId, String deploymentLibs) {
    if (deplLogger.isLoggable(Level.FINE)) {
        deplLogger.log(Level.FINE, "ASClassLoaderUtil.getModuleClassPath " + "for module Id : " + moduleId);
    }
    StringBuilder classpath = new StringBuilder(getModulesClasspath(habitat));
    ClassLoaderHierarchy clh = habitat.getService(ClassLoaderHierarchy.class);
    final String commonClassPath = clh.getCommonClassPath();
    if (commonClassPath != null && commonClassPath.length() > 0) {
        classpath.append(commonClassPath).append(File.pathSeparator);
    }
    addDeployParamLibrariesForModule(classpath, moduleId, deploymentLibs, habitat);
    if (deplLogger.isLoggable(Level.FINE)) {
        deplLogger.log(Level.FINE, "Final classpath: " + classpath.toString());
    }
    return classpath.toString();
}
Also used : ClassLoaderHierarchy(org.glassfish.internal.api.ClassLoaderHierarchy)

Example 4 with ClassLoaderHierarchy

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

the class DolProvider method processDeploymentMetaData.

/**
 * This method populates the Application object from a ReadableArchive
 * @param archive the archive for the application
 */
public Application processDeploymentMetaData(ReadableArchive archive) throws Exception {
    FileArchive expandedArchive = null;
    File tmpFile = null;
    ExtendedDeploymentContext context = null;
    Logger logger = Logger.getAnonymousLogger();
    ClassLoader cl = null;
    try {
        String archiveName = Util.getURIName(archive.getURI());
        ArchiveHandler archiveHandler = deployment.getArchiveHandler(archive);
        if (archiveHandler == null) {
            throw new IllegalArgumentException(localStrings.getLocalString("deploy.unknownarchivetype", "Archive type of {0} was not recognized", archiveName));
        }
        DeployCommandParameters parameters = new DeployCommandParameters(new File(archive.getURI()));
        ActionReport report = new HTMLActionReporter();
        context = new DeploymentContextImpl(report, archive, parameters, env);
        context.setArchiveHandler(archiveHandler);
        String appName = archiveHandler.getDefaultApplicationName(archive, context);
        parameters.name = appName;
        if (archive instanceof InputJarArchive) {
            // we need to expand the archive first in this case
            tmpFile = File.createTempFile(archiveName, "");
            String path = tmpFile.getAbsolutePath();
            if (!tmpFile.delete()) {
                logger.log(Level.WARNING, "cannot.delete.temp.file", new Object[] { path });
            }
            File tmpDir = new File(path);
            tmpDir.deleteOnExit();
            if (!tmpDir.exists() && !tmpDir.mkdirs()) {
                throw new IOException("Unable to create directory " + tmpDir.getAbsolutePath());
            }
            expandedArchive = (FileArchive) archiveFactory.createArchive(tmpDir);
            archiveHandler.expand(archive, expandedArchive, context);
            context.setSource(expandedArchive);
        }
        context.setPhase(DeploymentContextImpl.Phase.PREPARE);
        ClassLoaderHierarchy clh = clhProvider.get();
        context.createDeploymentClassLoader(clh, archiveHandler);
        cl = context.getClassLoader();
        deployment.getDeployableTypes(context);
        deployment.getSniffers(archiveHandler, null, context);
        return processDOL(context);
    } finally {
        if (cl != null && cl instanceof PreDestroy) {
            try {
                PreDestroy.class.cast(cl).preDestroy();
            } catch (Exception e) {
            // ignore
            }
        }
        if (context != null) {
            context.postDeployClean(true);
        }
        if (expandedArchive != null) {
            try {
                expandedArchive.close();
            } catch (Exception e) {
            // ignore
            }
        }
        if (tmpFile != null && tmpFile.exists()) {
            try {
                FileUtils.whack(tmpFile);
            } catch (Exception e) {
            // ignore
            }
        }
    }
}
Also used : ArchiveHandler(org.glassfish.api.deployment.archive.ArchiveHandler) InputJarArchive(com.sun.enterprise.deployment.deploy.shared.InputJarArchive) IOException(java.io.IOException) ExtendedDeploymentContext(org.glassfish.internal.deployment.ExtendedDeploymentContext) Logger(java.util.logging.Logger) ActionReport(org.glassfish.api.ActionReport) ClassLoaderHierarchy(org.glassfish.internal.api.ClassLoaderHierarchy) IOException(java.io.IOException) SAXParseException(org.xml.sax.SAXParseException) DeployCommandParameters(org.glassfish.api.deployment.DeployCommandParameters) HTMLActionReporter(com.sun.enterprise.v3.common.HTMLActionReporter) PreDestroy(org.glassfish.hk2.api.PreDestroy) FileArchive(com.sun.enterprise.deploy.shared.FileArchive) File(java.io.File)

Aggregations

ClassLoaderHierarchy (org.glassfish.internal.api.ClassLoaderHierarchy)4 FileArchive (com.sun.enterprise.deploy.shared.FileArchive)1 InputJarArchive (com.sun.enterprise.deployment.deploy.shared.InputJarArchive)1 HTMLActionReporter (com.sun.enterprise.v3.common.HTMLActionReporter)1 File (java.io.File)1 IOException (java.io.IOException)1 URLClassLoader (java.net.URLClassLoader)1 Logger (java.util.logging.Logger)1 ActionReport (org.glassfish.api.ActionReport)1 DeployCommandParameters (org.glassfish.api.deployment.DeployCommandParameters)1 ArchiveHandler (org.glassfish.api.deployment.archive.ArchiveHandler)1 PreDestroy (org.glassfish.hk2.api.PreDestroy)1 ServiceLocator (org.glassfish.hk2.api.ServiceLocator)1 DelegatingClassLoader (org.glassfish.internal.api.DelegatingClassLoader)1 ExtendedDeploymentContext (org.glassfish.internal.deployment.ExtendedDeploymentContext)1 SAXParseException (org.xml.sax.SAXParseException)1