Example usage for java.util.jar Attributes put

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

Introduction

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

Prototype

public Object put(Object name, Object value) 

Source Link

Document

Associates the specified value with the specified attribute name (key) in this Map.

Usage

From source file:org.eclipse.ebr.maven.BundleMojo.java

private File generateFinalBundleManifest() throws MojoExecutionException {
    try {/*from ww  w  . ja  v a2 s .  c  o m*/
        File mfile = new File(outputDirectory, "META-INF/MANIFEST.MF");
        final InputStream is = new FileInputStream(mfile);
        Manifest mf;
        try {
            mf = new Manifest(is);
        } finally {
            is.close();
        }
        final Attributes attributes = mf.getMainAttributes();

        if (attributes.getValue(Name.MANIFEST_VERSION) == null) {
            attributes.put(Name.MANIFEST_VERSION, "1.0");
        }

        // shameless self-promotion
        attributes.putValue(CREATED_BY, "Eclipse Bundle Recipe Maven Plug-in");

        final String expandedVersion = getExpandedVersion();
        attributes.putValue(BUNDLE_VERSION, expandedVersion);

        mfile = getFinalBundleManifestFile();
        mfile.getParentFile().mkdirs();
        final BufferedOutputStream os = new BufferedOutputStream(new FileOutputStream(mfile));
        try {
            mf.write(os);
        } finally {
            os.close();
        }

        return mfile;
    } catch (final Exception e) {
        throw new MojoExecutionException("Error generating bundle manifest: " + e.getMessage(), e);
    }
}

From source file:org.eclipse.ebr.maven.BundleMojo.java

private File generateSourceBundleManifest() throws MojoExecutionException {
    try {//from w  w  w. j a  va 2  s  . c  om
        generateSourceBundleL10nFile();

        final Manifest mf = new Manifest();
        final Attributes attributes = mf.getMainAttributes();

        if (attributes.getValue(Name.MANIFEST_VERSION) == null) {
            attributes.put(Name.MANIFEST_VERSION, "1.0");
        }

        final String expandedVersion = getExpandedVersion();
        attributes.putValue(BUNDLE_VERSION, expandedVersion);
        attributes.putValue(BUNDLE_MANIFESTVERSION, "2");
        attributes.putValue(BUNDLE_SYMBOLICNAME, getSourceBundleSymbolicName());
        attributes.putValue(BUNDLE_NAME, I18N_KEY_PREFIX + I18N_KEY_BUNDLE_NAME);
        attributes.putValue(BUNDLE_VENDOR, I18N_KEY_PREFIX + I18N_KEY_BUNDLE_VENDOR);
        //attributes.putValue(BUNDLE_LOCALIZATION, BUNDLE_LOCALIZATION_DEFAULT_BASENAME);
        attributes.putValue("Eclipse-SourceBundle",
                project.getArtifactId() + ";version=\"" + expandedVersion + "\";roots:=\".\"");
        attributes.putValue(CREATED_BY, "Eclipse Bundle Recipe Maven Plug-in");

        final File mfile = getSourceBundleManifestFile();
        mfile.getParentFile().mkdirs();
        final BufferedOutputStream os = new BufferedOutputStream(new FileOutputStream(mfile));
        try {
            mf.write(os);
        } finally {
            os.close();
        }

        return mfile;
    } catch (final Exception e) {
        throw new MojoExecutionException("Error generating source bundle manifest: " + e.getMessage(), e);
    }
}

From source file:org.hecl.jarhack.JarHack.java

/**
 * The <code>substHecl</code> method takes the filenames of two
 * .jar's - one as input, the second as output, in addition to the
 * name of the application.  Where it counts, the old name (Hecl,
 * usually) is overridden with the new name, and the new .jar file
 * is written to the specified outfile.  Via the iconname argument
 * it is also possible to specify a new icon file to use.
 *
 * @param infile a <code>FileInputStream</code> value
 * @param outfile a <code>String</code> value
 * @param newname a <code>String</code> value
 * @param iconname a <code>String</code> value
 * @exception IOException if an error occurs
 *///from w w  w .j  a v a 2s  .com
