use of org.osgi.service.indexer.ResourceAnalyzer in project bnd by bndtools.
the class R5RepoContentProvider method generateIndex.
public void generateIndex(Set<File> files, OutputStream output, String repoName, URI baseUri, boolean pretty, Registry registry, LogService log) throws Exception {
RepoIndex indexer;
if (log != null)
indexer = new RepoIndex(log);
else
indexer = new RepoIndex();
indexer.addAnalyzer(new KnownBundleAnalyzer(), FrameworkUtil.createFilter("(name=*)"));
if (registry != null) {
List<ResourceAnalyzer> analyzers = registry.getPlugins(ResourceAnalyzer.class);
for (ResourceAnalyzer analyzer : analyzers) {
// TODO: where to get the filter property??
indexer.addAnalyzer(analyzer, null);
}
}
long modified = 0;
for (File file : files) modified = Math.max(modified, file.lastModified());
Map<String, String> config = new HashMap<String, String>();
config.put(ResourceIndexer.REPOSITORY_NAME, repoName);
config.put(ResourceIndexer.ROOT_URL, baseUri.toString());
config.put(ResourceIndexer.PRETTY, Boolean.toString(pretty));
config.put(ResourceIndexer.COMPRESSED, Boolean.toString(!pretty));
config.put(org.osgi.service.indexer.impl.RepoIndex.REPOSITORY_INCREMENT_OVERRIDE, Long.toString(modified));
indexer.index(files, output, config);
}
use of org.osgi.service.indexer.ResourceAnalyzer in project bnd by bndtools.
the class TestOSGiServices method testWhiteboardAnalyzer.
// Test whiteboard registration of Resource Analyzers.
public void testWhiteboardAnalyzer() throws Exception {
ServiceRegistration<ResourceAnalyzer> reg = context.registerService(ResourceAnalyzer.class, new WibbleAnalyzer(), null);
ServiceReference<ResourceIndexer> ref = context.getServiceReference(ResourceIndexer.class);
ResourceIndexer indexer = context.getService(ref);
StringWriter writer = new StringWriter();
Map<String, String> config = new HashMap<String, String>();
config.put(ResourceIndexer.ROOT_URL, tempDir.getAbsoluteFile().toURI().toString());
indexer.indexFragment(Collections.singleton(copyToTempFile(tempDir, "testdata/01-bsn+version.jar")), writer, config);
assertEquals(readStream(TestOSGiServices.class.getResourceAsStream("/testdata/fragment-wibble.txt")), writer.toString().trim());
context.ungetService(ref);
reg.unregister();
}
use of org.osgi.service.indexer.ResourceAnalyzer 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.ResourceAnalyzer in project bnd by bndtools.
the class RepoIndex method indexFile.
/*
* Index a file and return a resource for it.
*/
public IndexResult indexFile(File file) throws Exception {
IndexResult result = new IndexResult();
result.resource = new JarResource(file);
result.signature = getSignature();
synchronized (analyzers) {
for (Pair<ResourceAnalyzer, Filter> entry : analyzers) {
ResourceAnalyzer analyzer = entry.getFirst();
Filter filter = entry.getSecond();
if (filter == null || filter.match(result.resource.getProperties())) {
analyzer.analyzeResource(result.resource, result.capabilities, result.requirements);
}
}
}
return result;
}
use of org.osgi.service.indexer.ResourceAnalyzer in project bnd by bndtools.
the class AnalyzerTracker method addingService.
@Override
public TrackingStruct addingService(ServiceReference<ResourceAnalyzer> reference) {
TrackingStruct struct = new TrackingStruct();
try {
String filterStr = (String) reference.getProperty(ResourceAnalyzer.FILTER);
Filter filter = (filterStr != null) ? FrameworkUtil.createFilter(filterStr) : null;
ResourceAnalyzer analyzer = context.getService(reference);
if (analyzer == null)
return null;
struct = new TrackingStruct();
struct.analyzer = analyzer;
struct.filter = filter;
struct.valid = true;
indexer.addAnalyzer(analyzer, filter);
} catch (InvalidSyntaxException e) {
struct.valid = false;
log.log(reference, LogService.LOG_ERROR, "Ignoring ResourceAnalyzer due to invalid filter expression", e);
}
return struct;
}
Aggregations