Search in sources :

Example 1 with Requirement

use of org.osgi.service.indexer.Requirement in project bnd by bndtools.

the class BundleAnalyzer method doBREE.

private void doBREE(Resource resource, List<? super Requirement> reqs) throws Exception {
    @SuppressWarnings("deprecation") String breeStr = resource.getManifest().getMainAttributes().getValue(Constants.BUNDLE_REQUIREDEXECUTIONENVIRONMENT);
    Map<String, Map<String, String>> brees = OSGiHeader.parseHeader(breeStr);
    final String filter;
    if (!brees.isEmpty()) {
        if (brees.size() == 1) {
            String bree = brees.keySet().iterator().next();
            filter = EE.parseBREE(bree).toFilter();
        } else {
            StringBuilder builder = new StringBuilder().append("(|");
            for (String bree : brees.keySet()) {
                bree = OSGiHeader.removeDuplicateMarker(bree);
                builder.append(EE.parseBREE(bree).toFilter());
            }
            builder.append(')');
            filter = builder.toString();
        }
        Requirement requirement = new Builder().setNamespace(Namespaces.NS_EE).addDirective(Namespaces.DIRECTIVE_FILTER, filter).buildRequirement();
        reqs.add(requirement);
    }
}
Also used : Requirement(org.osgi.service.indexer.Requirement) Builder(org.osgi.service.indexer.Builder) Map(java.util.Map)

Example 2 with Requirement

use of org.osgi.service.indexer.Requirement in project bnd by bndtools.

the class RepoIndex method generateResource.

private Tag generateResource(File file, Map<String, String> config) throws Exception {
    JarResource resource = new JarResource(file);
    List<Capability> caps = new AddOnlyList<Capability>(new LinkedList<Capability>());
    List<Requirement> reqs = new AddOnlyList<Requirement>(new LinkedList<Requirement>());
    Tag resourceTag = new Tag(Schema.ELEM_RESOURCE);
    try {
        // Read config settings and save in thread local state
        if (config != null) {
            URL rootURL;
            String rootURLStr = config.get(ResourceIndexer.ROOT_URL);
            if (rootURLStr != null) {
                File rootDir = new File(rootURLStr);
                if (rootDir.isDirectory())
                    rootURL = rootDir.toURI().toURL();
                else
                    rootURL = new URL(rootURLStr);
            } else
                rootURL = new File(System.getProperty("user.dir")).toURI().toURL();
            String urlTemplate = config.get(ResourceIndexer.URL_TEMPLATE);
            bundleAnalyzer.setStateLocal(new GeneratorState(rootURL.toURI().normalize(), urlTemplate, resolvers));
        } else {
            bundleAnalyzer.setStateLocal(null);
        }
        // Iterate over the analyzers
        try {
            synchronized (analyzers) {
                for (Pair<ResourceAnalyzer, Filter> entry : analyzers) {
                    ResourceAnalyzer analyzer = entry.getFirst();
                    Filter filter = entry.getSecond();
                    if (filter == null || filter.match(resource.getProperties())) {
                        try {
                            analyzer.analyzeResource(resource, caps, reqs);
                        } catch (Exception e) {
                            log(LogService.LOG_ERROR, MessageFormat.format("Error calling analyzer \"{0}\" on resource {1}.", analyzer.getClass().getName(), resource.getLocation()), e);
                            StringWriter writer = new StringWriter();
                            Formatter comment = new Formatter(writer);
                            comment.format("Error calling analyzer \"%s\" on resource %s with message %s and stack: ", analyzer.getClass().getName(), resource.getLocation(), e);
                            comment.close();
                            e.printStackTrace(new PrintWriter(writer));
                            resourceTag.addComment(writer.toString());
                        }
                    }
                }
            }
        } finally {
            bundleAnalyzer.setStateLocal(null);
        }
    } finally {
        resource.close();
    }
    for (Capability cap : caps) {
        Tag capTag = new Tag(Schema.ELEM_CAPABILITY);
        capTag.addAttribute(Schema.ATTR_NAMESPACE, cap.getNamespace());
        appendAttributeAndDirectiveTags(capTag, cap.getAttributes(), cap.getDirectives());
        resourceTag.addContent(capTag);
    }
    for (Requirement req : reqs) {
        Tag reqTag = new Tag(Schema.ELEM_REQUIREMENT);
        reqTag.addAttribute(Schema.ATTR_NAMESPACE, req.getNamespace());
        appendAttributeAndDirectiveTags(reqTag, req.getAttributes(), req.getDirectives());
        resourceTag.addContent(reqTag);
    }
    return resourceTag;
}
Also used : ResourceAnalyzer(org.osgi.service.indexer.ResourceAnalyzer) Capability(org.osgi.service.indexer.Capability) Formatter(java.util.Formatter) URL(java.net.URL) InvalidSyntaxException(org.osgi.framework.InvalidSyntaxException) Requirement(org.osgi.service.indexer.Requirement) AddOnlyList(org.osgi.service.indexer.impl.util.AddOnlyList) StringWriter(java.io.StringWriter) Filter(org.osgi.framework.Filter) FrameworkUtil.createFilter(org.osgi.framework.FrameworkUtil.createFilter) Tag(org.osgi.service.indexer.impl.util.Tag) File(java.io.File) PrintWriter(java.io.PrintWriter)