public static void substHecl(InputStream infile, String outfile, String newname, String iconname,
        String scriptfile) throws IOException {

    JarInputStream jif = new JarInputStream(infile);
    Manifest mf = jif.getManifest();
    Attributes attrs = mf.getMainAttributes();

    Set keys = attrs.keySet();
    Iterator it = keys.iterator();
    while (it.hasNext()) {
        Object key = it.next();
        Object value = attrs.get(key);
        String keyname = key.toString();

        /* These are the three cases that interest us in
         * particular, where we need to make changes. */
        if (keyname.equals("MIDlet-Name")) {
            attrs.putValue(keyname, newname);
        } else if (keyname.equals("MIDlet-1")) {
            String valuestr = value.toString();
            /* FIXME - the stringsplit method is used for older
             * versions of GCJ.  Once newer versions are common,
             * it can go away.  Or not - it works just fine. */
            String properties[] = stringsplit(valuestr, ", ");
            attrs.putValue(keyname, newname + ", " + properties[1] + ", " + properties[2]);
        } else if (keyname.equals("MicroEdition-Configuration")) {
            cldcversion = value.toString();
        } else if (keyname.equals("MicroEdition-Profile")) {
            midpversion = value.toString();
        } else if (keyname.equals("MIDlet-Jar-URL")) {
            attrs.put(key, newname + ".jar");
        }
    }

    JarOutputStream jof = new JarOutputStream(new FileOutputStream(outfile), mf);

    byte[] buf = new byte[4096];

    /* Go through the various entries. */
    JarEntry entry;
    int read;
    while ((entry = jif.getNextJarEntry()) != null) {

        /* Don't copy the manifest file. */
        if ("META-INF/MANIFEST.MF".equals(entry.getName()))
            continue;

        /* Insert our own icon */
        if (iconname != null && "Hecl.png".equals(entry.getName())) {
            jof.putNextEntry(new JarEntry("Hecl.png"));
            FileInputStream inf = new FileInputStream(iconname);
            while ((read = inf.read(buf)) != -1) {
                jof.write(buf, 0, read);
            }
            inf.close();
        }
        /* Insert our own copy of the script file. */
        else if ("script.hcl".equals(entry.getName())) {
            jof.putNextEntry(new JarEntry("script.hcl"));
            FileInputStream inf = new FileInputStream(scriptfile);
            while ((read = inf.read(buf)) != -1) {
                jof.write(buf, 0, read);
            }
            inf.close();
        } else {
            /* Otherwise, just copy the entry. */
            jof.putNextEntry(entry);
            while ((read = jif.read(buf)) != -1) {
                jof.write(buf, 0, read);
            }
        }

        jof.closeEntry();
    }

    jof.flush();
    jof.close();
    jif.close();
}

From source file:org.jahia.services.modulemanager.util.ModuleUtils.java

/**
 * Calculates the value and set the Provide-Capability manifest attribute and modifies it.
 *
 * @param moduleId the ID of the module.
 * @param atts the manifest attributes//w  w w.j a  v a  2s  . c o m
 */
private static void populateProvideCapabilities(String moduleId, Attributes atts) {
    StringBuilder provide = new StringBuilder();
    String existingProvideValue = atts.getValue(ATTR_NAME_PROVIDE_CAPABILITY);
    if (StringUtils.isNotEmpty(existingProvideValue)) {
        provide.append(existingProvideValue).append(",");
    }
    provide.append(buildClauseProvideCapability(moduleId));
    String bundleName = atts.getValue(ATTR_NAME_BUNDLE_NAME);
    if (StringUtils.isNotEmpty(bundleName)) {
        provide.append(",").append(buildClauseProvideCapability(bundleName));
    }
    atts.put(ATTR_NAME_PROVIDE_CAPABILITY, provide.toString());
}

