Search in sources :

Example 86 with Manifest

use of java.util.jar.Manifest in project felix by apache.

the class Utils method merge.

public static void merge(File targetIndex, File target, File sourceIndex, File source) throws IOException {
    List targetFiles = readIndex(targetIndex);
    List sourceFiles = readIndex(sourceIndex);
    List result = new ArrayList(targetFiles);
    File manifestFile = new File(source, (String) sourceFiles.remove(0));
    Manifest resultManifest = Utils.readManifest(manifestFile);
    resultManifest.getMainAttributes().remove(new Name(Constants.DEPLOYMENTPACKAGE_FIXPACK));
    for (Iterator i = result.iterator(); i.hasNext(); ) {
        String targetFile = (String) i.next();
        if (!MANIFEST_NAME.equals(targetFile) && !resultManifest.getEntries().containsKey(targetFile)) {
            i.remove();
        }
    }
    for (Iterator iter = sourceFiles.iterator(); iter.hasNext(); ) {
        String path = (String) iter.next();
        File from = new File(source, path);
        File to = new File(target, path);
        if (targetFiles.contains(path)) {
            if (!to.delete()) {
                throw new IOException("Could not delete " + to);
            }
        } else {
            result.add(path);
        }
        if (!rename(from, to)) {
            throw new IOException("Could not rename " + from + " to " + to);
        }
    }
    targetFiles.removeAll(sourceFiles);
    for (Iterator iter = resultManifest.getEntries().keySet().iterator(); iter.hasNext(); ) {
        String path = (String) iter.next();
        Attributes sourceAttribute = (Attributes) resultManifest.getEntries().get(path);
        if ("true".equals(sourceAttribute.remove(new Name(Constants.DEPLOYMENTPACKAGE_MISSING)))) {
            targetFiles.remove(path);
        }
    }
    for (Iterator iter = targetFiles.iterator(); iter.hasNext(); ) {
        String path = (String) iter.next();
        File targetFile = new File(target, path);
        if (!targetFile.delete()) {
            throw new IOException("Could not delete " + targetFile);
        }
    }
    GZIPOutputStream outputStream = new GZIPOutputStream(new FileOutputStream(new File(target, MANIFEST_NAME)));
    try {
        resultManifest.write(outputStream);
    } finally {
        outputStream.close();
    }
    writeIndex(targetIndex, result);
}
Also used : GZIPOutputStream(java.util.zip.GZIPOutputStream) FileOutputStream(java.io.FileOutputStream) ArrayList(java.util.ArrayList) Iterator(java.util.Iterator) Attributes(java.util.jar.Attributes) ArrayList(java.util.ArrayList) List(java.util.List) IOException(java.io.IOException) Manifest(java.util.jar.Manifest) JarFile(java.util.jar.JarFile) File(java.io.File) Name(java.util.jar.Attributes.Name)

Example 87 with Manifest

use of java.util.jar.Manifest in project felix by apache.

the class ContentCopyingJarInputStreamTest method testCopyJarWithoutIndexAndWithManifestOk.

/**
 * Tests that we can copy a simple {@link JarInputStream}.
 */
public void testCopyJarWithoutIndexAndWithManifestOk() throws Exception {
    Manifest man = createManifest();
    createJar(man, false);
    assertJarContents(man);
}
Also used : Manifest(java.util.jar.Manifest)

Example 88 with Manifest

use of java.util.jar.Manifest in project felix by apache.

the class DeploymentPackageBuilderTest method testCreateMissingBundleResourceOk.

/**
 * Tests that we can build a deployment package with a bundle resource.
 */
@Test
public void testCreateMissingBundleResourceOk() throws Exception {
    DeploymentPackageBuilder dpBuilder = DeploymentPackageBuilder.create("dp-test", "1.0.0");
    dpBuilder.setFixPackage().add(dpBuilder.createBundleResource().setUrl(getTestBundle("bundle1")).setMissing());
    JarInputStream jis = new JarInputStream(dpBuilder.generate());
    assertNotNull(jis);
    Manifest manifest = jis.getManifest();
    assertManifestHeader(manifest, "DeploymentPackage-SymbolicName", "dp-test");
    assertManifestHeader(manifest, "DeploymentPackage-Version", "1.0.0");
    String filename = getBundleName("bundle1");
    assertManifestEntry(manifest, filename, "Name", filename);
    assertManifestEntry(manifest, filename, "Bundle-SymbolicName", "testbundles.bundle1");
    assertManifestEntry(manifest, filename, "Bundle-Version", "1.0.0");
    assertManifestEntry(manifest, filename, "DeploymentPackage-Missing", "true");
    int count = countJarEntries(jis);
    assertEquals("Expected two entries in the JAR!", 0, count);
}
Also used : JarInputStream(java.util.jar.JarInputStream) DeploymentPackageBuilder(org.apache.felix.deploymentadmin.itest.util.DeploymentPackageBuilder) Manifest(java.util.jar.Manifest) Test(org.junit.Test)