Example 3 with Requirement

use of org.osgi.service.indexer.Requirement in project bnd by bndtools.

the class TestOSGiFrameworkAnalyzer method testOsgiFrameworkSpecificationVersions.

public void testOsgiFrameworkSpecificationVersions() throws Exception {
    LinkedList<Capability> caps;
    LinkedList<Requirement> reqs;
    caps = new LinkedList<Capability>();
    reqs = new LinkedList<Requirement>();
    JarResource inputResource = new JarResource(new File("testdata/org.apache.felix.framework-4.0.2.jar"));
    new BundleAnalyzer(new XNullLogSvc()).analyzeResource(inputResource, caps, reqs);
    new OSGiFrameworkAnalyzer(new XNullLogSvc()).analyzeResource(inputResource, caps, reqs);
    assertEquals(new Version("4.3.0"), findCaps("osgi.contract", caps).get(0).getAttributes().get("version"));
    caps = new LinkedList<Capability>();
    reqs = new LinkedList<Requirement>();
    inputResource = new JarResource(new File("testdata/org.eclipse.osgi_3.7.2.v20120110-1415.jar"));
    new BundleAnalyzer(new XNullLogSvc()).analyzeResource(inputResource, caps, reqs);
    new OSGiFrameworkAnalyzer(new XNullLogSvc()).analyzeResource(inputResource, caps, reqs);
    assertEquals(new Version("4.3.0"), findCaps("osgi.contract", caps).get(0).getAttributes().get("version"));
    caps = new LinkedList<Capability>();
    reqs = new LinkedList<Requirement>();
    inputResource = new JarResource(new File("testdata/org.apache.felix.framework-3.2.2.jar"));
    new BundleAnalyzer(new XNullLogSvc()).analyzeResource(inputResource, caps, reqs);
    new OSGiFrameworkAnalyzer(new XNullLogSvc()).analyzeResource(inputResource, caps, reqs);
    assertEquals(new Version("4.2.0"), findCaps("osgi.contract", caps).get(0).getAttributes().get("version"));
    caps = new LinkedList<Capability>();
    reqs = new LinkedList<Requirement>();
    inputResource = new JarResource(new File("testdata/org.eclipse.osgi_3.6.2.R36x_v20110210.jar"));
    new BundleAnalyzer(new XNullLogSvc()).analyzeResource(inputResource, caps, reqs);
    new OSGiFrameworkAnalyzer(new XNullLogSvc()).analyzeResource(inputResource, caps, reqs);
    assertEquals(new Version("4.2.0"), findCaps("osgi.contract", caps).get(0).getAttributes().get("version"));
}
Also used : Requirement(org.osgi.service.indexer.Requirement) Capability(org.osgi.service.indexer.Capability) Version(org.osgi.framework.Version) File(java.io.File)

