Search in sources :

Example 36 with Attributes

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

the class JarCreator method manifestContent.

private byte[] manifestContent() throws IOException {
    Manifest manifest;
    if (manifestFile != null) {
        FileInputStream in = new FileInputStream(manifestFile);
        manifest = new Manifest(in);
    } else {
        manifest = new Manifest();
    }
    Attributes attributes = manifest.getMainAttributes();
    attributes.put(Attributes.Name.MANIFEST_VERSION, "1.0");
    Attributes.Name createdBy = new Attributes.Name("Created-By");
    if (attributes.getValue(createdBy) == null) {
        attributes.put(createdBy, "blaze");
    }
    if (mainClass != null) {
        attributes.put(Attributes.Name.MAIN_CLASS, mainClass);
    }
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    manifest.write(out);
    return out.toByteArray();
}
Also used : Attributes(java.util.jar.Attributes) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Manifest(java.util.jar.Manifest) FileInputStream(java.io.FileInputStream)

Example 37 with Attributes

use of java.util.jar.Attributes in project buck by facebook.

the class Main method makeManifest.

/**
     * Creates and returns the manifest to use for the output. This may
     * modify {@link #outputResources} (removing the pre-existing manifest).
     *
     * @return {@code non-null;} the manifest
     */
private Manifest makeManifest() throws IOException {
    byte[] manifestBytes = outputResources.get(MANIFEST_NAME);
    Manifest manifest;
    Attributes attribs;
    if (manifestBytes == null) {
        // We need to construct an entirely new manifest.
        manifest = new Manifest();
        attribs = manifest.getMainAttributes();
        attribs.put(Attributes.Name.MANIFEST_VERSION, "1.0");
    } else {
        manifest = new Manifest(new ByteArrayInputStream(manifestBytes));
        attribs = manifest.getMainAttributes();
        outputResources.remove(MANIFEST_NAME);
    }
    String createdBy = attribs.getValue(CREATED_BY);
    if (createdBy == null) {
        createdBy = "";
    } else {
        createdBy += " + ";
    }
    createdBy += "dx " + Version.VERSION;
    attribs.put(CREATED_BY, createdBy);
    attribs.putValue("Dex-Location", DexFormat.DEX_IN_JAR_NAME);
    return manifest;
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) Attributes(java.util.jar.Attributes) CstString(com.android.dx.rop.cst.CstString) Manifest(java.util.jar.Manifest)

Example 38 with Attributes

use of java.util.jar.Attributes in project XobotOS by xamarin.

the class ZipInputStream method closeEntry.

/**
     * Closes the current ZIP entry and positions to read the next entry.
     *
     * @throws IOException
     *             if an {@code IOException} occurs.
     */
public void closeEntry() throws IOException {
    checkClosed();
    if (currentEntry == null) {
        return;
    }
    if (currentEntry instanceof java.util.jar.JarEntry) {
        Attributes temp = ((JarEntry) currentEntry).getAttributes();
        if (temp != null && temp.containsKey("hidden")) {
            return;
        }
    }
    /*
         * The following code is careful to leave the ZipInputStream in a
         * consistent state, even when close() results in an exception. It does
         * so by:
         *  - pushing bytes back into the source stream
         *  - reading a data descriptor footer from the source stream
         *  - resetting fields that manage the entry being closed
         */
    // Ensure all entry bytes are read
    Exception failure = null;
    try {
        Streams.skipAll(this);
    } catch (Exception e) {
        failure = e;
    }
    int inB, out;
    if (currentEntry.compressionMethod == ZipEntry.DEFLATED) {
        inB = inf.getTotalIn();
        out = inf.getTotalOut();
    } else {
        inB = inRead;
        out = inRead;
    }
    int diff = entryIn - inB;
    // Pushback any required bytes
    if (diff != 0) {
        ((PushbackInputStream) in).unread(buf, len - diff, diff);
    }
    try {
        readAndVerifyDataDescriptor(inB, out);
    } catch (Exception e) {
        if (failure == null) {
            // otherwise we're already going to throw
            failure = e;
        }
    }
    inf.reset();
    lastRead = inRead = entryIn = len = 0;
    crc.reset();
    currentEntry = null;
    if (failure != null) {
        if (failure instanceof IOException) {
            throw (IOException) failure;
        } else if (failure instanceof RuntimeException) {
            throw (RuntimeException) failure;
        }
        AssertionError error = new AssertionError();
        error.initCause(failure);
        throw error;
    }
}
Also used : PushbackInputStream(java.io.PushbackInputStream) Attributes(java.util.jar.Attributes) IOException(java.io.IOException) JarEntry(java.util.jar.JarEntry) IOException(java.io.IOException)

Example 39 with Attributes

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

the class StrictJarVerifier method initEntry.

/**
     * Invoked for each new JAR entry read operation from the input
     * stream. This method constructs and returns a new {@link VerifierEntry}
     * which contains the certificates used to sign the entry and its hash value
     * as specified in the JAR MANIFEST format.
     *
     * @param name
     *            the name of an entry in a JAR file which is <b>not</b> in the
     *            {@code META-INF} directory.
     * @return a new instance of {@link VerifierEntry} which can be used by
     *         callers as an {@link OutputStream}.
     */
