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.openhab.tools.analysis.checkstyle.RequireBundleCheck.java

@Override
protected void processFiltered(File file, FileText fileText) {
    try {/* www . jav a  2  s  .c o  m*/
        // We use Manifest class here instead of ManifestParser,
        // because it is easier to get the content of the headers
        // in the MANIFEST.MF
        Manifest manifest = new Manifest(new FileInputStream(file));
        Attributes attributes = manifest.getMainAttributes();

        String fragmentHost = attributes.getValue(FRAGMENT_HOST_HEADER_NAME);
        String bundleSymbolicName = attributes.getValue(BUNDLE_SYMBOLIC_NAME_HEADER_NAME);

        boolean testBundle = false;
        if (StringUtils.isNotBlank(fragmentHost) && StringUtils.isNotBlank(bundleSymbolicName)) {
            testBundle = bundleSymbolicName.startsWith(fragmentHost)
                    && bundleSymbolicName.substring(fragmentHost.length()).startsWith(".test");
        }

        String requireBundleHeaderValue = attributes.getValue(REQUIRE_BUNDLE_HEADER_NAME);
        if (requireBundleHeaderValue != null && !testBundle) {

            int lineNumber = findLineNumberSafe(fileText, requireBundleHeaderValue, 0,
                    REQUIRE_BUNDLE_HEADER_NAME + " header line number not found.");
            log(lineNumber, "The MANIFEST.MF file must not contain any Require-Bundle entries. "
                    + "Instead, Import-Package must be used.");
        } else if (requireBundleHeaderValue != null && testBundle) {
            String[] bundleNames = requireBundleHeaderValue.split(",");
            for (String bundleName : bundleNames) {
                if (!allowedRequireBundles.contains(bundleName)) {
                    int lineNumber = findLineNumberSafe(fileText, requireBundleHeaderValue, 0,
                            "Header value not found.");
                    log(lineNumber,
                            "The MANIFEST.MF file of a test fragment must not contain Require-Bundle entries other than "
                                    + getAllowedBundlesString() + ".");
                    break;
                }
            }
        }
    } catch (

    FileNotFoundException e) {
        logger.error("An exception was thrown while trying to open the file " + file.getPath(), e);
    } catch (IOException e) {
        logger.error("An exception was thrown while trying to read the file " + file.getPath(), e);
    }
}

From source file:rapture.kernel.JarApiImplTest.java

private byte[] createTestJar() {
    Manifest manifest = new Manifest();
    manifest.getMainAttributes().put(Attributes.Name.MANIFEST_VERSION, "1.0");
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    try (JarOutputStream target = new JarOutputStream(baos, manifest)) {
        File testJar = new File(TEST_JAR_PATH);
        if (!testJar.exists() || !testJar.canRead()) {
            return null;
        }//from w ww  . j  av  a 2s .  c  om
        add(testJar, target);
    } catch (IOException e) {
        log.error(e);
    }
    return baos.toByteArray();
}

From source file:org.echocat.nodoodle.NodoodleInformationFactory.java

public String getManifestValue(Name mainAttributes) {
    final String result;
    final Manifest manifest = getManifest();
    if (manifest != null) {
        final Object plainVersion = manifest.getMainAttributes().get(mainAttributes);
        if (plainVersion != null) {
            result = plainVersion.toString();
        } else {/*from w w  w  .  j a va 2s  .c o m*/
            result = null;
        }
    } else {
        result = null;
    }
    return result;
}

From source file:acmi.l2.clientmod.xdat.XdatEditor.java

private String readAppVersion() throws IOException, URISyntaxException {
    try (JarFile jarFile = new JarFile(
            Paths.get(getClass().getProtectionDomain().getCodeSource().getLocation().toURI()).toFile())) {
        Manifest manifest = jarFile.getManifest();
        return manifest.getMainAttributes().getValue("Version");
    }//from w  ww  .  jav a 2  s.  co  m
}

From source file:com.buschmais.maexo.test.spring.SpringTest.java

/**
 * {@inheritDoc}//from   w w w.  ja v a  2s  .c om
 */
@Override
protected Manifest getManifest() {
    // let the testing framework create/load the manifest
    Manifest mf = super.getManifest();
    StringBuffer imports = new StringBuffer(IMPORT_PACKAGES);
    String importPackages = mf.getMainAttributes().getValue(org.osgi.framework.Constants.IMPORT_PACKAGE);
    if (importPackages != null) {
        imports.append(',');
        imports.append(importPackages);
    }
    mf.getMainAttributes().putValue(org.osgi.framework.Constants.IMPORT_PACKAGE, imports.toString());
    return mf;
}

