Example usage for java.util.jar Manifest write

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

Introduction

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

Prototype

public void write(OutputStream out) throws IOException 

Source Link

Document

Writes the Manifest to the specified OutputStream.

Usage

From source file:org.wso2.carbon.server.util.Utils.java

/**
 * Create an OSGi bundle out of a JAR file
 *
 * @param jarFile  The jarfile to be bundled
 * @param bundle   The bundle to be created
 * @param manifest The manifest file//from   ww  w.  j  a va2s  .  com
 * @throws IOException If an error occurs while reading the jar or creating the bundle
 */
public static void createBundle(File jarFile, File bundle, Manifest manifest) throws IOException {
    String extractedDirPath = JAR_TO_BUNDLE_DIR + File.separator + System.currentTimeMillis() + Math.random();
    File extractedDir = new File(extractedDirPath);
    if (!extractedDir.mkdirs()) {
        throw new IOException("Fail to create the directory: " + extractedDir.getAbsolutePath());
    }
    FileOutputStream mfos = null;
    FileOutputStream p2InfOs = null;
    try {
        Utils.copyFileToDir(jarFile, extractedDir);
        String metaInfPath = extractedDirPath + File.separator + "META-INF";
        if (!new File(metaInfPath).mkdirs()) {
            throw new IOException("Failed to create the directory: " + metaInfPath);
        }
        mfos = new FileOutputStream(metaInfPath + File.separator + "MANIFEST.MF");
        manifest.write(mfos);

        File p2InfFile = new File(metaInfPath + File.separator + "p2.inf");
        if (!p2InfFile.createNewFile()) {
            throw new IOException("Fail to create the file: " + p2InfFile.getAbsolutePath());
        }
        p2InfOs = new FileOutputStream(p2InfFile);
        p2InfOs.write("instructions.configure=markStarted(started:true);".getBytes());
        p2InfOs.flush();

        Utils.archiveDir(bundle.getAbsolutePath(), extractedDirPath);
        Utils.deleteDir(extractedDir);

    } finally {
        try {
            if (mfos != null) {
                mfos.close();
            }
        } catch (IOException e) {
            System.out.println("Unable to close the OutputStream " + e.getMessage());
            e.printStackTrace();
        }

        try {
            if (p2InfOs != null) {
                p2InfOs.close();
            }
        } catch (IOException e) {
            System.out.println("Unable to close the OutputStream " + e.getMessage());
            e.printStackTrace();
        }
    }
}

From source file:pxb.android.tinysign.TinySign.java

private static String writeMF(File dir, Manifest manifest, ZipOutputStream zos)
        throws NoSuchAlgorithmException, IOException {
    MessageDigest md = MessageDigest.getInstance("SHA1");
    DigestOutputStream dos = new DigestOutputStream(zos, md);
    zipAndSha1(dir, zos, dos, manifest);
    Attributes main = manifest.getMainAttributes();
    main.putValue("Manifest-Version", "1.0");
    main.putValue("Created-By", "tiny-sign-" + TinySign.class.getPackage().getImplementationVersion());
    zos.putNextEntry(new ZipEntry("META-INF/MANIFEST.MF"));
    manifest.write(dos);
    zos.closeEntry();/* www  .j ava 2 s .  c  o m*/
    return eBase64(md.digest());
}

From source file:pxb.android.tinysign.TinySign.java

private static byte[] writeSF(ZipOutputStream zos, Manifest sf, String sha1Manifest) throws Exception {
    Signature signature = instanceSignature();
    zos.putNextEntry(new ZipEntry("META-INF/CERT.SF"));
    SignatureOutputStream out = new SignatureOutputStream(zos, signature);
    out.write("Signature-Version: 1.0\r\n".getBytes("UTF-8"));
    out.write(("Created-By: tiny-sign-" + TinySign.class.getPackage().getImplementationVersion() + "\r\n")
            .getBytes("UTF-8"));
    out.write("SHA1-Digest-Manifest: ".getBytes("UTF-8"));
    out.write(sha1Manifest.getBytes("UTF-8"));
    out.write('\r');
    out.write('\n');

    sf.write(out);

    zos.closeEntry();// w ww.  j  av  a  2s . c o m

    return signature.sign();
}

From source file:test.BuilderTest.java

