Search in sources :

Example 1 with Attributes

use of java.util.jar.Attributes 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 Attributes

use of java.util.jar.Attributes 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 Attributes

use of java.util.jar.Attributes 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 Attributes

use of java.util.jar.Attributes 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 Attributes

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

the class WarBundleManifestGenerator method createBundleManifest.

private static Attributes createBundleManifest(Attributes originalManifest, String url, JarFile jarFile) {
    HashMap<String, String> res = new HashMap<String, String>();
    for (Entry<Object, Object> entries : originalManifest.entrySet()) {
        res.put(entries.getKey().toString(), String.valueOf(entries.getValue()));
    }
    MultiMap<String> params = parseQueryString(url);
    //follow RFC66 documentation:
    //#1 Bundle-Version
    String version = params.getString(Constants.BUNDLE_VERSION);
    if (version != null) {
        res.put(Constants.BUNDLE_VERSION, version);
    } else {
        String versionInManifest = (String) res.get(Constants.BUNDLE_VERSION);
        if (versionInManifest == null) {
            res.put(Constants.BUNDLE_VERSION, MISSING_VERSION);
        }
    }
    //#2 Bundle_ManifestVersion
    String manversion = params.getString(Constants.BUNDLE_MANIFESTVERSION);
    if (manversion != null) {
        res.put(Constants.BUNDLE_MANIFESTVERSION, manversion);
    } else {
        int manv = 2;
        try {
            String versionInManifest = (String) res.get(Constants.BUNDLE_MANIFESTVERSION);
            if (versionInManifest != null) {
                manv = Integer.parseInt(versionInManifest.trim());
            }
        } catch (NumberFormatException nfe) {
        }
        res.put(Constants.BUNDLE_MANIFESTVERSION, String.valueOf(manv < 2 ? 2 : manv));
    }
    //#3 Bundle-SymbolicName
    String symbname = params.getString(Constants.BUNDLE_SYMBOLICNAME);
    if (symbname != null) {
        res.put(Constants.BUNDLE_SYMBOLICNAME, symbname);
    } else {
        symbname = (String) res.get(Constants.BUNDLE_SYMBOLICNAME);
        if (symbname == null) {
            //derive the symbolic name from the url.
            int lastSlash = url.lastIndexOf('/');
            int beforeQueryString = url.indexOf(lastSlash, '?');
            if (beforeQueryString == -1) {
                beforeQueryString = url.indexOf(lastSlash, '#');
                if (beforeQueryString == -1) {
                    beforeQueryString = url.length();
                }
            }
            symbname = url.substring(lastSlash + 1, beforeQueryString);
            //todo: something better probably.
            res.put(Constants.BUNDLE_SYMBOLICNAME, symbname);
        }
    }
    //#4 Bundle-Classpath
    String extraBundleClasspath = params.getString(Constants.BUNDLE_CLASSPATH);
    String alreadyBundleClasspath = res.get(Constants.BUNDLE_CLASSPATH);
    if (alreadyBundleClasspath == null) {
        StringBuilder bundleClasspath = new StringBuilder();
        if (jarFile == null || jarFile.getJarEntry("WEB-INF/classes/") != null) {
            bundleClasspath.append("WEB-INF/classes");
        }
        if (jarFile != null) {
            List<String> libs = getJarsInWebInfLib(jarFile);
            if (extraBundleClasspath != null) {
                libs.add(extraBundleClasspath);
            }
            for (String lib : libs) {
                if (bundleClasspath.length() != 0) {
                    bundleClasspath.append(",");
                }
                bundleClasspath.append(lib);
            }
        }
        alreadyBundleClasspath = bundleClasspath.toString();
    }
    //if there is already a manifest and it specifies the Bundle-Classpath.
    //for now let's trust that one.
    //please note that the draft of the spec implies that we should be parsing the existing
    //header and merge it with the missing stuff so this does not follow the spec yet.
    res.put(Constants.BUNDLE_CLASSPATH, alreadyBundleClasspath + (extraBundleClasspath == null ? "" : "," + extraBundleClasspath));
    //#5 Import-Package
    String extraImportPackage = params.getString(Constants.IMPORT_PACKAGE);
    String alreadyImportPackage = res.get(Constants.IMPORT_PACKAGE);
    if (alreadyImportPackage == null) {
        //The spec does not specify that the jsp imports are optional
        //kind of nice to have them optional so we can run simple wars in
        //simple environments.
        alreadyImportPackage = "javax.servlet; version=\"2.5\"," + "javax.servlet.http;version=\"2.5\"," + "javax.el;version=\"1.0\"" + "javax.jsp;version=\"2.1\";resolution:=optional," + "javax.jsp.tagext;version=\"2.1\";resolution:=optional";
    }
    if (extraImportPackage != null) {
        //if there is already a manifest and it specifies the Bundle-Classpath.
        //for now let's trust that one.
        //please note that the draft of the spec implies that we should be parsing the existing
        //header and merge it with the missing stuff so this does not follow the spec yet.
        res.put(Constants.IMPORT_PACKAGE, (alreadyImportPackage == null ? "" : alreadyImportPackage + ",") + extraImportPackage);
    }
    //#6 Export-Package
    String extraExportPackage = params.getString(Constants.EXPORT_PACKAGE);
    String alreadyExportPackage = res.get(Constants.EXPORT_PACKAGE);
    if (extraExportPackage != null) {
        //if there is already a manifest and it specifies the Bundle-Classpath.
        //for now let's trust that one.
        //please note that the draft of the spec implies that we should be parsing the existing
        //header and merge it with the missing stuff so this does not follow the spec yet.
        res.put(Constants.EXPORT_PACKAGE, (alreadyExportPackage == null ? "" : alreadyExportPackage + ",") + extraImportPackage);
    }
    //#7 Web-ContextPath
    String webContextPath = params.getString("Web-ContextPath");
    if (webContextPath != null) {
        res.put("Web-ContextPath", webContextPath);
    } else {
        webContextPath = res.get("Web-ContextPath");
        if (webContextPath == null) {
            //we choose to use the symbolic name as the default context path.
            if (symbname.endsWith(".war")) {
                webContextPath = "/" + symbname.substring(0, symbname.length() - ".war".length());
            } else {
                webContextPath = "/" + symbname;
            }
            res.put("Web-ContextPath", webContextPath);
        }
    }
    //#8 Web-JSPExtractLocation
    String jspExtractLocation = params.getString("Web-JSPExtractLocation");
    if (jspExtractLocation != null) {
        res.put("Web-JSPExtractLocation", jspExtractLocation);
    } else {
    //nothing to do.
    }
    Attributes newAttrs = new Attributes();
    for (Entry<String, String> e : res.entrySet()) {
        newAttrs.putValue(e.getKey(), e.getValue());
    }
    return newAttrs;
}
Also used : HashMap(java.util.HashMap) Attributes(java.util.jar.Attributes)

Aggregations

Attributes (java.util.jar.Attributes)629 Manifest (java.util.jar.Manifest)356 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