Search in sources :

Example 21 with ReadableArchive

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

the class GenericAnnotationDetector method scanArchive.

@Override
public void scanArchive(ReadableArchive archive) {
    try {
        int crFlags = ClassReader.SKIP_CODE | ClassReader.SKIP_DEBUG | ClassReader.SKIP_FRAMES;
        Enumeration<String> entries = archive.entries();
        while (entries.hasMoreElements()) {
            String entryName = entries.nextElement();
            if (entryName.endsWith(".class")) {
                // scan class files
                InputStream is = archive.getEntry(entryName);
                try {
                    ClassReader cr = new ClassReader(is);
                    cr.accept(this, crFlags);
                    if (found) {
                        return;
                    }
                } finally {
                    is.close();
                }
            } else if ((entryName.endsWith(".jar") || entryName.endsWith(".rar") || entryName.endsWith(".war") || entryName.endsWith(".ear")) && !entryName.contains("/")) {
                // scan class files inside top level jar
                try {
                    ReadableArchive jarSubArchive = null;
                    try {
                        jarSubArchive = archive.getSubArchive(entryName);
                        Enumeration<String> jarEntries = jarSubArchive.entries();
                        while (jarEntries.hasMoreElements()) {
                            String jarEntryName = jarEntries.nextElement();
                            if (jarEntryName.endsWith(".class")) {
                                try (InputStream is = jarSubArchive.getEntry(jarEntryName)) {
                                    ClassReader cr = new ClassReader(is);
                                    cr.accept(this, crFlags);
                                    if (found) {
                                        return;
                                    }
                                }
                            }
                        }
                    } finally {
                        jarSubArchive.close();
                    }
                } catch (Exception ioe) {
                    Object[] args = { entryName, ioe };
                    deplLogger.log(Level.WARNING, JAR_ENTRY_ERROR, args);
                }
            }
        }
    } catch (Exception e) {
        deplLogger.log(Level.WARNING, FAILED_ANNOTATION_SCAN, e);
    }
}
Also used : Enumeration(java.util.Enumeration) InputStream(java.io.InputStream) ClassReader(org.objectweb.asm.ClassReader) ReadableArchive(org.glassfish.api.deployment.archive.ReadableArchive) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException)

Example 22 with ReadableArchive

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

the class MTProvisionCommand method execute.

public void execute(AdminCommandContext context) {
    final ActionReport report = context.getActionReport();
    final Logger logger = context.getLogger();
    if (app == null) {
        report.setMessage("Application " + appname + " needs to be deployed first before provisioned to tenant");
        report.setActionExitCode(ActionReport.ExitCode.FAILURE);
        return;
    }
    ReadableArchive archive = null;
    DeployCommandParameters commandParams = app.getDeployParameters(appRef);
    commandParams.contextroot = contextroot;
    commandParams.target = DeploymentUtils.DAS_TARGET_NAME;
    commandParams.name = DeploymentUtils.getInternalNameForTenant(appname, tenant);
    commandParams.enabled = Boolean.TRUE;
    commandParams.origin = DeployCommandParameters.Origin.mt_provision;
    try {
        URI uri = new URI(app.getLocation());
        File file = new File(uri);
        if (!file.exists()) {
            throw new Exception(localStrings.getLocalString("fnf", "File not found", file.getAbsolutePath()));
        }
        archive = archiveFactory.openArchive(file);
        ExtendedDeploymentContext deploymentContext = deployment.getBuilder(logger, commandParams, report).source(archive).build();
        Properties appProps = deploymentContext.getAppProps();
        appProps.putAll(app.getDeployProperties());
        // app props so we also need to override that
        if (contextroot != null) {
            appProps.setProperty(ServerTags.CONTEXT_ROOT, contextroot);
        }
        deploymentContext.setModulePropsMap(app.getModulePropertiesMap());
        deploymentContext.setTenant(tenant, appname);
        expandCustomizationJar(deploymentContext.getTenantDir());
        deployment.deploy(deploymentContext);
        deployment.registerTenantWithAppInDomainXML(appname, deploymentContext);
    } catch (Throwable e) {
        report.setActionExitCode(ActionReport.ExitCode.FAILURE);
        report.setMessage(e.getMessage());
        report.setFailureCause(e);
    } finally {
        try {
            if (archive != null) {
                archive.close();
            }
        } catch (IOException e) {
        // ignore
        }
    }
}
Also used : DeployCommandParameters(org.glassfish.api.deployment.DeployCommandParameters) IOException(java.io.IOException) ActionReport(org.glassfish.api.ActionReport) Logger(java.util.logging.Logger) ReadableArchive(org.glassfish.api.deployment.archive.ReadableArchive) ExtendedDeploymentContext(org.glassfish.internal.deployment.ExtendedDeploymentContext) Properties(java.util.Properties) URI(java.net.URI) File(java.io.File) IOException(java.io.IOException)