VerifierEntry initEntry(String name) {
    // been found, do not verify.
    if (manifest == null || signatures.isEmpty()) {
        return null;
    }
    Attributes attributes = manifest.getAttributes(name);
    // entry has no digest
    if (attributes == null) {
        return null;
    }
    ArrayList<Certificate[]> certChains = new ArrayList<Certificate[]>();
    Iterator<Map.Entry<String, HashMap<String, Attributes>>> it = signatures.entrySet().iterator();
    while (it.hasNext()) {
        Map.Entry<String, HashMap<String, Attributes>> entry = it.next();
        HashMap<String, Attributes> hm = entry.getValue();
        if (hm.get(name) != null) {
            // Found an entry for entry name in .SF file
            String signatureFile = entry.getKey();
            Certificate[] certChain = certificates.get(signatureFile);
            if (certChain != null) {
                certChains.add(certChain);
            }
        }
    }
    // entry is not signed
    if (certChains.isEmpty()) {
        return null;
    }
    Certificate[][] certChainsArray = certChains.toArray(new Certificate[certChains.size()][]);
    for (int i = 0; i < DIGEST_ALGORITHMS.length; i++) {
        final String algorithm = DIGEST_ALGORITHMS[i];
        final String hash = attributes.getValue(algorithm + "-Digest");
        if (hash == null) {
            continue;
        }
        byte[] hashBytes = hash.getBytes(StandardCharsets.ISO_8859_1);
        try {
            return new VerifierEntry(name, MessageDigest.getInstance(algorithm), hashBytes, certChainsArray, verifiedEntries);
        } catch (NoSuchAlgorithmException ignored) {
        }
    }
    return null;
}
Also used : HashMap(java.util.HashMap) Attributes(java.util.jar.Attributes) ArrayList(java.util.ArrayList) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) HashMap(java.util.HashMap) Map(java.util.Map) X509Certificate(java.security.cert.X509Certificate) Certificate(java.security.cert.Certificate)

Example 40 with Attributes

use of java.util.jar.Attributes in project hadoop by apache.

the class TestFileUtil method testCreateJarWithClassPath.

@Test(timeout = 30000)
public void testCreateJarWithClassPath() throws Exception {
    // setup test directory for files
    Assert.assertFalse(tmp.exists());
    Assert.assertTrue(tmp.mkdirs());
    // create files expected to match a wildcard
    List<File> wildcardMatches = Arrays.asList(new File(tmp, "wildcard1.jar"), new File(tmp, "wildcard2.jar"), new File(tmp, "wildcard3.JAR"), new File(tmp, "wildcard4.JAR"));
    for (File wildcardMatch : wildcardMatches) {
        Assert.assertTrue("failure creating file: " + wildcardMatch, wildcardMatch.createNewFile());
    }
    // create non-jar files, which we expect to not be included in the classpath
    Assert.assertTrue(new File(tmp, "text.txt").createNewFile());
    Assert.assertTrue(new File(tmp, "executable.exe").createNewFile());
    Assert.assertTrue(new File(tmp, "README").createNewFile());
    // create classpath jar
    String wildcardPath = tmp.getCanonicalPath() + File.separator + "*";
    String nonExistentSubdir = tmp.getCanonicalPath() + Path.SEPARATOR + "subdir" + Path.SEPARATOR;
    List<String> classPaths = Arrays.asList("", "cp1.jar", "cp2.jar", wildcardPath, "cp3.jar", nonExistentSubdir);
    String inputClassPath = StringUtils.join(File.pathSeparator, classPaths);
    String[] jarCp = FileUtil.createJarWithClassPath(inputClassPath + File.pathSeparator + "unexpandedwildcard/*", new Path(tmp.getCanonicalPath()), System.getenv());
    String classPathJar = jarCp[0];
    assertNotEquals("Unexpanded wildcard was not placed in extra classpath", jarCp[1].indexOf("unexpanded"), -1);
    // verify classpath by reading manifest from jar file
    JarFile jarFile = null;
    try {
        jarFile = new JarFile(classPathJar);
        Manifest jarManifest = jarFile.getManifest();
        Assert.assertNotNull(jarManifest);
        Attributes mainAttributes = jarManifest.getMainAttributes();
        Assert.assertNotNull(mainAttributes);
        Assert.assertTrue(mainAttributes.containsKey(Attributes.Name.CLASS_PATH));
        String classPathAttr = mainAttributes.getValue(Attributes.Name.CLASS_PATH);
        Assert.assertNotNull(classPathAttr);
        List<String> expectedClassPaths = new ArrayList<String>();
        for (String classPath : classPaths) {
            if (classPath.length() == 0) {
                continue;
            }
            if (wildcardPath.equals(classPath)) {
                // add wildcard matches
                for (File wildcardMatch : wildcardMatches) {
                    expectedClassPaths.add(wildcardMatch.toURI().toURL().toExternalForm());
                }
            } else {
                File fileCp = null;
                if (!new Path(classPath).isAbsolute()) {
                    fileCp = new File(tmp, classPath);
                } else {
                    fileCp = new File(classPath);
                }
                if (nonExistentSubdir.equals(classPath)) {
                    // expect to maintain trailing path separator if present in input, even
                    // if directory doesn't exist yet
                    expectedClassPaths.add(fileCp.toURI().toURL().toExternalForm() + Path.SEPARATOR);
                } else {
                    expectedClassPaths.add(fileCp.toURI().toURL().toExternalForm());
                }
            }
        }
        List<String> actualClassPaths = Arrays.asList(classPathAttr.split(" "));
        Collections.sort(expectedClassPaths);
        Collections.sort(actualClassPaths);
        Assert.assertEquals(expectedClassPaths, actualClassPaths);
    } finally {
        if (jarFile != null) {
            try {
                jarFile.close();
            } catch (IOException e) {
                LOG.warn("exception closing jarFile: " + classPathJar, e);
            }
        }
    }
}
Also used : Attributes(java.util.jar.Attributes) ArrayList(java.util.ArrayList) IOException(java.io.IOException) JarFile(java.util.jar.JarFile) Manifest(java.util.jar.Manifest) JarFile(java.util.jar.JarFile) File(java.io.File) Test(org.junit.Test)

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