Java Code Examples for java.util.jar.Attributes#remove()

The following examples show how to use java.util.jar.Attributes#remove() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: EmbeddedFelixFramework.java    From brooklyn-server with Apache License 2.0 6 votes vote down vote up
private static byte[] buildExtensionBundle(Manifest manifest) throws IOException {
    Attributes atts = manifest.getMainAttributes();

    //the following properties are invalid in extension bundles
    atts.remove(new Attributes.Name(Constants.IMPORT_PACKAGE));
    atts.remove(new Attributes.Name(Constants.REQUIRE_BUNDLE));
    atts.remove(new Attributes.Name(Constants.BUNDLE_NATIVECODE));
    atts.remove(new Attributes.Name(Constants.DYNAMICIMPORT_PACKAGE));
    atts.remove(new Attributes.Name(Constants.BUNDLE_ACTIVATOR));

    //mark as extension bundle
    atts.putValue(Constants.FRAGMENT_HOST, "system.bundle; extension:=framework");

    //create the jar containing the manifest
    ByteArrayOutputStream jar = new ByteArrayOutputStream();
    JarOutputStream out = new JarOutputStream(jar, manifest);
    out.close();
    return jar.toByteArray();
}
 
Example 2
Source File: ContentComparator.java    From netbeans with Apache License 2.0 5 votes vote down vote up
/**
 *  Compares two manifest files. First check is for number of attributes,
 * next one is comparing name-value pairs.
 *
 *@param mf1, mf2 manifests to compare
 *@param ignoredEntries array of manifest entries to ignore
 *@return true if files contains the same entries/values in manifest
 */
public static boolean equalsManifest(File mf1, File mf2, String[] ignoredEntries) {
    if (ignoredEntries == null) {
        ignoredEntries = new String[] {};
    }
    try {
        Manifest m1 = new Manifest(new FileInputStream(mf1));
        Manifest m2 = new Manifest(new FileInputStream(mf2));
        Attributes a1 = m1.getMainAttributes();
        Attributes a2 = m2.getMainAttributes();
        if (a1.size() != a2.size()) {
            return false;
        }
        for (Iterator<Object> i = a1.keySet().iterator(); i.hasNext();) {
            Attributes.Name a = (Attributes.Name) i.next();
            boolean b = true;
            for (int j = 0; j < ignoredEntries.length; j++) {
                if (a.toString().equals(ignoredEntries[j])) {
                    a2.remove(a);
                    b = false;
                    break;
                }
            }
            if (b && (a1.get(a).equals(a2.get(a)))) {
                a2.remove(a);
            }
        }
        return a2.isEmpty();
    } catch (FileNotFoundException fnfe) {
        LOGGER.log(Level.WARNING, "Exception from test - comparing manifests", fnfe); //NOI18N
    } catch (IOException ioe) {
        LOGGER.log(Level.WARNING, "Exception from test - comparing manifests", ioe); //NOI18N
    }
    return false;
}
 
Example 3
Source File: ContentComparator.java    From netbeans with Apache License 2.0 5 votes vote down vote up
/**
 *  Compares two manifest files. First check is for number of attributes,
 * next one is comparing name-value pairs.
 *
 *@param mf1, mf2 manifests to compare
 *@param ignoredEntries array of manifest entries to ignore
 *@return true if files contains the same entries/values in manifest
 */
public static boolean equalsManifest(File mf1, File mf2, String[] ignoredEntries) {
    if (ignoredEntries == null) {
        ignoredEntries = new String[]{};
    }
    try {
        Manifest m1 = new Manifest(new FileInputStream(mf1));
        Manifest m2 = new Manifest(new FileInputStream(mf2));
        Attributes a1 = m1.getMainAttributes();
        Attributes a2 = m2.getMainAttributes();
        if (a1.size() != a2.size()) {
            return false;
        }
        for (Iterator<Object> i = a1.keySet().iterator(); i.hasNext();) {
            Attributes.Name a = (Attributes.Name) i.next();
            boolean b = true;
            for (int j = 0; j < ignoredEntries.length; j++) {
                if (a.toString().equals(ignoredEntries[j])) {
                    a2.remove(a);
                    b = false;
                    break;
                }
            }
            if (b && (a1.get(a).equals(a2.get(a)))) {
                a2.remove(a);
            }
        }
        return a2.isEmpty();
    } catch (FileNotFoundException fnfe) {
        System.err.println("Exception from test - comparing manifests");
        fnfe.printStackTrace(System.err);
    } catch (IOException ioe) {
        System.err.println("Exception from test - comparing manifests");
        ioe.printStackTrace(System.err);
    }
    return false;
}
 
