Search in sources :

Example 1 with Attrs

use of aQute.bnd.header.Attrs in project felix by apache.

the class BlueprintPlugin method analyzeJar.

public boolean analyzeJar(Analyzer analyzer) throws Exception {
    String mode = analyzer.getProperty("service_mode");
    if (mode == null) {
        mode = "service";
    }
    transformer.setParameter("nsh_interface", analyzer.getProperty("nsh_interface") != null ? analyzer.getProperty("nsh_interface") : "");
    transformer.setParameter("nsh_namespace", analyzer.getProperty("nsh_namespace") != null ? analyzer.getProperty("nsh_namespace") : "");
    Set<String> headers = Create.set();
    String bpHeader = analyzer.getProperty("Bundle-Blueprint", "OSGI-INF/blueprint");
    Map<String, ? extends Map<String, String>> map = Processor.parseHeader(bpHeader, null);
    bpHeader = "";
    for (String root : map.keySet()) {
        Jar jar = analyzer.getJar();
        Map<String, Resource> dir = jar.getDirectories().get(root);
        if (dir == null || dir.isEmpty()) {
            Resource resource = jar.getResource(root);
            if (resource != null) {
                process(analyzer, root, resource, headers);
                if (bpHeader.length() > 0) {
                    bpHeader += ",";
                }
                bpHeader += root;
            }
            continue;
        }
        for (Map.Entry<String, Resource> entry : dir.entrySet()) {
            String path = entry.getKey();
            Resource resource = entry.getValue();
            if (PATHS.matcher(path).matches()) {
                process(analyzer, path, resource, headers);
                if (bpHeader.length() > 0) {
                    bpHeader += ",";
                }
                bpHeader += path;
            }
        }
    }
    if (!map.isEmpty()) {
        analyzer.setProperty("Bundle-Blueprint", bpHeader);
    }
    // Group and analyze
    Set<String> caps = Create.set();
    Set<String> reqs = Create.set();
    Map<String, Set<Clause>> hdrs = Create.map();
    for (String str : headers) {
        int idx = str.indexOf(':');
        if (idx < 0) {
            analyzer.warning((new StringBuilder("Error analyzing services in blueprint resource: ")).append(str).toString());
            continue;
        }
        String h = str.substring(0, idx).trim();
        String v = str.substring(idx + 1).trim();
        Clause[] hc = parseHeader(v);
        // Convert generic caps/reqs
        if ("Import-Service".equals(h)) {
            if (!"service".equals(mode)) {
                Clause clause = hc[0];
                String multiple = clause.getDirective("multiple");
                String avail = clause.getDirective("availability");
                String filter = clause.getAttribute("filter");
                StringBuilder sb = new StringBuilder();
                sb.append("osgi.service;effective:=active;");
                if ("optional".equals(avail)) {
                    sb.append("resolution:=optional;");
                }
                if ("true".equals(multiple)) {
                    sb.append("cardinality:=multiple;");
                }
                if (filter == null) {
                    filter = "(" + Constants.OBJECTCLASS + "=" + clause.getName() + ")";
                } else if (!filter.startsWith("(") && !filter.endsWith(")")) {
                    filter = "(&(" + Constants.OBJECTCLASS + "=" + clause.getName() + ")(" + filter + "))";
                } else {
                    filter = "(&(" + Constants.OBJECTCLASS + "=" + clause.getName() + ")" + filter + ")";
                }
                sb.append("filter:=\"").append(filter).append("\"");
                reqs.add(sb.toString());
            } else if (!"generic".equals(mode)) {
                Set<Clause> clauses = hdrs.get(h);
                if (clauses == null) {
                    clauses = new HashSet<Clause>();
                    hdrs.put(h, clauses);
                }
                clauses.addAll(Arrays.asList(hc));
            }
        } else if ("Export-Service".equals(h)) {
            if (!"service".equals(mode)) {
                StringBuilder sb = new StringBuilder();
                sb.append("osgi.service;effective:=active;objectClass");
                if (hc.length > 1) {
                    sb.append(":List<String>=\"");
                } else {
                    sb.append("=\"");
                }
                for (int i = 0; i < hc.length; i++) {
                    if (i > 0) {
                        sb.append(",");
                    }
                    sb.append(hc[i].getName());
                }
                sb.append("\"");
                for (int i = 0; i < hc[0].getAttributes().length; i++) {
                    sb.append(";");
                    sb.append(hc[0].getAttributes()[i].getName());
                    sb.append("=\"");
                    sb.append(hc[0].getAttributes()[i].getValue());
                    sb.append("\"");
                }
                caps.add(sb.toString());
            } else if (!"generic".equals(mode)) {
                Set<Clause> clauses = hdrs.get(h);
                if (clauses == null) {
                    clauses = new HashSet<Clause>();
                    hdrs.put(h, clauses);
                }
                clauses.addAll(Arrays.asList(hc));
            }
        } else {
            Set<Clause> clauses = hdrs.get(h);
            if (clauses == null) {
                clauses = new HashSet<Clause>();
                hdrs.put(h, clauses);
            }
            clauses.addAll(Arrays.asList(hc));
        }
    }
    if (!caps.isEmpty()) {
        StringBuilder sb = new StringBuilder();
        String header = analyzer.getProperty("Provide-Capability");
        if (header != null) {
            sb.append(header);
        }
        for (String cap : caps) {
            if (sb.length() > 0) {
                sb.append(",");
            }
            sb.append(cap);
        }
        analyzer.setProperty("Provide-Capability", sb.toString());
    }
    if (!reqs.isEmpty()) {
        StringBuilder sb = new StringBuilder();
        String header = analyzer.getProperty("Require-Capability");
        if (header != null) {
            sb.append(header);
        }
        for (String req : reqs) {
            if (sb.length() > 0) {
                sb.append(",");
            }
            sb.append(req);
        }
        analyzer.setProperty("Require-Capability", sb.toString());
    }
    // Merge
    for (String header : hdrs.keySet()) {
        if ("Import-Class".equals(header) || "Import-Package".equals(header)) {
            Set<Clause> newAttr = hdrs.get(header);
            for (Clause a : newAttr) {
                String pkg = a.getName();
                if ("Import-Class".equals(header)) {
                    int n = a.getName().lastIndexOf('.');
                    if (n > 0) {
                        pkg = pkg.subSequence(0, n).toString();
                    } else {
                        continue;
                    }
                }
                PackageRef pkgRef = analyzer.getPackageRef(pkg);
                if (!analyzer.getReferred().containsKey(pkgRef)) {
                    Attrs attrs = analyzer.getReferred().put(pkgRef);
                    for (Attribute attribute : a.getAttributes()) {
                        attrs.put(attribute.getName(), attribute.getValue());
                    }
                }
            }
        } else {
            Set<String> merge = Create.set();
            String org = analyzer.getProperty(header);
            if (org != null && !org.isEmpty()) {
                for (Clause clause : parseHeader(org)) {
                    merge.add(clause.toString());
                }
            }
            for (Clause clause : hdrs.get(header)) {
                merge.add(clause.toString());
            }
            StringBuilder sb = new StringBuilder();
            for (String clause : merge) {
                if (sb.length() > 0) {
                    sb.append(",");
                }
                sb.append(clause);
            }
            analyzer.setProperty(header, sb.toString());
        }
    }
    return false;
}
Also used : HashSet(java.util.HashSet) Set(java.util.Set) Attribute(org.apache.felix.utils.manifest.Attribute) Resource(aQute.bnd.osgi.Resource) Attrs(aQute.bnd.header.Attrs) Jar(aQute.bnd.osgi.Jar) Clause(org.apache.felix.utils.manifest.Clause) PackageRef(aQute.bnd.osgi.Descriptors.PackageRef) Map(java.util.Map) HashSet(java.util.HashSet)

