Example usage for java.util.jar Attributes keySet

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

Introduction

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

Prototype

public Set<Object> keySet() 

Source Link

Document

Returns a Set view of the attribute names (keys) contained in this Map.

Usage

From source file:com.commercehub.dropwizard.BuildInfoServlet.java

@Override
public void init(ServletConfig config) {
    objectWriter = new ObjectMapper().writer();

    cacheControl = new CacheControl();
    cacheControl.setMustRevalidate(true);
    cacheControl.setNoCache(true);// w ww .j a va 2  s. co  m
    cacheControl.setNoStore(true);

    // cache build information
    manifestAttributes = new Properties();
    ImmutableSet<String> attributeBlacklist = getAttributeBlacklist(config);
    try {
        Enumeration<URL> resources = getClass().getClassLoader().getResources(JarFile.MANIFEST_NAME);
        if (resources != null) {
            while (resources.hasMoreElements()) {
                Manifest manifest = new Manifest(resources.nextElement().openStream());
                Attributes attributes = manifest.getMainAttributes();
                for (Object key : attributes.keySet()) {
                    Attributes.Name attrName = (Attributes.Name) key;
                    if (!attributeBlacklist.contains(attrName.toString())) {
                        manifestAttributes.setProperty(attrName.toString(), attributes.getValue(attrName));
                    }
                }
            }
        }
    } catch (IOException e) {
        log.warn("Unable to retrieve build info", e);
    }
}

From source file:com.vmware.vfabric.hyperic.plugin.vfws.BmxResult.java

public Properties parseToProperties() throws IOException {
    InputStream is = _response.getEntity().getContent();
    props = new Properties();

    Manifest manifest = new Manifest(is);
    Attributes attributes = manifest.getMainAttributes();
    if (null == attributes) {
        _log.error("Unable to parse results. No attributes found");
        return null;
    }/*from  w w  w  .j av  a  2  s . com*/

    for (Iterator<Object> it = attributes.keySet().iterator(); it.hasNext();) {
        Name key = (Name) it.next();
        if (key == null) {
            _log.debug("Skipping null key");
            continue;
        }
        Object value = attributes.get(key);
        if (value.getClass() != String.class) {
            _log.error("Attribute value not of class String");
            continue;
        }
        String keyName = key.toString();
        String val = (String) value;
        props.put(keyName, val);
    }
    return props;
}

From source file:com.vmware.vfabric.hyperic.plugin.vfws.VfwsCollector.java

private void parse(HttpResponse response) throws IOException {
    InputStream is = response.getEntity().getContent();

    if (is == null) {
        log.error("Unable to retrieve results. InputStream is null");
        return;//from   w  ww  .  j  av a 2 s . co  m
    }

    Manifest manifest = new Manifest(is);
    Attributes attributes = manifest.getMainAttributes();
    if (null == attributes) {
        log.error("Unable to parse results. No attributes found");
        return;
    }

    Iterator it = attributes.keySet().iterator();
    while (it.hasNext()) {
        Name key = (Name) it.next();
        if (key == null) {
            log.debug("Skipping null key");
            continue;
        }
        Object value = attributes.get(key);
        if (value.getClass() != String.class) {
            log.error("Attribute value not of class String");
            continue;
        }
        String keyName = key.toString();
        String val = (String) value;
        // BusyWorkers and IdleWorkers have u in the values
        if (keyName.contains("Workers")) {
            setWorkers(keyName, val);
        } else if (keyName.contains("StartTime")) {
            setStartTime(keyName, val);
        } else {
            setValue(keyName, val);
        }
    }
}

From source file:org.alfresco.web.scripts.ShareManifest.java

/**
 * Retrieve a list of attribute names (Strings) for the main
 * section of a manifest./*from w  w  w .j a v a  2  s . co m*/
 * 
 * @return       The list of attribute names.
 */
public List<String> mainAttributeNames() {
    List<String> names = Collections.emptyList();

    Attributes attributes = manifest.getMainAttributes();
    if (attributes != null) {
        names = namesToStrings(attributes.keySet());
    }

    return names;
}

