Example usage for java.util.jar JarFile getManifest

List of usage examples for java.util.jar JarFile getManifest

Introduction

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

Prototype

public Manifest getManifest() throws IOException 

Source Link

Document

Returns the jar file manifest, or null if none.

Usage

From source file:org.krakenapps.main.Kraken.java

private void setBanner() throws IOException {
    try {//w  w  w.j av  a  2 s  . com
        String jarFileName = System.getProperty("java.class.path")
                .split(System.getProperty("path.separator"))[0];
        JarFile jar = new JarFile(jarFileName);
        Manifest mf = jar.getManifest();
        Attributes attrs = mf.getMainAttributes();
        BANNER = "Kraken (version " + attrs.getValue("Kraken-Version") + ")";
    } catch (FileNotFoundException e) {
        BANNER = "Kraken (Debug mode)";
    }
}

From source file:org.s4i.feature.FeatureMojo.java

private IFeaturePlugin createFeaturePlugin(IFeatureModel model, DependencyNode node)
        throws CoreException, MojoExecutionException {
    FeaturePlugin featurePlugin = new FeaturePlugin();
    featurePlugin.setModel(model);//from  ww w  .ja  v  a  2 s .c o m
    File file = node.getArtifact().getFile();
    if (file == null) {
        /* the file was already resolved, it's just that is not available in the dependency node */
        file = new File(this.localRepository.getBasedir(), this.localRepository.pathOf(node.getArtifact()));
    }

    String type = node.getArtifact().getType();
    if (!"jar".equals(type)) {
        getLog().error("Unsupported type \"" + type + "\" for artifact: " + node.getArtifact().getId());
        return null;
    }

    try {
        JarFile jarFile = new JarFile(file);
        Manifest manifest = jarFile.getManifest();
        if (manifest == null) {
            getLog().warn("Artifact is not an OSGi bundle and will be ignored (no manifest): "
                    + node.getArtifact().getId());
            return null;
        }

        Attributes attrs = manifest.getMainAttributes();
        String bsn = getBundleSymbolicName(attrs);
        String ver = getBundleVersion(attrs);
        if (bsn == null || ver == null) {
            getLog().warn("Artifact is not an OSGi bundle and will be ignored (no BSN/version): "
                    + node.getArtifact().getId());
            return null;
        }

        featurePlugin.setId(bsn);
        featurePlugin.setVersion(ver);
        featurePlugin.setFragment(isFragment(attrs));

        getLog().info("Adding OSGi bundle to feature: " + node.getArtifact().getId());
    } catch (Exception e) {
        getLog().error("Artifact is not an OSGi bundle and will be ignored: " + node.getArtifact().getId(), e);
        return null;
    }

    long size = file.length() / 1024;
    featurePlugin.setInstallSize(size);
    featurePlugin.setDownloadSize(size);
    featurePlugin.setUnpack(false);
    return featurePlugin;
}

From source file:org.moe.cli.ParameterParserTest.java

