Example usage for java.util.jar Manifest getMainAttributes

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

Introduction

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

Prototype

public Attributes getMainAttributes() 

Source Link

Document

Returns the main Attributes for the Manifest.

Usage

From source file:org.wso2.carbon.integration.framework.utils.CodeCoverageUtils.java

private synchronized static void addEmmaDynamicImportPackage(String jarFilePath) throws IOException {
    if (!jarFilePath.endsWith(".jar")) {
        throw new IllegalArgumentException(
                "Jar file should have the extension .jar. " + jarFilePath + " is invalid");
    }/*from  w  w w.j  av  a  2s.  com*/
    JarFile jarFile = new JarFile(jarFilePath);
    Manifest manifest = jarFile.getManifest();
    if (manifest == null) {
        throw new IllegalArgumentException(jarFilePath + " does not contain a MANIFEST.MF file");
    }
    String fileSeparator = (File.separatorChar == '\\') ? "\\" : File.separator;
    String jarFileName = jarFilePath;
    if (jarFilePath.lastIndexOf(fileSeparator) != -1) {
        jarFileName = jarFilePath.substring(jarFilePath.lastIndexOf(fileSeparator) + 1);
    }
    ArchiveManipulator archiveManipulator;
    String tempExtractedDir;
    try {
        archiveManipulator = new ArchiveManipulator();
        tempExtractedDir = System.getProperty("basedir") + File.separator + "target" + File.separator
                + jarFileName.substring(0, jarFileName.lastIndexOf('.'));
        new ArchiveManipulatorUtil().extractFile(jarFilePath, tempExtractedDir);
    } finally {
        jarFile.close();
    }

    String dynamicImports = manifest.getMainAttributes().getValue("DynamicImport-Package");
    if (dynamicImports != null) {
        manifest.getMainAttributes().putValue("DynamicImport-Package", dynamicImports + ",com.vladium.*");
    } else {
        manifest.getMainAttributes().putValue("DynamicImport-Package", "com.vladium.*");
    }
    File newManifest = new File(
            tempExtractedDir + File.separator + "META-INF" + File.separator + "MANIFEST.MF");
    FileOutputStream manifestOut = null;
    try {
        manifestOut = new FileOutputStream(newManifest);
        manifest.write(manifestOut);
    } catch (IOException e) {
        log.error("Could not write content to new MANIFEST.MF file", e);
    } finally {
        if (manifestOut != null) {
            manifestOut.close();
        }
    }
    archiveManipulator.archiveDir(jarFilePath, tempExtractedDir);
    FileManipulator.deleteDir(tempExtractedDir);
}

From source file:org.jboss.windup.decorator.archive.ManifestVersionMapperDecorator.java

protected boolean matchesAll(Manifest mf) {
    boolean matched = false;

    // check the main attributes....
    matched = matchAttributes(mf.getMainAttributes());

    // if this doesn't match the main attributes, go through attribute groups trying to match.
    if (!matched) {
        // prepare the fallback attributes, ordered by name...
        List<String> attributeNames = new ArrayList<String>(mf.getEntries().keySet());
        Collections.sort(attributeNames);

        for (String attributeName : attributeNames) {
            matched = matchAttributes(mf.getAttributes(attributeName));

            if (matched) {
                return true;
            }/*from ww w.j a va  2 s .  c  om*/
        }
    }

    return matched;
}

From source file:jp.gr.java_conf.schkit.utils.ApplicationInfo.java

private void loadManifest() throws Exception {
    if (classLoader == null)
        return;//from ww w. j a v a2  s .  co  m

    URL resource = classLoader.findResource(MANIFEST_PATH);
    if (resource == null)
        return;

    Manifest manifest = new Manifest(resource.openStream());

    Attributes attrs = manifest.getMainAttributes();

    String value;
    value = attrs.getValue("Implementation-Title");
    if (value != null)
        name = value;
    value = attrs.getValue("Implementation-Version");
    if (value != null)
        version = value;
    value = attrs.getValue("Create-By");
    if (value != null)
        author = value;
    value = attrs.getValue("License-By");
    if (value != null)
        license = value;

    if (classLotation != null) {
        Date date = new Date(classLotation.openConnection().getLastModified());
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy.MM.dd HH:mm:ss");
        modified = sdf.format(date);
    }
}

From source file:com.redhat.rcm.maven.plugin.buildmetadata.util.ManifestHelper.java