From source file:org.jboss.pnc.buildagent.server.BootstrapUndertowBuildAgentHandlers.java

private String getManifestInformation() {
    String result = "";
    try {/*from  w w  w  .j av a 2  s  .c om*/
        final Enumeration<URL> resources = Welcome.class.getClassLoader().getResources("META-INF/MANIFEST.MF");

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

            log.trace("Processing jar resource " + jarUrl);
            if (jarUrl.getFile().contains("build-agent")) {
                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) {
        log.trace("Error retrieving information from manifest", e);
    }

    return result;
}

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

private Manifest createManifestInstance(final Properties buildMetaDataProperties) {
    final Manifest manifest = new Manifest();
    final Attributes attributes = fetchAttributes(manifest);
    manifest.getMainAttributes().put(Attributes.Name.MANIFEST_VERSION, "1.0");
    for (final Map.Entry<Object, Object> entry : buildMetaDataProperties.entrySet()) {
        final String key = ObjectUtils.toString(entry.getKey(), null);
        if (key.length() > MANIFEST_KEY_MAX_LENGTH) {
            continue;
        }//  w ww.j  a v  a 2s.c o  m

        final String normalizedKey = normalize(key);
        final String value = ObjectUtils.toString(entry.getValue(), null);

        attributes.putValue(normalizedKey, value);
    }

    return manifest;
}

From source file:org.jboss.as.test.manualmode.layered.LayeredDistributionTestCase.java

private void buildProductModule(String layer) throws Exception {
    File layerDir = new File(layersDir, layer);
    File moduleDir = new File(layerDir, "org" + File.separator + "jboss" + File.separator + "as"
            + File.separator + "product" + File.separator + "test");
    Assert.assertTrue(moduleDir.mkdirs());
    File moduleXmlFile = new File(moduleDir, "module.xml");
    FileUtils.copyInputStreamToFile(/*from w w  w  .  j  av  a 2s  .c  o m*/
            LayeredDistributionTestCase.class.getResourceAsStream("/layered/product-module.xml"),
            moduleXmlFile);

    File manifestDir = new File(moduleDir, "classes" + File.separator + "META-INF");
    Assert.assertTrue(manifestDir.mkdirs());

    File moduleManifestFile = new File(manifestDir, "MANIFEST.MF");
    Manifest m = new Manifest();
    m.getMainAttributes().put(Attributes.Name.MANIFEST_VERSION, "1.0");
    m.getMainAttributes().putValue("JBoss-Product-Release-Name", PRODUCT_NAME);
    m.getMainAttributes().putValue("JBoss-Product-Release-Version", PRODUCT_VERSION);
    OutputStream manifestStream = new BufferedOutputStream(new FileOutputStream(moduleManifestFile));
    m.write(manifestStream);
    manifestStream.flush();
    manifestStream.close();

    // set product.conf        
    File binDir = new File(AS_PATH, "bin");
    Assert.assertTrue(binDir.exists());
    File productConf = new File(binDir, "product.conf");
    if (productConf.exists())
        productConf.delete();
    FileUtils.writeStringToFile(productConf, "slot=test" + System.getProperty("line.separator"));

}

From source file:org.thiesen.ant.git.ExtractGitInfo.java

private String loadVersion() {
    try {// w  w  w  .j  av  a2s.c o  m
        final Enumeration<URL> resources = getClass().getClassLoader().getResources("META-INF/MANIFEST.MF");
        while (resources.hasMoreElements()) {

            final Manifest manifest = new Manifest(resources.nextElement().openStream());

            final Attributes mainAttributes = manifest.getMainAttributes();

            if ("gitant".equalsIgnoreCase(mainAttributes.getValue("Project-Name"))) {
                return mainAttributes.getValue("Project-Version");
            }

        }

    } catch (final IOException E) {
        // do nothing
    }
    return "unknown version";

}

From source file:org.thiesen.ant.git.ExtractGitInfo.java

private String loadJGitVersion() {
    try {//from   w w w  .  j a v a 2 s.c  o  m
        final Enumeration<URL> resources = getClass().getClassLoader().getResources("META-INF/MANIFEST.MF");
        while (resources.hasMoreElements()) {

            final Manifest manifest = new Manifest(resources.nextElement().openStream());

            final Attributes mainAttributes = manifest.getMainAttributes();

            if ("org.eclipse.jgit".equalsIgnoreCase(mainAttributes.getValue("Bundle-SymbolicName"))) {
                return mainAttributes.getValue("Implementation-Title") + " "
                        + mainAttributes.getValue("Implementation-Version");
            }

        }

    } catch (final IOException E) {
        // do nothing
    }
    return "unknown version";

}