@Test
public void linkUniversalLibrary() throws Exception {

    File project = tmpDir.newFolder();
    File outputJar = new File(project, "TestLib.jar");

    // prepare file with ldFlags
    String flags = "-lTestLibrary";
    File ldFlags = new File(project, "ldflags");
    ldFlags.createNewFile();//from ww w  .j a  v a  2  s. co  m
    PrintWriter write = new PrintWriter(ldFlags);
    write.print(flags);
    write.close();

    ClassLoader cl = this.getClass().getClassLoader();
    URL library = cl.getResource("natives/universal/libTestLibrary.a");
    URL headersURL = cl.getResource("natives/Headers/TestFramework.h");

    File headers = new File(headersURL.getPath());
    URL bundle = cl.getResource("moe_logo.png");
    CommandLine argc = parseArgs(new String[] { "--library", library.getPath(), "--headers",
            headers.getParentFile().getPath(), "--package-name", "org", "--output-file-path",
            outputJar.getPath(), "--ld-flags", ldFlags.getPath(), "--bundle", bundle.getPath() });

    IExecutor executor = ExecutorManager.getExecutorByParams(argc);
    assertNotNull(executor);
    assertTrue(executor instanceof ThirdPartyLibraryLinkExecutor);

    // generate binding & prepare output jar
    executor.execute();

    // check output jar file existence
    assertTrue(outputJar.exists());

    JarFile jarFile = new JarFile(outputJar);
    Manifest manifest = jarFile.getManifest();

    Attributes attributes = manifest.getMainAttributes();
    String manifestLDFlags = attributes.getValue("MOE_CUSTOM_LINKER_FLAGS");
    Set<String> realLDFlags = new HashSet<String>(Arrays.asList(flags.split(";")));
    realLDFlags.add("-ObjC");
    assertEquals(realLDFlags, new HashSet<String>(Arrays.asList(manifestLDFlags.split(";"))));

    String manifestSimFramework = attributes.getValue("MOE_ThirdpartyFramework_universal");
    assertEquals(manifestSimFramework, "./lib/libTestLibrary.a");

    String manifestBundle = attributes.getValue("MOE_BUNDLE_FILE_RESOURCES");
    assertEquals(manifestBundle, "./bundle/moe_logo.png;");

    assertNotNull(jarFile.getEntry("bundle/moe_logo.png"));
    assertNotNull(jarFile.getEntry("lib/libTestLibrary.a"));

    jarFile.close();

}

From source file:org.eclipse.emf.mwe.utils.StandaloneSetup.java

protected String getBundleNameFromManifest(JarFile jarFile) throws IOException {
    Manifest manifest = jarFile.getManifest();
    if (manifest != null) {
        return manifest.getMainAttributes().getValue("Bundle-SymbolicName");
    }//w  ww.ja va 2 s  . c o  m
    return null;
}

From source file:org.moe.cli.ParameterParserTest.java

@Test
public void linkFramework() throws Exception {

    File project = tmpDir.newFolder();
    File outputJar = new File(project, "TestFramework.jar");

    // prepare file with ldFlags
    String flags = "-framework TestFramework";
    File ldFlags = new File(project, "ldflags");
    ldFlags.createNewFile();/*from ww  w .ja  v a  2s.c  o  m*/
    PrintWriter write = new PrintWriter(ldFlags);
    write.print(flags);
    write.close();

    ClassLoader cl = this.getClass().getClassLoader();
    URL simFramework = cl.getResource("natives/device/TestFramework.framework");
    URL devFramework = cl.getResource("natives/simulator/TestFramework.framework");

    URL bundle = cl.getResource("moe_logo.png");
    CommandLine argc = parseArgs(new String[] { "--framework",
            String.format("%s:%s", simFramework.getPath(), devFramework.getPath()), "--package-name", "org",
            "--output-file-path", outputJar.getPath(), "--ld-flags", ldFlags.getPath(), "--bundle",
            bundle.getPath() });

    IExecutor executor = ExecutorManager.getExecutorByParams(argc);
    assertNotNull(executor);
    assertTrue(executor instanceof ThirdPartyFrameworkLinkExecutor);

    // generate binding & prepare output jar
    executor.execute();

    // check output jar file existence
    assertTrue(outputJar.exists());

    JarFile jarFile = new JarFile(outputJar);
    Manifest manifest = jarFile.getManifest();

    Attributes attributes = manifest.getMainAttributes();
    String manifestLDFlags = attributes.getValue("MOE_CUSTOM_LINKER_FLAGS");
    assertEquals(flags + ";", manifestLDFlags);

    String manifestBundle = attributes.getValue("MOE_BUNDLE_FILE_RESOURCES");
    assertEquals(manifestBundle, "./bundle/moe_logo.png;");

    String manifestFramework = attributes.getValue("MOE_ThirdpartyFramework_ios_simulator");
    assertEquals(manifestFramework, "./lib/iphonesimulator/TestFramework.framework");

    String manifestDevFramework = attributes.getValue("MOE_ThirdpartyFramework_ios_device");
    assertEquals(manifestDevFramework, "./lib/iphoneos/TestFramework.framework");

    assertNotNull(jarFile.getEntry("bundle/moe_logo.png"));
    assertNotNull(jarFile.getEntry("lib/iphonesimulator/TestFramework.framework"));
    assertNotNull(jarFile.getEntry("lib/iphoneos/TestFramework.framework"));

    jarFile.close();

}

