Search in sources :

Example 1 with ApplicationHolder

use of org.glassfish.javaee.core.deployment.ApplicationHolder in project Payara by payara.

the class DeploymentImpl method scanForLibJars.

// This method creates and returns a List of BeanDeploymentArchives for each
// Weld enabled jar under /lib of an existing Archive.
private List<RootBeanDeploymentArchive> scanForLibJars(ReadableArchive archive, Collection<EjbDescriptor> ejbs, DeploymentContext context) {
    List<ReadableArchive> libJars = null;
    ApplicationHolder holder = context.getModuleMetaData(ApplicationHolder.class);
    if ((holder != null) && (holder.app != null)) {
        String libDir = holder.app.getLibraryDirectory();
        if (libDir != null && !libDir.isEmpty()) {
            Enumeration<String> entries = archive.entries(libDir);
            while (entries.hasMoreElements()) {
                final String entryName = entries.nextElement();
                // if a jar is directly in lib dir and not WEB-INF/lib/foo/bar.jar
                if (DOLUtils.isScanningAllowed(holder.app, entryName) && entryName.endsWith(JAR_SUFFIX) && entryName.indexOf(SEPARATOR_CHAR, libDir.length() + 1) == -1) {
                    try {
                        ReadableArchive jarInLib = archive.getSubArchive(entryName);
                        if (jarInLib != null && (jarInLib.exists(META_INF_BEANS_XML) || WeldUtils.isImplicitBeanArchive(context, jarInLib))) {
                            if (libJars == null) {
                                libJars = new ArrayList<>();
                            }
                            libJars.add(jarInLib);
                        }
                    } catch (IOException e) {
                        logger.log(FINE, CDILoggerInfo.EXCEPTION_SCANNING_JARS, new Object[] { e });
                    }
                }
            }
        }
    }
    if (libJars != null) {
        String libDir = holder.app.getLibraryDirectory();
        for (ReadableArchive libJarArchive : libJars) {
            createLibJarBda(libJarArchive, ejbs, libDir);
        }
    }
    return libJarRootBdas;
}
Also used : ApplicationHolder(org.glassfish.javaee.core.deployment.ApplicationHolder) IOException(java.io.IOException) ReadableArchive(org.glassfish.api.deployment.archive.ReadableArchive)

Example 2 with ApplicationHolder

use of org.glassfish.javaee.core.deployment.ApplicationHolder in project Payara by payara.

the class WeldDeployer method addWeldListenerToAllWars.

private void addWeldListenerToAllWars(DeploymentContext context) {
    // if there's at least 1 ejb jar then add the listener to all wars
    ApplicationHolder applicationHolder = context.getModuleMetaData(ApplicationHolder.class);
    if (applicationHolder != null) {
        if (applicationHolder.app.getBundleDescriptors(EjbBundleDescriptor.class).size() > 0) {
            Set<WebBundleDescriptor> webBundleDescriptors = applicationHolder.app.getBundleDescriptors(WebBundleDescriptor.class);
            for (WebBundleDescriptor oneWebBundleDescriptor : webBundleDescriptors) {
                // Add the Weld Listener if it does not already exist..
                // we have to do this regardless because the war may not be cdi-enabled but an ejb is.
                oneWebBundleDescriptor.addAppListenerDescriptorToFirst(new AppListenerDescriptorImpl(WELD_LISTENER));
                oneWebBundleDescriptor.addAppListenerDescriptor(new AppListenerDescriptorImpl(WeldTerminationListenerProxy.class.getName()));
            }
        }
    }
}
Also used : ApplicationHolder(org.glassfish.javaee.core.deployment.ApplicationHolder) AppListenerDescriptorImpl(org.glassfish.web.deployment.descriptor.AppListenerDescriptorImpl) WebBundleDescriptor(com.sun.enterprise.deployment.WebBundleDescriptor)

Example 3 with ApplicationHolder

use of org.glassfish.javaee.core.deployment.ApplicationHolder 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 4 with ApplicationHolder

use of org.glassfish.javaee.core.deployment.ApplicationHolder in project Payara by payara.

the class EarHandler method expand.