private Attributes fetchAttributes(final Manifest manifest) {
    if (StringUtils.isBlank(manifestSection) || "Main".equals(manifestSection)) {
        return manifest.getMainAttributes();
    }//ww  w  .j  a  va2 s.  c o  m

    Attributes attributes = manifest.getAttributes(manifestSection);
    if (attributes == null) {
        attributes = new Attributes();
        manifest.getEntries().put(manifestSection, attributes);
    }
    return attributes;
}

From source file:org.jumpmind.util.AbstractVersion.java

protected Attributes findManifestAttributes() {
    InputStream is = null;/*from   ww  w  .  ja  v  a 2  s.c o  m*/
    try {
        Enumeration<URL> resources = getClass().getClassLoader().getResources("META-INF/MANIFEST.MF");
        while (resources.hasMoreElements()) {
            is = resources.nextElement().openStream();
            Manifest manifest = new Manifest(is);
            Attributes attributes = manifest.getMainAttributes();
            if (getArtifactName().equals(attributes.getValue("Project-Artifact"))) {
                return attributes;
            }
        }
    } catch (IOException e) {
        // nothing to do, really
    } finally {
        IOUtils.closeQuietly(is);
    }
    return null;
}

From source file:com.mgmtp.perfload.core.console.meta.LtMetaInfoHandler.java

/**
 * Dumps the specified meta information to specified writer.
 * //from w  w w. ja v a2  s.c  o  m
 * @param metaInfo
 *            the meta information object
 * @param writer
 *            the writer
 */
public void dumpMetaInfo(final LtMetaInfo metaInfo, final Writer writer) {
    PrintWriter pr = new PrintWriter(writer);

    URL url = getClass().getProtectionDomain().getCodeSource().getLocation();
    if (url.getPath().endsWith(".jar")) {
        try {
            JarFile jarFile = new JarFile(toFile(url));
            Manifest mf = jarFile.getManifest();
            Attributes attr = mf.getMainAttributes();
            pr.printf("perfload.implementation.version=%s", attr.getValue("Implementation-Version"));
            pr.println();
            pr.printf("perfload.implementation.date=%s", attr.getValue("Implementation-Date"));
            pr.println();
            pr.printf("perfload.implementation.revision=%s", attr.getValue("Implementation-Revision"));
            pr.println();
        } catch (IOException ex) {
            log.error(ex.getMessage(), ex);
        }
    }

    pr.printf("test.file=%s", metaInfo.getTestplanFileName());
    pr.println();

    pr.printf("test.start=%s", DateTimeFormatter.ISO_LOCAL_DATE_TIME.format(metaInfo.getStartTimestamp()));
    pr.println();
    pr.printf("test.finish=%s", DateTimeFormatter.ISO_LOCAL_DATE_TIME.format(metaInfo.getFinishTimestamp()));
    pr.println();

    List<Daemon> daemonList = metaInfo.getDaemons();
    Collections.sort(daemonList);

    for (Daemon daemon : daemonList) {
        pr.printf("daemon.%d=%s:%d", daemon.getId(), daemon.getHost(), daemon.getPort());
        pr.println();
    }

    List<String> lpTargets = metaInfo.getLpTargets();
    if (!lpTargets.isEmpty()) {
        Collections.sort(lpTargets);
        pr.printf("targets=%s", on(',').join(lpTargets));
        pr.println();
    }

    List<String> lpOperations = metaInfo.getLpOperations();
    if (!lpOperations.isEmpty()) {
        Collections.sort(lpOperations);
        pr.printf("operations=%s", on(',').join(lpOperations));
        pr.println();
    }

    List<Executions> executionsList = metaInfo.getExecutionsList();
    Collections.sort(executionsList);
    for (Executions executions : executionsList) {
        pr.printf("executions.%s.%s=%d", executions.operation, executions.target, executions.executions);
        pr.println();
    }
}

From source file:org.craftercms.commons.monitoring.VersionMonitor.java

/**
 * Create a VersionMonitor pojo instance based on a given Manifest File.
 * Empty values are assign if the keys in the manifest are missing.
 * @param manifest Manifest File that contains the VersionMonitor Information.
 *///from w w w .  jav  a 2  s.c  o  m
private VersionMonitor(Manifest manifest) {
    Attributes mainAttrs = manifest.getMainAttributes();
    datetime = DATETIME_FORMATTER.format(new Date());
    initFromManifest(mainAttrs);
    initRuntime();
    initOS();
    initTomcat();
}

