Search in sources :

Example 46 with Attributes

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

the class DeploymentInstaller method analyseFile.

private ResolveRequest analyseFile(File file) throws IOException {
    log(LogService.LOG_INFO, null, "Resolving bundle archive: %s", file.getAbsolutePath());
    String fileUriStr = file.toURI().toString();
    String indexUriStr;
    String name;
    String symbolicName;
    String version = "";
    List<Requirement> requirements = new LinkedList<>();
    try (JarFile jar = new JarFile(file)) {
        Attributes manifestAttribs = jar.getManifest().getMainAttributes();
        symbolicName = manifestAttribs.getValue(Constants.DEPLOYMENT_SYMBOLIC_NAME);
        if (symbolicName == null) {
            symbolicName = file.getName();
        }
        name = manifestAttribs.getValue(Constants.DEPLOYMENT_NAME);
        if (name == null) {
            name = symbolicName;
        }
        version = manifestAttribs.getValue(Constants.DEPLOYMENT_VERSION);
        if (version == null) {
            version = UNKNOWN_DEPLOYMENT_VERSION;
        }
        requirements.addAll(RequirementParser.parseRequireBundle(manifestAttribs.getValue(org.osgi.framework.Constants.REQUIRE_BUNDLE)));
        requirements.addAll(RequirementParser.parseRequireCapability(manifestAttribs.getValue(org.osgi.framework.Constants.REQUIRE_CAPABILITY)));
        if (requirements.isEmpty()) {
            throw new IllegalArgumentException(String.format("Missing %s or %s header in manifest in %s", org.osgi.framework.Constants.REQUIRE_BUNDLE, org.osgi.framework.Constants.REQUIRE_CAPABILITY, file.getAbsolutePath()));
        }
        JarEntry indexEntry = findEntry(jar, Constants.INDEX_FILE, Constants.DEFAULT_INDEX_FILE);
        if (indexEntry == null) {
            throw new IllegalArgumentException("Missing index entry in " + file.getAbsolutePath());
        }
        indexUriStr = "jar:" + fileUriStr + "!/" + indexEntry.getName();
    }
    try {
        ResolveRequest request = new ResolveRequest(name, symbolicName, version, Collections.singletonList(new URI(indexUriStr)), requirements);
        return request;
    } catch (URISyntaxException e) {
        throw new IOException("Unable to convert index URI " + indexUriStr, e);
    }
}
Also used : Requirement(org.osgi.resource.Requirement) Attributes(java.util.jar.Attributes) ResolveRequest(org.apache.felix.fileinstall.plugins.resolver.ResolveRequest) URISyntaxException(java.net.URISyntaxException) IOException(java.io.IOException) JarFile(java.util.jar.JarFile) JarEntry(java.util.jar.JarEntry) URI(java.net.URI) LinkedList(java.util.LinkedList)

Example 47 with Attributes

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

the class DPSigner method createSignatureFile.

private Manifest createSignatureFile(Manifest manifest) throws IOException {
    byte[] mfRawBytes;
    try (ByteArrayOutputStream baos = new ByteArrayOutputStream()) {
        manifest.write(baos);
        mfRawBytes = baos.toByteArray();
    }
    Manifest sf = new Manifest();
    Attributes sfMain = sf.getMainAttributes();
    Map<String, Attributes> sfEntries = sf.getEntries();
    sfMain.put(Attributes.Name.SIGNATURE_VERSION, "1.0");
    sfMain.putValue("Created-By", "Apache Felix DeploymentPackageBuilder");
    sfMain.putValue(m_digestAlg + "-Digest-Manifest", calculateDigest(mfRawBytes));
    sfMain.putValue(m_digestAlg + "-Digest-Manifest-Main-Attribute", calculateDigest(getRawBytesMainAttributes(manifest)));
    for (Entry<String, Attributes> entry : manifest.getEntries().entrySet()) {
        String name = entry.getKey();
        byte[] entryData = getRawBytesAttributes(entry.getValue());
        sfEntries.put(name, getDigestAttributes(entryData));
    }
    return sf;
}
Also used : Attributes(java.util.jar.Attributes) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Manifest(java.util.jar.Manifest)

Example 48 with Attributes

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

the class DeploymentPackageBuilder method createManifest.

private Manifest createManifest(List<ArtifactData> files) throws Exception {
    Manifest manifest = new Manifest();
    Attributes main = manifest.getMainAttributes();
    main.putValue("Manifest-Version", "1.0");
    main.putValue("DeploymentPackage-SymbolicName", m_symbolicName);
    main.putValue("DeploymentPackage-Version", m_version);
    if ((m_fixPackageVersion != null) && !"".equals(m_fixPackageVersion)) {
        main.putValue("DeploymentPackage-FixPack", m_fixPackageVersion);
    }
    Map<String, Attributes> entries = manifest.getEntries();
    for (ArtifactData file : files) {
        Attributes attrs = new Attributes();
        attrs.putValue("Name", file.getFilename());
        if (file.isBundle()) {
            attrs.putValue("Bundle-SymbolicName", file.getSymbolicName());
            attrs.putValue("Bundle-Version", file.getVersion());
            if (file.isCustomizer()) {
                attrs.putValue("DeploymentPackage-Customizer", "true");
                attrs.putValue("Deployment-ProvidesResourceProcessor", file.getProcessorPid());
            }
        } else if (file.isResourceProcessorNeeded()) {
            attrs.putValue("Resource-Processor", file.getProcessorPid());
        }
        if (file.isMissing()) {
            attrs.putValue("DeploymentPackage-Missing", "true");
        }
        if (isAddSignatures()) {
            m_signer.addDigestAttribute(attrs, file);
        }
        entries.put(file.getFilename(), attrs);
    }
    return manifest;
}
Also used : Attributes(java.util.jar.Attributes) Manifest(java.util.jar.Manifest)

Example 49 with Attributes

use of java.util.jar.Attributes 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 50 with Attributes

use of java.util.jar.Attributes 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)

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