/**
 * Test the name section/*  w ww  . j  a  v  a  2  s .  co  m*/
 */

public static void testNamesection() throws Exception {
    Builder b = new Builder();
    try {
        b.addClasspath(IO.getFile("jar/osgi.jar"));
        b.setProperty(Constants.NAMESECTION,
                "org/osgi/service/event/*;MD5='${md5;${@}}';SHA1='${sha1;${@}}';MD5H='${md5;${@};hex}'");
        b.setProperty(Constants.PRIVATEPACKAGE, "org.osgi.service.event");
        Jar build = b.build();
        assertOk(b);
        build.calcChecksums(new String[] { "MD5", "SHA1" });
        assertTrue(b.check());
        Manifest m = build.getManifest();
        m.write(System.err);

        assertNotNull(m.getAttributes("org/osgi/service/event/EventAdmin.class").getValue("MD5"));
        assertNotNull(m.getAttributes("org/osgi/service/event/EventAdmin.class").getValue("SHA1"));
        assertEquals(m.getAttributes("org/osgi/service/event/EventAdmin.class").getValue("MD5-Digest"),
                m.getAttributes("org/osgi/service/event/EventAdmin.class").getValue("MD5"));
    } finally {
        b.close();
    }

}

From source file:test.BuilderTest.java

public static void testSimpleWab() throws Exception {
    Builder b = new Builder();
    try {/*from w  w  w .j a v a2  s .c  o m*/
        b.setProperty("-wab", "");
        b.setProperty("Private-Package", "org.osgi.service.event");
        b.addClasspath(IO.getFile("jar/osgi.jar"));
        Jar jar = b.build();
        assertTrue(b.check());

        Manifest m = jar.getManifest();
        m.write(System.err);
        assertNotNull(b.getImports().getByFQN("org.osgi.framework"));
    } finally {
        b.close();
    }

}

From source file:test.BuilderTest.java

/**
 * Test private imports. We first build a jar with a import:=private packge.
 * Then place it// w w w. j av  a2  s. c  o m
 * 
 * @throws Exception
 */

public static void testClassnames() throws Exception {
    Builder b = new Builder();
    try {
        b.addClasspath(IO.getFile("jar/osgi.jar"));
        b.addClasspath(IO.getFile("jar/ds.jar"));
        b.addClasspath(IO.getFile("jar/ifc112.jar"));
        b.setProperty("Export-Package", "*");
        b.setProperty("C1", "${classes;implementing;org.osgi.service.component.*}");
        b.setProperty("C2", "${classes;extending;org.xml.sax.helpers.*}");
        b.setProperty("C3", "${classes;importing;org.xml.sax}");
        b.setProperty("C4", "${classes;named;*Parser*}");
        b.setProperty("C5", "${classes;named;*Parser*;version;45.*}");
        Jar jar = b.build();
        assertTrue(b.check());

        Manifest m = jar.getManifest();
        m.write(System.err);
        Attributes main = m.getMainAttributes();
        assertList(asl(
                "org.eclipse.equinox.ds.service.ComponentContextImpl,org.eclipse.equinox.ds.service.ComponentFactoryImpl,org.eclipse.equinox.ds.service.ComponentInstanceImpl"),
                asl(main.getValue("C1")));
        assertList(asl("org.eclipse.equinox.ds.parser.ElementHandler, "
                + "org.eclipse.equinox.ds.parser.IgnoredElement,"
                + "org.eclipse.equinox.ds.parser.ImplementationElement,"
                + "org.eclipse.equinox.ds.parser.ParserHandler, "
                + "org.eclipse.equinox.ds.parser.PropertiesElement,"
                + "org.eclipse.equinox.ds.parser.PropertyElement, "
                + "org.eclipse.equinox.ds.parser.ProvideElement, "
                + "org.eclipse.equinox.ds.parser.ReferenceElement, "
                + "org.eclipse.equinox.ds.parser.ServiceElement,"
                + "org.eclipse.equinox.ds.parser.ComponentElement"), asl(main.getValue("C2")));
        assertList(asl(
                "org.eclipse.equinox.ds.parser.ComponentElement,org.eclipse.equinox.ds.parser.ElementHandler,org.eclipse.equinox.ds.parser.IgnoredElement,org.eclipse.equinox.ds.parser.ImplementationElement,org.eclipse.equinox.ds.parser.Parser,org.eclipse.equinox.ds.parser.ParserHandler,org.eclipse.equinox.ds.parser.PropertiesElement,org.eclipse.equinox.ds.parser.PropertyElement,org.eclipse.equinox.ds.parser.ProvideElement,org.eclipse.equinox.ds.parser.ReferenceElement,org.eclipse.equinox.ds.parser.ServiceElement"),
                asl(main.getValue("C3")));
        assertList(asl(
                "org.eclipse.equinox.ds.parser.XMLParserNotAvailableException,org.eclipse.equinox.ds.parser.Parser,org.eclipse.equinox.ds.parser.ParserHandler,netscape.application.HTMLParser,org.eclipse.equinox.ds.parser.ParserConstants,org.osgi.util.xml.XMLParserActivator"),
                asl(main.getValue("C4")));
        assertEquals("netscape.application.HTMLParser", main.getValue("C5"));
    } finally {
        b.close();
    }
}