From source file:org.alfresco.web.scripts.ShareManifest.java

/**
 * Retrieve a list of attribute names (Strings) for the named section of a manifest.
 * /* w  ww  .  j a  v a  2 s .  co m*/
 * @param section    Section name.
 * @return           The list of attribute names.
 */
public List<String> attributeNames(String section) {
    List<String> names = Collections.emptyList();

    Attributes attributes = manifest.getAttributes(section);
    if (attributes != null) {
        names = namesToStrings(attributes.keySet());
    }

    return names;
}

From source file:org.apache.sling.maven.slingstart.PreparePackageMojoTest.java

@Test
public void testBSNRenaming() throws Exception {
    // Provide the system with some artifacts that are known to be in the local .m2 repo
    // These are explicitly included in the test section of the pom.xml
    PreparePackageMojo ppm = getMojoUnderTest("org.apache.sling/org.apache.sling.commons.classloader/1.3.2",
            "org.apache.sling/org.apache.sling.commons.classloader/1.3.2/app",
            "org.apache.sling/org.apache.sling.commons.json/2.0.12");
    try {/*from www. jav a 2  s . c om*/
        String modelTxt = "[feature name=:launchpad]\n" + "[artifacts]\n"
                + "  org.apache.sling/org.apache.sling.commons.classloader/1.3.2\n" + ""
                + "[feature name=rename_test]\n"
                + "  org.apache.sling/org.apache.sling.commons.json/2.0.12 [bundle:rename-bsn=r-foo.bar.renamed.sling.commons.json]\n";

        Model model = ModelReader.read(new StringReader(modelTxt), null);
        ppm.execute(model);

        File orgJar = getMavenArtifactFile(getMavenRepoRoot(), "org.apache.sling",
                "org.apache.sling.commons.json", "2.0.12");
        File generatedJar = new File(ppm.getTmpDir() + "/r-foo.bar.renamed.sling.commons.json-2.0.12.jar");

        compareJarContents(orgJar, generatedJar);

        try (JarFile jfOrg = new JarFile(orgJar); JarFile jfNew = new JarFile(generatedJar)) {
            Manifest mfOrg = jfOrg.getManifest();
            Manifest mfNew = jfNew.getManifest();

            Attributes orgAttrs = mfOrg.getMainAttributes();
            Attributes newAttrs = mfNew.getMainAttributes();
            for (Object key : orgAttrs.keySet()) {
                String orgVal = orgAttrs.getValue(key.toString());
                String newVal = newAttrs.getValue(key.toString());

                if ("Bundle-SymbolicName".equals(key.toString())) {
                    assertEquals("Should have recorded the original Bundle-SymbolicName", orgVal,
                            newAttrs.getValue("X-Original-Bundle-SymbolicName"));

                    assertEquals("r-foo.bar.renamed.sling.commons.json", newVal);
                } else {
                    assertEquals("Different keys: " + key, orgVal, newVal);
                }
            }
        }
    } finally {
        FileUtils.deleteDirectory(new File(ppm.project.getBuild().getDirectory()));
    }
}

From source file:org.docx4j.jaxb.Context.java

