Search in sources :

Example 1 with Manifest

use of java.util.jar.Manifest in project elasticsearch by elastic.

the class JarHellTests method testBadJDKVersionInJar.

public void testBadJDKVersionInJar() throws Exception {
    Path dir = createTempDir();
    Manifest manifest = new Manifest();
    Attributes attributes = manifest.getMainAttributes();
    attributes.put(Attributes.Name.MANIFEST_VERSION, "1.0.0");
    attributes.put(new Attributes.Name("X-Compile-Target-JDK"), "bogus");
    URL[] jars = { makeJar(dir, "foo.jar", manifest, "Foo.class") };
    try {
        JarHell.checkJarHell(jars);
        fail("did not get expected exception");
    } catch (IllegalStateException e) {
        assertTrue(e.getMessage().equals("version string must be a sequence of nonnegative decimal integers separated by \".\"'s and may have leading zeros but was bogus"));
    }
}
Also used : Path(java.nio.file.Path) Attributes(java.util.jar.Attributes) Manifest(java.util.jar.Manifest) URL(java.net.URL)

Example 2 with Manifest

use of java.util.jar.Manifest in project elasticsearch by elastic.

the class JarHellTests method testRequiredJDKVersionIsOK.

public void testRequiredJDKVersionIsOK() throws Exception {
    Path dir = createTempDir();
    Manifest manifest = new Manifest();
    Attributes attributes = manifest.getMainAttributes();
    attributes.put(Attributes.Name.MANIFEST_VERSION, "1.0.0");
    attributes.put(new Attributes.Name("X-Compile-Target-JDK"), "1.7");
    URL[] jars = { makeJar(dir, "foo.jar", manifest, "Foo.class") };
    JarHell.checkJarHell(jars);
}
Also used : Path(java.nio.file.Path) Attributes(java.util.jar.Attributes) Manifest(java.util.jar.Manifest) URL(java.net.URL)

Example 3 with Manifest

use of java.util.jar.Manifest in project elasticsearch by elastic.

the class JarHellTests method testGoodESVersionInJar.

/** make sure if a plugin is compiled against the same ES version, it works */
public void testGoodESVersionInJar() throws Exception {
    Path dir = createTempDir();
    Manifest manifest = new Manifest();
    Attributes attributes = manifest.getMainAttributes();
    attributes.put(Attributes.Name.MANIFEST_VERSION, "1.0.0");
    attributes.put(new Attributes.Name("X-Compile-Elasticsearch-Version"), Version.CURRENT.toString());
    URL[] jars = { makeJar(dir, "foo.jar", manifest, "Foo.class") };
    JarHell.checkJarHell(jars);
}
Also used : Path(java.nio.file.Path) Attributes(java.util.jar.Attributes) Manifest(java.util.jar.Manifest) URL(java.net.URL)

Example 4 with Manifest

use of java.util.jar.Manifest in project elasticsearch by elastic.

the class JarHellTests method testBadESVersionInJar.

/** make sure if a plugin is compiled against a different ES version, it fails */
public void testBadESVersionInJar() throws Exception {
    Path dir = createTempDir();
    Manifest manifest = new Manifest();
    Attributes attributes = manifest.getMainAttributes();
    attributes.put(Attributes.Name.MANIFEST_VERSION, "1.0.0");
    attributes.put(new Attributes.Name("X-Compile-Elasticsearch-Version"), "1.0-bogus");
    URL[] jars = { makeJar(dir, "foo.jar", manifest, "Foo.class") };
    try {
        JarHell.checkJarHell(jars);
        fail("did not get expected exception");
    } catch (IllegalStateException e) {
        assertTrue(e.getMessage().contains("requires Elasticsearch 1.0-bogus"));
    }
}
Also used : Path(java.nio.file.Path) Attributes(java.util.jar.Attributes) Manifest(java.util.jar.Manifest) URL(java.net.URL)

Example 5 with Manifest

use of java.util.jar.Manifest in project jetty.project by eclipse.

the class SelectiveJarResource method copyTo.

/** 
     * @see org.eclipse.jetty.util.resource.JarResource#copyTo(java.io.File)
     */
@Override
public void copyTo(File directory) throws IOException {
    if (_includes == null)
        _includes = DEFAULT_INCLUDES;
    if (_excludes == null)
        _excludes = DEFAULT_EXCLUDES;
    //parts of the jar file are copied
    if (!exists())
        return;
    String urlString = this.getURL().toExternalForm().trim();
    int endOfJarUrl = urlString.indexOf("!/");
    int startOfJarUrl = (endOfJarUrl >= 0 ? 4 : 0);
    if (endOfJarUrl < 0)
        throw new IOException("Not a valid jar url: " + urlString);
    URL jarFileURL = new URL(urlString.substring(startOfJarUrl, endOfJarUrl));
    try (InputStream is = jarFileURL.openConnection().getInputStream();
        JarInputStream jin = new JarInputStream(is)) {
        JarEntry entry;
        while ((entry = jin.getNextJarEntry()) != null) {
            String entryName = entry.getName();
            LOG.debug("Looking at " + entryName);
            String dotCheck = entryName.replace('\\', '/');
            dotCheck = URIUtil.canonicalPath(dotCheck);
            if (dotCheck == null) {
                LOG.info("Invalid entry: " + entryName);
                continue;
            }
            File file = new File(directory, entryName);
            if (entry.isDirectory()) {
                if (isIncluded(entryName)) {
                    if (!isExcluded(entryName)) {
                        // Make directory
                        if (!file.exists())
                            file.mkdirs();
                    } else
                        LOG.debug("{} dir is excluded", entryName);
                } else
                    LOG.debug("{} dir is NOT included", entryName);
            } else {
                //entry is a file, is it included?
                if (isIncluded(entryName)) {
                    if (!isExcluded(entryName)) {
                        // make directory (some jars don't list dirs)
                        File dir = new File(file.getParent());
                        if (!dir.exists())
                            dir.mkdirs();
                        // Make file
                        try (OutputStream fout = new FileOutputStream(file)) {
                            IO.copy(jin, fout);
                        }
                        // touch the file.
                        if (entry.getTime() >= 0)
                            file.setLastModified(entry.getTime());
                    } else
                        LOG.debug("{} file is excluded", entryName);
                } else
                    LOG.debug("{} file is NOT included", entryName);
            }
        }
        Manifest manifest = jin.getManifest();
        if (manifest != null) {
            if (isIncluded("META-INF") && !isExcluded("META-INF")) {
                File metaInf = new File(directory, "META-INF");
                metaInf.mkdir();
                File f = new File(metaInf, "MANIFEST.MF");
                try (OutputStream fout = new FileOutputStream(f)) {
                    manifest.write(fout);
                }
            }
        }
    }
}
Also used : JarInputStream(java.util.jar.JarInputStream) JarInputStream(java.util.jar.JarInputStream) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) FileOutputStream(java.io.FileOutputStream) IOException(java.io.IOException) JarEntry(java.util.jar.JarEntry) Manifest(java.util.jar.Manifest) File(java.io.File) URL(java.net.URL)

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