Example usage for java.util.jar Manifest getAttributes

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

Introduction

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

Prototype

public Attributes getAttributes(String name) 

Source Link

Document

Returns the Attributes for the specified entry name.

Usage

From source file:cascading.flow.hadoop.util.HadoopUtil.java

private static PlatformInfo getPlatformInfoInternal() {
    URL url = JobConf.class.getResource(JobConf.class.getSimpleName() + ".class");

    if (url == null || !url.toString().startsWith("jar"))
        return new PlatformInfo("Hadoop", null, null);

    String path = url.toString();
    String manifestPath = path.substring(0, path.lastIndexOf("!") + 1) + "/META-INF/MANIFEST.MF";

    Manifest manifest;

    try {/*ww w . j  a  v  a2  s. c  om*/
        manifest = new Manifest(new URL(manifestPath).openStream());
    } catch (IOException exception) {
        LOG.warn("unable to get manifest from {}", manifestPath, exception);

        return new PlatformInfo("Hadoop", null, null);
    }

    Attributes attributes = manifest.getAttributes("org/apache/hadoop");

    if (attributes == null) {
        LOG.debug("unable to get Hadoop manifest attributes");
        return new PlatformInfo("Hadoop", null, null);
    }

    String vendor = attributes.getValue("Implementation-Vendor");
    String version = attributes.getValue("Implementation-Version");

    return new PlatformInfo("Hadoop", vendor, version);
}

From source file:com.googlecode.idea.red5.SelectRed5LocationDialog.java

private String getVersion() {
    String home = homeDir.getText();
    if (home.length() == 0) {
        return EMPTY;
    }//from   www .  j a  v a  2  s .  c  o  m
    @NonNls
    final String pathToRed5Jar = new StringBuilder().append(home).append(separator).append("red5.jar")
            .toString();
    File red5 = new File(pathToRed5Jar);
    if (red5.exists()) {
        try {
            JarFile jar = new JarFile(pathToRed5Jar);
            Manifest manifest = jar.getManifest();
            Attributes attrs = manifest.getAttributes("");
            if (attrs != null) {
                String version = attrs.getValue("Red5-Version");
                if (version.startsWith("0.7")) {
                    logger.debug("Setting to version 0.7.");
                    return VERSION_DOT_SEVEN;
                } else if (version.startsWith("0.6")) {
                    logger.debug("Setting to version 0.6.");
                    return VERSION_DOT_SIX;
                }
            }
        } catch (IOException e) {
            logger.error("No version found! Returning the default.");
        }
    }
    return VERSION_DOT_EIGHT;
}

From source file:com.qmetry.qaf.automation.core.ConfigurationManager.java

private static Map<String, String> getBuildInfo() {
    Manifest manifest = null;
    Map<String, String> buildInfo = new HashMap<String, String>();
    JarFile jar = null;/*from www. j  ava  2 s  . c  o m*/
    try {
        URL url = ConfigurationManager.class.getProtectionDomain().getCodeSource().getLocation();
        File file = new File(url.toURI());
        jar = new JarFile(file);
        manifest = jar.getManifest();
    } catch (NullPointerException ignored) {
    } catch (URISyntaxException ignored) {
    } catch (IOException ignored) {
    } catch (IllegalArgumentException ignored) {
    } finally {
        if (null != jar)
            try {
                jar.close();
            } catch (IOException e) {
                log.warn(e.getMessage());
            }
    }

    if (manifest == null) {
        return buildInfo;
    }

    try {
        Attributes attributes = manifest.getAttributes("Build-Info");
        Set<Entry<Object, Object>> entries = attributes.entrySet();
        for (Entry<Object, Object> e : entries) {
            buildInfo.put(String.valueOf(e.getKey()), String.valueOf(e.getValue()));
        }
    } catch (NullPointerException e) {
        // Fall through
    }

    return buildInfo;
}

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();
    }//from  w  ww . j  av a  2 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:com.github.wolf480pl.mias4j.util.AbstractTransformingClassLoader.java