From source file:test.BuilderTest.java

public static void testMultipleExport2() throws Exception {
    File cp[] = { IO.getFile("jar/asm.jar") };
    Builder bmaker = new Builder();
    try {/*from  ww  w .  jav a2  s  . com*/
        Properties p = new Properties();
        p.setProperty("Export-Package",
                "org.objectweb.asm;version=1.1, org.objectweb.asm;version=1.2, org.objectweb.asm;version=2.3");
        bmaker.setProperties(p);
        bmaker.setClasspath(cp);
        Jar jar = bmaker.build();
        assertTrue(bmaker.check());
        jar.getManifest().write(System.err);
        Manifest m = jar.getManifest();
        m.write(System.err);
        String ip = m.getMainAttributes().getValue("Export-Package");
        assertTrue(ip.indexOf("org.objectweb.asm;version=\"1.1\"") >= 0);
        assertTrue(ip.indexOf("org.objectweb.asm;version=\"1.2\"") >= 0);
        assertTrue(ip.indexOf("org.objectweb.asm;version=\"2.3\"") >= 0);
    } finally {
        bmaker.close();
    }
}

From source file:test.BuilderTest.java

public static void testDuplicateExport() throws Exception {
    File cp[] = { IO.getFile("jar/asm.jar") };
    Builder bmaker = new Builder();
    try {/*from   w w w  .j a v  a 2  s.co m*/
        Properties p = new Properties();
        p.setProperty("Import-Package", "*");
        p.setProperty("Export-Package", "org.*;version=1.2,org.objectweb.asm;version=1.3");
        bmaker.setProperties(p);
        bmaker.setClasspath(cp);
        Jar jar = bmaker.build();
        assertTrue(bmaker.check());
        Manifest m = jar.getManifest();
        m.write(System.err);
        String ip = m.getMainAttributes().getValue("Export-Package");
        assertTrue(ip.indexOf("org.objectweb.asm;version=\"1.2\"") >= 0);
    } finally {
        bmaker.close();
    }
}

From source file:test.BuilderTest.java

public static void testImportExportBadVersion() throws Exception {
    Builder b = new Builder();
    try {//from   ww w. j av  a 2  s  .  c o  m
        b.addClasspath(IO.getFile("jar/ds.jar"));
        b.set(Analyzer.BUNDLE_VERSION, "0.9.5-@#SNAPSHOT");
        b.set(Analyzer.EXPORT_PACKAGE, "*;version=0.9.5-@#SNAPSHOT");
        b.set(Analyzer.IMPORT_PACKAGE, "*;version=0.9.5-@#SNAPSHOT");

        Jar jar = b.build();
        assertTrue(b.check());
        Manifest m = jar.getManifest();
        m.write(System.err);
        assertEquals(m.getMainAttributes().getValue("Bundle-Version"), "0.9.5.SNAPSHOT");

        assertNotNull(b.getExports().getByFQN("org.eclipse.equinox.ds.parser"));
        assertEquals("0.9.5.SNAPSHOT", b.getExports().getByFQN("org.eclipse.equinox.ds.parser").getVersion());

        assertNotNull(b.getImports().getByFQN("org.osgi.framework"));
        assertEquals("0.9.5.SNAPSHOT", b.getImports().getByFQN("org.osgi.framework").getVersion());
    } finally {
        b.close();
    }
}