Example 2 with Attrs

use of aQute.bnd.header.Attrs in project felix by apache.

the class BundlePlugin method addLocalPackages.

private static void addLocalPackages(File outputDirectory, Analyzer analyzer) throws IOException {
    Packages packages = new Packages();
    if (outputDirectory != null && outputDirectory.isDirectory()) {
        // scan classes directory for potential packages
        DirectoryScanner scanner = new DirectoryScanner();
        scanner.setBasedir(outputDirectory);
        scanner.setIncludes(new String[] { "**/*.class" });
        scanner.addDefaultExcludes();
        scanner.scan();
        String[] paths = scanner.getIncludedFiles();
        for (int i = 0; i < paths.length; i++) {
            packages.put(analyzer.getPackageRef(getPackageName(paths[i])));
        }
    }
    Packages exportedPkgs = new Packages();
    Packages privatePkgs = new Packages();
    boolean noprivatePackages = "!*".equals(analyzer.getProperty(Analyzer.PRIVATE_PACKAGE));
    for (PackageRef pkg : packages.keySet()) {
        // mark all source packages as private by default (can be overridden by export list)
        privatePkgs.put(pkg);
        // we can't export the default package (".") and we shouldn't export internal packages
        String fqn = pkg.getFQN();
        if (noprivatePackages || !(".".equals(fqn) || fqn.contains(".internal") || fqn.contains(".impl"))) {
            exportedPkgs.put(pkg);
        }
    }
    Properties properties = analyzer.getProperties();
    String exported = properties.getProperty(Analyzer.EXPORT_PACKAGE);
    if (exported == null) {
        if (!properties.containsKey(Analyzer.EXPORT_CONTENTS)) {
            // no -exportcontents overriding the exports, so use our computed list
            for (Attrs attrs : exportedPkgs.values()) {
                attrs.put(Constants.SPLIT_PACKAGE_DIRECTIVE, "merge-first");
            }
            properties.setProperty(Analyzer.EXPORT_PACKAGE, Processor.printClauses(exportedPkgs));
        } else {
            // leave Export-Package empty (but non-null) as we have -exportcontents
            properties.setProperty(Analyzer.EXPORT_PACKAGE, "");
        }
    } else if (exported.indexOf(LOCAL_PACKAGES) >= 0) {
        String newExported = StringUtils.replace(exported, LOCAL_PACKAGES, Processor.printClauses(exportedPkgs));
        properties.setProperty(Analyzer.EXPORT_PACKAGE, newExported);
    }
    String internal = properties.getProperty(Analyzer.PRIVATE_PACKAGE);
    if (internal == null) {
        if (!privatePkgs.isEmpty()) {
            for (Attrs attrs : privatePkgs.values()) {
                attrs.put(Constants.SPLIT_PACKAGE_DIRECTIVE, "merge-first");
            }
            properties.setProperty(Analyzer.PRIVATE_PACKAGE, Processor.printClauses(privatePkgs));
        } else {
            // if there are really no private packages then use "!*" as this will keep the Bnd Tool happy
            properties.setProperty(Analyzer.PRIVATE_PACKAGE, "!*");
        }
    } else if (internal.indexOf(LOCAL_PACKAGES) >= 0) {
        String newInternal = StringUtils.replace(internal, LOCAL_PACKAGES, Processor.printClauses(privatePkgs));
        properties.setProperty(Analyzer.PRIVATE_PACKAGE, newInternal);
    }
}
Also used : Packages(aQute.bnd.osgi.Packages) DirectoryScanner(org.codehaus.plexus.util.DirectoryScanner) Attrs(aQute.bnd.header.Attrs) PackageRef(aQute.bnd.osgi.Descriptors.PackageRef) Properties(java.util.Properties)

