Example usage for java.util.jar Attributes putValue

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

Introduction

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

Prototype

public String putValue(String name, String value) 

Source Link

Document

Associates the specified value with the specified attribute name, specified as a String.

Usage

From source file:com.hellblazer.process.JavaProcessTest.java

protected void copyTestJarFile() throws Exception {
    String classFileName = HelloWorld.class.getCanonicalName().replace('.', '/') + ".class";
    URL classFile = getClass().getResource("/" + classFileName);
    assertNotNull(classFile);//from   w w w. ja  va  2 s  . c o  m

    Manifest manifest = new Manifest();
    Attributes attributes = manifest.getMainAttributes();
    attributes.putValue("Manifest-Version", "1.0");
    attributes.putValue("Main-Class", HelloWorld.class.getCanonicalName());

    FileOutputStream fos = new FileOutputStream(new File(testDir, TEST_JAR));
    JarOutputStream jar = new JarOutputStream(fos, manifest);
    JarEntry entry = new JarEntry(classFileName);
    jar.putNextEntry(entry);
    InputStream in = classFile.openStream();
    byte[] buffer = new byte[1024];
    for (int read = in.read(buffer); read != -1; read = in.read(buffer)) {
        jar.write(buffer, 0, read);
    }
    in.close();
    jar.closeEntry();
    jar.close();
}

From source file:Main.java

private static void addImports(Attributes attrigutes, BundleDescription compositeDesc,
        ExportPackageDescription[] matchingExports) {
    ExportPackageDescription[] exports = compositeDesc.getExportPackages();
    List systemExports = getSystemExports(matchingExports);
    if (exports.length == 0 && systemExports.size() == 0)
        return;/*from w  w  w  . j  a v  a2 s  . c om*/
    StringBuffer importStatement = new StringBuffer();
    Collection importedNames = new ArrayList(exports.length);
    int i = 0;
    for (; i < exports.length; i++) {
        if (i != 0)
            importStatement.append(',');
        importedNames.add(exports[i].getName());
        getImportFrom(exports[i], importStatement);
    }
    for (Iterator iSystemExports = systemExports.iterator(); iSystemExports.hasNext();) {
        ExportPackageDescription systemExport = (ExportPackageDescription) iSystemExports.next();
        if (!importedNames.contains(systemExport.getName())) {
            if (i != 0)
                importStatement.append(',');
            i++;
            importStatement.append(systemExport.getName()).append(ELEMENT_SEPARATOR)
                    .append(Constants.BUNDLE_SYMBOLICNAME_ATTRIBUTE).append('=')
                    .append(Constants.SYSTEM_BUNDLE_SYMBOLICNAME);
        }
    }
    attrigutes.putValue(Constants.IMPORT_PACKAGE, importStatement.toString());
}

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

/**
 * ??/*from   ww  w.  j av  a  2  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

@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.taobao.android.apatch.ApkPatch.java

@SuppressWarnings("deprecation")
protected Manifest getMeta() {
    Manifest manifest = new Manifest();
    Attributes main = manifest.getMainAttributes();
    main.putValue("Manifest-Version", "1.0");
    main.putValue("Created-By", "1.0 (ApkPatch)");
    main.putValue("Created-Time", new Date(System.currentTimeMillis()).toGMTString());
    main.putValue("From-File", baseFiles.get(0).getName());
    main.putValue("To-File", newFiles.get(0).getName());
    main.putValue("Patch-Name", name);
    main.putValue(name + "-Patch-Classes", Formater.dotStringList(classes));
    main.putValue(name + "-Prepare-Classes", Formater.dotStringList(prepareClasses));
    main.putValue(name + "-Used-Methods", Formater.dotStringList(usedMethods));
    main.putValue(name + "-Modified-Classes", Formater.dotStringList(modifiedClasses));
    main.putValue(name + "-Used-Classes", Formater.dotStringList(usedClasses));
    main.putValue(name + "-add-classes", Formater.dotStringList(addClasses));
    return manifest;
}

From source file:fr.inria.atlanmod.neo4emf.neo4jresolver.runtimes.internal.Neo4JRuntimesManager.java

public void initializeRuntimeMetadata(AbstractNeo4jRuntimeInstaller installer, IPath path) {
    File dirFile = path.toFile();
    File manifestFile = path.append(META_INF).append(MANIFEST_MF).toFile();
    FileOutputStream outputStream = null;
    try {/*from w w w .j a  v  a2 s  . c  o  m*/
        FileUtils.forceMkdir(manifestFile.getParentFile());
        outputStream = new FileOutputStream(manifestFile);
        Manifest manifest = new Manifest();
        Attributes atts = manifest.getMainAttributes();
        atts.put(Attributes.Name.MANIFEST_VERSION, "1.0");
        atts.putValue(BUNDLE_MANIFEST_VERSION, "2");
        atts.putValue(BUNDLE_NAME, installer.getName());
        atts.putValue(BUNDLE_SYMBOLIC_NAME, installer.getId());
        atts.putValue(BUNDLE_VERSION, installer.getVersion());
        atts.putValue(BUNDLE_CLASS_PATH, buildJarFilesList(dirFile));
        atts.putValue(EXPORT_PACKAGE, buildPackagesList(dirFile));
        manifest.write(outputStream);
        load();
    } catch (Exception e) {
        Logger.log(Logger.SEVERITY_ERROR, e);
    } finally {
        IOUtils.closeQuietly(outputStream);
    }
}

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

/**
 * From the constructor of {@link JarInputStream}:
 * <p>/*from  ww w  .  jav 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.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;
        }//from  w  w  w. jav 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.eclipse.gemini.blueprint.test.AbstractOnTheFlyBundleCreatorTests.java

/**
 * Creates the default manifest in case none if found on the disk. By
 * default, the imports are synthetised based on the test class bytecode.
 * /*  w w w.j  a  v a  2 s . c om*/
 * @return default manifest for the jar created on the fly
 */
protected Manifest createDefaultManifest() {
    Manifest manifest = new Manifest();
    Attributes attrs = manifest.getMainAttributes();

    // manifest versions
    attrs.put(Attributes.Name.MANIFEST_VERSION, "1.0");
    attrs.putValue(Constants.BUNDLE_MANIFESTVERSION, "2");

    String description = getName() + "-" + getClass().getName();
    // name/description
    attrs.putValue(Constants.BUNDLE_NAME, "TestBundle-" + description);
    attrs.putValue(Constants.BUNDLE_SYMBOLICNAME, "TestBundle-" + description);
    attrs.putValue(Constants.BUNDLE_DESCRIPTION, "on-the-fly test bundle");

    // activator
    attrs.putValue(Constants.BUNDLE_ACTIVATOR, JUnitTestActivator.class.getName());

    // add Import-Package entry
    addImportPackage(manifest);

    if (logger.isDebugEnabled())
        logger.debug("Created manifest:" + manifest.getMainAttributes().entrySet());
    return manifest;
}

From source file:com.aimluck.eip.wiki.WikiSelectData.java

/**
 * @return/* w w w.  jav  a  2 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;
}