From source file:org.jahia.services.modulemanager.util.ModuleUtils.java

/**
 * Calculates the value and set the Require-Capability manifest attribute.
 *
 * @param moduleId the ID of the module//from  w w  w  . ja  v a2  s.c o m
 * @param atts the manifest attributes
 */
private static void populateRequireCapabilities(String moduleId, Attributes atts) {
    List<String> caps = getRequireCapabilities(moduleId, atts.getValue(ATTR_NAME_JAHIA_DEPENDS));
    if (caps.size() > 0) {
        StringBuilder require = new StringBuilder();
        String existingRequireValue = atts.getValue(ATTR_NAME_REQUIRE_CAPABILITY);
        if (StringUtils.isNotEmpty(existingRequireValue)) {
            require.append(existingRequireValue);
        }
        for (String cap : caps) {
            if (require.length() > 0) {
                require.append(",");
            }
            require.append(cap);
        }
        atts.put(ATTR_NAME_REQUIRE_CAPABILITY, require.toString());
    }
}

From source file:org.kepler.kar.KARFile.java

/**
 * @param lsid//www .  j  a  v a2s .  c  om
 * @throws IOException
 */
public void setLSID(KeplerLSID lsid) throws IOException {
    Attributes atts = getManifest().getMainAttributes();
    if (atts.containsKey(LSID)) {
        atts.remove(LSID);
    }
    atts.put(LSID, lsid.toString());
}

From source file:org.kepler.kar.KARFile.java

/**
 * Set the version of the KARFile.//from  w  w w.ja  v a 2  s  .  co  m
 * 
 * @param version
 * @throws IOException
 *             if the given version is not supported
 */
public void setVersion(String version) throws IOException {
    if (_supportedVersions.contains(version)) {
        Attributes atts = getManifest().getMainAttributes();
        if (atts.containsKey(KAR_VERSION)) {
            atts.remove(KAR_VERSION);
        }
        atts.put(KAR_VERSION, version);
    } else {
        throw new IOException(version + " is not a supported KAR version.\n" + getSupportedVersionString());
    }
}

From source file:org.nuclos.server.customcode.codegenerator.NuclosJavaCompilerComponent.java

private Manifest getManifest() {
    HashCodeBuilder builder = new HashCodeBuilder(11, 17);
    for (CodeGenerator gen : getAllCurrentGenerators()) {
        builder.append(gen.hashForManifest());
    }//from w  w  w.  java  2 s .c o  m

    Manifest manifest = new Manifest();
    Attributes mainAttributes = manifest.getMainAttributes();
    mainAttributes.put(Attributes.Name.MANIFEST_VERSION, "1.0");
    mainAttributes.put(NUCLOS_CODE_NUCLET, "default");
    mainAttributes.put(NUCLOS_CODE_HASH, String.valueOf(builder.toHashCode()));
    return manifest;
}

From source file:org.talend.designer.maven.utils.ClasspathsJarGenerator.java

public static String createJar(Property property, String classpath, String separator) throws Exception {
    String newClasspath = generateClasspathForManifest(classpath, separator);

    Manifest manifest = new Manifest();
    Attributes a = manifest.getMainAttributes();
    a.put(Attributes.Name.MANIFEST_VERSION, "1.0"); //$NON-NLS-1$
    a.put(Attributes.Name.IMPLEMENTATION_VENDOR, "Talend Open Studio"); //$NON-NLS-1$
    a.put(Attributes.Name.CLASS_PATH, newClasspath);

    String jarLocation = getJarLocation(property);
    File jarFile = new File(jarLocation);
    if (!jarFile.exists()) {
        jarFile.createNewFile();//w w w.j a  v a2  s.c  o  m
    }
    JarOutputStream stream = null;
    try {
        stream = new JarOutputStream(new FileOutputStream(jarLocation), manifest);
        stream.flush();
    } finally {
        stream.close();
    }

    return getFinalClasspath(classpath, separator, jarLocation);
}