Example usage for java.util.jar Manifest Manifest

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

Introduction

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

Prototype

public Manifest(Manifest man) 

Source Link

Document

Constructs a new Manifest that is a copy of the specified Manifest.

Usage

From source file:org.eclipse.emf.mwe.core.WorkflowEngine.java

/**
 * Tries to read the exact build version from the Manifest of the core.workflow plugin. Therefore the Manifest file
 * is located and the version is read from the attribute 'Bundle-Version'.
 * //from  w  ww.j a  va 2 s .co  m
 * @return The build version string, format "4.1.1, Build 200609291913"
 */
private String getVersion() {
    try {
        URL url = new URL(MWEPlugin.INSTANCE.getBaseURL() + "META-INF/MANIFEST.MF");
        final Manifest manifest = new Manifest(url.openStream());
        // Original value : "4.1.1.200609291913"
        // Resulting value : "4.1.1, Build 200609291913"
        String version = manifest.getMainAttributes().getValue("Bundle-Version");
        final int lastPoint = version.lastIndexOf('.');
        return version.substring(0, lastPoint) + ", Build " + version.substring(lastPoint + 1);
    } catch (Exception e) {
        logger.warn("Couldn't compute version of mwe.core bundle.", e);
        return "unkown version";
    }
}

From source file:com.facebook.buck.java.JarDirectoryStepTest.java

@Test
public void shouldMergeManifestsIfAsked() throws IOException {
    Manifest fromJar = createManifestWithExampleSection(ImmutableMap.of("Not-Seen", "ever"));
    Manifest fromUser = createManifestWithExampleSection(ImmutableMap.of("cake", "cheese"));

    Manifest seenManifest = jarDirectoryAndReadManifest(fromJar, fromUser, true);

    Manifest expectedManifest = new Manifest(fromJar);
    expectedManifest.getEntries().putAll(fromUser.getEntries());
    assertEquals(expectedManifest.getEntries(), seenManifest.getEntries());
}

From source file:org.eclipse.gemini.blueprint.test.AbstractOnTheFlyBundleCreatorTests.java

private Manifest createManifestFrom(Resource resource) {
    Assert.notNull(resource, "unable to create manifest for empty resources");
    try {/*from w w  w.  j a  va  2s  .  c om*/
        return new Manifest(resource.getInputStream());
    } catch (IOException ex) {
        throw (RuntimeException) new IllegalArgumentException("cannot create manifest from " + resource)
                .initCause(ex);
    }
}

From source file:org.apache.sling.maven.slingstart.PreparePackageMojoTest.java