From source file:org.hyperic.hq.plugin.jboss7.JBossDetectorBase.java

final String getVersion(Map<String, String> args) {
    String version = "not found";

    File jbossAsServerJar = null;
    String mp = args.get("mp");
    List<String> modulesPAths = Arrays.asList(mp.split(File.pathSeparator));
    log.debug("[getVersion] modulesPAths=" + modulesPAths);
    for (String path : modulesPAths) {
        Collection<File> files = listFileTree(new File(path));
        for (File file : files) {
            String name = file.getName();
            if (name.startsWith("jboss-as-server") && name.endsWith(".jar")) {
                jbossAsServerJar = file;
            }/*from   w  ww .j av  a 2  s .co m*/
        }
    }

    if (jbossAsServerJar != null) {
        try {
            JarFile jarFile = new JarFile(jbossAsServerJar);
            log.debug("[getVersion] jboss-as-server.jar = '" + jarFile.getName() + "'");
            Attributes attributes = jarFile.getManifest().getMainAttributes();
            jarFile.close();
            version = attributes.getValue("JBossAS-Release-Version");
        } catch (IOException e) {
            log.debug("[getVersion] Error getting JBoss version (" + e + ")", e);
        }
    } else {
        log.debug("[getVersion] 'jboss-as-server.*.jar' not found.");
    }

    return version;
}

From source file:org.moe.cli.ParameterParserTest.java

@Test
public void linkLibrary() throws Exception {

    File project = tmpDir.newFolder();
    File outputJar = new File(project, "TestLib.jar");

    // prepare file with ldFlags
    String flags = "-lTestLibrary";
    File ldFlags = new File(project, "ldflags");
    ldFlags.createNewFile();//w w  w.j  av  a2 s  . c  o m
    PrintWriter write = new PrintWriter(ldFlags);
    write.print(flags);
    write.close();

    ClassLoader cl = this.getClass().getClassLoader();
    URL library = cl.getResource("natives/simulator/libTestLibrary.a");
    URL deviceLibrary = cl.getResource("natives/device/libTestLibrary.a");
    URL headersURL = cl.getResource("natives/Headers/TestFramework.h");

    File headers = new File(headersURL.getPath());
    URL bundle = cl.getResource("moe_logo.png");
    CommandLine argc = parseArgs(new String[] { "--library",
            String.format("%s:%s", library.getPath(), deviceLibrary.getPath()), "--headers",
            headers.getParentFile().getPath(), "--package-name", "org", "--output-file-path",
            outputJar.getPath(), "--ld-flags", ldFlags.getPath(), "--bundle", bundle.getPath() });

    IExecutor executor = ExecutorManager.getExecutorByParams(argc);
    assertNotNull(executor);
    assertTrue(executor instanceof ThirdPartyLibraryLinkExecutor);

    // generate binding & prepare output jar
    executor.execute();

    // check output jar file existence
    assertTrue(outputJar.exists());

    JarFile jarFile = new JarFile(outputJar);
    Manifest manifest = jarFile.getManifest();

    Attributes attributes = manifest.getMainAttributes();
    String manifestLDFlags = attributes.getValue("MOE_CUSTOM_LINKER_FLAGS");
    Set<String> realLDFlags = new HashSet<String>(Arrays.asList(flags.split(";")));
    realLDFlags.add("-ObjC");
    assertEquals(realLDFlags, new HashSet<String>(Arrays.asList(manifestLDFlags.split(";"))));

    String manifestSimFramework = attributes.getValue("MOE_ThirdpartyFramework_ios_simulator");
    assertEquals("./lib/iphonesimulator/libTestLibrary.a", manifestSimFramework);

    String manifestDevFramework = attributes.getValue("MOE_ThirdpartyFramework_ios_device");
    assertEquals("./lib/iphoneos/libTestLibrary.a", manifestDevFramework);

    String manifestBundle = attributes.getValue("MOE_BUNDLE_FILE_RESOURCES");
    assertEquals("./bundle/moe_logo.png;", manifestBundle);

    assertNotNull(jarFile.getEntry("bundle/moe_logo.png"));
    assertNotNull(jarFile.getEntry("lib/iphonesimulator/libTestLibrary.a"));
    assertNotNull(jarFile.getEntry("lib/iphoneos/libTestLibrary.a"));

    jarFile.close();

}