public static void searchManifestsForJAXBImplementationInfo(ClassLoader loader) {
    Enumeration resEnum;/*from  ww w .j  a  va2s .  c  o  m*/
    try {
        resEnum = loader.getResources(JarFile.MANIFEST_NAME);
        while (resEnum.hasMoreElements()) {
            InputStream is = null;
            try {
                URL url = (URL) resEnum.nextElement();
                //                   System.out.println("\n\n" + url);
                is = url.openStream();
                if (is != null) {
                    Manifest manifest = new Manifest(is);

                    Attributes mainAttribs = manifest.getMainAttributes();
                    String impTitle = mainAttribs.getValue("Implementation-Title");
                    if (impTitle != null && impTitle.contains("JAXB Reference Implementation")
                            || impTitle.contains("org.eclipse.persistence")) {

                        log.info("\n" + url);
                        for (Object key2 : mainAttribs.keySet()) {

                            log.info(key2 + " : " + mainAttribs.getValue((java.util.jar.Attributes.Name) key2));
                        }
                    }

                    // In 2.1.3, it is in here
                    for (String key : manifest.getEntries().keySet()) {
                        //System.out.println(key);                       
                        if (key.equals("com.sun.xml.bind.v2.runtime")) {
                            log.info("Found JAXB reference implementation in " + url);
                            mainAttribs = manifest.getAttributes((String) key);

                            for (Object key2 : mainAttribs.keySet()) {
                                log.info(key2 + " : "
                                        + mainAttribs.getValue((java.util.jar.Attributes.Name) key2));
                            }
                        }
                    }

                }
            } catch (Exception e) {
                // Silently ignore 
                //                  log.error(e.getMessage(), e);
            } finally {
                IOUtils.closeQuietly(is);
            }
        }
    } catch (IOException e1) {
        // Silently ignore 
        //           log.error(e1);
    }

}

From source file:org.eclipse.gemini.blueprint.test.FilteringProbeBuilder.java

private void appendManifest(TinyBundle bundle) throws IOException {

    DefaultResourceLoader loader = new DefaultResourceLoader();
    Manifest manifest = new Manifest(loader.getResource(manifestLocation).getInputStream());
    Attributes attr = manifest.getMainAttributes();
    for (Object key : attr.keySet()) {
        String k = key.toString();
        String v = attr.getValue(k);

        // append optional import for org.ops4j.pax.exam
        if (k.equalsIgnoreCase(Constants.IMPORT_PACKAGE)) {
            if (StringUtils.hasText(v)) {
                v = v + ",";
            }//from  ww w . j a va 2  s .c om
        }
        v = v + "org.ops4j.pax.exam;resolution:=optional";
        bundle.set(k, v);
    }
}

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
 *///w  w  w .j av a  2  s.  c om
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.kepler.kar.KARBuilder.java

/**
 * Prepare the KAR Manifest based on the kar items.
 *
 * @param overrideModDeps - Optional override of kar's module dependencies, 
 * set null for normal use.//from   w w  w.  ja v  a  2  s  .  co  m
 * @throws Exception
 */
private void prepareManifest(String overrideModDeps) throws Exception {

    _manifest.addMainAttribute(KARFile.LSID.toString(), _karLSID.toString());
    if (overrideModDeps == null) {
        _manifest.addMainAttribute(KARFile.MOD_DEPEND.toString(),
                ModuleDependencyUtil.buildModuleDependenciesString());
    } else {
        //System.out.println("KARBuilder prepareManifest using overrideModDeps:"+overrideModDeps);
        _manifest.addMainAttribute(KARFile.MOD_DEPEND.toString(), overrideModDeps);
    }

    // add all the KAREntry attributes to the KARManifest
    Vector<KAREntry> toRemove = new Vector<KAREntry>(1);
    for (KAREntry ke : _karItems.keySet()) {

        Attributes atts = ke.getAttributes();

        // Check required attributes
        String entryLSID = atts.getValue(KAREntry.LSID);
        String entryType = atts.getValue(KAREntry.TYPE);
        String entryHandler = atts.getValue(KAREntry.HANDLER);

        if (entryLSID == null || entryType == null || entryHandler == null) {
            log.warn(ke.getName()
                    + " KAREntry did not have an LSID, Type, or Handler attribute.  KAREntry removed.");
            toRemove.add(ke);

        } else {

            // add all attributes of the karentry to the manifest
            for (Object att : atts.keySet()) {
                if (att instanceof Name) {
                    Name attName = (Name) att;
                    String attValue = atts.getValue(attName);
                    _manifest.addEntryAttribute(ke, attName.toString(), attValue);
                }
            }
        }
    }
    if (toRemove.size() > 0) {

        for (KAREntry ke : toRemove) {
            _karItemLSIDs.remove(ke.getLSID());
            _karItems.remove(ke);
        }

    }

}