@Test
public void testSubsystemBaseGeneration() throws Exception {
    // Provide the system with some artifacts that are known to be in the local .m2 repo
    // These are explicitly included in the test section of the pom.xml
    PreparePackageMojo ppm = getMojoUnderTest("org.apache.sling/org.apache.sling.commons.classloader/1.3.2",
            "org.apache.sling/org.apache.sling.commons.classloader/1.3.2/app",
            "org.apache.sling/org.apache.sling.commons.contentdetection/1.0.2",
            "org.apache.sling/org.apache.sling.commons.json/2.0.12",
            "org.apache.sling/org.apache.sling.commons.mime/2.1.8",
            "org.apache.sling/org.apache.sling.commons.osgi/2.3.0",
            "org.apache.sling/org.apache.sling.commons.threads/3.2.0");

    try {//from   ww  w  .  j  a v  a2 s .  c  om
        // The launchpad feature is a prerequisite for the model
        String modelTxt = "[feature name=:launchpad]\n" + "[artifacts]\n"
                + "  org.apache.sling/org.apache.sling.commons.classloader/1.3.2\n" + ""
                + "[feature name=test1 type=osgi.subsystem.composite]\n" + ""
                + "[:subsystem-manifest startLevel=123]\n"
                + "  Subsystem-Description: Extra subsystem headers can go here including very long ones that would span multiple lines in a manifest\n"
                + "  Subsystem-Copyright: (c) 2015 yeah!\n" + "" + "[artifacts]\n"
                + "  org.apache.sling/org.apache.sling.commons.osgi/2.3.0\n" + ""
                + "[artifacts startLevel=10]\n" + "  org.apache.sling/org.apache.sling.commons.json/2.0.12\n"
                + "  org.apache.sling/org.apache.sling.commons.mime/2.1.8\n" + ""
                + "[artifacts startLevel=20 runModes=foo,bar,:blah]\n"
                + "  org.apache.sling/org.apache.sling.commons.threads/3.2.0\n" + ""
                + "[artifacts startLevel=100 runModes=bar]\n"
                + "  org.apache.sling/org.apache.sling.commons.contentdetection/1.0.2\n";
        Model model = ModelReader.read(new StringReader(modelTxt), null);
        ppm.execute(model);

        File generatedFile = new File(ppm.getTmpDir() + "/test1.subsystem-base");
        try (JarFile jf = new JarFile(generatedFile)) {
            // Test META-INF/MANIFEST.MF
            Manifest mf = jf.getManifest();
            Attributes attrs = mf.getMainAttributes();
            String expected = "Potential_Bundles/0/org.apache.sling.commons.osgi-2.3.0.jar|"
                    + "Potential_Bundles/10/org.apache.sling.commons.json-2.0.12.jar|"
                    + "Potential_Bundles/10/org.apache.sling.commons.mime-2.1.8.jar";
            assertEquals(expected, attrs.getValue("_all_"));
            assertEquals("Potential_Bundles/20/org.apache.sling.commons.threads-3.2.0.jar",
                    attrs.getValue("foo"));
            assertEquals(
                    "Potential_Bundles/20/org.apache.sling.commons.threads-3.2.0.jar|"
                            + "Potential_Bundles/100/org.apache.sling.commons.contentdetection-1.0.2.jar",
                    attrs.getValue("bar"));

            // Test SUBSYSTEM-MANIFEST-BASE.MF
            ZipEntry smbZE = jf.getEntry("SUBSYSTEM-MANIFEST-BASE.MF");
            try (InputStream smbIS = jf.getInputStream(smbZE)) {
                Manifest smbMF = new Manifest(smbIS);
                Attributes smbAttrs = smbMF.getMainAttributes();
                assertEquals("test1", smbAttrs.getValue("Subsystem-SymbolicName"));
                assertEquals("osgi.subsystem.composite", smbAttrs.getValue("Subsystem-Type"));
                assertEquals("(c) 2015 yeah!", smbAttrs.getValue("Subsystem-Copyright"));
                assertEquals(
                        "Extra subsystem headers can go here including very long ones "
                                + "that would span multiple lines in a manifest",
                        smbAttrs.getValue("Subsystem-Description"));
            }

            // Test embedded bundles
            File mrr = getMavenRepoRoot();
            File soj = getMavenArtifactFile(mrr, "org.apache.sling", "org.apache.sling.commons.osgi", "2.3.0");
            ZipEntry sojZE = jf.getEntry("Potential_Bundles/0/org.apache.sling.commons.osgi-2.3.0.jar");
            try (InputStream is = jf.getInputStream(sojZE)) {
                assertArtifactsEqual(soj, is);
            }

            File sjj = getMavenArtifactFile(mrr, "org.apache.sling", "org.apache.sling.commons.json", "2.0.12");
            ZipEntry sjZE = jf.getEntry("Potential_Bundles/10/org.apache.sling.commons.json-2.0.12.jar");
            try (InputStream is = jf.getInputStream(sjZE)) {
                assertArtifactsEqual(sjj, is);
            }

            File smj = getMavenArtifactFile(mrr, "org.apache.sling", "org.apache.sling.commons.mime", "2.1.8");
            ZipEntry smjZE = jf.getEntry("Potential_Bundles/10/org.apache.sling.commons.mime-2.1.8.jar");
            try (InputStream is = jf.getInputStream(smjZE)) {
                assertArtifactsEqual(smj, is);
            }

            File stj = getMavenArtifactFile(mrr, "org.apache.sling", "org.apache.sling.commons.threads",
                    "3.2.0");
            ZipEntry stjZE = jf.getEntry("Potential_Bundles/20/org.apache.sling.commons.threads-3.2.0.jar");
            try (InputStream is = jf.getInputStream(stjZE)) {
                assertArtifactsEqual(stj, is);
            }

            File ctj = getMavenArtifactFile(mrr, "org.apache.sling",
                    "org.apache.sling.commons.contentdetection", "1.0.2");
            ZipEntry ctjZE = jf
                    .getEntry("Potential_Bundles/100/org.apache.sling.commons.contentdetection-1.0.2.jar");
            try (InputStream is = jf.getInputStream(ctjZE)) {
                assertArtifactsEqual(ctj, is);
            }
        }
    } finally {
        FileUtils.deleteDirectory(new File(ppm.project.getBuild().getDirectory()));
    }
}

