Search in sources :

Example 1 with ResourceAnalyzer

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);
}
Also used : ResourceAnalyzer(org.osgi.service.indexer.ResourceAnalyzer) HashMap(java.util.HashMap) KnownBundleAnalyzer(org.osgi.service.indexer.impl.KnownBundleAnalyzer) File(java.io.File) RepoIndex(org.osgi.service.indexer.impl.RepoIndex)

Example 2 with ResourceAnalyzer

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();
}
Also used : WibbleAnalyzer(org.example.tests.utils.WibbleAnalyzer) ResourceAnalyzer(org.osgi.service.indexer.ResourceAnalyzer) StringWriter(java.io.StringWriter) HashMap(java.util.HashMap) Matchers.anyString(org.mockito.Matchers.anyString) ResourceIndexer(org.osgi.service.indexer.ResourceIndexer)

Example 3 with ResourceAnalyzer

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();
}
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 4 with ResourceAnalyzer

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;
}
Also used : ResourceAnalyzer(org.osgi.service.indexer.ResourceAnalyzer) Filter(org.osgi.framework.Filter) FrameworkUtil.createFilter(org.osgi.framework.FrameworkUtil.createFilter)

Example 5 with ResourceAnalyzer

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;
}
Also used : ResourceAnalyzer(org.osgi.service.indexer.ResourceAnalyzer) Filter(org.osgi.framework.Filter) InvalidSyntaxException(org.osgi.framework.InvalidSyntaxException) TrackingStruct(org.osgi.service.indexer.osgi.AnalyzerTracker.TrackingStruct)

Aggregations

ResourceAnalyzer (org.osgi.service.indexer.ResourceAnalyzer)9 File (java.io.File)6 HashMap (java.util.HashMap)6 StringWriter (java.io.StringWriter)5 Matchers.anyString (org.mockito.Matchers.anyString)4 Filter (org.osgi.framework.Filter)3 Capability (org.osgi.service.indexer.Capability)3 Requirement (org.osgi.service.indexer.Requirement)3 Resource (org.osgi.service.indexer.Resource)3 ResourceIndexer (org.osgi.service.indexer.ResourceIndexer)3 List (java.util.List)2 Utils.copyToTempFile (org.example.tests.utils.Utils.copyToTempFile)2 WibbleAnalyzer (org.example.tests.utils.WibbleAnalyzer)2 FrameworkUtil.createFilter (org.osgi.framework.FrameworkUtil.createFilter)2 InvalidSyntaxException (org.osgi.framework.InvalidSyntaxException)2 RepoIndex (org.osgi.service.indexer.impl.RepoIndex)2 LogService (org.osgi.service.log.LogService)2 Project (aQute.bnd.build.Project)1 WorkspaceR5Repository (bndtools.central.WorkspaceR5Repository)1 IOException (java.io.IOException)1