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();
}
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()));
}
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());
}
}
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());
}
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;
}
}
Aggregations