Search in sources :

Example 1 with Referral

use of aQute.bnd.deployer.repository.api.Referral in project bnd by bndtools.

the class AbstractIndexedRepo method init.

protected final synchronized void init(boolean ignoreCachedFile) throws Exception {
    if (!initialised) {
        clear();
        // Load the available providers from the workspace plugins.
        loadAllContentProviders();
        // Load the request repository content providers, if specified
        if (requestedContentProviderList != null && requestedContentProviderList.length() > 0) {
            generatingProviders.clear();
            // Find the requested providers from the available ones.
            StringTokenizer tokenizer = new StringTokenizer(requestedContentProviderList, "|");
            while (tokenizer.hasMoreTokens()) {
                String token = tokenizer.nextToken().trim();
                IRepositoryContentProvider provider = allContentProviders.get(token);
                if (provider == null) {
                    warning("Unknown repository content provider \"%s\".", token);
                } else {
                    generatingProviders.add(provider);
                }
            }
            if (generatingProviders.isEmpty()) {
                warning("No valid repository index generators were found, requested list was: [%s]", requestedContentProviderList);
            }
        }
        // Initialise index locations
        indexLocations = loadIndexes();
        // Create the callback for new referral and resource objects
        final URLConnector connector = getConnector();
        IRepositoryIndexProcessor processor = new IRepositoryIndexProcessor() {

            public void processResource(Resource resource) {
                identityMap.put(resource);
                capabilityIndex.addResource(resource);
            }

            public void processReferral(URI parentUri, Referral referral, int maxDepth, int currentDepth) {
                try {
                    URI indexLocation = new URI(referral.getUrl());
                    try {
                        CachingUriResourceHandle indexHandle = new CachingUriResourceHandle(indexLocation, getCacheDirectory(), connector, (String) null);
                        indexHandle.setReporter(reporter);
                        InputStream indexStream = GZipUtils.detectCompression(IO.stream(indexHandle.request()));
                        readIndex(indexLocation.getPath(), indexLocation, indexStream, allContentProviders.values(), this, logService);
                    } catch (Exception e) {
                        warning("Unable to read referral index at URL '%s' from parent index '%s': %s", indexLocation, parentUri, e);
                    }
                } catch (URISyntaxException e) {
                    warning("Invalid referral URL '%s' from parent index '%s': %s", referral.getUrl(), parentUri, e);
                }
            }
        };
        // Parse the indexes
        for (URI indexLocation : indexLocations) {
            try {
                CachingUriResourceHandle indexHandle = new CachingUriResourceHandle(indexLocation, getCacheDirectory(), connector, (String) null);
                // OR 2) online is false
                if (indexHandle.cachedFile != null && !ignoreCachedFile && ((System.currentTimeMillis() - indexHandle.cachedFile.lastModified() < this.cacheTimeoutSeconds * 1000) || !this.online)) {
                    indexHandle.sha = indexHandle.getCachedSHA();
                    if (indexHandle.sha != null && !this.online) {
                        System.out.println(String.format("Offline. Using cached %s.", indexLocation));
                    }
                }
                indexHandle.setReporter(reporter);
                File indexFile = indexHandle.request();
                InputStream indexStream = GZipUtils.detectCompression(IO.stream(indexFile));
                readIndex(indexFile.getName(), indexLocation, indexStream, allContentProviders.values(), processor, logService);
            } catch (Exception e) {
                error("Unable to read index at URL '%s': %s", indexLocation, e);
            }
        }
        initialised = true;
    }
}
Also used : IRepositoryIndexProcessor(aQute.bnd.deployer.repository.api.IRepositoryIndexProcessor) InputStream(java.io.InputStream) DefaultURLConnector(aQute.bnd.deployer.http.DefaultURLConnector) URLConnector(aQute.bnd.service.url.URLConnector) Resource(org.osgi.resource.Resource) URISyntaxException(java.net.URISyntaxException) URI(java.net.URI) URISyntaxException(java.net.URISyntaxException) FileNotFoundException(java.io.FileNotFoundException) MalformedURLException(java.net.MalformedURLException) StringTokenizer(java.util.StringTokenizer) Referral(aQute.bnd.deployer.repository.api.Referral) IRepositoryContentProvider(aQute.bnd.deployer.repository.api.IRepositoryContentProvider) File(java.io.File)

Example 2 with Referral

use of aQute.bnd.deployer.repository.api.Referral in project bnd by bndtools.

