Example usage for java.util.jar Manifest Manifest

List of usage examples for java.util.jar Manifest Manifest

Introduction

In this page you can find the example usage for java.util.jar Manifest Manifest.

Prototype

public Manifest(Manifest man) 

Source Link

Document

Constructs a new Manifest that is a copy of the specified Manifest.

Usage

From source file:org.apache.sling.tooling.support.install.impl.InstallServlet.java

private void installBasedOnDirectory(HttpServletResponse resp, final File dir)
        throws FileNotFoundException, IOException {

    InstallationResult result = null;//w  w  w  .  ja  v a  2s  . c  om

    if (dir.exists() && dir.isDirectory()) {
        logger.info("Checking dir {} for bundle install", dir);
        final File manifestFile = new File(dir, JarFile.MANIFEST_NAME);
        if (manifestFile.exists()) {
            FileInputStream fis = null;
            try {
                fis = new FileInputStream(manifestFile);
                final Manifest mf = new Manifest(fis);

                final String symbolicName = mf.getMainAttributes().getValue(Constants.BUNDLE_SYMBOLICNAME);
                if (symbolicName != null) {
                    // search bundle
                    Bundle found = getBundle(symbolicName);

                    final File tempFile = File.createTempFile(dir.getName(), "bundle");
                    try {
                        createJar(dir, tempFile, mf);

                        final InputStream in = new FileInputStream(tempFile);
                        try {
                            String location = dir.getAbsolutePath();

                            installOrUpdateBundle(found, in, location);
                            result = new InstallationResult(true, null);
                            resp.setStatus(200);
                            result.render(resp.getWriter());
                            return;
                        } catch (final BundleException be) {
                            logAndWriteError("Unable to install/update bundle from dir " + dir, be, resp);
                        }
                    } finally {
                        tempFile.delete();
                    }
                } else {
                    logAndWriteError("Manifest in " + dir + " does not have a symbolic name", resp);
                }
            } finally {
                IOUtils.closeQuietly(fis);
            }
        } else {
            result = new InstallationResult(false, "Dir " + dir + " does not have a manifest");
            logAndWriteError("Dir " + dir + " does not have a manifest", resp);
        }
    } else {
        result = new InstallationResult(false, "Dir " + dir + " does not exist");
        logAndWriteError("Dir " + dir + " does not exist", resp);
    }
}

From source file:org.xwiki.extension.repository.internal.core.DefaultCoreExtensionScanner.java

@Override
public DefaultCoreExtension loadEnvironmentExtension(DefaultCoreExtensionRepository repository) {
    InputStream is = this.environment.getResourceAsStream("/META-INF/MANIFEST.MF");

    if (is != null) {
        // Probably running in an application server
        try {//from   w ww . j av a2s.co m
            Manifest manifest = new Manifest(is);

            Attributes manifestAttributes = manifest.getMainAttributes();
            String extensionId = manifestAttributes.getValue(MavenUtils.MF_EXTENSION_ID);

            if (extensionId != null) {
                String[] mavenId = StringUtils.split(extensionId, ':');

                String descriptorPath = String.format("/META-INF/maven/%s/%s/pom.xml", mavenId[0], mavenId[1]);

                URL descriptorURL = this.environment.getResource(descriptorPath);

                if (descriptorURL != null) {
                    try {
                        DefaultCoreExtension coreExtension = parseMavenPom(descriptorURL, repository);

                        return coreExtension;
                    } catch (Exception e) {
                        this.logger.warn("Failed to parse extension descriptor [{}]", descriptorURL, e);
                    }
                } else {
                    this.logger.warn("Can't find resource file [{}] which contains distribution informations.",
                            descriptorPath);
                }
            }
        } catch (IOException e) {
            this.logger.error("Failed to parse environment META-INF/MANIFEST.MF file", e);
        } finally {
            IOUtils.closeQuietly(is);
        }
    }

    this.logger.debug("No declared environmennt extension");

    return null;
}

From source file:org.eclipse.gemini.blueprint.test.FilteringProbeBuilder.java

private void appendManifest(TinyBundle bundle) throws IOException {

    DefaultResourceLoader loader = new DefaultResourceLoader();
    Manifest manifest = new Manifest(loader.getResource(manifestLocation).getInputStream());
    Attributes attr = manifest.getMainAttributes();
    for (Object key : attr.keySet()) {
        String k = key.toString();
        String v = attr.getValue(k);

        // append optional import for org.ops4j.pax.exam
        if (k.equalsIgnoreCase(Constants.IMPORT_PACKAGE)) {
            if (StringUtils.hasText(v)) {
                v = v + ",";
            }//  w  w w  . j  a v a 2  s.c  om
        }
        v = v + "org.ops4j.pax.exam;resolution:=optional";
        bundle.set(k, v);
    }
}