Example 3 with Attrs

use of aQute.bnd.header.Attrs in project bnd by bndtools.

the class AnnotationHeadersTest method testWithAttrs.

public void testWithAttrs() throws Exception {
    Builder b = new Builder();
    try {
        b.addClasspath(new File("bin"));
        b.setProperty("Private-Package", "test.annotationheaders.attrs");
        b.build();
        assertTrue(b.check());
        Manifest m = b.getJar().getManifest();
        m.write(System.out);
        Parameters p = new Parameters(m.getMainAttributes().getValue("Provide-Capability"));
        Attrs attrs = p.get("nsx");
        assertNotNull(attrs);
        assertEquals(Long.valueOf(3), attrs.getTyped("foo"));
        p = new Parameters(m.getMainAttributes().getValue("Bundle-License"));
        attrs = p.get("license");
        assertNotNull(attrs);
        assertEquals("abc", attrs.get("foo"));
        p = new Parameters(m.getMainAttributes().getValue("Require-Capability"));
        // namespaces must be "osgi.ee", "nsx" and "nsy" ONLY
        assertNotNull(p.get("nsx"));
        assertNotNull(p.get("nsy"));
        assertNotNull(p.get("nsz"));
        assertNotNull(p.get("param"));
        assertNotNull(p.get("osgi.ee"));
        assertEquals("spurious Require-Capability namespaces", 5, p.size());
        attrs = p.get("nsx");
        assertEquals(Arrays.asList("abc", "def"), attrs.getTyped("foo"));
        attrs = p.get("nsy");
        assertEquals("hello", attrs.get("value"));
        attrs = p.get("nsz");
        assertEquals("(nsz=*)", attrs.get("filter:"));
        assertEquals("world", attrs.get("hello"));
        attrs = p.get("param");
        assertEquals("(&(a=hello)(b=goodbye))", attrs.get("filter:"));
    } finally {
        b.close();
    }
}
Also used : Parameters(aQute.bnd.header.Parameters) Builder(aQute.bnd.osgi.Builder) Attrs(aQute.bnd.header.Attrs) Manifest(java.util.jar.Manifest) File(java.io.File)

