Search in sources :

Example 11 with Requirement

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

the class TestBundleAnalyzer method testPackageImports.

public void testPackageImports() throws Exception {
    BundleAnalyzer a = new BundleAnalyzer(new XNullLogSvc());
    LinkedList<Capability> caps = new LinkedList<Capability>();
    LinkedList<Requirement> reqs = new LinkedList<Requirement>();
    a.analyzeResource(new JarResource(new File("testdata/05-import.jar")), caps, reqs);
    Requirement pkgImport = findReqs("osgi.wiring.package", reqs).get(0);
    assertEquals("(&(osgi.wiring.package=org.example.a)(version>=1.0.0)(!(version>=2.0.0)))", pkgImport.getDirectives().get("filter"));
}
Also used : Requirement(org.osgi.service.indexer.Requirement) Capability(org.osgi.service.indexer.Capability) File(java.io.File) LinkedList(java.util.LinkedList)

Example 12 with Requirement

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

the class TestIndexer method testLogErrorsFromAnalyzer.

public void testLogErrorsFromAnalyzer() throws Exception {
    ResourceAnalyzer badAnalyzer = new ResourceAnalyzer() {

        public void analyzeResource(Resource resource, List<Capability> capabilities, List<Requirement> requirements) throws Exception {
            throw new Exception("Bang!");
        }
    };
    ResourceAnalyzer goodAnalyzer = mock(ResourceAnalyzer.class);
    LogService log = mock(LogService.class);
    RepoIndex indexer = new RepoIndex(log);
    indexer.addAnalyzer(badAnalyzer, null);
    indexer.addAnalyzer(goodAnalyzer, null);
    // Run the indexer
    Map<String, String> props = new HashMap<String, String>();
    props.put(ResourceIndexer.ROOT_URL, new File("testdata").getAbsoluteFile().toURI().toURL().toString());
    StringWriter writer = new StringWriter();
    indexer.indexFragment(Collections.singleton(new File("testdata/subdir/01-bsn+version.jar")), writer, props);
    // The "good" analyzer should have been called
    verify(goodAnalyzer).analyzeResource(any(Resource.class), anyListOf(Capability.class), anyListOf(Requirement.class));
    // The log service should have been notified about the exception
    ArgumentCaptor<Exception> exceptionCaptor = ArgumentCaptor.forClass(Exception.class);
    verify(log).log(eq(LogService.LOG_ERROR), any(String.class), exceptionCaptor.capture());
    assertEquals("Bang!", exceptionCaptor.getValue().getMessage());
}
Also used : ResourceAnalyzer(org.osgi.service.indexer.ResourceAnalyzer) Capability(org.osgi.service.indexer.Capability) HashMap(java.util.HashMap) Resource(org.osgi.service.indexer.Resource) Matchers.anyString(org.mockito.Matchers.anyString) IOException(java.io.IOException) Requirement(org.osgi.service.indexer.Requirement) StringWriter(java.io.StringWriter) List(java.util.List) File(java.io.File) LogService(org.osgi.service.log.LogService)

Example 13 with Requirement

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

the class SCRAnalyzer method analyzeResource.

public void analyzeResource(Resource resource, List<Capability> caps, List<Requirement> reqs) throws Exception {
    String header = null;
    Manifest manifest = resource.getManifest();
    if (manifest != null)
        header = manifest.getMainAttributes().getValue(ComponentConstants.SERVICE_COMPONENT);
    if (header == null)
        return;
    StringTokenizer tokenizer = new StringTokenizer(header, ",");
    Version highest = null;
    while (tokenizer.hasMoreTokens()) {
        String pattern = tokenizer.nextToken().trim();
        List<String> paths = Util.findMatchingPaths(resource, pattern);
        if (paths != null)
            for (String path : paths) {
                Version version = processScrXml(resource, path, caps, reqs);
                if (version == null)
                    continue;
                if (highest == null || (version.compareTo(highest) > 0))
                    highest = version;
            }
    }
    if (highest != null) {
        Version lower = new Version(highest.getMajor(), highest.getMinor(), 0);
        Version upper = new Version(highest.getMajor() + 1, 0, 0);
        Requirement requirement = createRequirement(new VersionRange(true, lower, upper, false));
        reqs.add(requirement);
    }
}
Also used : Requirement(org.osgi.service.indexer.Requirement) StringTokenizer(java.util.StringTokenizer) Version(org.osgi.framework.Version) VersionRange(org.osgi.service.indexer.impl.types.VersionRange) Manifest(java.util.jar.Manifest)

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