Search in sources :

Example 41 with ReadableArchive

use of org.glassfish.api.deployment.archive.ReadableArchive in project Payara by payara.

the class EarPersistenceArchivist method open.

/**
 * Reads persistence.xml from spec defined pu roots of an ear.
 * Spec defined pu roots are - (1)Non component jars in root of ear (2)jars in lib of ear
 */
@Override
public Object open(Archivist main, ReadableArchive earArchive, final RootDeploymentDescriptor descriptor) throws IOException, SAXParseException {
    if (deplLogger.isLoggable(Level.FINE)) {
        deplLogger.logp(Level.FINE, "EarArchivist", "readPersistenceDeploymentDescriptors", "archive = {0}", earArchive.getURI());
    }
    Map<String, ReadableArchive> probablePersitenceArchives = new HashMap<>();
    try {
        if (!(descriptor instanceof Application)) {
            return null;
        }
        final Application app = Application.class.cast(descriptor);
        // TODO: need to compute includeRoot, not hard-code it, in the next invocation. The flag should be set to true if operating in v2 compatibility mode false otherwise.
        // Check with Hong how to get hold of the flag here?
        EARBasedPersistenceHelper.addLibraryAndTopLevelCandidates(earArchive, app, true, /* includeRoot */
        probablePersitenceArchives);
        for (Map.Entry<String, ReadableArchive> pathToArchiveEntry : probablePersitenceArchives.entrySet()) {
            readPersistenceDeploymentDescriptor(main, pathToArchiveEntry.getValue(), pathToArchiveEntry.getKey(), descriptor);
        }
    } finally {
        for (Archive subArchive : probablePersitenceArchives.values()) {
            subArchive.close();
        }
    }
    return null;
}
Also used : ReadableArchive(org.glassfish.api.deployment.archive.ReadableArchive) Archive(org.glassfish.api.deployment.archive.Archive) HashMap(java.util.HashMap) ReadableArchive(org.glassfish.api.deployment.archive.ReadableArchive) Application(com.sun.enterprise.deployment.Application) Map(java.util.Map) HashMap(java.util.HashMap)

Example 42 with ReadableArchive

use of org.glassfish.api.deployment.archive.ReadableArchive in project Payara by payara.

the class DescriptorFactory method createApplicationDescriptor.

/**
 * Returns the parsed DOL object from archive
 *
 * @param archiveFile original archive file
 * @param destRootDir root destination directory where the application
 *        should be expanded under in case of archive deployment
 * @param parentCl parent classloader
 *
 * @return the parsed DOL object
 */
public ResultHolder createApplicationDescriptor(File archiveFile, File destRootDir, ClassLoader parentCl) throws IOException {
    ReadableArchive archive = null;
    Application application = null;
    try {
        Descriptor.setBoundsChecking(false);
        archive = archiveFactory.openArchive(archiveFile);
        ArchiveHandler archiveHandler = deployment.getArchiveHandler(archive);
        ActionReport dummyReport = new HTMLActionReporter();
        String appName = DeploymentUtils.getDefaultEEName(archiveFile.getName());
        DeployCommandParameters params = new DeployCommandParameters();
        params.name = appName;
        ExtendedDeploymentContext context = new DeploymentContextImpl(dummyReport, archive, params, env);
        context.setArchiveHandler(archiveHandler);
        if (!archiveFile.isDirectory()) {
            // expand archive
            File destDir = new File(destRootDir, appName);
            if (destDir.exists()) {
                FileUtils.whack(destDir);
            }
            destDir.mkdirs();
            archiveHandler.expand(archive, archiveFactory.createArchive(destDir), context);
            archive.close();
            archive = archiveFactory.openArchive(destDir);
            context.setSource(archive);
        }
        // issue 14564
        context.addTransientAppMetaData(ExtendedDeploymentContext.IS_TEMP_CLASSLOADER, Boolean.TRUE);
        String archiveType = context.getArchiveHandler().getArchiveType();
        ClassLoader cl = archiveHandler.getClassLoader(parentCl, context);
        Archivist archivist = archivistFactory.getArchivist(archiveType, cl);
        if (archivist == null) {
            throw new IOException("Cannot determine the Java EE module type for " + archive.getURI());
        }
        archivist.setAnnotationProcessingRequested(true);
        String xmlValidationLevel = dasConfig.getDeployXmlValidation();
        archivist.setXMLValidationLevel(xmlValidationLevel);
        if (xmlValidationLevel.equals("none")) {
            archivist.setXMLValidation(false);
        }
        archivist.setRuntimeXMLValidation(false);
        try {
            application = applicationFactory.openArchive(appName, archivist, archive, true);
        } catch (SAXParseException e) {
            throw new IOException(e);
        }
        if (application != null) {
            application.setClassLoader(cl);
            application.visit((ApplicationVisitor) new ApplicationValidator());
        }
    } finally {
        if (archive != null) {
            archive.close();
        }
        // We need to reset it after descriptor building
        Descriptor.setBoundsChecking(true);
    }
    ResultHolder result = new ResultHolder();
    result.application = application;
    result.archive = archive;
    return result;
}
Also used : ArchiveHandler(org.glassfish.api.deployment.archive.ArchiveHandler) Archivist(com.sun.enterprise.deployment.archivist.Archivist) IOException(java.io.IOException) ActionReport(org.glassfish.api.ActionReport) ExtendedDeploymentContext(org.glassfish.internal.deployment.ExtendedDeploymentContext) DeploymentContextImpl(org.glassfish.deployment.common.DeploymentContextImpl) DeployCommandParameters(org.glassfish.api.deployment.DeployCommandParameters) ApplicationValidator(com.sun.enterprise.deployment.util.ApplicationValidator) SAXParseException(org.xml.sax.SAXParseException) HTMLActionReporter(com.sun.enterprise.v3.common.HTMLActionReporter) ReadableArchive(org.glassfish.api.deployment.archive.ReadableArchive) Application(com.sun.enterprise.deployment.Application) File(java.io.File)

