Search in sources :

Example 21 with Manifest

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

the class JarDirectoryStepTest method entriesFromTheGivenManifestShouldOverrideThoseInTheJars.

@Test
public void entriesFromTheGivenManifestShouldOverrideThoseInTheJars() throws IOException {
    String expected = "1.4";
    // Write the manifest, setting the implementation version
    Path tmp = folder.newFolder();
    Manifest manifest = new Manifest();
    manifest.getMainAttributes().putValue(MANIFEST_VERSION.toString(), "1.0");
    manifest.getMainAttributes().putValue(IMPLEMENTATION_VERSION.toString(), expected);
    Path manifestFile = tmp.resolve("manifest");
    try (OutputStream fos = Files.newOutputStream(manifestFile)) {
        manifest.write(fos);
    }
    // Write another manifest, setting the implementation version to something else
    manifest = new Manifest();
    manifest.getMainAttributes().putValue(MANIFEST_VERSION.toString(), "1.0");
    manifest.getMainAttributes().putValue(IMPLEMENTATION_VERSION.toString(), "1.0");
    Path input = tmp.resolve("input.jar");
    try (CustomZipOutputStream out = ZipOutputStreams.newOutputStream(input)) {
        ZipEntry entry = new ZipEntry("META-INF/MANIFEST.MF");
        out.putNextEntry(entry);
        manifest.write(out);
    }
    Path output = tmp.resolve("output.jar");
    JarDirectoryStep step = new JarDirectoryStep(new ProjectFilesystem(tmp), output, ImmutableSortedSet.of(Paths.get("input.jar")), /* main class */
    null, tmp.resolve("manifest"), /* merge manifest */
    true, /* blacklist */
    ImmutableSet.of());
    ExecutionContext context = TestExecutionContext.newInstance();
    assertEquals(0, step.execute(context).getExitCode());
    try (Zip zip = new Zip(output, false)) {
        byte[] rawManifest = zip.readFully("META-INF/MANIFEST.MF");
        manifest = new Manifest(new ByteArrayInputStream(rawManifest));
        String version = manifest.getMainAttributes().getValue(IMPLEMENTATION_VERSION);
        assertEquals(expected, version);
    }
}
Also used : Path(java.nio.file.Path) Zip(com.facebook.buck.testutil.Zip) ExecutionContext(com.facebook.buck.step.ExecutionContext) TestExecutionContext(com.facebook.buck.step.TestExecutionContext) ByteArrayInputStream(java.io.ByteArrayInputStream) ZipOutputStream(java.util.zip.ZipOutputStream) JarOutputStream(java.util.jar.JarOutputStream) OutputStream(java.io.OutputStream) CustomZipOutputStream(com.facebook.buck.zip.CustomZipOutputStream) ZipEntry(java.util.zip.ZipEntry) CustomZipOutputStream(com.facebook.buck.zip.CustomZipOutputStream) FakeProjectFilesystem(com.facebook.buck.testutil.FakeProjectFilesystem) ProjectFilesystem(com.facebook.buck.io.ProjectFilesystem) Manifest(java.util.jar.Manifest) Test(org.junit.Test)

Example 22 with Manifest

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

the class StubJarIntegrationTest method abiJarManifestShouldContainHashesOfItsFiles.

@Test
public void abiJarManifestShouldContainHashesOfItsFiles() throws IOException {
    Path out = Paths.get("junit-abi.jar");
    Path regularJar = testDataDir.resolve("junit.jar");
    new StubJar(regularJar).writeTo(filesystem, out);
    try (JarFile stubJar = new JarFile(filesystem.resolve(out).toFile())) {
        Manifest manifest = stubJar.getManifest();
        Enumeration<JarEntry> entries = stubJar.entries();
        while (entries.hasMoreElements()) {
            JarEntry entry = entries.nextElement();
            if (JarFile.MANIFEST_NAME.equals(entry.getName())) {
                continue;
            }
            String seenDigest = manifest.getAttributes(entry.getName()).getValue("Murmur3-128-Digest");
            String expectedDigest;
            try (InputStream inputStream = stubJar.getInputStream(entry)) {
                ByteSource byteSource = ByteSource.wrap(ByteStreams.toByteArray(inputStream));
                expectedDigest = byteSource.hash(Hashing.murmur3_128()).toString();
            }
            assertEquals(String.format("Digest mismatch for %s", entry.getName()), expectedDigest, seenDigest);
        }
    }
}
Also used : Path(java.nio.file.Path) InputStream(java.io.InputStream) ByteSource(com.google.common.io.ByteSource) JarFile(java.util.jar.JarFile) Manifest(java.util.jar.Manifest) JarEntry(java.util.jar.JarEntry) Test(org.junit.Test)