From source file:com.commercehub.dropwizard.BuildInfoServlet.java

@Override
public void init(ServletConfig config) {
    objectWriter = new ObjectMapper().writer();

    cacheControl = new CacheControl();
    cacheControl.setMustRevalidate(true);
    cacheControl.setNoCache(true);/*from  w w w  .  j  a va2  s  . c o  m*/
    cacheControl.setNoStore(true);

    // cache build information
    manifestAttributes = new Properties();
    ImmutableSet<String> attributeBlacklist = getAttributeBlacklist(config);
    try {
        Enumeration<URL> resources = getClass().getClassLoader().getResources(JarFile.MANIFEST_NAME);
        if (resources != null) {
            while (resources.hasMoreElements()) {
                Manifest manifest = new Manifest(resources.nextElement().openStream());
                Attributes attributes = manifest.getMainAttributes();
                for (Object key : attributes.keySet()) {
                    Attributes.Name attrName = (Attributes.Name) key;
                    if (!attributeBlacklist.contains(attrName.toString())) {
                        manifestAttributes.setProperty(attrName.toString(), attributes.getValue(attrName));
                    }
                }
            }
        }
    } catch (IOException e) {
        log.warn("Unable to retrieve build info", e);
    }
}

From source file:com.npower.dm.msm.JADCreator.java

public SoftwareInformation getSoftwareInformation(File file) throws IOException {
    JarFile jar = new JarFile(file);
    Manifest manifest = jar.getManifest();
    SoftwareInformation info = new SoftwareInformation();
    info.setVendor(manifest.getMainAttributes().getValue("MIDlet-Vendor"));
    info.setName(manifest.getMainAttributes().getValue("MIDlet-Name"));
    info.setVersion(manifest.getMainAttributes().getValue("MIDlet-Version"));
    String midpVersion = manifest.getMainAttributes().getValue("MicroEdition-Profile");
    if (StringUtils.isNotEmpty(midpVersion) && midpVersion.trim().equalsIgnoreCase("MIDP-2.0")) {
        info.setMidp2(true);//from  w  ww.  j  a  va2  s . c  o  m
    }
    return info;
}

From source file:org.apache.sling.maven.bundlesupport.AbstractBundleDeployMojo.java

/**
 * Change the version in jar//from  w w w  .j a  v a  2s.  c om
 * 
 * @param newVersion
 * @param file
 * @return
 * @throws MojoExecutionException
 */
protected File changeVersion(File file, String oldVersion, String newVersion) throws MojoExecutionException {
    String fileName = file.getName();
    int pos = fileName.indexOf(oldVersion);
    fileName = fileName.substring(0, pos) + newVersion + fileName.substring(pos + oldVersion.length());

    JarInputStream jis = null;
    JarOutputStream jos;
    OutputStream out = null;
    JarFile sourceJar = null;
    try {
        // now create a temporary file and update the version
        sourceJar = new JarFile(file);
        final Manifest manifest = sourceJar.getManifest();
        manifest.getMainAttributes().putValue("Bundle-Version", newVersion);

        jis = new JarInputStream(new FileInputStream(file));
        final File destJar = new File(file.getParentFile(), fileName);
        out = new FileOutputStream(destJar);
        jos = new JarOutputStream(out, manifest);

        jos.setMethod(JarOutputStream.DEFLATED);
        jos.setLevel(Deflater.BEST_COMPRESSION);

        JarEntry entryIn = jis.getNextJarEntry();
        while (entryIn != null) {
            JarEntry entryOut = new JarEntry(entryIn.getName());
            entryOut.setTime(entryIn.getTime());
            entryOut.setComment(entryIn.getComment());
            jos.putNextEntry(entryOut);
            if (!entryIn.isDirectory()) {
                IOUtils.copy(jis, jos);
            }
            jos.closeEntry();
            jis.closeEntry();
            entryIn = jis.getNextJarEntry();
        }

        // close the JAR file now to force writing
        jos.close();
        return destJar;
    } catch (IOException ioe) {
        throw new MojoExecutionException("Unable to update version in jar file.", ioe);
    } finally {
        if (sourceJar != null) {
            try {
                sourceJar.close();
            } catch (IOException ex) {
                // close
            }
        }
        IOUtils.closeQuietly(jis);
        IOUtils.closeQuietly(out);
    }

}