Search in sources :

Example 1 with AddOnlyList

use of org.osgi.service.indexer.impl.util.AddOnlyList in project bnd by bndtools.

the class RepoIndex method generateResource.

private Tag generateResource(File file, Map<String, String> config) throws Exception {
    JarResource resource = new JarResource(file);
    List<Capability> caps = new AddOnlyList<Capability>(new LinkedList<Capability>());
    List<Requirement> reqs = new AddOnlyList<Requirement>(new LinkedList<Requirement>());
    Tag resourceTag = new Tag(Schema.ELEM_RESOURCE);
    try {
        // Read config settings and save in thread local state
        if (config != null) {
            URL rootURL;
            String rootURLStr = config.get(ResourceIndexer.ROOT_URL);
            if (rootURLStr != null) {
                File rootDir = new File(rootURLStr);
                if (rootDir.isDirectory())
                    rootURL = rootDir.toURI().toURL();
                else
                    rootURL = new URL(rootURLStr);
            } else
                rootURL = new File(System.getProperty("user.dir")).toURI().toURL();
            String urlTemplate = config.get(ResourceIndexer.URL_TEMPLATE);
            bundleAnalyzer.setStateLocal(new GeneratorState(rootURL.toURI().normalize(), urlTemplate, resolvers));
        } else {
            bundleAnalyzer.setStateLocal(null);
        }
        // Iterate over the analyzers
        try {
            synchronized (analyzers) {
                for (Pair<ResourceAnalyzer, Filter> entry : analyzers) {
                    ResourceAnalyzer analyzer = entry.getFirst();
                    Filter filter = entry.getSecond();
                    if (filter == null || filter.match(resource.getProperties())) {
                        try {
                            analyzer.analyzeResource(resource, caps, reqs);
                        } catch (Exception e) {
                            log(LogService.LOG_ERROR, MessageFormat.format("Error calling analyzer \"{0}\" on resource {1}.", analyzer.getClass().getName(), resource.getLocation()), e);
                            StringWriter writer = new StringWriter();
                            Formatter comment = new Formatter(writer);
                            comment.format("Error calling analyzer \"%s\" on resource %s with message %s and stack: ", analyzer.getClass().getName(), resource.getLocation(), e);
                            comment.close();
                            e.printStackTrace(new PrintWriter(writer));
                            resourceTag.addComment(writer.toString());
                        }
                    }
                }
            }
        } finally {
            bundleAnalyzer.setStateLocal(null);
        }
    } finally {
        resource.close();
    }
    for (Capability cap : caps) {
        Tag capTag = new Tag(Schema.ELEM_CAPABILITY);
        capTag.addAttribute(Schema.ATTR_NAMESPACE, cap.getNamespace());
        appendAttributeAndDirectiveTags(capTag, cap.getAttributes(), cap.getDirectives());
        resourceTag.addContent(capTag);
    }
    for (Requirement req : reqs) {
        Tag reqTag = new Tag(Schema.ELEM_REQUIREMENT);
        reqTag.addAttribute(Schema.ATTR_NAMESPACE, req.getNamespace());
        appendAttributeAndDirectiveTags(reqTag, req.getAttributes(), req.getDirectives());
        resourceTag.addContent(reqTag);
    }
    return resourceTag;
}
Also used : ResourceAnalyzer(org.osgi.service.indexer.ResourceAnalyzer) Capability(org.osgi.service.indexer.Capability) Formatter(java.util.Formatter) URL(java.net.URL) InvalidSyntaxException(org.osgi.framework.InvalidSyntaxException) Requirement(org.osgi.service.indexer.Requirement) AddOnlyList(org.osgi.service.indexer.impl.util.AddOnlyList) StringWriter(java.io.StringWriter) Filter(org.osgi.framework.Filter) FrameworkUtil.createFilter(org.osgi.framework.FrameworkUtil.createFilter) Tag(org.osgi.service.indexer.impl.util.Tag) File(java.io.File) PrintWriter(java.io.PrintWriter)

Aggregations

File (java.io.File)1 PrintWriter (java.io.PrintWriter)1 StringWriter (java.io.StringWriter)1 URL (java.net.URL)1 Formatter (java.util.Formatter)1 Filter (org.osgi.framework.Filter)1 FrameworkUtil.createFilter (org.osgi.framework.FrameworkUtil.createFilter)1 InvalidSyntaxException (org.osgi.framework.InvalidSyntaxException)1 Capability (org.osgi.service.indexer.Capability)1 Requirement (org.osgi.service.indexer.Requirement)1 ResourceAnalyzer (org.osgi.service.indexer.ResourceAnalyzer)1 AddOnlyList (org.osgi.service.indexer.impl.util.AddOnlyList)1 Tag (org.osgi.service.indexer.impl.util.Tag)1