From source file:org.springframework.cloud.function.context.AbstractSpringFunctionAdapterInitializer.java

private static Class<?> getStartClass(List<URL> list) {
    logger.info("Searching manifests: " + list);
    for (URL url : list) {
        try {/*from   w  w w  .jav a  2s  .  c  om*/
            logger.info("Searching manifest: " + url);
            InputStream inputStream = url.openStream();
            try {
                Manifest manifest = new Manifest(inputStream);
                String startClass = manifest.getMainAttributes().getValue("Start-Class");
                if (startClass != null) {
                    return ClassUtils.forName(startClass,
                            AbstractSpringFunctionAdapterInitializer.class.getClassLoader());
                }
            } finally {
                inputStream.close();
            }
        } catch (Exception ex) {
        }
    }
    return null;
}

From source file:dalma.container.ClassLoaderImpl.java

/**
 * Add a file to the path. This classloader reads the manifest, if
 * available, and adds any additional class path jars specified in the
 * manifest./*from   w  w w  .  ja  v  a2  s. co m*/
 *
 * @param pathComponent the file which is to be added to the path for
 *                      this class loader
 *
 * @throws IOException if data needed from the file cannot be read.
 */
public void addPathFile(File pathComponent) throws IOException {
    pathComponents.addElement(pathComponent);

    if (pathComponent.isDirectory()) {
        return;
    }

    String absPathPlusTimeAndLength = pathComponent.getAbsolutePath() + pathComponent.lastModified() + "-"
            + pathComponent.length();
    String classpath = pathMap.get(absPathPlusTimeAndLength);
    if (classpath == null) {
        ZipFile jarFile = null;
        InputStream manifestStream = null;
        try {
            jarFile = new ZipFile(pathComponent);
            manifestStream = jarFile.getInputStream(new ZipEntry("META-INF/MANIFEST.MF"));

            if (manifestStream == null) {
                return;
            }
            Manifest manifest = new Manifest(manifestStream);
            classpath = manifest.getMainAttributes().getValue("Class-Path");

        } finally {
            if (manifestStream != null) {
                manifestStream.close();
            }
            if (jarFile != null) {
                jarFile.close();
            }
        }
        if (classpath == null) {
            classpath = "";
        }
        pathMap.put(absPathPlusTimeAndLength, classpath);
    }

    if (!"".equals(classpath)) {
        URL baseURL = pathComponent.toURL();
        StringTokenizer st = new StringTokenizer(classpath);
        while (st.hasMoreTokens()) {
            String classpathElement = st.nextToken();
            URL libraryURL = new URL(baseURL, classpathElement);
            if (!libraryURL.getProtocol().equals("file")) {
                logger.fine("Skipping jar library " + classpathElement
                        + " since only relative URLs are supported by this" + " loader");
                continue;
            }
            File libraryFile = new File(libraryURL.getFile());
            if (libraryFile.exists() && !isInPath(libraryFile)) {
                addPathFile(libraryFile);
            }
        }
    }
}

From source file:org.overlord.commons.osgi.vfs.VfsBundle.java

/**
 * Throws a "not found" error while also logging information found in the manifest.  This
 * latter part will help diagnose which JAR was *supposed* to have been detected.
 * @param url//from  w w w  .  j  a va2 s. c  om
 */
private void throwNotFoundError(URL url) {
    InputStream manifestStream = null;
    StringBuilder builder = new StringBuilder();
    try {
        String manifestPath = "META-INF/MANIFEST.MF"; //$NON-NLS-1$
        URL manifestURL = new URL(url.toExternalForm() + manifestPath);
        manifestStream = manifestURL.openStream();
        Manifest manifest = new Manifest(manifestStream);
        Attributes attributes = manifest.getMainAttributes();
        for (Entry<Object, Object> entry : attributes.entrySet()) {
            String key = String.valueOf(entry.getKey());
            String value = String.valueOf(entry.getValue());
            builder.append(key).append(": ").append(value).append("\n"); //$NON-NLS-1$ //$NON-NLS-2$
        }
    } catch (Exception e) {
    } finally {
        IOUtils.closeQuietly(manifestStream);
    }
    // Include the full manifest in the exception - this is helpful to diagnose which
    // JAR gave us fits.  Hopefully this will not happen!
    throw new RuntimeException("Failed to create a Vfs.Dir for URL: " + url //$NON-NLS-1$
            + "\n--Manifest--\n====================" + builder.toString()); //$NON-NLS-1$
}

