Search in sources :

Example 46 with ReadableArchive

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

the class UndeployedLaunchable method newUndeployedLaunchable.

static UndeployedLaunchable newUndeployedLaunchable(final ServiceLocator habitat, final ReadableArchive ra, final String callerSuppliedMainClassName, final String callerSuppliedAppName, final ClassLoader classLoader) throws IOException, SAXParseException, UserError {
    ArchivistFactory af = Util.getArchivistFactory();
    /*
         * Try letting the factory decide what type of archive this is.  That
         * will often allow an app client or an EAR archive to be detected
         * automatically.
         */
    Archivist archivist = af.getArchivist(ModuleType.CAR.toString(), classLoader);
    if (archivist == null) {
        throw new UserError(localStrings.get("appclient.invalidArchive", ra.getURI().toASCIIString()));
    }
    final ArchiveType moduleType = archivist.getModuleType();
    if (moduleType != null && moduleType.equals(DOLUtils.carType())) {
        return new UndeployedLaunchable(habitat, ra, (AppClientArchivist) archivist, callerSuppliedMainClassName);
    } else if (moduleType != null && moduleType.equals(DOLUtils.earType())) {
        /*
             * Locate the app client submodule that matches the main class name
             * or the app client name.
             */
        Application app = (Application) archivist.open(ra);
        for (ModuleDescriptor<BundleDescriptor> md : app.getModules()) {
            if (!md.getModuleType().equals(DOLUtils.carType())) {
                continue;
            }
            ApplicationClientDescriptor acd = (ApplicationClientDescriptor) md.getDescriptor();
            final String displayName = acd.getDisplayName();
            final String appName = acd.getModuleID();
            ArchiveFactory archiveFactory = Util.getArchiveFactory();
            ReadableArchive clientRA = archiveFactory.openArchive(ra.getURI().resolve(md.getArchiveUri()));
            /*
                 * Choose this nested app client if the caller-supplied name
                 * matches, or if the caller-supplied main class matches, or
                 * if neither was provided.  
                 */
            final boolean useThisClient = (displayName != null && displayName.equals(callerSuppliedAppName)) || (appName != null && appName.equals(callerSuppliedAppName)) || (callerSuppliedMainClassName != null && clientRA.exists(classToResource(callerSuppliedMainClassName)) || (callerSuppliedAppName == null && callerSuppliedMainClassName == null));
            if (useThisClient) {
                return new UndeployedLaunchable(habitat, clientRA, acd, callerSuppliedMainClassName);
            }
            clientRA.close();
        }
        throw new UserError(localStrings.get("appclient.noMatchingClientInEAR", ra.getURI(), callerSuppliedMainClassName, callerSuppliedAppName));
    } else {
        /*
             * There is a possibility that the user is trying to launch an
             * archive that is more than one type of archive: such as an EJB
             * but also an app client (because the manifest identifies a main
             * class, for example).
             *
             * Earlier the archivist factory might have returned the other type
             * of archivist - such as the EJB archivist.  Now see if the app
             * client archivist will work when selected directly.
             */
        archivist = af.getArchivist(DOLUtils.carType());
        /*
             * Try to open the archive as an app client archive just to see
             * if it works.
             */
        RootDeploymentDescriptor tempACD = archivist.open(ra);
        if (tempACD != null && tempACD instanceof ApplicationClientDescriptor) {
            /*
                 * Start with a fresh archivist - unopened - so we can request
                 * anno processing, etc. before opening it for real.
                 */
            archivist = af.getArchivist(DOLUtils.carType());
            return new UndeployedLaunchable(habitat, ra, (AppClientArchivist) archivist, callerSuppliedMainClassName);
        }
        throw new UserError(localStrings.get("appclient.unexpectedArchive", ra.getURI()));
    }
}
Also used : ArchiveFactory(com.sun.enterprise.deploy.shared.ArchiveFactory) AppClientArchivist(com.sun.enterprise.deployment.archivist.AppClientArchivist) Archivist(com.sun.enterprise.deployment.archivist.Archivist) ArchiveType(org.glassfish.api.deployment.archive.ArchiveType) ApplicationClientDescriptor(com.sun.enterprise.deployment.ApplicationClientDescriptor) AppClientArchivist(com.sun.enterprise.deployment.archivist.AppClientArchivist) ArchivistFactory(com.sun.enterprise.deployment.archivist.ArchivistFactory) ModuleDescriptor(org.glassfish.deployment.common.ModuleDescriptor) RootDeploymentDescriptor(org.glassfish.deployment.common.RootDeploymentDescriptor) ReadableArchive(org.glassfish.api.deployment.archive.ReadableArchive) Application(com.sun.enterprise.deployment.Application)

