Example usage for java.util.jar Manifest getMainAttributes

List of usage examples for java.util.jar Manifest getMainAttributes

Introduction

In this page you can find the example usage for java.util.jar Manifest getMainAttributes.

Prototype

public Attributes getMainAttributes() 

Source Link

Document

Returns the main Attributes for the Manifest.

Usage

From source file:org.apache.sling.osgi.obr.Resource.java

public static Resource create(URL file) throws IOException {
    JarInputStream jar = null;/*from ww  w .j  a va 2 s. c  o  m*/
    try {
        URLConnection conn = file.openConnection();
        jar = new JarInputStream(conn.getInputStream());
        Manifest manifest = jar.getManifest();
        if (manifest == null) {
            throw new IOException(file + " is not a valid JAR file: Manifest not first entry");
        }
        return new Resource(file, manifest.getMainAttributes(), conn.getContentLength());
    } finally {
        IOUtils.closeQuietly(jar);
    }
}

From source file:org.apache.axiom.util.stax.dialect.StAXDialectDetector.java

private static StAXDialect detectDialectFromJarManifest(URL rootUrl) {
    Manifest manifest;
    try {//from  w ww  .  j ava2  s .co m
        URL metaInfUrl = new URL(rootUrl, "META-INF/MANIFEST.MF");
        InputStream is = metaInfUrl.openStream();
        try {
            manifest = new Manifest(is);
        } finally {
            is.close();
        }
    } catch (IOException ex) {
        log.warn("Unable to load manifest for StAX implementation at " + rootUrl);
        return UnknownStAXDialect.INSTANCE;
    }
    Attributes attrs = manifest.getMainAttributes();
    String title = attrs.getValue(IMPLEMENTATION_TITLE);
    String symbolicName = attrs.getValue(BUNDLE_SYMBOLIC_NAME);
    if (symbolicName != null) {
        int i = symbolicName.indexOf(';');
        if (i != -1) {
            symbolicName = symbolicName.substring(0, i);
        }
    }
    String vendor = attrs.getValue(IMPLEMENTATION_VENDOR);
    if (vendor == null) {
        vendor = attrs.getValue(BUNDLE_VENDOR);
    }
    String version = attrs.getValue(IMPLEMENTATION_VERSION);
    if (version == null) {
        version = attrs.getValue(BUNDLE_VERSION);
    }
    if (log.isDebugEnabled()) {
        log.debug("StAX implementation at " + rootUrl + " is:\n" + "  Title:         " + title + "\n"
                + "  Symbolic name: " + symbolicName + "\n" + "  Vendor:        " + vendor + "\n"
                + "  Version:       " + version);
    }

    // For the moment, the dialect detection is quite simple, but in the future we will probably
    // have to differentiate by version number
    if (vendor != null && vendor.toLowerCase().indexOf("woodstox") != -1) {
        return WoodstoxDialect.INSTANCE;
    } else if (title != null && title.indexOf("SJSXP") != -1) {
        return new SJSXPDialect(false);
    } else if ("com.bea.core.weblogic.stax".equals(symbolicName)) {
        // Weblogic's StAX implementation doesn't support CDATA section reporting and there are
        // a couple of additional test cases (with respect to BEA's reference implementation)
        // that fail.
        log.warn("Weblogic's StAX implementation is unsupported and some Axiom features will not work "
                + "as expected! Please use Woodstox instead.");
        // This is the best match we can return in this case.
        return BEADialect.INSTANCE;
    } else if ("BEA".equals(vendor)) {
        return BEADialect.INSTANCE;
    } else if ("com.ibm.ws.prereq.banshee".equals(symbolicName)) {
        return XLXP2Dialect.INSTANCE;
    } else {
        return null;
    }
}

From source file:org.apache.axis2.jaxws.description.builder.JAXWSRIWSDLGenerator.java