Example 43 with ReadableArchive

use of org.glassfish.api.deployment.archive.ReadableArchive in project Payara by payara.

the class EarDeployer method subContext.

private ExtendedDeploymentContext subContext(final Application application, final DeploymentContext context, final String moduleUri) {
    ExtendedDeploymentContext moduleContext = ((ExtendedDeploymentContext) context).getModuleDeploymentContexts().get(moduleUri);
    if (moduleContext != null) {
        return moduleContext;
    }
    final ReadableArchive subArchive;
    try {
        subArchive = context.getSource().getSubArchive(moduleUri);
        subArchive.setParentArchive(context.getSource());
    } catch (IOException ioe) {
        deplLogger.log(Level.WARNING, ERROR_OCCURRED, ioe);
        return null;
    }
    final Properties moduleProps = getModuleProps(context, moduleUri);
    ActionReport subReport = context.getActionReport().addSubActionsReport();
    moduleContext = new DeploymentContextImpl(subReport, context.getSource(), context.getCommandParameters(OpsParams.class), env) {

        @Override
        public ClassLoader getClassLoader() {
            try {
                if (context.getClassLoader() == null) {
                    return null;
                }
                EarClassLoader appCl = EarClassLoader.class.cast(context.getClassLoader());
                if (((ExtendedDeploymentContext) context).getPhase() == Phase.PREPARE) {
                    return appCl;
                } else {
                    return appCl.getModuleClassLoader(moduleUri);
                }
            } catch (ClassCastException e) {
                return context.getClassLoader();
            }
        }

        @Override
        public ClassLoader getFinalClassLoader() {
            try {
                EarClassLoader finalEarCL = (EarClassLoader) context.getFinalClassLoader();
                return finalEarCL.getModuleClassLoader(moduleUri);
            } catch (ClassCastException e) {
                return context.getClassLoader();
            }
        }

        @Override
        public ReadableArchive getSource() {
            return subArchive;
        }

        @Override
        public Properties getAppProps() {
            return context.getAppProps();
        }

        @Override
        public <U extends OpsParams> U getCommandParameters(Class<U> commandParametersType) {
            return context.getCommandParameters(commandParametersType);
        }

        @Override
        public void addTransientAppMetaData(String metaDataKey, Object metaData) {
            context.addTransientAppMetaData(metaDataKey, metaData);
        }

        @Override
        public <T> T getTransientAppMetaData(String metaDataKey, Class<T> metadataType) {
            return context.getTransientAppMetaData(metaDataKey, metadataType);
        }

        @Override
        public Properties getModuleProps() {
            return moduleProps;
        }

        @Override
        public ReadableArchive getOriginalSource() {
            try {
                File appRoot = context.getSourceDir();
                File origModuleFile = new File(appRoot, moduleUri);
                return archiveFactory.openArchive(origModuleFile);
            } catch (IOException ioe) {
                return null;
            }
        }

        @Override
        public File getScratchDir(String subDirName) {
            String modulePortion = Util.getURIName(getSource().getURI());
            return (new File(super.getScratchDir(subDirName), modulePortion));
        }

        @Override
        public <T> T getModuleMetaData(Class<T> metadataType) {
            try {
                return metadataType.cast(application.getModuleByUri(moduleUri));
            } catch (Exception e) {
                // let's first try the extensions mechanisms...
                if (RootDeploymentDescriptor.class.isAssignableFrom(metadataType)) {
                    for (RootDeploymentDescriptor extension : application.getModuleByUri(moduleUri).getExtensionsDescriptors((Class<RootDeploymentDescriptor>) metadataType)) {
                        // we assume there can only be one type of
                        if (extension != null) {
                            try {
                                return metadataType.cast(extension);
                            } catch (Exception e1) {
                            // next one...
                            }
                        }
                    }
                }
                return context.getModuleMetaData(metadataType);
            }
        }
    };
    ((ExtendedDeploymentContext) context).getModuleDeploymentContexts().put(moduleUri, moduleContext);
    moduleContext.setParentContext((ExtendedDeploymentContext) context);
    moduleContext.setModuleUri(moduleUri);
    ArchiveHandler subHandler = context.getModuleArchiveHandlers().get(moduleUri);
    moduleContext.setArchiveHandler(subHandler);
    return moduleContext;
}
Also used : ArchiveHandler(org.glassfish.api.deployment.archive.ArchiveHandler) IOException(java.io.IOException) ExtendedDeploymentContext(org.glassfish.internal.deployment.ExtendedDeploymentContext) ActionReport(org.glassfish.api.ActionReport) IOException(java.io.IOException) ReadableArchive(org.glassfish.api.deployment.archive.ReadableArchive) File(java.io.File)