Example 47 with ReadableArchive

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

the class FacadeLaunchable method selectFacadeFromGroup.

private static FacadeLaunchable selectFacadeFromGroup(final ServiceLocator habitat, final URI groupFacadeURI, final ArchiveFactory af, final String groupURIs, final String callerSpecifiedMainClassName, final String callerSpecifiedAppClientName, final String anchorDir) throws IOException, BootException, URISyntaxException, SAXParseException, UserError {
    String[] archiveURIs = groupURIs.split(" ");
    /*
         * Search the app clients in the group in order, checking each for
         * a match on either the caller-specified main class or the caller-specified
         * client name.
         */
    if (archiveURIs.length == 0) {
        final String msg = MessageFormat.format(logger.getResourceBundle().getString("appclient.noClientsInGroup"), groupFacadeURI);
        throw new UserError(msg);
    }
    /*
         * Save the client names and main classes in case we need them in an
         * error to the user.
         */
    final List<String> knownClientNames = new ArrayList<String>();
    final List<String> knownMainClasses = new ArrayList<String>();
    for (String uriText : archiveURIs) {
        URI clientFacadeURI = groupFacadeURI.resolve(uriText);
        ReadableArchive clientFacadeRA = af.openArchive(clientFacadeURI);
        Manifest facadeMF = clientFacadeRA.getManifest();
        if (facadeMF == null) {
            throw new UserError(MessageFormat.format(logger.getResourceBundle().getString("appclient.noMFInFacade"), clientFacadeRA instanceof FileArchive ? 1 : 0, new File(clientFacadeRA.getURI().getPath()).getAbsolutePath()));
        }
        Attributes facadeMainAttrs = facadeMF.getMainAttributes();
        if (facadeMainAttrs == null) {
            throw new UserError(MessageFormat.format(logger.getResourceBundle().getString("appclient.MFMissingEntry"), new File(clientFacadeRA.getURI().getPath()).getAbsolutePath(), GLASSFISH_APPCLIENT));
        }
        final String gfAppClient = facadeMainAttrs.getValue(GLASSFISH_APPCLIENT);
        if (gfAppClient == null || gfAppClient.length() == 0) {
            throw new UserError(MessageFormat.format(logger.getResourceBundle().getString("appclient.MFMissingEntry"), new File(clientFacadeRA.getURI().getPath()).getAbsolutePath(), GLASSFISH_APPCLIENT));
        }
        URI clientURI = clientFacadeURI.resolve(gfAppClient);
        ReadableArchive clientRA = af.openArchive(clientURI);
        if (!clientRA.exists()) {
            throw new UserError(MessageFormat.format(logger.getResourceBundle().getString("appclient.missingClient"), new File(clientRA.getURI().getSchemeSpecificPart()).getAbsolutePath()));
        }
        AppClientArchivist facadeClientArchivist = getArchivist(habitat);
        MultiReadableArchive combinedRA = openCombinedReadableArchive(habitat, clientFacadeRA, clientRA);
        final ApplicationClientDescriptor facadeClientDescriptor = facadeClientArchivist.open(combinedRA);
        final String moduleID = Launchable.LaunchableUtil.moduleID(groupFacadeURI, clientURI, facadeClientDescriptor);
        final Manifest clientMF = clientRA.getManifest();
        if (clientMF == null) {
            throw new UserError(MessageFormat.format(logger.getResourceBundle().getString("appclient.noMFInFacade"), clientRA instanceof FileArchive ? 1 : 0, new File(clientRA.getURI().getSchemeSpecificPart()).getAbsolutePath()));
        }
        Attributes mainAttrs = clientMF.getMainAttributes();
        if (mainAttrs == null) {
            throw new UserError(MessageFormat.format(logger.getResourceBundle().getString("appclient.MFMissingEntry"), new File(clientFacadeRA.getURI().getPath()).getAbsolutePath(), Attributes.Name.MAIN_CLASS.toString()));
        }
        final String clientMainClassName = mainAttrs.getValue(Attributes.Name.MAIN_CLASS);
        if (clientMainClassName == null || clientMainClassName.length() == 0) {
            throw new UserError(MessageFormat.format(logger.getResourceBundle().getString("appclient.MFMissingEntry"), new File(clientFacadeRA.getURI().getPath()).getAbsolutePath(), Attributes.Name.MAIN_CLASS.toString()));
        }
        knownMainClasses.add(clientMainClassName);
        knownClientNames.add(moduleID);
        /*
             * Look for an entry corresponding to the
             * main class or app name the caller requested.  Treat as a%
             * special case if the user specifies no main class and no
             * app name - use the first app client present.  Also use the
             * first app client if there is only one present; warn if
             * the user specified a main class or a name but it does not
             * match the single app client that's present.
             */
        FacadeLaunchable facade = null;
        if (Launchable.LaunchableUtil.matchesAnyClass(clientRA, callerSpecifiedMainClassName)) {
            facade = new FacadeLaunchable(habitat, clientFacadeRA, facadeMainAttrs, clientRA, callerSpecifiedMainClassName, anchorDir);
            /*
                 * If the caller-specified class name does not match the
                 * Main-Class setting for this client archive then inform the user.
                 */
            if (!clientMainClassName.equals(callerSpecifiedMainClassName)) {
                logger.log(Level.INFO, MessageFormat.format(logger.getResourceBundle().getString("appclient.foundMainClassDiffFromManifest"), new Object[] { groupFacadeURI, moduleID, callerSpecifiedMainClassName, clientMainClassName }));
            }
        } else if (Launchable.LaunchableUtil.matchesName(moduleID, groupFacadeURI, facadeClientDescriptor, callerSpecifiedAppClientName)) {
            /*
                 * Get the main class name from the matching client JAR's manifest.
                 */
            facade = new FacadeLaunchable(habitat, clientFacadeRA, facadeMainAttrs, clientRA, clientMainClassName, anchorDir);
        } else if (archiveURIs.length == 1) {
            /*
                 * If only one client exists, use the main class recorded in
                 * the group facade unless the caller specified one.
                 */
            facade = new FacadeLaunchable(habitat, clientFacadeRA, facadeMainAttrs, clientRA, (callerSpecifiedMainClassName != null ? callerSpecifiedMainClassName : facadeMainAttrs.getValue(GLASSFISH_APPCLIENT_MAIN_CLASS)), anchorDir);
            /*
                 * If the user specified a main class or an app name then warn
                 * if that value does not match the one client we found - but
                 * go ahead an run it anyway.
                 */
            if ((callerSpecifiedMainClassName != null && !clientMainClassName.equals(callerSpecifiedMainClassName)) || (callerSpecifiedAppClientName != null && !Launchable.LaunchableUtil.matchesName(moduleID, groupFacadeURI, facadeClientDescriptor, callerSpecifiedAppClientName))) {
                logger.log(Level.WARNING, MessageFormat.format(logger.getResourceBundle().getString("appclient.singleNestedClientNoMatch"), new Object[] { groupFacadeURI, knownClientNames.toString(), knownMainClasses.toString(), callerSpecifiedMainClassName, callerSpecifiedAppClientName }));
            }
        }
        if (facade != null) {
            return facade;
        }
    }
    /*
         * No client facade matched the caller-provided selection (either
         * main class name or app client name), or there are multiple clients
         * but the caller did not specify either mainClass or name.
         * Yet we know we're working
         * with a group facade, so report the failure to find a matching
         * client as an error.
         */
    String msg;
    if ((callerSpecifiedAppClientName == null) && (callerSpecifiedMainClassName == null)) {
        final String format = logger.getResourceBundle().getString("appclient.multClientsNoChoice");
        msg = MessageFormat.format(format, knownMainClasses.toString(), knownClientNames.toString());
    } else {
        final String format = logger.getResourceBundle().getString("appclient.noMatchingClientInGroup");
        msg = MessageFormat.format(format, groupFacadeURI, callerSpecifiedMainClassName, callerSpecifiedAppClientName, knownMainClasses.toString(), knownClientNames.toString());
    }
    throw new UserError(msg);
}
Also used : ArrayList(java.util.ArrayList) Attributes(java.util.jar.Attributes) AppClientArchivist(com.sun.enterprise.deployment.archivist.AppClientArchivist) ACCAppClientArchivist(org.glassfish.appclient.common.ACCAppClientArchivist) MultiReadableArchive(com.sun.enterprise.deployment.deploy.shared.MultiReadableArchive) ApplicationClientDescriptor(com.sun.enterprise.deployment.ApplicationClientDescriptor) Manifest(java.util.jar.Manifest) URI(java.net.URI) FileArchive(com.sun.enterprise.deploy.shared.FileArchive) ReadableArchive(org.glassfish.api.deployment.archive.ReadableArchive) MultiReadableArchive(com.sun.enterprise.deployment.deploy.shared.MultiReadableArchive) File(java.io.File)

