Search in sources :

Example 76 with Attributes

use of java.util.jar.Attributes in project sling by apache.

the class PreparePackageMojoTest method testSubsystemBaseGeneration.

@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.johnzon/1.0.0", "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 {
        // 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.johnzon/1.0.0\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.johnzon-1.0.0.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.johnzon", "1.0.0");
            ZipEntry sjZE = jf.getEntry("Potential_Bundles/10/org.apache.sling.commons.johnzon-1.0.0.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()));
    }
}
Also used : JarInputStream(java.util.jar.JarInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) ZipEntry(java.util.zip.ZipEntry) Model(org.apache.sling.provisioning.model.Model) StringReader(java.io.StringReader) Attributes(java.util.jar.Attributes) JarFile(java.util.jar.JarFile) Manifest(java.util.jar.Manifest) JarFile(java.util.jar.JarFile) File(java.io.File) Test(org.junit.Test)

Example 77 with Attributes

use of java.util.jar.Attributes in project sling by apache.

the class PreparePackageMojoTest method testBSNRenaming.

@Test
public void testBSNRenaming() 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.johnzon/1.0.0");
    try {
        String modelTxt = "[feature name=:launchpad]\n" + "[artifacts]\n" + "  org.apache.sling/org.apache.sling.commons.classloader/1.3.2\n" + "" + "[feature name=rename_test]\n" + "  org.apache.sling/org.apache.sling.commons.johnzon/1.0.0 [bundle:rename-bsn=r-foo.bar.renamed.sling.commons.johnzon]\n";
        Model model = ModelReader.read(new StringReader(modelTxt), null);
        ppm.execute(model);
        File orgJar = getMavenArtifactFile(getMavenRepoRoot(), "org.apache.sling", "org.apache.sling.commons.johnzon", "1.0.0");
        File generatedJar = new File(ppm.getTmpDir() + "/r-foo.bar.renamed.sling.commons.johnzon-1.0.0.jar");
        compareJarContents(orgJar, generatedJar);
        try (JarFile jfOrg = new JarFile(orgJar);
            JarFile jfNew = new JarFile(generatedJar)) {
            Manifest mfOrg = jfOrg.getManifest();
            Manifest mfNew = jfNew.getManifest();
            Attributes orgAttrs = mfOrg.getMainAttributes();
            Attributes newAttrs = mfNew.getMainAttributes();
            for (Object key : orgAttrs.keySet()) {
                String orgVal = orgAttrs.getValue(key.toString());
                String newVal = newAttrs.getValue(key.toString());
                if ("Bundle-SymbolicName".equals(key.toString())) {
                    assertEquals("Should have recorded the original Bundle-SymbolicName", orgVal, newAttrs.getValue("X-Original-Bundle-SymbolicName"));
                    assertEquals("r-foo.bar.renamed.sling.commons.johnzon", newVal);
                } else {
                    assertEquals("Different keys: " + key, orgVal, newVal);
                }
            }
        }
    } finally {
        FileUtils.deleteDirectory(new File(ppm.project.getBuild().getDirectory()));
    }
}
Also used : Model(org.apache.sling.provisioning.model.Model) StringReader(java.io.StringReader) Attributes(java.util.jar.Attributes) JarFile(java.util.jar.JarFile) Manifest(java.util.jar.Manifest) JarFile(java.util.jar.JarFile) File(java.io.File) Test(org.junit.Test)

Example 78 with Attributes

use of java.util.jar.Attributes in project bnd by bndtools.

the class AttributesTest method testRemoveDirective.

/**
	 * Remove a version attribute A mandatory attribute adds the common and tst
	 * properties to the import. We remove them using remove:=*
	 * 
	 * @throws Exception
	 */
