Search in sources :

Example 1 with InputJarArchive

use of com.sun.enterprise.deployment.deploy.shared.InputJarArchive 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(DeploymentProperties.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(DeploymentProperties.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(DeploymentProperties.COMPATIBILITY, compatProp);
            }
        }
        if (System.getSecurityManager() != null) {
            // procee declared permissions
            earDeclaredPC = PermsArchiveDelegate.getDeclaredPermissions(SMGlobalPolicyUtil.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(Level.FINE))
                _logger.fine("added declaredPermissions to earlib: " + earDeclaredPC);
            addEEOrDeclaredPermissions(earLibCl, eeGarntsMap.get(SMGlobalPolicyUtil.CommponentType.ear), true);
            if (_logger.isLoggable(Level.FINE))
                _logger.fine("added all ee permissions to earlib: " + eeGarntsMap.get(SMGlobalPolicyUtil.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(SMGlobalPolicyUtil.CommponentType.ejb), true);
            if (_logger.isLoggable(Level.FINE))
                _logger.fine("ee permissions added: " + eeGarntsMap.get(SMGlobalPolicyUtil.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 : 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) DelegatingClassLoader(org.glassfish.internal.api.DelegatingClassLoader) XMLStreamException(javax.xml.stream.XMLStreamException) PrivilegedActionException(java.security.PrivilegedActionException) SAXParseException(org.xml.sax.SAXParseException) DeployCommandParameters(org.glassfish.api.deployment.DeployCommandParameters) URLClassLoader(java.net.URLClassLoader) PreDestroy(org.glassfish.hk2.api.PreDestroy)

Example 2 with InputJarArchive

use of com.sun.enterprise.deployment.deploy.shared.InputJarArchive in project Payara by payara.

the class ClientJarWriter method mergeContentsToClientArtifactsManager.

private File mergeContentsToClientArtifactsManager(final File generatedClientJARFile, final ClientArtifactsManager clientArtifactsManager) throws IOException, URISyntaxException {
    /*
         * First, move the existing JAR to another name so when the caller
         * creates the generated client JAR it does not overwrite the existing
         * file created by the app client deployer.
         */
    final File movedGeneratedFile = File.createTempFile(generatedClientJARFile.getName(), ".tmp", generatedClientJARFile.getParentFile());
    FileUtils.renameFile(generatedClientJARFile, movedGeneratedFile);
    final ReadableArchive existingGeneratedJAR = new InputJarArchive();
    existingGeneratedJAR.open(movedGeneratedFile.toURI());
    try {
        for (Enumeration e = existingGeneratedJAR.entries(); e.hasMoreElements(); ) {
            final String entryName = (String) e.nextElement();
            final URI entryURI = new URI("jar", movedGeneratedFile.toURI().toASCIIString() + "!/" + entryName, null);
            final Artifacts.FullAndPartURIs uris = new Artifacts.FullAndPartURIs(entryURI, entryName);
            clientArtifactsManager.add(uris);
        }
        /*
             * Handle the manifest explicitly because the Archive entries()
             * method consciously omits it.
             */
        final Artifacts.FullAndPartURIs manifestURIs = new Artifacts.FullAndPartURIs(new URI("jar", movedGeneratedFile.toURI().toASCIIString() + "!/" + JarFile.MANIFEST_NAME, null), JarFile.MANIFEST_NAME);
        clientArtifactsManager.add(manifestURIs);
        return movedGeneratedFile;
    } finally {
        existingGeneratedJAR.close();
    }
}
Also used : InputJarArchive(com.sun.enterprise.deployment.deploy.shared.InputJarArchive) Enumeration(java.util.Enumeration) ReadableArchive(org.glassfish.api.deployment.archive.ReadableArchive) JarFile(java.util.jar.JarFile) URI(java.net.URI)

Example 3 with InputJarArchive

use of com.sun.enterprise.deployment.deploy.shared.InputJarArchive in project Payara by payara.

the class AppClientScanner method doProcess.

/**
 * This scanner will scan the given main class for annotation processing.
 * The archiveFile and libJarFiles correspond to classpath.
 * @param archiveFile
 * @param desc
 * @param classLoader
 */
private void doProcess(ReadableArchive archive, ApplicationClientDescriptor desc, ClassLoader classLoader) throws IOException {
    if (AnnotationUtils.getLogger().isLoggable(Level.FINE)) {
        AnnotationUtils.getLogger().fine("archiveFile is " + archive.getURI().toASCIIString());
        AnnotationUtils.getLogger().fine("classLoader is " + classLoader);
    }
    // always add main class
    String mainClassName = desc.getMainClassName();
    addScanClassName(mainClassName);
    // add callback handle if it exist in appclient-client.xml
    String callbackHandler = desc.getCallbackHandler();
    if (callbackHandler != null && !callbackHandler.trim().equals("")) {
        addScanClassName(desc.getCallbackHandler());
    }
    GenericAnnotationDetector detector = new GenericAnnotationDetector(managedBeanAnnotations);
    if (detector.hasAnnotationInArchive(archive)) {
        if (archive instanceof FileArchive) {
            addScanDirectory(new File(archive.getURI()));
        } else if (archive instanceof InputJarArchive) {
            /*
                 * This is during deployment, so use the faster code path using
                 * the File object.
                 */
            URI uriToAdd = archive.getURI();
            addScanJar(scanJar(uriToAdd));
        } else if (archive instanceof MultiReadableArchive) {
            /*
                 * During app client launches, scan the developer's archive
                 * which is in slot #1, not the facade archive which is in
                 * slot #0.  Also, use URIs instead of File objects because
                 * during Java Web Start launches we don't have access to
                 * File objects.
                 */
            addScanURI(scanURI(((MultiReadableArchive) archive).getURI(1)));
        }
    }
    this.classLoader = classLoader;
    // = archive;
    this.archiveFile = null;
}
Also used : InputJarArchive(com.sun.enterprise.deployment.deploy.shared.InputJarArchive) GenericAnnotationDetector(org.glassfish.deployment.common.GenericAnnotationDetector) FileArchive(com.sun.enterprise.deploy.shared.FileArchive) MultiReadableArchive(com.sun.enterprise.deployment.deploy.shared.MultiReadableArchive) File(java.io.File) URI(java.net.URI)

Example 4 with InputJarArchive

use of com.sun.enterprise.deployment.deploy.shared.InputJarArchive 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

InputJarArchive (com.sun.enterprise.deployment.deploy.shared.InputJarArchive)4 FileArchive (com.sun.enterprise.deploy.shared.FileArchive)2 File (java.io.File)2 URI (java.net.URI)2 ActionReport (org.glassfish.api.ActionReport)2 DeployCommandParameters (org.glassfish.api.deployment.DeployCommandParameters)2 PreDestroy (org.glassfish.hk2.api.PreDestroy)2 ExtendedDeploymentContext (org.glassfish.internal.deployment.ExtendedDeploymentContext)2 SAXParseException (org.xml.sax.SAXParseException)2 AbstractArchiveHandler (com.sun.enterprise.deploy.shared.AbstractArchiveHandler)1 MultiReadableArchive (com.sun.enterprise.deployment.deploy.shared.MultiReadableArchive)1 DDPermissionsLoader (com.sun.enterprise.security.integration.DDPermissionsLoader)1 HTMLActionReporter (com.sun.enterprise.v3.common.HTMLActionReporter)1 IOException (java.io.IOException)1 URL (java.net.URL)1 URLClassLoader (java.net.URLClassLoader)1 PrivilegedAction (java.security.PrivilegedAction)1 PrivilegedActionException (java.security.PrivilegedActionException)1 Enumeration (java.util.Enumeration)1 JarFile (java.util.jar.JarFile)1