Example usage for java.util.jar Manifest getEntries

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

Introduction

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

Prototype

public Map<String, Attributes> getEntries() 

Source Link

Document

Returns a Map of the entries contained in this Manifest.

Usage

From source file:Main.java

public static void main(String[] argv) throws Exception {
    JarFile jarfile = new JarFile("filename.jar");

    Manifest manifest = jarfile.getManifest();

    Map map = manifest.getEntries();

    for (Iterator it = map.keySet().iterator(); it.hasNext();) {
        String entryName = (String) it.next();

        Attributes attrs = (Attributes) map.get(entryName);

        for (Iterator it2 = attrs.keySet().iterator(); it2.hasNext();) {
            Attributes.Name attrName = (Attributes.Name) it2.next();

            String attrValue = attrs.getValue(attrName);
        }/*w  w  w .j  a v  a 2s .  c o m*/
    }
}

From source file:com.xebialabs.deployit.cli.ext.mustachify.dar.DarManifestParser.java

private static DarManifest parse(Manifest manifest) {
    Attributes mainAttributes = manifest.getMainAttributes();
    validate(mainAttributes);//  ww  w  . j a  v a  2  s.c  o  m

    Iterable<DarManifestEntry> darEntries = filter(transform(manifest.getEntries().entrySet(),
            new Function<Entry<String, Attributes>, DarManifestEntry>() {
                @Override
                public DarManifestEntry apply(Entry<String, Attributes> input) {
                    String entryName = input.getKey();
                    Attributes entryAttributes = input.getValue();
                    return (DarManifestEntry.isDarEntry(entryName, entryAttributes)
                            ? DarManifestEntry.fromEntryAttributes(entryName, entryAttributes)
                            : DarManifestEntry.NULL);
                }
            }), not(new Predicate<Object>() {
                @Override
                public boolean apply(Object input) {
                    return (input == DarManifestEntry.NULL);
                }
            }));
    return new DarManifest(mainAttributes.getValue(APPLICATION_ATTRIBUTE_NAME),
            mainAttributes.getValue(VERSION_ATTRIBUTE_NAME), copyOf(darEntries));
}

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

/** Copy all the files in a manifest from input to output. */
private static void copyFiles(Manifest manifest, JarFile in, JarArchiveOutputStream out, long timestamp)
        throws IOException {
    final byte[] buffer = new byte[4096];
    int num;//from w  w w. j a  va  2  s.c o m

    final Map<String, Attributes> entries = manifest.getEntries();
    final List<String> names = new ArrayList<>(entries.keySet());
    Collections.sort(names);
    for (final String name : names) {
        final JarEntry inEntry = in.getJarEntry(name);
        if (inEntry.getMethod() == JarArchiveEntry.STORED) {
            // Preserve the STORED method of the input entry.
            out.putArchiveEntry(new JarArchiveEntry(inEntry));
        } else {
            // Create a new entry so that the compressed len is recomputed.
            final JarArchiveEntry je = new JarArchiveEntry(name);
            je.setTime(timestamp);
            out.putArchiveEntry(je);
        }

        final InputStream data = in.getInputStream(inEntry);
        while ((num = data.read(buffer)) > 0) {
            out.write(buffer, 0, num);
        }
        out.flush();
        out.closeArchiveEntry();
    }
}

From source file:net.sf.keystore_explorer.crypto.signing.JarSigner.java

private static String getManifestEntriesAttrs(JarFile jar) throws IOException {

    StringBuilder sbManifest = new StringBuilder();

    // Get current manifest
    Manifest manifest = jar.getManifest();

    // Write out entry attributes to manifest
    if (manifest != null) {
        // Get entry attributes
        Map<String, Attributes> entries = manifest.getEntries();

        boolean firstEntry = true;

        // For each entry...
        for (String entryName : entries.keySet()) {
            // Get entry's attributes
            Attributes entryAttrs = entries.get(entryName);

            // Completely ignore entries that contain only a xxx-Digest
            // attribute
            if ((entryAttrs.size() == 1) && (entryAttrs.keySet().toArray()[0].toString().endsWith("-Digest"))) {
                continue;
            }//  w  ww.j a va  2 s .  co  m

            if (!firstEntry) {
                // Entries subequent to the first are split by a newline
                sbManifest.append(CRLF);
            }

            // Get entry attributes as a string to preserve their order
            String manifestEntryAttributes = getManifestEntryAttrs(jar, entryName);

            // Write them out
            sbManifest.append(manifestEntryAttributes);

            // The next entry will not be the first entry
            firstEntry = false;
        }
    }

    return sbManifest.toString();
}