From source file:com.izforge.izpack.installer.unpacker.UnpackerBase.java

private void logIntro() {
    final String startMessage = messages.get("installer.started");
    char[] chars = new char[startMessage.length()];
    Arrays.fill(chars, '=');
    logger.info(new String(chars));
    logger.info(startMessage);/*from   ww  w .j  av  a  2s.c  o  m*/

    URLClassLoader cl = (URLClassLoader) getClass().getClassLoader();
    InputStream is = null;
    try {
        URL url = cl.findResource("META-INF/MANIFEST.MF");
        is = url.openStream();
        Manifest manifest = new Manifest(is);
        Attributes attr = manifest.getMainAttributes();
        logger.info(messages.get("installer.version", attr.getValue("Created-By")));
    } catch (IOException e) {
        logger.log(Level.WARNING, "IzPack version not found in manifest", e);
    } finally {
        IOUtils.closeQuietly(is);
    }

    logger.info(messages.get("installer.platform", matcher.getCurrentPlatform()));
}

From source file:org.jahia.services.templates.JahiaTemplatesPackageHandler.java

/**
 * Extract data from the MANIFEST.MF file and builds the
 * JahiaTemplatesPackage object//  w  ww. ja v  a2 s.c om
 *
 * @param file the package file to read
 */
private static JahiaTemplatesPackage read(File file) {

    JahiaTemplatesPackage templatePackage = new JahiaTemplatesPackage();
    // extract data from the META-INF/MANIFEST.MF file
    try {
        File manifestFile = new File(file, "META-INF/MANIFEST.MF");
        if (manifestFile.exists()) {
            InputStream manifestStream = new BufferedInputStream(new FileInputStream(manifestFile), 1024);
            Manifest manifest = new Manifest(manifestStream);
            IOUtils.closeQuietly(manifestStream);
            String packageName = (String) manifest.getMainAttributes().get(new Attributes.Name("package-name"));
            String rootFolder = (String) manifest.getMainAttributes().get(new Attributes.Name("root-folder"));
            String moduleType = (String) manifest.getMainAttributes().get(new Attributes.Name("module-type"));
            String implementationVersionStr = (String) manifest.getMainAttributes()
                    .get(new Attributes.Name("Implementation-Version"));
            if (packageName == null) {
                packageName = file.getName();
            }
            if (rootFolder == null) {
                rootFolder = file.getName();
            }

            String depends = (String) manifest.getMainAttributes().get(new Attributes.Name("depends"));
            if (depends != null) {
                String[] dependencies = depends.split(",");
                for (String dependency : dependencies) {
                    templatePackage.setDepends(dependency.trim());
                }
            }

            String definitions = (String) manifest.getMainAttributes().get(new Attributes.Name("definitions"));
            if (definitions != null) {
                String[] defs = definitions.split(",");
                for (String defFile : defs) {
                    templatePackage.getDefinitionsFiles().add(defFile.trim());
                }
            }

            String imports = (String) manifest.getMainAttributes().get(new Attributes.Name("initial-imports"));
            if (imports != null) {
                String[] importFiles = imports.split(",");
                for (String imp : importFiles) {
                    templatePackage.addInitialImport(imp.trim());
                }
            }

            String resourceBundle = (String) manifest.getMainAttributes()
                    .get(new Attributes.Name("resource-bundle"));
            if (StringUtils.isNotBlank(resourceBundle)) {
                templatePackage.setResourceBundleName(resourceBundle.trim());
            }

            templatePackage.setName(packageName);
            templatePackage.setRootFolder(rootFolder);
            templatePackage.setModuleType(moduleType);
            if (implementationVersionStr != null) {
                templatePackage.setVersion(new Version(implementationVersionStr));
            }
        }
    } catch (IOException ioe) {
        logger.warn("Failed extracting module package data from META-INF/MANIFEST.MF file for package " + file,
                ioe);
    }

    return templatePackage;
}

From source file:org.docx4j.jaxb.Context.java