@Override
public void expand(ReadableArchive source, WritableArchive target, DeploymentContext context) throws IOException {
    // expand the top level first so we could read application.xml
    super.expand(source, target, context);
    ReadableArchive source2 = null;
    try {
        /*
             * We know that the expansion is into a directory, so we should know that target is a FileArchive which is also readable
             * as-is.
             */
        source2 = (FileArchive) target;
        ApplicationHolder holder = getApplicationHolder(source2, context, false);
        // now start to expand the sub modules
        for (ModuleDescriptor md : holder.app.getModules()) {
            String moduleUri = md.getArchiveUri();
            ReadableArchive subArchive = null;
            WritableArchive subTarget = null;
            ReadableArchive subArchiveToExpand = null;
            try {
                subArchive = source2.getSubArchive(moduleUri);
                if (subArchive == null) {
                    _logger.log(Level.WARNING, "Exception while locating sub archive: " + moduleUri);
                    continue;
                }
                // optimize performance by retrieving the archive handler
                // based on module type first
                ArchiveHandler subHandler = getArchiveHandlerFromModuleType(md.getModuleType());
                if (subHandler == null) {
                    subHandler = deployment.getArchiveHandler(subArchive);
                }
                context.getModuleArchiveHandlers().put(moduleUri, subHandler);
                if (subHandler != null) {
                    subTarget = target.createSubArchive(FileUtils.makeFriendlyFilenameExtension(moduleUri));
                    /*
                         * A subarchive might be packaged as a subdirectory (instead of a nested JAR) in an EAR. If so and if it has the same
                         * name as the directory into which we'll expand the submodule, make sure it is also of the correct archive type (i.e.,
                         * directory and not JAR) in which case we don't need to expand it because the developer already did so before
                         * packaging.
                         */
                    subArchiveToExpand = chooseSubArchiveToExpand(moduleUri, subTarget, subArchive, source2);
                    if (subArchiveToExpand != null) {
                        subHandler.expand(subArchiveToExpand, subTarget, context);
                    } else {
                        /*
                             * The target for expansion is the same URI as the subarchive. Make sure they are the same type; if so, we just skip the
                             * expansion. Otherwise, we would leave a JAR where the rest of deployment expects a subdirectory so throw an exception
                             * in that case.
                             */
                        if (!areSameStorageType(subTarget, subArchive)) {
                            final String msg = MessageFormat.format(_logger.getResourceBundle().getString("enterprise.deployment.backend.badSubModPackaging"), subArchive.getURI().toASCIIString(), subArchive.getClass().getName());
                            throw new RuntimeException(msg);
                        }
                    }
                // Keep the original submodule file because the app client deployer needs it.
                /*
                         * // delete the original module file File origSubArchiveFile = new File( target.getURI().getSchemeSpecificPart(),
                         * moduleUri); origSubArchiveFile.delete();
                         */
                }
            } catch (IOException ioe) {
                _logger.log(Level.FINE, "Exception while processing " + moduleUri, ioe);
            } finally {
                try {
                    if (subArchive != null) {
                        subArchive.close();
                    }
                    if (subTarget != null) {
                        subTarget.close();
                    }
                    if (subArchiveToExpand != null) {
                        subArchiveToExpand.close();
                    }
                } catch (IOException ioe) {
                // ignore
                }
            }
        }
    } finally {
        if (source2 != null) {
            source2.close();
        }
    }
}
Also used : ModuleDescriptor(org.glassfish.deployment.common.ModuleDescriptor) ArchiveHandler(org.glassfish.api.deployment.archive.ArchiveHandler) AbstractArchiveHandler(com.sun.enterprise.deploy.shared.AbstractArchiveHandler) ApplicationHolder(org.glassfish.javaee.core.deployment.ApplicationHolder) IOException(java.io.IOException) ReadableArchive(org.glassfish.api.deployment.archive.ReadableArchive) WritableArchive(org.glassfish.api.deployment.archive.WritableArchive)

Example 5 with ApplicationHolder

use of org.glassfish.javaee.core.deployment.ApplicationHolder in project Payara by payara.

the class EarHandler method getApplicationHolder.

private ApplicationHolder getApplicationHolder(ReadableArchive source, DeploymentContext context, boolean isDirectory) {
    ApplicationHolder holder = context.getModuleMetaData(ApplicationHolder.class);
    if (holder == null || holder.app == null) {
        try {
            DeployCommandParameters params = context.getCommandParameters(DeployCommandParameters.class);
            if (params != null && params.altdd != null) {
                source.addArchiveMetaData(DeploymentProperties.ALT_DD, params.altdd);
            }
            long start = System.currentTimeMillis();
            ApplicationArchivist archivist = habitat.getService(ApplicationArchivist.class);
            archivist.setAnnotationProcessingRequested(true);
            String xmlValidationLevel = dasConfig.getDeployXmlValidation();
            archivist.setXMLValidationLevel(xmlValidationLevel);
            if (xmlValidationLevel.equals("none")) {
                archivist.setXMLValidation(false);
            }
            holder = new ApplicationHolder(archivist.createApplication(source, isDirectory));
            _logger.log(FINE, "time to read application.xml {0}", System.currentTimeMillis() - start);
        } catch (IOException | SAXParseException e) {
            throw new RuntimeException(e);
        }
        context.addModuleMetaData(holder);
    }
    if (holder.app == null) {
        throw new RuntimeException(strings.get("errReadMetadata"));
    }
    return holder;
}
Also used : DeployCommandParameters(org.glassfish.api.deployment.DeployCommandParameters) ApplicationHolder(org.glassfish.javaee.core.deployment.ApplicationHolder) SAXParseException(org.xml.sax.SAXParseException) ApplicationArchivist(com.sun.enterprise.deployment.archivist.ApplicationArchivist) IOException(java.io.IOException)

Aggregations

ApplicationHolder (org.glassfish.javaee.core.deployment.ApplicationHolder)7 ReadableArchive (org.glassfish.api.deployment.archive.ReadableArchive)5 IOException (java.io.IOException)4 AbstractArchiveHandler (com.sun.enterprise.deploy.shared.AbstractArchiveHandler)2 DeployCommandParameters (org.glassfish.api.deployment.DeployCommandParameters)2 ArchiveHandler (org.glassfish.api.deployment.archive.ArchiveHandler)2 ArchiveType (org.glassfish.api.deployment.archive.ArchiveType)2 ModuleDescriptor (org.glassfish.deployment.common.ModuleDescriptor)2 SAXParseException (org.xml.sax.SAXParseException)2 WebBundleDescriptor (com.sun.enterprise.deployment.WebBundleDescriptor)1 ApplicationArchivist (com.sun.enterprise.deployment.archivist.ApplicationArchivist)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 URL (java.net.URL)1 URLClassLoader (java.net.URLClassLoader)1 PrivilegedAction (java.security.PrivilegedAction)1 PrivilegedActionException (java.security.PrivilegedActionException)1 XMLStreamException (javax.xml.stream.XMLStreamException)1