Search in sources :

Example 31 with Attributes

use of java.util.jar.Attributes in project robovm by robovm.

the class URLClassLoader method definePackage.

/**
     * Defines a new package using the information extracted from the specified
     * manifest.
     *
     * @param packageName
     *            the name of the new package.
     * @param manifest
     *            the manifest containing additional information for the new
     *            package.
     * @param url
     *            the URL to the code source for the new package.
     * @return the created package.
     * @throws IllegalArgumentException
     *             if a package with the given name already exists.
     */
protected Package definePackage(String packageName, Manifest manifest, URL url) throws IllegalArgumentException {
    Attributes mainAttributes = manifest.getMainAttributes();
    String dirName = packageName.replace('.', '/') + "/";
    Attributes packageAttributes = manifest.getAttributes(dirName);
    boolean noEntry = false;
    if (packageAttributes == null) {
        noEntry = true;
        packageAttributes = mainAttributes;
    }
    String specificationTitle = packageAttributes.getValue(Attributes.Name.SPECIFICATION_TITLE);
    if (specificationTitle == null && !noEntry) {
        specificationTitle = mainAttributes.getValue(Attributes.Name.SPECIFICATION_TITLE);
    }
    String specificationVersion = packageAttributes.getValue(Attributes.Name.SPECIFICATION_VERSION);
    if (specificationVersion == null && !noEntry) {
        specificationVersion = mainAttributes.getValue(Attributes.Name.SPECIFICATION_VERSION);
    }
    String specificationVendor = packageAttributes.getValue(Attributes.Name.SPECIFICATION_VENDOR);
    if (specificationVendor == null && !noEntry) {
        specificationVendor = mainAttributes.getValue(Attributes.Name.SPECIFICATION_VENDOR);
    }
    String implementationTitle = packageAttributes.getValue(Attributes.Name.IMPLEMENTATION_TITLE);
    if (implementationTitle == null && !noEntry) {
        implementationTitle = mainAttributes.getValue(Attributes.Name.IMPLEMENTATION_TITLE);
    }
    String implementationVersion = packageAttributes.getValue(Attributes.Name.IMPLEMENTATION_VERSION);
    if (implementationVersion == null && !noEntry) {
        implementationVersion = mainAttributes.getValue(Attributes.Name.IMPLEMENTATION_VERSION);
    }
    String implementationVendor = packageAttributes.getValue(Attributes.Name.IMPLEMENTATION_VENDOR);
    if (implementationVendor == null && !noEntry) {
        implementationVendor = mainAttributes.getValue(Attributes.Name.IMPLEMENTATION_VENDOR);
    }
    return definePackage(packageName, specificationTitle, specificationVersion, specificationVendor, implementationTitle, implementationVersion, implementationVendor, isSealed(manifest, dirName) ? url : null);
}
Also used : Attributes(java.util.jar.Attributes)

Example 32 with Attributes

use of java.util.jar.Attributes in project robovm by robovm.

the class OldJarURLConnectionTest method test_getMainAttributes.

public void test_getMainAttributes() throws Exception {
    URL u = createContent("lf.jar", "swt.dll");
    juc = (JarURLConnection) u.openConnection();
    java.util.jar.Attributes a = juc.getMainAttributes();
    assertEquals("Returned incorrect Attributes", "1.0", a.get(java.util.jar.Attributes.Name.MANIFEST_VERSION));
    URL invURL = createContent("InvalidJar.jar", "Test.class");
    JarURLConnection juConn = (JarURLConnection) invURL.openConnection();
    try {
        juConn.getMainAttributes();
        fail("IOException was not thrown.");
    } catch (java.io.IOException io) {
    //expected
    }
}
Also used : JarURLConnection(java.net.JarURLConnection) IOException(java.io.IOException) Attributes(java.util.jar.Attributes) URL(java.net.URL)

Example 33 with Attributes

use of java.util.jar.Attributes in project bazel by bazelbuild.

the class SingleJar method createManifest.

/**
   * Creates a manifest and returns an input stream for its contents.
   */