public static void searchManifestsForJAXBImplementationInfo(ClassLoader loader) {
    Enumeration resEnum;//  w  ww. j a  va 2  s.  co m
    try {
        resEnum = loader.getResources(JarFile.MANIFEST_NAME);
        while (resEnum.hasMoreElements()) {
            InputStream is = null;
            try {
                URL url = (URL) resEnum.nextElement();
                //                   System.out.println("\n\n" + url);
                is = url.openStream();
                if (is != null) {
                    Manifest manifest = new Manifest(is);

                    Attributes mainAttribs = manifest.getMainAttributes();
                    String impTitle = mainAttribs.getValue("Implementation-Title");
                    if (impTitle != null && impTitle.contains("JAXB Reference Implementation")
                            || impTitle.contains("org.eclipse.persistence")) {

                        log.info("\n" + url);
                        for (Object key2 : mainAttribs.keySet()) {

                            log.info(key2 + " : " + mainAttribs.getValue((java.util.jar.Attributes.Name) key2));
                        }
                    }

                    // In 2.1.3, it is in here
                    for (String key : manifest.getEntries().keySet()) {
                        //System.out.println(key);                       
                        if (key.equals("com.sun.xml.bind.v2.runtime")) {
                            log.info("Found JAXB reference implementation in " + url);
                            mainAttribs = manifest.getAttributes((String) key);

                            for (Object key2 : mainAttribs.keySet()) {
                                log.info(key2 + " : "
                                        + mainAttribs.getValue((java.util.jar.Attributes.Name) key2));
                            }
                        }
                    }

                }
            } catch (Exception e) {
                // Silently ignore 
                //                  log.error(e.getMessage(), e);
            } finally {
                IOUtils.closeQuietly(is);
            }
        }
    } catch (IOException e1) {
        // Silently ignore 
        //           log.error(e1);
    }

}

From source file:org.rhq.plugins.jbossas.util.FileContentDelegate.java

/**
 * Retrieves the SHA256 for a deployed application.
 * 1) If the app is exploded then return RHQ-Sha256 manifest attribute.
 *   1.1) If RHQ-Sha256 is missing then compute it, save it and return the result.
 * 2) If the app is an archive then compute SHA256 on fly and return it.
 *
 * @param deploymentFile deployment file
 * @return//from www  .  j  a v a 2s  .c o m
 */
public String getSHA(File deploymentFile) {
    String sha = null;
    try {
        if (deploymentFile.isDirectory()) {
            File manifestFile = new File(deploymentFile.getAbsolutePath(), MANIFEST_RELATIVE_PATH);
            if (manifestFile.exists()) {
                InputStream manifestStream = new FileInputStream(manifestFile);
                Manifest manifest = null;
                try {
                    manifest = new Manifest(manifestStream);
                    sha = manifest.getMainAttributes().getValue(RHQ_SHA_256);
                } finally {
                    manifestStream.close();
                }
            }

            if (sha == null || sha.trim().isEmpty()) {
                sha = computeAndSaveSHA(deploymentFile);
            }
        } else {
            sha = new MessageDigestGenerator(MessageDigestGenerator.SHA_256).calcDigestString(deploymentFile);
        }
    } catch (IOException ex) {
        throw new RuntimeException("Problem calculating digest of package [" + deploymentFile.getPath() + "].",
                ex);
    }

    return sha;
}

From source file:org.commonjava.maven.ext.manip.io.PomIO.java

/**
 * Retrieves the SHA this was built with.
 *
 * @return the GIT sha of this codebase.
 * @throws ManipulationException if an error occurs.
 *//*from  ww  w.j a v a  2 s  .  c o  m*/
public static String getManifestInformation() throws ManipulationException {
    String result = "";
    try {
        final Enumeration<URL> resources = PomIO.class.getClassLoader().getResources("META-INF/MANIFEST.MF");

        while (resources.hasMoreElements()) {
            final URL jarUrl = resources.nextElement();

            if (jarUrl.getFile().contains("pom-manipulation-")) {
                final Manifest manifest = new Manifest(jarUrl.openStream());

                result = manifest.getMainAttributes().getValue("Implementation-Version");
                result += " ( SHA: " + manifest.getMainAttributes().getValue("Scm-Revision") + " ) ";
                break;
            }
        }
    } catch (final IOException e) {
        throw new ManipulationException("Error retrieving information from manifest", e);
    }

    return result;
}

From source file:net.sourceforge.jweb.maven.mojo.OneExecutablejarMojo.java

private Manifest getManifest() throws IOException {
    // Copy the template's boot-manifest.mf file
    ZipInputStream zipIS = openOnejarTemplateArchive();
    Manifest manifest = new Manifest(getFileBytes(zipIS, "boot-manifest.mf"));
    IOUtils.closeQuietly(zipIS);/*from w  w  w  .j a v a 2 s . c  o  m*/

    // If the client has specified a mainClass argument, add the proper entry to the manifest
    if (mainClass != null) {
        manifest.getMainAttributes().putValue("One-Jar-Main-Class", mainClass);
    }

    // If the client has specified an implementationVersion argument, add it also
    // (It's required and defaulted, so this always executes...)
    //
    // TODO: The format of this manifest entry is not "hard and fast".  Some specs call for "implementationVersion",
    // some for "implemenation-version", and others use various capitalizations of these two.  It's likely that a
    // better solution then this "brute-force" bit here is to allow clients to configure these entries from the
    // Maven POM.
    if (implementationVersion != null) {
        manifest.getMainAttributes().putValue("ImplementationVersion", implementationVersion);
    }

    return manifest;
}