From source file:com.thoughtworks.go.plugin.infra.plugininfo.GoPluginOSGiManifestTest.java

private String valueFor(final String prefix) throws IOException {
    if (!manifestFile.exists()) {
        return null;
    }//from   w  ww  .ja v a  2s.  c om

    FileInputStream manifestInputStream = new FileInputStream(manifestFile);
    Manifest manifest = new Manifest(manifestInputStream);
    Attributes entries = manifest.getMainAttributes();

    return entries.getValue(prefix);
}

From source file:org.apache.sling.testing.mock.sling.NodeTypeDefinitionScanner.java

/**
 * Find all node type definition classpath paths by searching all MANIFEST.MF files in the classpath and reading
 * the paths from the "Sling-Nodetypes" entry.
 * The order of the paths from each entry is preserved, but the overall order when multiple bundles define such an entry
 * is not deterministic and may not be correct according to the dependencies between the node type definitions.
 * @return List of node type definition class paths
 *//*ww  w.j a v  a  2 s.c  o m*/
private static List<String> findeNodeTypeDefinitions() {
    List<String> nodeTypeDefinitions = new ArrayList<String>();
    try {
        Enumeration<URL> resEnum = NodeTypeDefinitionScanner.class.getClassLoader()
                .getResources(JarFile.MANIFEST_NAME);
        while (resEnum.hasMoreElements()) {
            try {
                URL url = (URL) resEnum.nextElement();
                InputStream is = url.openStream();
                if (is != null) {
                    try {
                        Manifest manifest = new Manifest(is);
                        Attributes mainAttribs = manifest.getMainAttributes();
                        String nodeTypeDefinitionList = mainAttribs.getValue("Sling-Nodetypes");
                        String[] nodeTypeDefinitionArray = StringUtils.split(nodeTypeDefinitionList, ",");
                        if (nodeTypeDefinitionArray != null) {
                            for (String nodeTypeDefinition : nodeTypeDefinitionArray) {
                                if (!StringUtils.isBlank(nodeTypeDefinition)) {
                                    nodeTypeDefinitions.add(StringUtils.trim(nodeTypeDefinition));
                                }
                            }
                        }
                    } finally {
                        is.close();
                    }
                }
            } catch (Throwable ex) {
                log.warn("Unable to read JAR manifest.", ex);
            }
        }
    } catch (IOException ex2) {
        log.warn("Unable to read JAR manifests.", ex2);
    }
    return nodeTypeDefinitions;
}

From source file:org.orbisgis.core.plugin.BundleTools.java

private static List<String> getClassPath() {
    List<String> classPath = new LinkedList<String>();
    // Read declared class in the manifest
    try {// w  ww  . j  av  a  2  s .  co  m
        Enumeration<URL> resources = BundleTools.class.getClassLoader().getResources("META-INF/MANIFEST.MF");
        while (resources.hasMoreElements()) {
            URL url = resources.nextElement();
            try {
                Manifest manifest = new Manifest(url.openStream());
                String value = manifest.getMainAttributes().getValue(Attributes.Name.CLASS_PATH);
                if (value != null) {
                    String[] pathElements = value.split(" ");
                    if (pathElements == null) {
                        pathElements = new String[] { value };
                    }
                    classPath.addAll(Arrays.asList(pathElements));
                }
            } catch (IOException ex) {
                LOGGER.warn("Unable to retrieve Jar MANIFEST " + url, ex);
            }
        }
    } catch (IOException ex) {
        LOGGER.warn("Unable to retrieve Jar MANIFEST", ex);
    }
    // Read packages in the class path
    String javaClasspath = System.getProperty("java.class.path");
    String[] pathElements = javaClasspath.split(":");
    if (pathElements == null) {
        pathElements = new String[] { javaClasspath };
    }
    classPath.addAll(Arrays.asList(pathElements));
    return classPath;
}