Example 44 with ReadableArchive

use of org.glassfish.api.deployment.archive.ReadableArchive in project Payara by payara.

the class AppClientDeployerHelper method JAROfExpandedSubmodule.

/**
 * If the specified URI is for an expanded submodule, makes a copy of
 * the submodule as a JAR and returns the URI for the copy.
 * @param classPathElement
 * @return URI to the safe copy of the submodule, relative to the top level
 * if the classPathElement is for a submodule; null otherwise
 */
File JAROfExpandedSubmodule(final URI candidateSubmoduleURI) throws IOException {
    ReadableArchive source = new FileArchive();
    source.open(dc().getSource().getParentArchive().getURI().resolve(expandedDirURI(candidateSubmoduleURI)));
    OutputJarArchive target = new OutputJarArchive();
    target.create(dc().getScratchDir("xml").toURI().resolve(candidateSubmoduleURI));
    /*
         * Copy the manifest explicitly because the ReadableArchive
         * entries() method omits it.
         */
    Manifest mf = source.getManifest();
    OutputStream os = target.putNextEntry(JarFile.MANIFEST_NAME);
    mf.write(os);
    target.closeEntry();
    copyArchive(source, target, Collections.EMPTY_SET);
    target.close();
    return new File(target.getURI());
}
Also used : OutputJarArchive(com.sun.enterprise.deployment.deploy.shared.OutputJarArchive) OutputStream(java.io.OutputStream) FileArchive(com.sun.enterprise.deploy.shared.FileArchive) ReadableArchive(org.glassfish.api.deployment.archive.ReadableArchive) Manifest(java.util.jar.Manifest) JarFile(java.util.jar.JarFile) File(java.io.File)

Example 45 with ReadableArchive

use of org.glassfish.api.deployment.archive.ReadableArchive in project Payara by payara.

the class AppClientDeployerHelper method generateAppClientFacade.

