Example usage for java.util.jar Attributes Attributes

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

Introduction

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

Prototype

public Attributes() 

Source Link

Document

Constructs a new, empty Attributes object with default size.

Usage

From source file:com.tesora.dve.common.PELogUtils.java

private static Attributes readManifestFile() throws PEException {
    try {/*w  w w .ja va  2s.  co m*/
        Enumeration<URL> resources = PELogUtils.class.getClassLoader().getResources(MANIFEST_FILE_NAME);

        Attributes attrs = new Attributes();
        while (resources.hasMoreElements()) {
            URL url = resources.nextElement();
            if (url.toString().contains(CORE_PROJECT_NAME)) {
                Manifest manifest = new Manifest(url.openStream());
                attrs = manifest.getMainAttributes();
                break;
            }
        }
        return attrs;
    } catch (Exception e) {
        throw new PEException("Error retrieving build manifest", e);
    }
}

From source file:com.xebialabs.deployit.maven.packager.ManifestPackager.java

public void addDeployableArtifact(DeployableArtifactItem item) {
    if ("Dar".equals(item.getType()))
        return;//ww w. ja  v  a  2 s  .  c  o  m

    if ("Pom".equals(item.getType()))
        return;

    final Map<String, Attributes> entries = manifest.getEntries();
    final Attributes attributes = new Attributes();
    final String type = item.getType();
    final File location = new File(item.getLocation());

    attributes.putValue("CI-Type", type);
    if (item.hasName())
        attributes.putValue("CI-Name", item.getName());

    String darLocation = (item.getDarLocation() == null ? type : item.getDarLocation());

    if (item.isFolder()) {
        entries.put(darLocation, attributes);
    } else {
        if (location.isAbsolute())
            entries.put(darLocation + "/" + location.getName(), attributes);
        else
            entries.put(darLocation + "/" + item.getLocation(), attributes);
    }

    final File targetDir = new File(targetDirectory, darLocation);
    if (generateManifestOnly) {
        System.out.println("Skip copying artifact " + item.getName() + " to " + targetDir);
        return;
    }
    targetDir.mkdirs();

    File locationTargetDirs;
    //do not create missing directories is there are no parents or if the file is absolute
    if (location.isAbsolute() || location.getParent() == null) {
        locationTargetDirs = targetDir;
    } else {
        locationTargetDirs = new File(targetDir, location.getParent());
        locationTargetDirs.mkdirs();
    }

    try {
        if (location.isDirectory()) {
            FileUtils.copyDirectoryToDirectory(location, locationTargetDirs);
        } else {
            FileUtils.copyFileToDirectory(location, locationTargetDirs);
        }
    } catch (IOException e) {
        throw new RuntimeException("Fail to copy of " + location + " to " + targetDir, e);

    }

}

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  .  ja  va  2  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.jvm.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);
    Attributes attrs = new Attributes();
    attrs.putValue("Not-Seen", "ever");
    attrs.putValue("cake", "cheese");
    expectedManifest.getEntries().put("example", attrs);
    assertEquals(expectedManifest.getEntries(), seenManifest.getEntries());
}

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

private Manifest createManifestWithExampleSection(Map<String, String> attributes) {
    Manifest manifest = new Manifest();
    Attributes attrs = new Attributes();
    for (Map.Entry<String, String> stringStringEntry : attributes.entrySet()) {
        attrs.put(new Attributes.Name(stringStringEntry.getKey()), stringStringEntry.getValue());
    }/*from   w ww.ja v a  2  s  .  c o m*/
    manifest.getEntries().put("example", attrs);
    return manifest;
}

From source file:com.aimluck.eip.gpdb.GpdbRecordSelectData.java

/**
 * ??//ww  w  .j ava2 s  .co  m
 * 
 * @return 
 */
@Override
protected Attributes getColumnMap() {
    Attributes map = new Attributes();
    map.putValue("gpdb_id", EipTGpdb.GPDB_ID_PK_COLUMN);
    return map;
}

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

/**
 * From the constructor of {@link JarInputStream}:
 * <p>//from ww w . ja v  a 2  s.  co m
 * 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

/**
 * Write a .SF file with a digest the specified manifest.
 *//*from  w w  w .  j av  a 2  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:com.aimluck.eip.wiki.WikiSelectData.java

/**
 * @return/*from   ww w .j  a  v  a2 s .  c om*/
 *
 */
@Override
protected Attributes getColumnMap() {
    Attributes map = new Attributes();
    map.putValue("wiki_name", EipTWiki.WIKI_NAME_PROPERTY);
    map.putValue("parent_name", EipTWiki.PARENT_ID_PROPERTY);
    map.putValue("update_user", EipTWiki.UPDATE_USER_ID_PROPERTY);
    map.putValue("update_date", EipTWiki.UPDATE_DATE_PROPERTY);
    return map;
}

From source file:com.android.builder.internal.packaging.sign.SignatureExtension.java

/**
 * Adds / updates the signature for an entry. If this entry has no signature, or its digest
 * doesn't match the one in the signature file (or manifest), it will be updated.
 *
 * @param entry the entry/*from w w  w.j a v  a2  s.  c o m*/
 * @throws IOException failed to compute the entry's digest
 */
private void setDigestForEntry(@NonNull StoredEntry entry) throws IOException {
    String entryName = entry.getCentralDirectoryHeader().getName();
    byte[] entryDigestArray = mMessageDigest.digest(entry.read());
    String entryDigest = new String(Base64.encodeBase64(entryDigestArray), Charsets.US_ASCII);

    Attributes signatureAttributes = mSignatureFile.getEntries().get(entryName);
    if (signatureAttributes == null) {
        signatureAttributes = new Attributes();
        mSignatureFile.getEntries().put(entryName, signatureAttributes);
        mDirty = true;
    }

    if (!entryDigest.equals(signatureAttributes.getValue(mDigestAlgorithm.entryAttributeName))) {
        signatureAttributes.putValue(mDigestAlgorithm.entryAttributeName, entryDigest);
        mDirty = true;
    }

    /*
     * setAttribute will not mark the manifest as changed if the attribute is already there
     * and with the same value.
     */
    mManifestExtension.setAttribute(entryName, mDigestAlgorithm.entryAttributeName, entryDigest);
}