protected Package definePackage(String name, Manifest man, URL codeSourceURL) {
    String path = toInternalName(name) + "/";
    Attributes attr = man.getAttributes(path);
    String specTitle = null;//from  w  ww  .j av  a2s.  c  o m
    String specVersion = null;
    String specVendor = null;
    String implTitle = null;
    String implVersion = null;
    String implVendor = null;
    String sealed = null;

    if (attr != null) {
        specTitle = attr.getValue(Name.SPECIFICATION_TITLE);
        specVersion = attr.getValue(Name.SPECIFICATION_VERSION);
        specVendor = attr.getValue(Name.SPECIFICATION_VENDOR);
        implTitle = attr.getValue(Name.IMPLEMENTATION_TITLE);
        implVersion = attr.getValue(Name.IMPLEMENTATION_VERSION);
        implVendor = attr.getValue(Name.IMPLEMENTATION_VENDOR);
        sealed = attr.getValue(Name.SEALED);
    }

    attr = man.getMainAttributes();
    if (attr != null) {
        if (specTitle != null) {
            specTitle = attr.getValue(Name.SPECIFICATION_TITLE);
        }
        if (specVersion != null) {
            specVersion = attr.getValue(Name.SPECIFICATION_VERSION);
        }
        if (specVendor != null) {
            specVendor = attr.getValue(Name.SPECIFICATION_VENDOR);
        }
        if (implTitle != null) {
            implTitle = attr.getValue(Name.IMPLEMENTATION_TITLE);
        }
        if (implVersion != null) {
            implVersion = attr.getValue(Name.IMPLEMENTATION_VERSION);
        }
        if (implVendor != null) {
            implVendor = attr.getValue(Name.IMPLEMENTATION_VENDOR);
        }
        if (sealed != null) {
            sealed = attr.getValue(Name.SEALED);
        }
    }

    return definePackage(name, specTitle, specVersion, specVendor, implTitle, implVersion, implVendor,
            sealed.equalsIgnoreCase("true") ? codeSourceURL : null);

}

From source file:com.facebook.buck.jvm.java.JarDirectoryStepTest.java

/**
 * From the constructor of {@link JarInputStream}:
 * <p>//from  ww  w  .j  a v  a 2  s. c om
 * This implementation assumes the META-INF/MANIFEST.MF entry
 * should be either the first or the second entry (when preceded
 * by the dir META-INF/). It skips the META-INF/ and then
 * "consumes" the MANIFEST.MF to initialize the Manifest object.
 * <p>
 * A simple implementation of {@link JarDirectoryStep} would iterate over all entries to be
 * included, adding them to the output jar, while merging manifest files, writing the merged
 * manifest as the last item in the jar. That will generate jars the {@code JarInputStream} won't
 * be able to find the manifest for.
 */