Example 89 with Manifest

use of java.util.jar.Manifest in project felix by apache.

the class DeploymentPackageBuilderTest method testResourceFilterOk.

/**
 * Tests that we can filter a resource.
 */
@Test
public void testResourceFilterOk() throws Exception {
    DeploymentPackageBuilder dpBuilder = DeploymentPackageBuilder.create("dp-test", "1.0.0");
    dpBuilder.add(dpBuilder.createBundleResource().setUrl(getTestBundle("bundle2"))).add(dpBuilder.createBundleResource().setVersion("1.1.0").setFilter(new JarManifestManipulatingFilter("Bundle-Version", "1.1.0", "Foo", "bar")).setUrl(getTestBundle("bundle1")));
    JarInputStream jis = new JarInputStream(dpBuilder.generate());
    assertNotNull(jis);
    Manifest manifest = jis.getManifest();
    assertManifestHeader(manifest, "DeploymentPackage-SymbolicName", "dp-test");
    assertManifestHeader(manifest, "DeploymentPackage-Version", "1.0.0");
    String filename = getBundleName("bundle1");
    assertManifestEntry(manifest, filename, "Name", filename);
    assertManifestEntry(manifest, filename, "Bundle-SymbolicName", "testbundles.bundle1");
    assertManifestEntry(manifest, filename, "Bundle-Version", "1.1.0");
    filename = getBundleName("bundle2");
    assertManifestEntry(manifest, filename, "Name", filename);
    assertManifestEntry(manifest, filename, "Bundle-SymbolicName", "testbundles.bundle2");
    assertManifestEntry(manifest, filename, "Bundle-Version", "1.0.0");
    try {
        byte[] buf = new byte[32 * 1024];
        JarEntry entry;
        while ((entry = jis.getNextJarEntry()) != null) {
            if (entry.getName().endsWith("valid-bundle1.jar")) {
                int read = jis.read(buf);
                JarInputStream jis2 = new JarInputStream(new ByteArrayInputStream(Arrays.copyOf(buf, read)));
                Manifest manifest2 = jis2.getManifest();
                Attributes mainAttributes = manifest2.getMainAttributes();
                assertEquals("1.1.0", mainAttributes.getValue("Bundle-Version"));
                assertEquals("bar", mainAttributes.getValue("Foo"));
                jis2.close();
            }
            jis.closeEntry();
        }
    } finally {
        jis.close();
    }
}
Also used : JarInputStream(java.util.jar.JarInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) DeploymentPackageBuilder(org.apache.felix.deploymentadmin.itest.util.DeploymentPackageBuilder) Attributes(java.util.jar.Attributes) JarManifestManipulatingFilter(org.apache.felix.deploymentadmin.itest.util.DeploymentPackageBuilder.JarManifestManipulatingFilter) Manifest(java.util.jar.Manifest) JarEntry(java.util.jar.JarEntry) Test(org.junit.Test)

Example 90 with Manifest

use of java.util.jar.Manifest in project felix by apache.

the class ManipulationMetadataTest method setUp.

public void setUp() {
    File manFile = new File("src/test/resources/manipulation/MANIFEST.MF");
    Manifest manifest;
    try {
        manifest = new Manifest(new FileInputStream(manFile));
        header = manifest.getMainAttributes().getValue("iPOJO-Components");
    } catch (FileNotFoundException e) {
        fail(e.getMessage());
    } catch (IOException e) {
        fail(e.getMessage());
    }
}
Also used : FileNotFoundException(java.io.FileNotFoundException) IOException(java.io.IOException) Manifest(java.util.jar.Manifest) File(java.io.File) FileInputStream(java.io.FileInputStream)

Aggregations

Manifest (java.util.jar.Manifest)1226 Attributes (java.util.jar.Attributes)392 File (java.io.File)391 IOException (java.io.IOException)336 JarFile (java.util.jar.JarFile)231 InputStream (java.io.InputStream)184 URL (java.net.URL)177 JarOutputStream (java.util.jar.JarOutputStream)145 FileOutputStream (java.io.FileOutputStream)131 Test (org.junit.Test)129 FileInputStream (java.io.FileInputStream)119 Jar (aQute.bnd.osgi.Jar)105 JarInputStream (java.util.jar.JarInputStream)104 Builder (aQute.bnd.osgi.Builder)99 ZipEntry (java.util.zip.ZipEntry)99 ByteArrayOutputStream (java.io.ByteArrayOutputStream)96 JarEntry (java.util.jar.JarEntry)96 ByteArrayInputStream (java.io.ByteArrayInputStream)93 ArrayList (java.util.ArrayList)83 Map (java.util.Map)83