Example 4
Source File: OutputConsumerPath.java    From tiny-remapper with GNU Lesser General Public License v3.0 5 votes vote down vote up
private static void fixManifest(Manifest manifest, TinyRemapper remapper) {
	Attributes mainAttrs = manifest.getMainAttributes();

	if (remapper != null) {
		String val = mainAttrs.getValue(Attributes.Name.MAIN_CLASS);
		if (val != null) mainAttrs.put(Attributes.Name.MAIN_CLASS, mapFullyQualifiedClassName(val, remapper));

		val = mainAttrs.getValue("Launcher-Agent-Class");
		if (val != null) mainAttrs.put("Launcher-Agent-Class", mapFullyQualifiedClassName(val, remapper));
	}

	mainAttrs.remove(Attributes.Name.SIGNATURE_VERSION);

	for (Iterator<Attributes> it = manifest.getEntries().values().iterator(); it.hasNext(); ) {
		Attributes attrs = it.next();

		for (Iterator<Object> it2 = attrs.keySet().iterator(); it2.hasNext(); ) {
			Attributes.Name attrName = (Attributes.Name) it2.next();
			String name = attrName.toString();

			if (name.endsWith("-Digest") || name.contains("-Digest-") || name.equals("Magic")) {
				it2.remove();
			}
		}

		if (attrs.isEmpty()) it.remove();
	}
}
 
Example 5
Source File: OsgiTest.java    From SimpleFlatMapper with MIT License 5 votes vote down vote up
private URL repackage(URL asm6) throws IOException {
    File tmpFile = File.createTempFile("bundle", "jar");
    try (InputStream fis = asm6.openStream();
         JarInputStream jis = new JarInputStream(fis);
    ) {
        Manifest man = jis.getManifest();

        Attributes mainAttributes = man.getMainAttributes();

        System.out.println("mainAttributes = " + mainAttributes.keySet());

        mainAttributes.remove(new Attributes.Name("Bundle-RequiredExecutionEnvironment"));
        mainAttributes.remove(new Attributes.Name("Require-Capability"));


        try (FileOutputStream fos = new FileOutputStream(tmpFile);
             JarOutputStream jos = new JarOutputStream(fos, man)
        ) {
            JarEntry zentry;
            while ((zentry = jis.getNextJarEntry()) != null) {
                jos.putNextEntry(zentry);
                IOUtils.copy(jis, jos);
            }
        } catch (Throwable t) {
            System.out.println("t = " + t);
            t.printStackTrace(System.out);
            throw t;
        }
    }

    return tmpFile.toURI().toURL();
}
 
Example 6
Source File: GuardDB.java    From yGuard with MIT License 4 votes vote down vote up
private void updateManifest(int manifestIndex, String inName, String outName, MessageDigest[] digests)
{
  // Create fresh section for entry, and enter "Name" header

  Manifest nm = newManifest[manifestIndex];
  Manifest om = oldManifest[manifestIndex];

  Attributes oldAtts = om.getAttributes(inName);
  Attributes newAtts = new Attributes();
  //newAtts.putValue(MANIFEST_NAME_TAG, outName);

  // copy over non-name and none digest entries
  if (oldAtts != null){
    for(Iterator it = oldAtts.entrySet().iterator(); it.hasNext();){
      Map.Entry entry = (Map.Entry) it.next();
      Object key = entry.getKey();
      String name = key.toString();
      if (!name.equalsIgnoreCase(MANIFEST_NAME_TAG) &&
          name.indexOf("Digest") == -1){
        newAtts.remove(name);
        newAtts.putValue(name, (String)entry.getValue());
      }
    }
  }

  // Create fresh digest entries in the new section
  if (digests != null && digests.length > 0)
  {
    // Digest-Algorithms header
    StringBuffer sb = new StringBuffer();
    for (int i = 0; i < digests.length; i++)
    {
      sb.append(digests[i].getAlgorithm());
      if (i < digests.length -1){
        sb.append(", ");
      }
    }
    newAtts.remove(MANIFEST_DIGESTALG_TAG);
    newAtts.putValue(MANIFEST_DIGESTALG_TAG, sb.toString());

    // *-Digest headers
    for (int i = 0; i < digests.length; i++)
    {
      newAtts.remove(digests[i].getAlgorithm() + "-Digest");
      newAtts.putValue(digests[i].getAlgorithm() + "-Digest", Tools.toBase64(digests[i].digest()));
    }
  }

  if (!newAtts.isEmpty()) {
    // Append the new section to the new manifest
    nm.getEntries().put(outName, newAtts);
  }
}