@Test
public void manifestShouldBeSecondEntryInJar() throws IOException {
    Path manifestPath = Paths.get(JarFile.MANIFEST_NAME);

    // Create a directory with a manifest in it and more than two files.
    Path dir = folder.newFolder();
    Manifest dirManifest = new Manifest();
    Attributes attrs = new Attributes();
    attrs.putValue("From-Dir", "cheese");
    dirManifest.getEntries().put("Section", attrs);

    Files.createDirectories(dir.resolve(manifestPath).getParent());
    try (OutputStream out = Files.newOutputStream(dir.resolve(manifestPath))) {
        dirManifest.write(out);
    }
    Files.write(dir.resolve("A.txt"), "hello world".getBytes(UTF_8));
    Files.write(dir.resolve("B.txt"), "hello world".getBytes(UTF_8));
    Files.write(dir.resolve("aa.txt"), "hello world".getBytes(UTF_8));
    Files.write(dir.resolve("bb.txt"), "hello world".getBytes(UTF_8));

    // Create a jar with a manifest and more than two other files.
    Path inputJar = folder.newFile("example.jar");
    try (ZipOutputStream zos = new ZipOutputStream(Files.newOutputStream(inputJar))) {
        byte[] data = "hello world".getBytes(UTF_8);
        ZipEntry entry = new ZipEntry("C.txt");
        zos.putNextEntry(entry);
        zos.write(data, 0, data.length);
        zos.closeEntry();

        entry = new ZipEntry("cc.txt");
        zos.putNextEntry(entry);
        zos.write(data, 0, data.length);
        zos.closeEntry();

        entry = new ZipEntry("META-INF/");
        zos.putNextEntry(entry);
        zos.closeEntry();

        // Note: at end of the stream. Technically invalid.
        entry = new ZipEntry(JarFile.MANIFEST_NAME);
        zos.putNextEntry(entry);
        Manifest zipManifest = new Manifest();
        attrs = new Attributes();
        attrs.putValue("From-Zip", "peas");
        zipManifest.getEntries().put("Section", attrs);
        zipManifest.write(zos);
        zos.closeEntry();
    }

    // Merge and check that the manifest includes everything
    Path output = folder.newFile("output.jar");
    JarDirectoryStep step = new JarDirectoryStep(new FakeProjectFilesystem(folder.getRoot()), output,
            ImmutableSortedSet.of(dir, inputJar), null, null);
    int exitCode = step.execute(TestExecutionContext.newInstance()).getExitCode();

    assertEquals(0, exitCode);

    Manifest manifest;
    try (InputStream is = Files.newInputStream(output); JarInputStream jis = new JarInputStream(is)) {
        manifest = jis.getManifest();
    }

    assertNotNull(manifest);
    Attributes readAttributes = manifest.getAttributes("Section");
    assertEquals(2, readAttributes.size());
    assertEquals("cheese", readAttributes.getValue("From-Dir"));
    assertEquals("peas", readAttributes.getValue("From-Zip"));
}

From source file:com.jrummyapps.busybox.signing.ZipSigner.java

/** Add the SHA1 of every file to the manifest, creating it if necessary. */
private static Manifest addDigestsToManifest(final JarFile jar) throws IOException, GeneralSecurityException {
    final Manifest input = jar.getManifest();
    final Manifest output = new Manifest();

    final Attributes main = output.getMainAttributes();
    main.putValue("Manifest-Version", MANIFEST_VERSION);
    main.putValue("Created-By", CREATED_BY);

    // We sort the input entries by name, and add them to the output manifest in sorted order.
    // We expect that the output map will be deterministic.
    final TreeMap<String, JarEntry> byName = new TreeMap<>();
    for (Enumeration<JarEntry> e = jar.entries(); e.hasMoreElements();) {
        JarEntry entry = e.nextElement();
        byName.put(entry.getName(), entry);
    }//from  w w w.  ja  v  a  2  s.com

    final MessageDigest md = MessageDigest.getInstance("SHA1");
    final byte[] buffer = new byte[4096];
    int num;

    for (JarEntry entry : byName.values()) {
        final String name = entry.getName();
        if (!entry.isDirectory() && !name.equals(JarFile.MANIFEST_NAME) && !name.equals(CERT_SF_NAME)
                && !name.equals(CERT_RSA_NAME)) {
            InputStream data = jar.getInputStream(entry);
            while ((num = data.read(buffer)) > 0) {
                md.update(buffer, 0, num);
            }

            Attributes attr = null;
            if (input != null) {
                attr = input.getAttributes(name);
            }
            attr = attr != null ? new Attributes(attr) : new Attributes();
            attr.putValue("SHA1-Digest", base64encode(md.digest()));
            output.getEntries().put(name, attr);
        }
    }

    return output;
}

From source file:dalma.container.ClassLoaderImpl.java

/**
 * Define the package information when the class comes from a
 * jar with a manifest// w w  w  .j  a v  a 2s  .c  o  m
 *
 * @param container the jar file containing the manifest
 * @param packageName the name of the package being defined.
 * @param manifest the jar's manifest
 */