Example 4 with Attrs

use of aQute.bnd.header.Attrs in project bnd by bndtools.

the class VersionPolicyTest method testProviderTypeR6.

/**
	 * Test if the implementation of "AnnotatedProviderInterface", which is
	 * annotated with OSGi R6 @ProviderType, causes import of the api package to
	 * use the provider version policy
	 */
public static void testProviderTypeR6() throws Exception {
    Builder b = new Builder();
    b.addClasspath(new File("bin"));
    b.setPrivatePackage("test.versionpolicy.implemented.osgi");
    b.setProperty("build", "123");
    Jar jar = b.build();
    assertTrue(b.check());
    Manifest m = jar.getManifest();
    m.write(System.err);
    Domain d = Domain.domain(m);
    Parameters params = d.getImportPackage();
    Attrs attrs = params.get("test.version.annotations.osgi");
    assertNotNull(attrs);
    assertEquals("[1.2,1.3)", attrs.get("version"));
}
Also used : Parameters(aQute.bnd.header.Parameters) Builder(aQute.bnd.osgi.Builder) Attrs(aQute.bnd.header.Attrs) Jar(aQute.bnd.osgi.Jar) Manifest(java.util.jar.Manifest) Domain(aQute.bnd.osgi.Domain) File(java.io.File)

Example 5 with Attrs

use of aQute.bnd.header.Attrs in project bnd by bndtools.

the class VersionPolicyTest method testConsumerType.

/**
	 * Tests if the implementation of the EventHandler (which is marked as a
	 * ConsumerType) causes the import of the api package to use the consumer
	 * version policy.
	 */
public static void testConsumerType() throws Exception {
    Builder a = new Builder();
    a.addClasspath(new File("bin"));
    a.setPrivatePackage("test.versionpolicy.uses");
    a.setExportPackage("test.versionpolicy.api");
    a.setProperty("build", "123");
    Jar jar = a.build();
    assertTrue(a.check());
    Manifest m = jar.getManifest();
    m.write(System.err);
    Domain d = Domain.domain(m);
    Parameters parameters = d.getImportPackage();
    Attrs attrs = parameters.get("test.versionpolicy.api");
    assertNotNull(attrs);
    assertEquals("[1.2,2)", attrs.get("version"));
}
Also used : Parameters(aQute.bnd.header.Parameters) Builder(aQute.bnd.osgi.Builder) Attrs(aQute.bnd.header.Attrs) Jar(aQute.bnd.osgi.Jar) Manifest(java.util.jar.Manifest) Domain(aQute.bnd.osgi.Domain) File(java.io.File)

Aggregations

Attrs (aQute.bnd.header.Attrs)180 Parameters (aQute.bnd.header.Parameters)80 File (java.io.File)46 Manifest (java.util.jar.Manifest)27 Jar (aQute.bnd.osgi.Jar)25 Map (java.util.Map)25 Builder (aQute.bnd.osgi.Builder)21 ArrayList (java.util.ArrayList)21 Domain (aQute.bnd.osgi.Domain)19 Version (aQute.bnd.version.Version)19 HashMap (java.util.HashMap)19 PackageRef (aQute.bnd.osgi.Descriptors.PackageRef)18 VersionedClause (aQute.bnd.build.model.clauses.VersionedClause)16 IOException (java.io.IOException)15 Entry (java.util.Map.Entry)11 Matcher (java.util.regex.Matcher)11 FileNotFoundException (java.io.FileNotFoundException)10 HashSet (java.util.HashSet)10 Attributes (java.util.jar.Attributes)10 MultiMap (aQute.lib.collections.MultiMap)9