Example 23 with ReadableArchive

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

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

the class ArchiveFactory method openArchive.

/**
 * Opens an existing archivist using the URL as the path.
 * The URL protocol will defines the type of desired archive
 * (jar, file, memory, etc...)
 * @param path url to the existing archive
 * @return the appropriate archive
 */
public ReadableArchive openArchive(URI path) throws IOException {
    String provider = path.getScheme();
    if (provider.equals("file")) {
        // this could be a jar file or a directory
        File f = new File(path);
        if (!f.exists())
            throw new FileNotFoundException(f.getPath());
        if (f.isFile()) {
            provider = "jar";
        }
    }
    try {
        ReadableArchive archive = habitat.getService(ReadableArchive.class, provider);
        if (archive == null) {
            deplLogger.log(Level.SEVERE, IMPLEMENTATION_NOT_FOUND, provider);
            throw new MalformedURLException("Protocol not supported : " + provider);
        }
        archive.open(path);
        return archive;
    } catch (MultiException e) {
        LogRecord lr = new LogRecord(Level.SEVERE, IMPLEMENTATION_NOT_FOUND);
        lr.setParameters(new Object[] { provider });
        lr.setThrown(e);
        deplLogger.log(lr);
        throw new MalformedURLException("Protocol not supported : " + provider);
    }
}
Also used : MalformedURLException(java.net.MalformedURLException) LogRecord(java.util.logging.LogRecord) FileNotFoundException(java.io.FileNotFoundException) ReadableArchive(org.glassfish.api.deployment.archive.ReadableArchive) File(java.io.File) MultiException(org.glassfish.hk2.api.MultiException)

Example 25 with ReadableArchive

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

the class ApplicationSignedJARManager method addJAR.

/**
 * Adds a JAR to the manager, returning the URI to the file to be
 * served.  This might be an auto-signed file if the original JAR is
 * unsigned.
 * @param uriWithinAnchor relative URI to the JAR within the anchor directory for the app
 * @param jarURI URI to the JAR file in the app to be served
 * @return URI to the JAR file to serve (either the original file or an auto-signed copy of the original)
 * @throws IOException
 */
public URI addJAR(final URI uriWithinAnchor, final URI absJARURI) throws IOException {
    /*
         * This method accomplishes three things:
         *
         * 1. Adds an entry to the map from relative URIs to the corresponding
         * static content for the JAR, creating an auto-signed content instance
         * if needed for an unsigned JAR.
         *
         * 2. Adds to the map from relative URI to aliases with which the JAR
         * is signed.
         *
         * 3. Adds to the map from alias to relative URIs signed with that alias.
         */
    // relative URI -> StaticContent
    Map.Entry<URI, StaticContent> result;
    final ReadableArchive arch = archiveFactory.openArchive(absJARURI);
    final Manifest archiveMF = arch.getManifest();
    if (archiveMF == null) {
        return null;
    }
    if (!isArchiveSigned(archiveMF)) {
        /*
             * The developer did not sign this JARs, so arrange for it to be
             * auto-signed.
             */
        result = autoSignedAppContentEntry(uriWithinAnchor, absJARURI);
        updateAliasToURIs(result.getKey(), autoSigningAlias);
        updateURIToAliases(result.getKey(), autoSigningAlias);
    } else {
        /*
             * The developer did sign this JAR, possibly with many certs.
             * For each cert add an association between the signing alias and
             * the JAR.
             */
        result = developerSignedAppContentEntry(absJARURI);
        Collection<String> aliasesUsedToSignJAR = new ArrayList<String>();
        for (Enumeration<String> entryNames = arch.entries("META-INF/"); entryNames.hasMoreElements(); ) {
            final String entryName = entryNames.nextElement();
            final String alias = signatureEntryName(entryName);
            updateURIToAliases(result.getKey(), alias);
        }
        addAliasToURIsEntry(result.getKey(), aliasesUsedToSignJAR);
    }
    arch.close();
    return result.getKey();
}
Also used : StaticContent(org.glassfish.appclient.server.core.jws.servedcontent.StaticContent) ArrayList(java.util.ArrayList) ReadableArchive(org.glassfish.api.deployment.archive.ReadableArchive) Manifest(java.util.jar.Manifest) HashMap(java.util.HashMap) AbstractMap(java.util.AbstractMap) Map(java.util.Map) URI(java.net.URI)

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