public static void testRemoveDirective() throws Exception {
    Jar javax = new Jar("test");
    Manifest m = new Manifest();
    m.getMainAttributes().putValue("Export-Package", "javax.microedition.io;a1=exp-1;a2=exp-2;a3=exp-3;x1=x1;x2=x2;x3=x3;mandatory:=\"a1,a2,a3,x1,x2,x3\"");
    javax.setManifest(m);
    Jar[] cp = { javax, new Jar(IO.getFile("jar/osgi.jar")) };
    Builder bmaker = new Builder();
    Properties p = new Properties();
    p.put("Import-Package", "javax.microedition.io;-remove-attribute:=a1|x?;a2=imp-2,*");
    p.put("Export-Package", "org.osgi.service.io");
    bmaker.setClasspath(cp);
    bmaker.setProperties(p);
    Jar jar = bmaker.build();
    assertTrue(bmaker.check());
    assertNotNull(bmaker.getImports());
    assertNotNull(bmaker.getImports().getByFQN("javax.microedition.io"));
    assertNotNull(bmaker.getImports().getByFQN("javax.microedition.io").get("a2"));
    jar.getManifest().write(System.err);
    Manifest manifest = jar.getManifest();
    Attributes main = manifest.getMainAttributes();
    String imprt = main.getValue("Import-Package");
    assertNotNull("Import package header", imprt);
    Parameters map = Processor.parseHeader(imprt, null);
    System.err.println("** " + map);
    Map<String, String> attrs = map.get("javax.microedition.io");
    assertNotNull(attrs);
    assertNull(attrs.get("a1"));
    assertNull(attrs.get("x1"));
    assertNull(attrs.get("x2"));
    assertNull(attrs.get("x3"));
    assertEquals("imp-2", attrs.get("a2"));
    assertEquals("exp-3", attrs.get("a3"));
}
Also used : Parameters(aQute.bnd.header.Parameters) Builder(aQute.bnd.osgi.Builder) Attributes(java.util.jar.Attributes) Jar(aQute.bnd.osgi.Jar) Manifest(java.util.jar.Manifest) Properties(java.util.Properties)

Example 79 with Attributes

use of java.util.jar.Attributes in project bnd by bndtools.

the class BuilderTest method testDigests.

/**
	 * Check if we can create digests
	 * 
	 * @throws Exception
	 */
public static void testDigests() throws Exception {
    Builder b = new Builder();
    try {
        b.addClasspath(IO.getFile(new File(""), "jar/osgi.jar"));
        b.setExportPackage("org.osgi.framework");
        b.setProperty(Constants.DIGESTS, "MD5, SHA1");
        Jar jar = b.build();
        assertTrue(b.check());
        File f = File.createTempFile("test", ".jar");
        jar.write(f);
        Jar other = new Jar(f);
        Manifest manifest = other.getManifest();
        assertNotNull(manifest);
        Attributes attrs = manifest.getAttributes("org/osgi/framework/BundleActivator.class");
        assertNotNull(attrs);
        assertEquals("RTRhr3kadnulINegRhpmog==", attrs.getValue("MD5-Digest"));
        assertEquals("BfVfpnE3Srx/0UWwtzNecrAGf8A=", attrs.getValue("SHA-Digest"));
        other.close();
    } finally {
        b.close();
    }
}
Also used : Builder(aQute.bnd.osgi.Builder) Attributes(java.util.jar.Attributes) Jar(aQute.bnd.osgi.Jar) Manifest(java.util.jar.Manifest) File(java.io.File)

Example 80 with Attributes

use of java.util.jar.Attributes in project bnd by bndtools.

the class CorruptManifest method testCorruptJar.

public static void testCorruptJar() throws Exception {
    Builder b = new Builder();
    b.setProperty("NL1", "\n");
    b.setProperty("NL2", "\r\n");
    b.setProperty("NL3", ".");
    b.setProperty("NL4", ".\n.\n");
    b.setProperty("NL5", ltext);
    b.setProperty("Export-Package", "*");
    b.setClasspath(new File[] { IO.getFile("jar/asm.jar") });
    Jar jar = b.build();
    Manifest manifest = jar.getManifest();
    jar.writeManifest(System.err);
    Attributes main = manifest.getMainAttributes();
    assertNull(main.getValue("NL1"));
    assertNull(main.getValue("NL2"));
    assertEquals(".", main.getValue("NL3"));
    assertEquals(".\n.\n", main.getValue("NL4"));
    assertEquals(ltext, main.getValue("NL5"));
    ByteArrayOutputStream bout = new ByteArrayOutputStream();
    jar.writeManifest(bout);
    bout.flush();
    System.err.println("-----");
    System.err.write(bout.toByteArray());
    System.err.println("-----");
    ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
    manifest = new Manifest(bin);
    main = manifest.getMainAttributes();
    assertNull(main.getValue("NL1"));
    assertNull(main.getValue("NL2"));
    assertEquals(".", main.getValue("NL3"));
    assertEquals(". . ", main.getValue("NL4"));
    assertEquals(rtext, main.getValue("NL5"));
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) Builder(aQute.bnd.osgi.Builder) Attributes(java.util.jar.Attributes) Jar(aQute.bnd.osgi.Jar) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Manifest(java.util.jar.Manifest)

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