From source file:com.orange.mmp.widget.WidgetManager.java

/**
 * Add branch ID to a widget Manifest before deploying it
 * @param widgetFile//from  ww  w. j  av a2s .c o m
 * @param branchId
 * @return a Widget instance
 * @throws IOException 
 * @throws MMPException 
 */
public Widget deployWidget(File widgetFile, String branchId) throws MMPException {
    Widget widget = new Widget();
    ZipInputStream zin = null;
    ZipOutputStream zout = null;

    try {
        JarFile jarFile = new JarFile(new File(widgetFile.toURI()));
        Manifest manifest = jarFile.getManifest();

        String tmpWidgetId = manifest.getMainAttributes()
                .getValue(FelixOSGiContainer.BUNDLE_SYMBOLICNAME_HEADER);
        widget.setBranchId(branchId);

        if (tmpWidgetId != null) {
            widget.setName(manifest.getMainAttributes().getValue(FelixOSGiContainer.BUNDLE_NAME_HEADER));
            widget.setId(tmpWidgetId + com.orange.mmp.widget.Constants.BRANCH_SUFFIX_PATTERN + branchId);
            manifest.getMainAttributes().putValue(FelixOSGiContainer.BUNDLE_SYMBOLICNAME_HEADER,
                    widget.getId());

            File tempFile = File.createTempFile(String.valueOf(System.currentTimeMillis()), ".jar");

            zin = new ZipInputStream(new FileInputStream(widgetFile));
            zout = new ZipOutputStream(new FileOutputStream(tempFile));

            ZipEntry entry = zin.getNextEntry();
            while (entry != null) {
                String name = entry.getName();
                zout.putNextEntry(new ZipEntry(name));
                if (!name.equals(com.orange.mmp.midlet.Constants.JAR_MANIFEST_ENTRY)) {
                    IOUtils.copy(zin, zout);
                } else {
                    manifest.write(zout);
                }
                entry = zin.getNextEntry();
            }

            widget.setLocation(tempFile.toURI());
            widget.setId(tmpWidgetId);
            widget.setLastModified(tempFile.lastModified());
            widget.setCategory(com.orange.mmp.core.Constants.MODULE_CATEGORY_WIDGET);
            widget.setVersion(new Version(
                    manifest.getMainAttributes().getValue(FelixOSGiContainer.BUNDLE_VERSION_HEADER)));
        } else {
            throw new MMPException("Invalid module archive, missing "
                    + FelixOSGiContainer.BUNDLE_SYMBOLICNAME_HEADER + " header");
        }

    } catch (IOException ioe) {
        throw new MMPException("Failed to deploy widget", ioe);
    } finally {
        if (zin != null) {
            try {
                zin.close();
            } catch (IOException ioe) {
                //NOP
            }
        }
        if (zout != null)
            try {
                zout.close();
            } catch (IOException ioe) {
                //NOP
            }
    }

    MMPOSGiContainer moduleContainer = (MMPOSGiContainer) ModuleContainerFactory.getInstance()
            .getModuleContainer();
    moduleContainer.deployModule(new File(widget.getLocation()));

    return widget;
}

From source file:com.github.jengelman.gradle.plugins.integration.TestFile.java

public Manifest getManifest() {
    assertIsFile();//from ww  w  .  j  av a  2  s. co m
    try {
        JarFile jarFile = new JarFile(this);
        try {
            return jarFile.getManifest();
        } finally {
            jarFile.close();
        }
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}