/**
 * Walk the classloader hierarchy and add to the classpath
 *
 * @param cl//from  w  w w  .  j  a v a2s .com
 * @param classpath
 */
private static void fillClassPath(ClassLoader cl, HashSet classpath) {
    while (cl != null) {
        if (cl instanceof URLClassLoader) {
            URL[] urls = ((URLClassLoader) cl).getURLs();
            for (int i = 0; (urls != null) && i < urls.length; i++) {
                String path = urls[i].getPath();
                //If it is a drive letter, adjust accordingly.
                if (path.length() >= 3 && path.charAt(0) == '/' && path.charAt(2) == ':')
                    path = path.substring(1);
                addPath(classpath, URLDecoder.decode(path));

                // if its a jar extract Class-Path entries from manifest
                File file = new File(urls[i].getFile());
                if (file.isFile()) {
                    FileInputStream fis = null;
                    try {
                        fis = new FileInputStream(file);
                        if (isJar(fis)) {
                            JarFile jar = new JarFile(file);
                            Manifest manifest = jar.getManifest();
                            if (manifest != null) {
                                Attributes attributes = manifest.getMainAttributes();
                                if (attributes != null) {
                                    String s = attributes.getValue(Attributes.Name.CLASS_PATH);
                                    String base = file.getParent();
                                    if (s != null) {
                                        StringTokenizer st = new StringTokenizer(s, " ");
                                        while (st.hasMoreTokens()) {
                                            String t = st.nextToken();
                                            addPath(classpath, base + File.separatorChar + t);
                                        }
                                    }
                                }
                            }
                        }
                    } catch (IOException ioe) {
                    } finally {
                        if (fis != null) {
                            try {
                                fis.close();
                            } catch (IOException ioe2) {
                            }
                        }
                    }
                }
            }
        }
        cl = cl.getParent();
    }
}

From source file:com.trsst.Common.java

public static Attributes getManifestAttributes() {
    Attributes result = null;/*from   w w w. jav  a2 s.  c o m*/
    Class<Common> clazz = Common.class;
    String className = clazz.getSimpleName() + ".class";
    URL classPath = clazz.getResource(className);
    if (classPath == null || !classPath.toString().startsWith("jar")) {
        // Class not from JAR
        return null;
    }
    String classPathString = classPath.toString();
    String manifestPath = classPathString.substring(0, classPathString.lastIndexOf("!") + 1)
            + "/META-INF/MANIFEST.MF";
    try {
        Manifest manifest = new Manifest(new URL(manifestPath).openStream());
        result = manifest.getMainAttributes();
    } catch (MalformedURLException e) {
        log.error("Could not locate manifest: " + manifestPath);
    } catch (IOException e) {
        log.error("Could not open manifest: " + manifestPath);
    }
    return result;
}

From source file:org.apache.sysml.utils.lite.BuildLite.java

/**
 * Build a lite jar based on the consolidated class names.
 * /*w w  w  .  j a  v a  2  s.  co  m*/
 * @param consolidateClassPathNames
 *            the consolidated class names
 * @throws IOException
 *             if an IOException occurs
 */
private static void createJarFromConsolidatedClassPathNames(Set<String> consolidateClassPathNames)
        throws IOException {
    System.out.println("\nCreating " + liteJarLocation + " file");
    ClassLoader cl = BuildLite.class.getClassLoader();

    Manifest mf = new Manifest();
    Attributes attr = mf.getMainAttributes();
    attr.putValue("" + Attributes.Name.MANIFEST_VERSION, "1.0");

    File file = new File(liteJarLocation);
    try (FileOutputStream fos = new FileOutputStream(file);
            JarOutputStream jos = new JarOutputStream(fos, mf)) {
        int numFilesWritten = 0;
        for (String classPathName : consolidateClassPathNames) {
            writeMessage(classPathName, ++numFilesWritten);
            InputStream is = cl.getResourceAsStream(classPathName);
            byte[] bytes = IOUtils.toByteArray(is);

            JarEntry je = new JarEntry(classPathName);
            jos.putNextEntry(je);
            jos.write(bytes);
        }

        writeIdentifierFileToLiteJar(jos, ++numFilesWritten);
        writeAdditionalResourcesToJar(jos, numFilesWritten);
    }

}