the class R5RepoContentProvider method parseIndex.

public void parseIndex(InputStream stream, URI baseUri, IRepositoryIndexProcessor listener, LogService log) throws Exception {
    XMLStreamReader reader = null;
    try {
        XMLInputFactory inputFactory = XMLInputFactory.newInstance();
        inputFactory.setProperty(XMLInputFactory.IS_NAMESPACE_AWARE, true);
        inputFactory.setProperty(XMLInputFactory.IS_VALIDATING, false);
        inputFactory.setProperty(XMLInputFactory.SUPPORT_DTD, false);
        reader = inputFactory.createXMLStreamReader(stream);
        ResourceBuilder resourceBuilder = null;
        CapReqBuilder capReqBuilder = null;
        while (reader.hasNext()) {
            int type = reader.next();
            String localName;
            switch(type) {
                case START_ELEMENT:
                    localName = reader.getLocalName();
                    if (TAG_REFERRAL.equals(localName)) {
                        String url = reader.getAttributeValue(null, ATTR_REFERRAL_URL);
                        String depth = reader.getAttributeValue(null, ATTR_REFERRAL_DEPTH);
                        Referral referral = new Referral(url, parseInt(depth));
                        listener.processReferral(baseUri, referral, 0, 0);
                    } else if (TAG_RESOURCE.equals(localName)) {
                        resourceBuilder = new ResourceBuilder();
                    } else if (TAG_CAPABILITY.equals(localName) || TAG_REQUIREMENT.equals(localName)) {
                        String namespace = reader.getAttributeValue(null, ATTR_NAMESPACE);
                        capReqBuilder = new CapReqBuilder(namespace);
                    } else if (TAG_ATTRIBUTE.equals(localName)) {
                        String name = reader.getAttributeValue(null, ATTR_NAME);
                        String valueStr = reader.getAttributeValue(null, ATTR_VALUE);
                        String typeAttr = reader.getAttributeValue(null, ATTR_TYPE);
                        if (capReqBuilder != null) {
                            // base URI.
                            if (ContentNamespace.CONTENT_NAMESPACE.equals(capReqBuilder.getNamespace()) && ContentNamespace.CAPABILITY_URL_ATTRIBUTE.equals(name)) {
                                URI resolvedUri = resolveUri(valueStr, baseUri);
                                capReqBuilder.addAttribute(name, resolvedUri);
                            } else {
                                Object convertedAttr = convertAttribute(valueStr, typeAttr);
                                capReqBuilder.addAttribute(name, convertedAttr);
                            }
                        }
                    } else if (TAG_DIRECTIVE.equals(localName)) {
                        String name = reader.getAttributeValue(null, ATTR_NAME);
                        String valueStr = reader.getAttributeValue(null, ATTR_VALUE);
                        if (capReqBuilder != null)
                            capReqBuilder.addDirective(name, valueStr);
                    }
                    break;
                case END_ELEMENT:
                    localName = reader.getLocalName();
                    if (TAG_CAPABILITY.equals(localName)) {
                        if (resourceBuilder != null && capReqBuilder != null)
                            resourceBuilder.addCapability(capReqBuilder);
                        capReqBuilder = null;
                    } else if (TAG_REQUIREMENT.equals(localName)) {
                        if (resourceBuilder != null && capReqBuilder != null)
                            resourceBuilder.addRequirement(capReqBuilder);
                        capReqBuilder = null;
                    } else if (TAG_RESOURCE.equals(localName)) {
                        if (resourceBuilder != null) {
                            Resource resource = resourceBuilder.build();
                            listener.processResource(resource);
                            resourceBuilder = null;
                        }
                    }
                    break;
            }
        }
    } finally {
        if (reader != null) {
            try {
                reader.close();
            } catch (Exception e) {
            }
        }
    }
}
Also used : CapReqBuilder(aQute.bnd.osgi.resource.CapReqBuilder) ResourceBuilder(aQute.bnd.osgi.resource.ResourceBuilder) XMLStreamReader(javax.xml.stream.XMLStreamReader) Referral(aQute.bnd.deployer.repository.api.Referral) Resource(org.osgi.resource.Resource) URI(java.net.URI) XMLInputFactory(javax.xml.stream.XMLInputFactory) URISyntaxException(java.net.URISyntaxException) XMLStreamException(javax.xml.stream.XMLStreamException) IOException(java.io.IOException)

Example 3 with Referral

use of aQute.bnd.deployer.repository.api.Referral in project bnd by bndtools.