Example 4 with Requirement

use of org.osgi.service.indexer.Requirement in project bnd by bndtools.

the class TestOSGiFrameworkAnalyzer method testOsgiFramework.

public void testOsgiFramework() throws Exception {
    LinkedList<Capability> caps = new LinkedList<Capability>();
    LinkedList<Requirement> reqs = new LinkedList<Requirement>();
    JarResource input = new JarResource(new File("testdata/org.apache.felix.framework-4.0.2.jar"));
    new BundleAnalyzer(new XNullLogSvc()).analyzeResource(input, caps, reqs);
    new OSGiFrameworkAnalyzer(new XNullLogSvc()).analyzeResource(input, caps, reqs);
    List<Capability> fwkCaps = findCaps("osgi.contract", caps);
    assertNotNull(fwkCaps);
    assertEquals(1, fwkCaps.size());
    Capability fwkCap = fwkCaps.get(0);
    assertEquals("OSGiFramework", fwkCap.getAttributes().get("osgi.contract"));
    assertEquals(new Version("4.3.0"), fwkCap.getAttributes().get("version"));
    assertEquals("org.osgi.framework.startlevel,org.osgi.framework.wiring,org.osgi.framework.hooks.bundle,org.osgi.framework.hooks.service,org.osgi.framework.hooks.resolver,org.osgi.framework.launch,org.osgi.framework,org.osgi.framework.hooks.weaving,org.osgi.service.packageadmin,org.osgi.service.url,org.osgi.service.startlevel,org.osgi.util.tracker", fwkCap.getDirectives().get("uses"));
}
Also used : Requirement(org.osgi.service.indexer.Requirement) Capability(org.osgi.service.indexer.Capability) Version(org.osgi.framework.Version) File(java.io.File) LinkedList(java.util.LinkedList)

Example 5 with Requirement

use of org.osgi.service.indexer.Requirement in project bnd by bndtools.

the class SCRAnalyzer method createRequirement.

private static Requirement createRequirement(VersionRange range) {
    Builder builder = new Builder().setNamespace(Namespaces.NS_EXTENDER);
    StringBuilder filter = new StringBuilder();
    filter.append('(').append(Namespaces.NS_EXTENDER).append('=').append(Namespaces.EXTENDER_SCR).append(')');
    filter.insert(0, "(&");
    Util.addVersionFilter(filter, range, VersionKey.PackageVersion);
    filter.append(')');
    builder.addDirective(Namespaces.DIRECTIVE_FILTER, filter.toString()).addDirective(Namespaces.DIRECTIVE_EFFECTIVE, Namespaces.EFFECTIVE_ACTIVE);
    Requirement requirement = builder.buildRequirement();
    return requirement;
}
Also used : Requirement(org.osgi.service.indexer.Requirement) Builder(org.osgi.service.indexer.Builder)

Aggregations

Requirement (org.osgi.service.indexer.Requirement)13 File (java.io.File)10 Capability (org.osgi.service.indexer.Capability)10 LinkedList (java.util.LinkedList)7 Version (org.osgi.framework.Version)6 StringWriter (java.io.StringWriter)2 Builder (org.osgi.service.indexer.Builder)2 ResourceAnalyzer (org.osgi.service.indexer.ResourceAnalyzer)2 IOException (java.io.IOException)1 PrintWriter (java.io.PrintWriter)1 URL (java.net.URL)1 Formatter (java.util.Formatter)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Map (java.util.Map)1 StringTokenizer (java.util.StringTokenizer)1 Manifest (java.util.jar.Manifest)1 Matchers.anyString (org.mockito.Matchers.anyString)1 Filter (org.osgi.framework.Filter)1 FrameworkUtil.createFilter (org.osgi.framework.FrameworkUtil.createFilter)1