protected final void generateAppClientFacade() throws IOException, URISyntaxException {
    OutputJarArchive facadeArchive = new OutputJarArchive();
    /*
         * Make sure the directory subtree to contain the facade exists.  If the
         * client URI within the EAR contains a directory then that directory
         * probably does not exist in the generated dir for this app...not yet
         * anyway...it is about to exist.
         */
    final File facadeFile = new File(facadeServerURI(dc));
    if (!facadeFile.getParentFile().exists()) {
        if (!facadeFile.getParentFile().mkdirs()) {
            final String msg = logger.getResourceBundle().getString("enterprise.deployment.appclient.errormkdirs");
            throw new IOException(MessageFormat.format(msg, facadeFile.getAbsolutePath()));
        }
    }
    facadeArchive.create(facadeServerURI(dc));
    ReadableArchive source = dc.getSource();
    Manifest sourceManifest = source.getManifest();
    if (sourceManifest == null) {
        final String msg = logger.getResourceBundle().getString("enterprise.deployment.appclient.noManifest");
        throw new IOException(MessageFormat.format(msg, source.getURI().toASCIIString()));
    }
    Manifest facadeManifest = facadeArchive.getManifest();
    initGeneratedManifest(sourceManifest, facadeManifest, facadeClassPath(), PUScanTargets(), application);
    /*
         * If the developer's app client JAR contains a splash screen, copy
         * it from the original JAR to the facade so the Java launcher can
         * display it when the app client is launched.
         */
    final Attributes srcMainAttrs = sourceManifest.getMainAttributes();
    if (srcMainAttrs == null) {
        final String msg = logger.getResourceBundle().getString("enterprise.deployment.appclient.noMainAttrs");
        throw new IOException(MessageFormat.format(msg, source.getURI().toASCIIString()));
    }
    String splash = srcMainAttrs.getValue(AppClientDeployer.SPLASH_SCREEN_IMAGE);
    if (splash != null) {
        copy(source, facadeArchive, splash);
    }
    /*
         * Write the manifest to the facade.
         */
    OutputStream os = facadeArchive.putNextEntry(JarFile.MANIFEST_NAME);
    facadeManifest.write(os);
    facadeArchive.closeEntry();
    /*
         * Write the updated descriptors to the facade.
         */
    writeUpdatedDescriptors(source, facadeArchive, appClientDesc);
    /*
         * Because of how persistence units are discovered and added to the
         * app client DOL object when the archivist reads the descriptor file,
         * add any META-INF/persistence.xml file from the developer's client
         * to the client facade.  (The generated descriptor and the
         * persistence.xml files need to be in the same archive.)
         */
    copyPersistenceUnitXML(source, facadeArchive);
    copyMainClass(facadeArchive);
    addTopLevelContentToClientFacade(facadeArchive);
    facadeArchive.close();
}
Also used : OutputJarArchive(com.sun.enterprise.deployment.deploy.shared.OutputJarArchive) OutputStream(java.io.OutputStream) Attributes(java.util.jar.Attributes) IOException(java.io.IOException) ReadableArchive(org.glassfish.api.deployment.archive.ReadableArchive) Manifest(java.util.jar.Manifest) JarFile(java.util.jar.JarFile) File(java.io.File)

Aggregations

ReadableArchive (org.glassfish.api.deployment.archive.ReadableArchive)97 IOException (java.io.IOException)46 File (java.io.File)28 URI (java.net.URI)18 ActionReport (org.glassfish.api.ActionReport)17 ExtendedDeploymentContext (org.glassfish.internal.deployment.ExtendedDeploymentContext)14 DeployCommandParameters (org.glassfish.api.deployment.DeployCommandParameters)12 WritableArchive (org.glassfish.api.deployment.archive.WritableArchive)12 Application (com.sun.enterprise.deployment.Application)10 JarFile (java.util.jar.JarFile)10 ArchiveHandler (org.glassfish.api.deployment.archive.ArchiveHandler)10 ApplicationInfo (org.glassfish.internal.data.ApplicationInfo)10 ArrayList (java.util.ArrayList)9 ModuleDescriptor (org.glassfish.deployment.common.ModuleDescriptor)9 ConfigurationDeploymentDescriptorFile (com.sun.enterprise.deployment.io.ConfigurationDeploymentDescriptorFile)8 Logger (java.util.logging.Logger)8 DeploymentDescriptorFile (com.sun.enterprise.deployment.io.DeploymentDescriptorFile)7 Manifest (java.util.jar.Manifest)7 HashSet (java.util.HashSet)6 HashMap (java.util.HashMap)5