Search in sources :

Example 1 with CurrentBeforeParentClassLoader

use of com.sun.enterprise.loader.CurrentBeforeParentClassLoader in project Payara by payara.

the class AddLibraryCommand method execute.

@Override
public void execute(AdminCommandContext context) {
    final ActionReport report = context.getActionReport();
    final Logger logger = Logger.getLogger("org.glassfish.deployment.admin");
    File libDir = env.getLibPath();
    if (type.equals("ext")) {
        libDir = new File(libDir, "ext");
    } else if (type.equals("app")) {
        libDir = new File(libDir, "applibs");
    }
    // library directory
    try {
        List<UnprocessedChangeEvent> unprocessed = new ArrayList<UnprocessedChangeEvent>();
        StringBuilder msg = new StringBuilder();
        ClassLoader commonLoader = commonClsLdr.getCommonClassLoader();
        CurrentBeforeParentClassLoader loader = null;
        if (commonLoader instanceof CurrentBeforeParentClassLoader) {
            loader = (CurrentBeforeParentClassLoader) commonLoader;
        }
        for (File libraryFile : files) {
            if (libraryFile.exists()) {
                logger.log(Level.FINER, "ready to add new library");
                File result = DeploymentCommandUtils.renameUploadedFileOrCopyInPlaceFile(libDir, libraryFile, logger, env);
                // Applib is its own classloader which does not have a method to load files,
                if (loader != null && !type.equals("applibs")) {
                    loader.addURL(result.toURI().toURL());
                    logger.log(Level.FINE, "added library to classloader", loader);
                } else {
                    PropertyChangeEvent pe = new PropertyChangeEvent(libDir, "add-library", null, libraryFile);
                    UnprocessedChangeEvent uce = new UnprocessedChangeEvent(pe, "add-library");
                    unprocessed.add(uce);
                    logger.log(Level.FINER, "library not added to classloader");
                }
            } else {
                msg.append(localStrings.getLocalString("lfnf", "Library file not found", libraryFile.getAbsolutePath()));
            }
        }
        if (msg.length() > 0) {
            logger.log(Level.WARNING, msg.toString());
            report.setActionExitCode(ActionReport.ExitCode.WARNING);
            report.setMessage(msg.toString());
        }
        if (!unprocessed.isEmpty()) {
            // set the restart required flag
            UnprocessedChangeEvents uces = new UnprocessedChangeEvents(unprocessed);
            List<UnprocessedChangeEvents> ucesList = new ArrayList<UnprocessedChangeEvents>();
            ucesList.add(uces);
            ucl.unprocessedTransactedEvents(ucesList);
        }
        // touch the domain.xml so instances restart will synch
        // over the libraries.
        dxp.touch();
    } catch (Exception e) {
        report.setActionExitCode(ActionReport.ExitCode.FAILURE);
        report.setMessage(e.getMessage());
    }
}
Also used : UnprocessedChangeEvents(org.jvnet.hk2.config.UnprocessedChangeEvents) PropertyChangeEvent(java.beans.PropertyChangeEvent) UnprocessedChangeEvent(org.jvnet.hk2.config.UnprocessedChangeEvent) ArrayList(java.util.ArrayList) ActionReport(org.glassfish.api.ActionReport) Logger(java.util.logging.Logger) CurrentBeforeParentClassLoader(com.sun.enterprise.loader.CurrentBeforeParentClassLoader) CurrentBeforeParentClassLoader(com.sun.enterprise.loader.CurrentBeforeParentClassLoader) File(java.io.File)

Example 2 with CurrentBeforeParentClassLoader

use of com.sun.enterprise.loader.CurrentBeforeParentClassLoader in project Payara by payara.

the class CommonClassLoaderServiceImpl method createCommonClassLoader.

private void createCommonClassLoader() {
    List<File> cpElements = new ArrayList<File>();
    File domainDir = env.getDomainRoot();
    // I am forced to use System.getProperty, as there is no API that makes
    // the installRoot available. Sad, but true. Check dev forum on this.
    final String installRoot = System.getProperty(SystemPropertyConstants.INSTALL_ROOT_PROPERTY);
    // In case of embedded GF, we may not have an installRoot.
    if (installRoot != null) {
        File installDir = new File(installRoot);
        File installLibPath = new File(installDir, "lib");
        if (installLibPath.isDirectory()) {
            Collections.addAll(cpElements, installLibPath.listFiles(new CompiletimeJarFileFilter()));
        }
    } else {
        logger.logp(Level.WARNING, "CommonClassLoaderServiceImpl", "createCommonClassLoader", KernelLoggerInfo.systemPropertyNull, SystemPropertyConstants.INSTALL_ROOT_PROPERTY);
    }
    // NOI18N
    File domainClassesDir = new File(domainDir, "lib/classes/");
    if (domainClassesDir.exists()) {
        cpElements.add(domainClassesDir);
    }
    // NOI18N
    final File domainLib = new File(domainDir, "lib/");
    if (domainLib.isDirectory()) {
        Collections.addAll(cpElements, domainLib.listFiles(new JarFileFilter()));
    }
    // See issue https://glassfish.dev.java.net/issues/show_bug.cgi?id=13612
    // We no longer add derby jars to launcher class loader, we add them to common class loader instead.
    cpElements.addAll(findDerbyClient());
    cpElements.addAll(findH2Client());
    List<URL> urls = new ArrayList<>();
    StringBuilder cp = new StringBuilder();
    for (File f : cpElements) {
        try {
            urls.add(f.toURI().toURL());
            if (cp.length() > 0) {
                cp.append(File.pathSeparator);
            }
            cp.append(f.getAbsolutePath());
        } catch (MalformedURLException e) {
            logger.log(Level.WARNING, KernelLoggerInfo.invalidClassPathEntry, new Object[] { f, e });
        }
    }
    commonClassPath = cp.toString();
    if (!urls.isEmpty()) {
        // Skip creation of an unnecessary classloader in the hierarchy,
        // when all it would have done was to delegate up.
        commonClassLoader = new CurrentBeforeParentClassLoader(urls.toArray(new URL[urls.size()]), APIClassLoader);
        commonClassLoader.enableCurrentBeforeParent();
    } else {
        logger.logp(Level.FINE, "CommonClassLoaderManager", "Skipping creation of CommonClassLoader " + "as there are no libraries available", "urls = {0}", new Object[] { urls });
    }
}
Also used : MalformedURLException(java.net.MalformedURLException) CurrentBeforeParentClassLoader(com.sun.enterprise.loader.CurrentBeforeParentClassLoader) ArrayList(java.util.ArrayList) JarFile(java.util.jar.JarFile) File(java.io.File) URL(java.net.URL)

Aggregations

CurrentBeforeParentClassLoader (com.sun.enterprise.loader.CurrentBeforeParentClassLoader)2 File (java.io.File)2 ArrayList (java.util.ArrayList)2 PropertyChangeEvent (java.beans.PropertyChangeEvent)1 MalformedURLException (java.net.MalformedURLException)1 URL (java.net.URL)1 JarFile (java.util.jar.JarFile)1 Logger (java.util.logging.Logger)1 ActionReport (org.glassfish.api.ActionReport)1 UnprocessedChangeEvent (org.jvnet.hk2.config.UnprocessedChangeEvent)1 UnprocessedChangeEvents (org.jvnet.hk2.config.UnprocessedChangeEvents)1