the class TestObrCapReqParsing method parseIndex.

private static List<Resource> parseIndex(InputStream stream, URI baseUri) throws Exception {
    ObrContentProvider parser = new ObrContentProvider(new BundleIndexerImpl());
    final List<Resource> resources = new LinkedList<Resource>();
    IRepositoryIndexProcessor processor = new IRepositoryIndexProcessor() {

        public void processResource(Resource resource) {
            resources.add(resource);
        }

        public void processReferral(URI parentUri, Referral referral, int maxDepth, int currentDepth) {
        }
    };
    parser.parseIndex(stream, baseUri, processor, new NullLogService());
    return resources;
}
Also used : Referral(aQute.bnd.deployer.repository.api.Referral) IRepositoryIndexProcessor(aQute.bnd.deployer.repository.api.IRepositoryIndexProcessor) Resource(org.osgi.resource.Resource) NullLogService(aQute.bnd.deployer.repository.NullLogService) URI(java.net.URI) BundleIndexerImpl(org.osgi.impl.bundle.bindex.BundleIndexerImpl) LinkedList(java.util.LinkedList)

Example 4 with Referral

use of aQute.bnd.deployer.repository.api.Referral in project bnd by bndtools.

the class ObrContentProvider method parseIndex.

public void parseIndex(InputStream stream, URI baseUri, IRepositoryIndexProcessor listener, LogService log) throws Exception {
    XMLInputFactory inputFactory = XMLInputFactory.newInstance();
    inputFactory.setProperty(XMLInputFactory.IS_NAMESPACE_AWARE, true);
    inputFactory.setProperty(XMLInputFactory.IS_VALIDATING, false);
    inputFactory.setProperty(XMLInputFactory.SUPPORT_DTD, false);
    StreamSource source = new StreamSource(stream, baseUri.toString());
    XMLStreamReader reader = inputFactory.createXMLStreamReader(source);
    ResourceBuilder resourceBuilder = null;
    CapReqBuilder capReqBuilder = null;
    while (reader.hasNext()) {
        int type = reader.next();
        String localName;
        switch(type) {
            case START_ELEMENT:
                localName = reader.getLocalName();
                if (TAG_REFERRAL.equals(localName)) {
                    Referral referral = new Referral(reader.getAttributeValue(null, ATTR_REFERRAL_URL), parseInt(reader.getAttributeValue(null, ATTR_REFERRAL_DEPTH)));
                    listener.processReferral(baseUri, referral, referral.getDepth(), 1);
                } else if (TAG_RESOURCE.equals(localName)) {
                    resourceBuilder = new ResourceBuilder();
                    String bsn = reader.getAttributeValue(null, ATTR_RESOURCE_SYMBOLIC_NAME);
                    String versionStr = reader.getAttributeValue(null, ATTR_RESOURCE_VERSION);
                    Version version = Version.parseVersion(versionStr);
                    String uri = reader.getAttributeValue(null, ATTR_RESOURCE_URI);
                    URI resolvedUri = resolveUri(uri, baseUri);
                    addBasicCapabilities(resourceBuilder, bsn, version, resolvedUri);
                } else if (TAG_CAPABILITY.equals(localName)) {
                    String obrName = reader.getAttributeValue(null, ATTR_NAME);
                    String namespace = mapObrNameToR5Namespace(obrName, false);
                    capReqBuilder = new CapReqBuilder(namespace);
                } else if (TAG_REQUIRE.equals(localName)) {
                    String obrName = reader.getAttributeValue(null, ATTR_NAME);
                    boolean extend = "true".equalsIgnoreCase(reader.getAttributeValue(null, ATTR_EXTEND));
                    String namespace = mapObrNameToR5Namespace(obrName, extend);
                    boolean optional = "true".equalsIgnoreCase(reader.getAttributeValue(null, ATTR_OPTIONAL));
                    capReqBuilder = new CapReqBuilder(namespace);
                    if (optional)
                        capReqBuilder.addDirective(Namespace.REQUIREMENT_RESOLUTION_DIRECTIVE, Namespace.RESOLUTION_OPTIONAL);
                    String filter = translateObrFilter(namespace, reader.getAttributeValue(null, ATTR_FILTER), log);
                    capReqBuilder.addDirective(Namespace.REQUIREMENT_FILTER_DIRECTIVE, filter);
                } else if (TAG_PROPERTY.equals(localName)) {
                    String name = reader.getAttributeValue(null, ATTR_PROPERTY_NAME);
                    String typeStr = reader.getAttributeValue(null, ATTR_PROPERTY_TYPE);
                    String valueStr = reader.getAttributeValue(null, ATTR_PROPERTY_VALUE);
                    if (capReqBuilder != null) {
                        name = mapObrPropertyToR5(capReqBuilder.getNamespace(), name);
                        if (PROPERTY_USES.equals(name))
                            capReqBuilder.addDirective(PROPERTY_USES, valueStr);
                        else {
                            Object value = convertProperty(valueStr, typeStr);
                            capReqBuilder.addAttribute(name, value);
                        }
                    }
                }
                break;
            case END_ELEMENT:
                localName = reader.getLocalName();
                if (TAG_RESOURCE.equals(localName)) {
                    if (resourceBuilder != null) {
                        Resource resource = resourceBuilder.build();
                        listener.processResource(resource);
                    }
                } else if (TAG_CAPABILITY.equals(localName)) {
                    if (resourceBuilder != null && capReqBuilder != null)
                        resourceBuilder.addCapability(capReqBuilder);
                    capReqBuilder = null;
                } else if (TAG_REQUIRE.equals(localName)) {
                    if (resourceBuilder != null && capReqBuilder != null)
                        resourceBuilder.addRequirement(capReqBuilder);
                    capReqBuilder = null;
                }
        }
    }
}
Also used : CapReqBuilder(aQute.bnd.osgi.resource.CapReqBuilder) ResourceBuilder(aQute.bnd.osgi.resource.ResourceBuilder) XMLStreamReader(javax.xml.stream.XMLStreamReader) Referral(aQute.bnd.deployer.repository.api.Referral) Version(org.osgi.framework.Version) StreamSource(javax.xml.transform.stream.StreamSource) Resource(org.osgi.resource.Resource) URI(java.net.URI) XMLInputFactory(javax.xml.stream.XMLInputFactory)

