Search in sources :

Example 1 with Resource

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

the class TestOSGiServices method testLogNotification.

// Test that exceptions thrown by analyzers are forwarded to the OSGi Log
// Service
public void testLogNotification() throws Exception {
    // Register mock LogService, to receive notifications
    LogService mockLog = mock(LogService.class);
    ServiceRegistration<LogService> mockLogReg = context.registerService(LogService.class, mockLog, null);
    // Register a broken analyzer that throws exceptions
    ResourceAnalyzer brokenAnalyzer = new ResourceAnalyzer() {

        public void analyzeResource(Resource resource, List<Capability> capabilities, List<Requirement> requirements) throws Exception {
            throw new Exception("Bang!");
        }
    };
    ServiceRegistration<ResourceAnalyzer> mockAnalyzerReg = context.registerService(ResourceAnalyzer.class, brokenAnalyzer, null);
    // Call the indexer
    ServiceReference<ResourceIndexer> ref = context.getServiceReference(ResourceIndexer.class);
    ResourceIndexer indexer = context.getService(ref);
    StringWriter writer = new StringWriter();
    Set<File> files = Collections.singleton(copyToTempFile(tempDir, "testdata/01-bsn+version.jar"));
    Map<String, String> config = new HashMap<String, String>();
    config.put(ResourceIndexer.ROOT_URL, tempDir.getAbsoluteFile().toURI().toString());
    indexer.indexFragment(files, writer, config);
    // Verify log output
    ArgumentCaptor<Exception> exceptionCaptor = ArgumentCaptor.forClass(Exception.class);
    verify(mockLog).log(any(ServiceReference.class), eq(LogService.LOG_ERROR), anyString(), exceptionCaptor.capture());
    assertEquals("Bang!", exceptionCaptor.getValue().getMessage());
    mockAnalyzerReg.unregister();
    mockLogReg.unregister();
}
Also used : ResourceAnalyzer(org.osgi.service.indexer.ResourceAnalyzer) HashMap(java.util.HashMap) Resource(org.osgi.service.indexer.Resource) Matchers.anyString(org.mockito.Matchers.anyString) Utils.deleteWithException(org.example.tests.utils.Utils.deleteWithException) ResourceIndexer(org.osgi.service.indexer.ResourceIndexer) ServiceReference(org.osgi.framework.ServiceReference) StringWriter(java.io.StringWriter) List(java.util.List) Utils.copyToTempFile(org.example.tests.utils.Utils.copyToTempFile) File(java.io.File) LogService(org.osgi.service.log.LogService)

Example 2 with Resource

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

the class TestJarResource method testJarFileContent.

public void testJarFileContent() throws Exception {
    JarResource resource = new JarResource(new File("testdata/01-bsn+version.jar"));
    Resource pkgInfoResource = resource.getChild("org/example/a/packageinfo");
    assertEquals("version 1.0", Utils.readStream(pkgInfoResource.getStream()));
}
Also used : Resource(org.osgi.service.indexer.Resource) File(java.io.File)

Example 3 with Resource

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

the class OSGiFrameworkAnalyzer method analyzeResource.

public void analyzeResource(Resource resource, List<Capability> caps, List<Requirement> reqs) throws Exception {
    Resource fwkFactorySvc = resource.getChild(SERVICE_FRAMEWORK_FACTORY);
    if (fwkFactorySvc != null) {
        Builder builder = new Builder().setNamespace(Namespaces.NS_CONTRACT).addAttribute(Namespaces.NS_CONTRACT, Namespaces.CONTRACT_OSGI_FRAMEWORK);
        Version specVersion = null;
        StringBuilder uses = new StringBuilder();
        boolean firstPkg = true;
        for (Capability cap : caps) {
            if (Namespaces.NS_WIRING_PACKAGE.equals(cap.getNamespace())) {
                // Add to the uses directive
                if (!firstPkg)
                    uses.append(',');
                String pkgName = (String) cap.getAttributes().get(Namespaces.NS_WIRING_PACKAGE);
                uses.append(pkgName);
                firstPkg = false;
                // map to OSGi spec version
                if (FRAMEWORK_PACKAGE.equals(pkgName)) {
                    Version frameworkPkgVersion = (Version) cap.getAttributes().get(Namespaces.ATTR_VERSION);
                    specVersion = mapFrameworkPackageVersion(frameworkPkgVersion);
                }
            }
        }
        if (specVersion != null)
            builder.addAttribute(Namespaces.ATTR_VERSION, specVersion);
        builder.addDirective(Namespaces.DIRECTIVE_USES, uses.toString());
        caps.add(builder.buildCapability());
    }
}
Also used : Capability(org.osgi.service.indexer.Capability) Version(org.osgi.framework.Version) Builder(org.osgi.service.indexer.Builder) Resource(org.osgi.service.indexer.Resource)

Example 4 with Resource

use of org.osgi.service.indexer.Resource 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 5 with Resource

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

the class SCRAnalyzer method processScrXml.

private Version processScrXml(Resource resource, String path, List<Capability> caps, List<Requirement> reqs) throws IOException {
    Resource childResource = resource.getChild(path);
    if (childResource == null) {
        if (log != null)
            log.log(LogService.LOG_WARNING, MessageFormat.format("Cannot analyse SCR requirement version: resource {0} does not contain path {1} referred from Service-Component header.", resource.getLocation(), path));
        return null;
    }
    SAXParserFactory spf = SAXParserFactory.newInstance();
    spf.setNamespaceAware(true);
    try {
        SAXParser parser = spf.newSAXParser();
        SCRContentHandler handler = new SCRContentHandler(caps, reqs);
        parser.parse(childResource.getStream(), handler);
        return handler.highest;
    } catch (Exception e) {
        if (log != null)
            log.log(LogService.LOG_ERROR, MessageFormat.format("Processing error: failed to parse child resource {0} in resource {1}.", path, resource.getLocation()), e);
        return null;
    }
}
Also used : Resource(org.osgi.service.indexer.Resource) SAXParser(javax.xml.parsers.SAXParser) IOException(java.io.IOException) SAXException(org.xml.sax.SAXException) SAXParserFactory(javax.xml.parsers.SAXParserFactory)

Aggregations

Resource (org.osgi.service.indexer.Resource)6 File (java.io.File)4 HashMap (java.util.HashMap)3 Capability (org.osgi.service.indexer.Capability)3 ResourceAnalyzer (org.osgi.service.indexer.ResourceAnalyzer)3 IOException (java.io.IOException)2 StringWriter (java.io.StringWriter)2 List (java.util.List)2 Matchers.anyString (org.mockito.Matchers.anyString)2 Builder (org.osgi.service.indexer.Builder)2 Requirement (org.osgi.service.indexer.Requirement)2 LogService (org.osgi.service.log.LogService)2 Project (aQute.bnd.build.Project)1 WorkspaceR5Repository (bndtools.central.WorkspaceR5Repository)1 InputStream (java.io.InputStream)1 OutputStream (java.io.OutputStream)1 URI (java.net.URI)1 HashSet (java.util.HashSet)1 SAXParser (javax.xml.parsers.SAXParser)1 SAXParserFactory (javax.xml.parsers.SAXParserFactory)1