From source file:com.qrmedia.commons.multispi.provider.ManifestEntryProvider.java

protected Set<String> processResource(Manifest manifest, final Class<?> serviceClass) {
    return ImmutableSet.copyOf(transform(filterValues(manifest.getEntries(), new Predicate<Attributes>() {
        public boolean apply(Attributes input) {
            return isManifestEntryOfImplementation(input, serviceClass);
        }//w  w  w.j  a  va 2  s. com
    }).keySet(), new ResourcePathsToClassNames()));
}

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

/**
 * Write a .SF file with a digest the specified manifest.
 *//*from  w  ww.  java2 s.  c  o  m*/
private static byte[] writeSignatureFile(Manifest manifest, OutputStream out)
        throws IOException, GeneralSecurityException {
    final Manifest sf = new Manifest();
    final Attributes main = sf.getMainAttributes();
    main.putValue("Manifest-Version", MANIFEST_VERSION);
    main.putValue("Created-By", CREATED_BY);

    final MessageDigest md = MessageDigest.getInstance("SHA1");
    final PrintStream print = new PrintStream(new DigestOutputStream(new ByteArrayOutputStream(), md), true,
            "UTF-8");

    // Digest of the entire manifest
    manifest.write(print);
    print.flush();
    main.putValue("SHA1-Digest-Manifest", base64encode(md.digest()));

    final Map<String, Attributes> entries = manifest.getEntries();
    for (final Map.Entry<String, Attributes> entry : entries.entrySet()) {
        // Digest of the manifest stanza for this entry.
        print.print("Name: " + entry.getKey() + "\r\n");
        for (final Map.Entry<Object, Object> att : entry.getValue().entrySet()) {
            print.print(att.getKey() + ": " + att.getValue() + "\r\n");
        }
        print.print("\r\n");
        print.flush();

        final Attributes sfAttr = new Attributes();
        sfAttr.putValue("SHA1-Digest", base64encode(md.digest()));
        sf.getEntries().put(entry.getKey(), sfAttr);
    }

    final ByteArrayOutputStream sos = new ByteArrayOutputStream();
    sf.write(sos);

    String value = sos.toString();
    String done = value.replace("Manifest-Version", "Signature-Version");

    out.write(done.getBytes());

    print.close();
    sos.close();

    return done.getBytes();
}

From source file:org.apache.xmlgraphics.util.ClasspathResource.java

private void loadManifests() {
        Enumeration e;/*ww  w .java2  s  . co  m*/
        try {

            Iterator it = getClassLoadersForResources().iterator();
            while (it.hasNext()) {
                ClassLoader classLoader = (ClassLoader) it.next();

                e = classLoader.getResources(MANIFEST_PATH);

                while (e.hasMoreElements()) {
                    final URL u = (URL) e.nextElement();
                    try {
                        final Manifest manifest = new Manifest(u.openStream());
                        final Map entries = manifest.getEntries();
                        final Iterator entrysetiterator = entries.entrySet().iterator();
                        while (entrysetiterator.hasNext()) {
                            final Map.Entry entry = (Map.Entry) entrysetiterator.next();
                            final String name = (String) entry.getKey();
                            final Attributes attributes = (Attributes) entry.getValue();
                            final String contentType = attributes.getValue(CONTENT_TYPE_KEY);
                            if (contentType != null) {
                                addToMapping(contentType, name, classLoader);
                            }
                        }
                    } catch (IOException io) {
                        // TODO: Log.
                    }
                }
            }

        } catch (IOException io) {
            // TODO: Log.
        }
    }

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

@Test
public void shouldMergeManifestsIfAsked() throws IOException {
    Manifest fromJar = createManifestWithExampleSection(ImmutableMap.of("Not-Seen", "ever"));
    Manifest fromUser = createManifestWithExampleSection(ImmutableMap.of("cake", "cheese"));

    Manifest seenManifest = jarDirectoryAndReadManifest(fromJar, fromUser, true);

    Manifest expectedManifest = new Manifest(fromJar);
    expectedManifest.getEntries().putAll(fromUser.getEntries());
    assertEquals(expectedManifest.getEntries(), seenManifest.getEntries());
}

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   ww  w.j  ava2  s  .  co  m

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

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

@Test
public void shouldNotMergeManifestsIfRequested() throws IOException {
    Manifest fromJar = createManifestWithExampleSection(ImmutableMap.of("Not-Seen", "ever"));
    Manifest fromUser = createManifestWithExampleSection(ImmutableMap.of("cake", "cheese"));

    Manifest seenManifest = jarDirectoryAndReadManifest(fromJar, fromUser, false);

    assertEquals(fromUser.getEntries(), seenManifest.getEntries());
}