Example 23 with Manifest

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

the class FakeProjectFilesystem method getJarManifest.

@Override
public Manifest getJarManifest(Path path) throws IOException {
    try (JarInputStream jar = new JarInputStream(newFileInputStream(path))) {
        Manifest result = jar.getManifest();
        if (result != null) {
            return result;
        }
        // JarInputStream will only find the manifest if it's the first entry, but we have code that
        // puts it elsewhere. We must search. Fortunately, this is test code! So we can be slow!
        JarEntry entry;
        while ((entry = jar.getNextJarEntry()) != null) {
            if (JarFile.MANIFEST_NAME.equals(entry.getName())) {
                result = new Manifest();
                result.read(jar);
                return result;
            }
        }
    }
    return null;
}
Also used : JarInputStream(java.util.jar.JarInputStream) Manifest(java.util.jar.Manifest) JarEntry(java.util.jar.JarEntry)

Example 24 with Manifest

use of java.util.jar.Manifest in project pinpoint by naver.

the class AgentDirGenerator method createJarFile.

private void createJarFile(File parentDir, String filepath) throws IOException {
    final String jarPath = parentDir.getPath() + File.separator + filepath;
    logger.debug("create jar:{}", jarPath);
    JarOutputStream jos = null;
    try {
        Manifest manifest = new Manifest();
        FileOutputStream out = new FileOutputStream(jarPath);
        jos = new JarOutputStream(out, manifest);
    } finally {
        IOUtils.closeQuietly(jos);
    }
}
Also used : FileOutputStream(java.io.FileOutputStream) JarOutputStream(java.util.jar.JarOutputStream) Manifest(java.util.jar.Manifest)

Example 25 with Manifest

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

the class URLClassLoader method createURLJarHandler.

private URLHandler createURLJarHandler(URL url) {
    String prefixName;
    String file = url.getFile();
    if (url.getFile().endsWith("!/")) {
        prefixName = "";
    } else {
        int sepIdx = file.lastIndexOf("!/");
        if (sepIdx == -1) {
            // Invalid URL, don't look here again
            return null;
        }
        sepIdx += 2;
        prefixName = file.substring(sepIdx);
    }
    try {
        URL jarURL = ((JarURLConnection) url.openConnection()).getJarFileURL();
        JarURLConnection juc = (JarURLConnection) new URL("jar", "", jarURL.toExternalForm() + "!/").openConnection();
        JarFile jf = juc.getJarFile();
        URLJarHandler jarH = new URLJarHandler(url, jarURL, jf, prefixName);
        if (jarH.getIndex() == null) {
            try {
                Manifest manifest = jf.getManifest();
                if (manifest != null) {
                    String classpath = manifest.getMainAttributes().getValue(Attributes.Name.CLASS_PATH);
                    if (classpath != null) {
                        searchList.addAll(0, getInternalURLs(url, classpath));
                    }
                }
            } catch (IOException e) {
            }
        }
        return jarH;
    } catch (IOException e) {
    }
    return null;
}
Also used : IOException(java.io.IOException) JarFile(java.util.jar.JarFile) Manifest(java.util.jar.Manifest)

Aggregations

Manifest (java.util.jar.Manifest)1226 Attributes (java.util.jar.Attributes)392 File (java.io.File)391 IOException (java.io.IOException)336 JarFile (java.util.jar.JarFile)231 InputStream (java.io.InputStream)184 URL (java.net.URL)177 JarOutputStream (java.util.jar.JarOutputStream)145 FileOutputStream (java.io.FileOutputStream)131 Test (org.junit.Test)129 FileInputStream (java.io.FileInputStream)119 Jar (aQute.bnd.osgi.Jar)105 JarInputStream (java.util.jar.JarInputStream)104 Builder (aQute.bnd.osgi.Builder)99 ZipEntry (java.util.zip.ZipEntry)99 ByteArrayOutputStream (java.io.ByteArrayOutputStream)96 JarEntry (java.util.jar.JarEntry)96 ByteArrayInputStream (java.io.ByteArrayInputStream)93 ArrayList (java.util.ArrayList)83 Map (java.util.Map)83