Search in sources :

Example 6 with Resource

use of aQute.bnd.osgi.Resource in project felix by apache.

the class PojoizationPluginTestCase method testAnalysisWithComponentOnlyMetadataXml.

public void testAnalysisWithComponentOnlyMetadataXml() throws Exception {
    PojoizationPlugin plugin = new PojoizationPlugin();
    Map<String, String> props = new HashMap<String, String>();
    Resource resource = new URLResource(getClass().getResource("/metadata-components-only.xml"));
    doReturn(jar).when(analyzer).getJar();
    doReturn(resource).when(jar).getResource(eq("META-INF/metadata.xml"));
    plugin.setReporter(reporter);
    plugin.setProperties(props);
    plugin.analyzeJar(analyzer);
    assertEquals("component { $class=\"com.acme.Thermometer\" }", analyzer.getProperty("IPOJO-Components"));
}
Also used : URLResource(aQute.bnd.osgi.URLResource) HashMap(java.util.HashMap) Resource(aQute.bnd.osgi.Resource) URLResource(aQute.bnd.osgi.URLResource)

Example 7 with Resource

use of aQute.bnd.osgi.Resource in project felix by apache.

the class PojoizationPluginTestCase method testAnalysisWithInstanceOnlyMetadataXml.

public void testAnalysisWithInstanceOnlyMetadataXml() throws Exception {
    PojoizationPlugin plugin = new PojoizationPlugin();
    Map<String, String> props = new HashMap<String, String>();
    Resource resource = new URLResource(getClass().getResource("/metadata-instances-only.xml"));
    doReturn(jar).when(analyzer).getJar();
    doReturn(resource).when(jar).getResource(eq("META-INF/metadata.xml"));
    plugin.setReporter(reporter);
    plugin.setProperties(props);
    plugin.analyzeJar(analyzer);
    assertEquals("instance { $component=\"com.acme.Thermometer\" }", analyzer.getProperty("IPOJO-Components"));
}
Also used : URLResource(aQute.bnd.osgi.URLResource) HashMap(java.util.HashMap) Resource(aQute.bnd.osgi.Resource) URLResource(aQute.bnd.osgi.URLResource)

Example 8 with Resource

use of aQute.bnd.osgi.Resource 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 9 with Resource

use of aQute.bnd.osgi.Resource in project felix by apache.

the class ManifestPlugin method exportScr.

private static void exportScr(Analyzer analyzer, Jar jar, File scrLocation, BuildContext buildContext, Log log) throws Exception {
    log.debug("Export SCR metadata to: " + scrLocation.getPath());
    scrLocation.mkdirs();
    // export SCR metadata files from OSGI-INF/
    Map<String, Resource> scrDir = jar.getDirectories().get("OSGI-INF");
    if (scrDir != null) {
        for (Map.Entry<String, Resource> entry : scrDir.entrySet()) {
            String path = entry.getKey();
            Resource resource = entry.getValue();
            writeSCR(resource, new File(scrLocation, path), buildContext, log);
        }
    }
    // export metatype files from OSGI-INF/metatype
    Map<String, Resource> metatypeDir = jar.getDirectories().get(MetaTypeService.METATYPE_DOCUMENTS_LOCATION);
    if (metatypeDir != null) {
        for (Map.Entry<String, Resource> entry : metatypeDir.entrySet()) {
            String path = entry.getKey();
            Resource resource = entry.getValue();
            writeSCR(resource, new File(scrLocation, path), buildContext, log);
        }
    }
}
Also used : Resource(aQute.bnd.osgi.Resource) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) File(java.io.File)

Example 10 with Resource

use of aQute.bnd.osgi.Resource in project felix by apache.

the class JpaPluginTest method assertTransformation.

private void assertTransformation(final String xmlStr, String expectedReqs) throws Exception {
    Analyzer analyzer = new Analyzer();
    Jar jar = new Jar("the-jar");
    Resource xml = new AbstractResource(0) {

        @Override
        protected byte[] getBytes() throws Exception {
            return xmlStr.getBytes();
        }
    };
    JpaPlugin plugin = new JpaPlugin();
    jar.putResource("the-persistence-xml", xml);
    analyzer.setJar(jar);
    analyzer.setProperty("Meta-Persistence", "the-persistence-xml");
    plugin.analyzeJar(analyzer);
    assertEquals(expectedReqs, analyzer.getProperty("Require-Capability"));
}
Also used : Resource(aQute.bnd.osgi.Resource) AbstractResource(aQute.bnd.osgi.AbstractResource) Jar(aQute.bnd.osgi.Jar) AbstractResource(aQute.bnd.osgi.AbstractResource) Analyzer(aQute.bnd.osgi.Analyzer)

Aggregations

Resource (aQute.bnd.osgi.Resource)147 Jar (aQute.bnd.osgi.Jar)87 Builder (aQute.bnd.osgi.Builder)83 File (java.io.File)76 XmlTester (aQute.bnd.test.XmlTester)48 JarResource (aQute.bnd.osgi.JarResource)20 Attributes (java.util.jar.Attributes)20 Map (java.util.Map)19 Manifest (java.util.jar.Manifest)19 FileResource (aQute.bnd.osgi.FileResource)17 LogService (org.osgi.service.log.LogService)15 HashMap (java.util.HashMap)14 Document (org.w3c.dom.Document)14 Properties (java.util.Properties)12 EmbeddedResource (aQute.bnd.osgi.EmbeddedResource)11 DocumentBuilder (javax.xml.parsers.DocumentBuilder)11 InputStream (java.io.InputStream)9 Attrs (aQute.bnd.header.Attrs)8 ArrayList (java.util.ArrayList)8 TreeMap (java.util.TreeMap)8