From source file:hudson.ClassicPluginStrategy.java

private static void parseClassPath(Manifest manifest, File archive, List<File> paths, String attributeName,
        String separator) throws IOException {
    String classPath = manifest.getMainAttributes().getValue(attributeName);
    if (classPath == null)
        return; // attribute not found
    for (String s : classPath.split(separator)) {
        File file = resolve(archive, s);
        if (file.getName().contains("*")) {
            // handle wildcard
            FileSet fs = new FileSet();
            File dir = file.getParentFile();
            fs.setDir(dir);// w  w  w.  j a v  a2s . c  om
            fs.setIncludes(file.getName());
            for (String included : fs.getDirectoryScanner(new Project()).getIncludedFiles()) {
                paths.add(new File(dir, included));
            }
        } else {
            if (!file.exists())
                throw new IOException("No such file: " + file);
            paths.add(file);
        }
    }
}

From source file:ml.shifu.shifu.ShifuCLI.java

/**
 * print version info for shifu//from   w  w  w  .j  a  v  a 2  s  .  com
 */
private static void printLogoAndVersion() {
    String findContainingJar = JarManager.findContainingJar(ShifuCLI.class);
    JarFile jar = null;
    try {
        jar = new JarFile(findContainingJar);
        final Manifest manifest = jar.getManifest();

        String vendor = manifest.getMainAttributes().getValue("vendor");
        String title = manifest.getMainAttributes().getValue("title");
        String version = manifest.getMainAttributes().getValue("version");
        String timestamp = manifest.getMainAttributes().getValue("timestamp");
        System.out.println(" ____  _   _ ___ _____ _   _ ");
        System.out.println("/ ___|| | | |_ _|  ___| | | |");
        System.out.println("\\___ \\| |_| || || |_  | | | |");
        System.out.println(" ___) |  _  || ||  _| | |_| |");
        System.out.println("|____/|_| |_|___|_|    \\___/ ");
        System.out.println("                             ");
        System.out.println(vendor + " " + title + " version " + version + " \ncompiled " + timestamp);
    } catch (Exception e) {
        throw new RuntimeException("unable to read pigs manifest file", e);
    } finally {
        if (jar != null) {
            try {
                jar.close();
            } catch (IOException e) {
                throw new RuntimeException("jar closed failed", e);
            }
        }
    }
}

From source file:org.eclipse.gemini.blueprint.test.internal.util.ManifestUtilsTest.java

public void testJarUtilsReadResource() throws Exception {
    Manifest mf = new Manifest();
    mf.getMainAttributes().putValue("foo", "bar");
    createJar(mf);//from  ww  w.  j  a  v a 2s .  c  om
    assertEquals(mf, JarUtils.getManifest(storage.getResource()));
}

From source file:org.eclipse.gemini.blueprint.test.internal.util.ManifestUtilsTest.java

public void testEmptyManifest() throws Exception {
    Manifest mf = new Manifest();
    mf.getMainAttributes().putValue("foo", "bar");
    createJar(mf);/*from w  w w  .  j av a2s  .  c o  m*/
    in = new JarInputStream(storage.getInputStream());
    assertEquals(mf, in.getManifest());
}

From source file:org.eclipse.gemini.blueprint.test.internal.util.ManifestUtilsTest.java

private void createJar(Manifest mf) throws Exception {
    mf.getMainAttributes().put(Attributes.Name.MANIFEST_VERSION, "1.0");
    JarOutputStream out = new JarOutputStream(storage.getOutputStream(), mf);
    out.flush();//from  w ww  .  j  a  va  2  s .c  o m
    out.close();
}