protected void definePackage(File container, String packageName, Manifest manifest) {
    String sectionName = packageName.replace('.', '/') + "/";

    String specificationTitle = null;
    String specificationVendor = null;
    String specificationVersion = null;
    String implementationTitle = null;
    String implementationVendor = null;
    String implementationVersion = null;
    String sealedString = null;
    URL sealBase = null;

    Attributes sectionAttributes = manifest.getAttributes(sectionName);
    if (sectionAttributes != null) {
        specificationTitle = sectionAttributes.getValue(Attributes.Name.SPECIFICATION_TITLE);
        specificationVendor = sectionAttributes.getValue(Attributes.Name.SPECIFICATION_VENDOR);
        specificationVersion = sectionAttributes.getValue(Attributes.Name.SPECIFICATION_VERSION);
        implementationTitle = sectionAttributes.getValue(Attributes.Name.IMPLEMENTATION_TITLE);
        implementationVendor = sectionAttributes.getValue(Attributes.Name.IMPLEMENTATION_VENDOR);
        implementationVersion = sectionAttributes.getValue(Attributes.Name.IMPLEMENTATION_VERSION);
        sealedString = sectionAttributes.getValue(Attributes.Name.SEALED);
    }

    Attributes mainAttributes = manifest.getMainAttributes();
    if (mainAttributes != null) {
        if (specificationTitle == null) {
            specificationTitle = mainAttributes.getValue(Attributes.Name.SPECIFICATION_TITLE);
        }
        if (specificationVendor == null) {
            specificationVendor = mainAttributes.getValue(Attributes.Name.SPECIFICATION_VENDOR);
        }
        if (specificationVersion == null) {
            specificationVersion = mainAttributes.getValue(Attributes.Name.SPECIFICATION_VERSION);
        }
        if (implementationTitle == null) {
            implementationTitle = mainAttributes.getValue(Attributes.Name.IMPLEMENTATION_TITLE);
        }
        if (implementationVendor == null) {
            implementationVendor = mainAttributes.getValue(Attributes.Name.IMPLEMENTATION_VENDOR);
        }
        if (implementationVersion == null) {
            implementationVersion = mainAttributes.getValue(Attributes.Name.IMPLEMENTATION_VERSION);
        }
        if (sealedString == null) {
            sealedString = mainAttributes.getValue(Attributes.Name.SEALED);
        }
    }

    if (sealedString != null && sealedString.equalsIgnoreCase("true")) {
        try {
            sealBase = new URL("file:" + container.getPath());
        } catch (MalformedURLException e) {
            // ignore
        }
    }

    definePackage(packageName, specificationTitle, specificationVersion, specificationVendor,
            implementationTitle, implementationVersion, implementationVendor, sealBase);
}

From source file:org.apache.catalina.loader.WebappClassLoader.java

/**
 * Returns true if the specified package name is sealed according to the
 * given manifest./*from  w w  w.  j a  va 2s  .  c  o m*/
 */
protected boolean isPackageSealed(String name, Manifest man) {

    String path = name + "/";
    Attributes attr = man.getAttributes(path);
    String sealed = null;
    if (attr != null) {
        sealed = attr.getValue(Name.SEALED);
    }
    if (sealed == null) {
        if ((attr = man.getMainAttributes()) != null) {
            sealed = attr.getValue(Name.SEALED);
        }
    }
    return "true".equalsIgnoreCase(sealed);

}

From source file:org.apache.felix.deploymentadmin.itest.util.DPSigner.java

public void sign(Manifest manifest, List<ArtifactData> files, PrivateKey privKey, X509Certificate cert,
        OutputStream os) throws Exception {
    // For each file, add its signature to the manifest
    for (ArtifactData file : files) {
        String filename = file.getFilename();
        Attributes attrs = manifest.getAttributes(filename);
        addDigestAttribute(attrs, file);
    }/*  w  ww .j  a v  a2 s .  co m*/

    try (ZipOutputStream zos = new ZipOutputStream(os)) {
        writeSignedManifest(manifest, zos, privKey, cert);

        for (ArtifactData file : files) {
            ZipEntry entry = new ZipEntry(file.getFilename());
            zos.putNextEntry(entry);

            try (InputStream is = file.createInputStream()) {
                byte[] buf = new byte[1024];
                int read;
                while ((read = is.read(buf)) > 0) {
                    zos.write(buf, 0, read);
                }
            }
        }
    }
}