Example 48 with ReadableArchive

use of org.glassfish.api.deployment.archive.ReadableArchive 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 49 with ReadableArchive

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

the class EarHandler method initCompositeMetaData.

// do any necessary meta data initialization for composite handler
@Override
public void initCompositeMetaData(DeploymentContext context) {
    final ReadableArchive source = context.getSource();
    // populate ear level metadata
    getApplicationHolder(source, context, true);
    if (!context.getAppProps().containsKey(RuntimeTagNames.PAYARA_ENABLE_IMPLICIT_CDI)) {
        try {
            Boolean cdiEnabled = new GFApplicationXmlParser(source).isEnableImplicitCDI();
            if (cdiEnabled != null) {
                context.getAppProps().put(RuntimeTagNames.PAYARA_ENABLE_IMPLICIT_CDI, cdiEnabled.toString().toLowerCase());
            }
        } catch (XMLStreamException | IOException ex) {
            throw new RuntimeException(ex);
        }
    }
}
Also used : XMLStreamException(javax.xml.stream.XMLStreamException) IOException(java.io.IOException) ReadableArchive(org.glassfish.api.deployment.archive.ReadableArchive)

Example 50 with ReadableArchive

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

the class DOLUtils method getLibraryJarURIs.

/**
 * @param app
 * @param archive
 * @return an empty list if the archive does not have a parent
 * @throws Exception
 */
public static List<URI> getLibraryJarURIs(Application app, ReadableArchive archive) throws Exception {
    List<URL> libraryURLs = new ArrayList<>();
    List<URI> libraryURIs = new ArrayList<>();
    // add libraries referenced through manifest
    libraryURLs.addAll(DeploymentUtils.getManifestLibraries(archive));
    ReadableArchive parentArchive = archive.getParentArchive();
    if (parentArchive == null) {
        return Collections.emptyList();
    }
    File appRoot = new File(parentArchive.getURI());
    // add libraries jars inside application lib directory
    libraryURLs.addAll(ASClassLoaderUtil.getAppLibDirLibrariesAsList(appRoot, app.getLibraryDirectory(), null));
    for (URL url : libraryURLs) {
        libraryURIs.add(Util.toURI(url));
    }
    return libraryURIs;
}
Also used : ArrayList(java.util.ArrayList) ReadableArchive(org.glassfish.api.deployment.archive.ReadableArchive) URI(java.net.URI) ConfigurationDeploymentDescriptorFile(com.sun.enterprise.deployment.io.ConfigurationDeploymentDescriptorFile) File(java.io.File) URL(java.net.URL)

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