private InputStream createManifest() throws IOException {
    Manifest manifest = new Manifest();
    Attributes attributes = manifest.getMainAttributes();
    attributes.put(Attributes.Name.MANIFEST_VERSION, "1.0");
    attributes.put(new Attributes.Name("Created-By"), "blaze-singlejar");
    if (mainClass != null) {
        attributes.put(Attributes.Name.MAIN_CLASS, mainClass);
    }
    if (extraManifestContent != null) {
        ByteArrayInputStream in = new ByteArrayInputStream(extraManifestContent.getBytes("UTF8"));
        manifest.read(in);
    }
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    manifest.write(out);
    return new ByteArrayInputStream(out.toByteArray());
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) Attributes(java.util.jar.Attributes) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Manifest(java.util.jar.Manifest)

Example 34 with Attributes

use of java.util.jar.Attributes in project platform_frameworks_base by android.

the class StrictJarManifest method write.

/**
     * Writes out the attribute information of the specified manifest to the
     * specified {@code OutputStream}
     *
     * @param manifest
     *            the manifest to write out.
     * @param out
     *            The {@code OutputStream} to write to.
     * @throws IOException
     *             If an error occurs writing the {@code StrictJarManifest}.
     */
static void write(StrictJarManifest manifest, OutputStream out) throws IOException {
    CharsetEncoder encoder = StandardCharsets.UTF_8.newEncoder();
    ByteBuffer buffer = ByteBuffer.allocate(LINE_LENGTH_LIMIT);
    Attributes.Name versionName = Attributes.Name.MANIFEST_VERSION;
    String version = manifest.mainAttributes.getValue(versionName);
    if (version == null) {
        versionName = Attributes.Name.SIGNATURE_VERSION;
        version = manifest.mainAttributes.getValue(versionName);
    }
    if (version != null) {
        writeEntry(out, versionName, version, encoder, buffer);
        Iterator<?> entries = manifest.mainAttributes.keySet().iterator();
        while (entries.hasNext()) {
            Attributes.Name name = (Attributes.Name) entries.next();
            if (!name.equals(versionName)) {
                writeEntry(out, name, manifest.mainAttributes.getValue(name), encoder, buffer);
            }
        }
    }
    out.write(LINE_SEPARATOR);
    Iterator<String> i = manifest.getEntries().keySet().iterator();
    while (i.hasNext()) {
        String key = i.next();
        writeEntry(out, Attributes.Name.NAME, key, encoder, buffer);
        Attributes attributes = manifest.entries.get(key);
        Iterator<?> entries = attributes.keySet().iterator();
        while (entries.hasNext()) {
            Attributes.Name name = (Attributes.Name) entries.next();
            writeEntry(out, name, attributes.getValue(name), encoder, buffer);
        }
        out.write(LINE_SEPARATOR);
    }
}
Also used : Attributes(java.util.jar.Attributes) CharsetEncoder(java.nio.charset.CharsetEncoder) ByteBuffer(java.nio.ByteBuffer)

Example 35 with Attributes

use of java.util.jar.Attributes in project gocd by gocd.

the class JarUtil method getManifestKey.

public static String getManifestKey(String jar, String key) {
    String version = null;
    try {
        JarFile jarFile = new JarFile(jar);
        Manifest manifest = jarFile.getManifest();
        if (manifest != null) {
            Attributes attributes = manifest.getMainAttributes();
            version = attributes.getValue(key);
        }
    } catch (IOException e) {
        LOG.error("Exception while trying to read Go-Version from " + jar + ":" + e.toString());
    }
    return version;
}
Also used : Attributes(java.util.jar.Attributes) IOException(java.io.IOException) JarFile(java.util.jar.JarFile) Manifest(java.util.jar.Manifest)

Aggregations

Attributes (java.util.jar.Attributes)631 Manifest (java.util.jar.Manifest)358 IOException (java.io.IOException)152 File (java.io.File)143 JarFile (java.util.jar.JarFile)106 URL (java.net.URL)92 Map (java.util.Map)78 InputStream (java.io.InputStream)62 HashMap (java.util.HashMap)62 Jar (aQute.bnd.osgi.Jar)49 Builder (aQute.bnd.osgi.Builder)43 ArrayList (java.util.ArrayList)43 ByteArrayOutputStream (java.io.ByteArrayOutputStream)42 Test (org.junit.Test)41 ZipEntry (java.util.zip.ZipEntry)39 FileOutputStream (java.io.FileOutputStream)37 JarOutputStream (java.util.jar.JarOutputStream)36 ByteArrayInputStream (java.io.ByteArrayInputStream)33 FileInputStream (java.io.FileInputStream)33 JarEntry (java.util.jar.JarEntry)32