Example 5 with Referral

use of aQute.bnd.deployer.repository.api.Referral in project bndtools by bndtools.

the class WorkspaceR5Repository method loadProjectIndex.

public void loadProjectIndex(final IProject project, InputStream index, URI baseUri) {
    synchronized (projectMap) {
        try {
            cleanProject(project);
            IRepositoryIndexProcessor processor = new IRepositoryIndexProcessor() {

                @Override
                public void processResource(Resource resource) {
                    addResource(project, resource);
                }

                @Override
                public void processReferral(URI parentUri, Referral referral, int maxDepth, int currentDepth) {
                // ignore: we don't create any referrals
                }
            };
            contentProvider.parseIndex(index, baseUri, processor, logAdapter);
        } catch (Exception e) {
            logger.logError(MessageFormat.format("Failed to process index file for bundles in project {0}.", project.getName()), e);
        } finally {
            IO.close(index);
        }
    }
}
Also used : Referral(aQute.bnd.deployer.repository.api.Referral) IRepositoryIndexProcessor(aQute.bnd.deployer.repository.api.IRepositoryIndexProcessor) Resource(org.osgi.resource.Resource) URI(java.net.URI) IOException(java.io.IOException)

Aggregations

Referral (aQute.bnd.deployer.repository.api.Referral)5 URI (java.net.URI)5 Resource (org.osgi.resource.Resource)5 IRepositoryIndexProcessor (aQute.bnd.deployer.repository.api.IRepositoryIndexProcessor)3 CapReqBuilder (aQute.bnd.osgi.resource.CapReqBuilder)2 ResourceBuilder (aQute.bnd.osgi.resource.ResourceBuilder)2 IOException (java.io.IOException)2 URISyntaxException (java.net.URISyntaxException)2 XMLInputFactory (javax.xml.stream.XMLInputFactory)2 XMLStreamReader (javax.xml.stream.XMLStreamReader)2 DefaultURLConnector (aQute.bnd.deployer.http.DefaultURLConnector)1 NullLogService (aQute.bnd.deployer.repository.NullLogService)1 IRepositoryContentProvider (aQute.bnd.deployer.repository.api.IRepositoryContentProvider)1 URLConnector (aQute.bnd.service.url.URLConnector)1 File (java.io.File)1 FileNotFoundException (java.io.FileNotFoundException)1 InputStream (java.io.InputStream)1 MalformedURLException (java.net.MalformedURLException)1 LinkedList (java.util.LinkedList)1 StringTokenizer (java.util.StringTokenizer)1