Search in sources :

Example 56 with Resource

use of org.osgi.resource.Resource 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 57 with Resource

use of org.osgi.resource.Resource in project bnd by bndtools.

the class VersionedResourceIndex method put.

public synchronized void put(Resource resource) {
    String id = getResourceIdentity(resource);
    if (id == null)
        throw new IllegalArgumentException("Missing identity capability on resource");
    Version version = getResourceVersion(resource);
    SortedMap<Version, Resource> versionMap = map.get(id);
    if (versionMap == null) {
        versionMap = new TreeMap<Version, Resource>();
        map.put(id, versionMap);
    }
    versionMap.put(version, resource);
}
Also used : Version(aQute.bnd.version.Version) RepoResourceUtils.getResourceVersion(aQute.bnd.deployer.repository.RepoResourceUtils.getResourceVersion) Resource(org.osgi.resource.Resource)

Example 58 with Resource

use of org.osgi.resource.Resource 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 59 with Resource

use of org.osgi.resource.Resource in project bnd by bndtools.

the class AbstractIndexedRepo method findExactMatch.

ResourceHandle findExactMatch(String identity, String version) throws Exception {
    VersionRange range = new VersionRange(version);
    if (range.isRange())
        return null;
    Resource resource = identityMap.getExact(identity, range.getLow());
    if (resource == null)
        return null;
    return mapResourceToHandle(resource);
}
Also used : Resource(org.osgi.resource.Resource) VersionRange(aQute.bnd.version.VersionRange)

Example 60 with Resource

use of org.osgi.resource.Resource in project bnd by bndtools.

the class Bndrun method resolve.

public <T> T resolve(boolean failOnChanges, boolean writeOnChanges, Converter<T, Collection<? extends HeaderClause>> runbundlesFormatter) throws Exception {
    try (ProjectResolver projectResolver = new ProjectResolver(this)) {
        try {
            Map<Resource, List<Wire>> resolution = projectResolver.resolve();
            if (!projectResolver.isOk()) {
                return runbundlesFormatter.convert(Collections.<VersionedClause>emptyList());
            }
            Set<Resource> resources = resolution.keySet();
            List<VersionedClause> runBundles = new ArrayList<>();
            for (Resource resource : resources) {
                VersionedClause runBundle = ResourceUtils.toVersionClause(resource, "[===,==+)");
                if (!runBundles.contains(runBundle)) {
                    runBundles.add(runBundle);
                }
            }
            Collections.sort(runBundles, new Comparator<VersionedClause>() {

                @Override
                public int compare(VersionedClause a, VersionedClause b) {
                    int diff = a.getName().compareTo(b.getName());
                    return (diff != 0) ? diff : a.getVersionRange().compareTo(b.getVersionRange());
                }
            });
            File runFile = getPropertiesFile();
            BndEditModel bem = new BndEditModel(getWorkspace());
            Document doc = new Document(IO.collect(runFile));
            bem.loadFrom(doc);
            List<VersionedClause> bemRunBundles = bem.getRunBundles();
            if (bemRunBundles == null)
                bemRunBundles = new ArrayList<>();
            String originalRunbundlesString = runbundlesWrappedFormatter.convert(bemRunBundles);
            logger.debug("Original -runbundles was:\n\t {}", originalRunbundlesString);
            String runbundlesString = runbundlesWrappedFormatter.convert(runBundles);
            logger.debug("Resolved -runbundles is:\n\t {}", runbundlesString);
            List<VersionedClause> deltaAdd = new ArrayList<>(runBundles);
            deltaAdd.removeAll(bemRunBundles);
            List<VersionedClause> deltaRemove = new ArrayList<>(bemRunBundles);
            deltaRemove.removeAll(runBundles);
            boolean added = bemRunBundles.addAll(deltaAdd);
            boolean removed = bemRunBundles.removeAll(deltaRemove);
            if (added || removed) {
                if (failOnChanges && !bemRunBundles.isEmpty()) {
                    error("The runbundles have changed. Failing the build!\nWas: %s\nIs: %s", originalRunbundlesString, runbundlesString);
                    return runbundlesFormatter.convert(Collections.<VersionedClause>emptyList());
                }
                if (writeOnChanges) {
                    bem.setRunBundles(bemRunBundles);
                    String runBundlesProperty = bem.getDocumentChanges().get(Constants.RUNBUNDLES);
                    logger.debug("Writing changes to {}", runFile.getAbsolutePath());
                    logger.debug("{}:{}", Constants.RUNBUNDLES, runBundlesProperty);
                    bem.saveChangesTo(doc);
                    IO.store(doc.get(), runFile);
                }
            }
            return runbundlesFormatter.convert(bemRunBundles);
        } finally {
            getInfo(projectResolver);
        }
    }
}
Also used : VersionedClause(aQute.bnd.build.model.clauses.VersionedClause) Resource(org.osgi.resource.Resource) ArrayList(java.util.ArrayList) Document(aQute.bnd.properties.Document) ArrayList(java.util.ArrayList) List(java.util.List) File(java.io.File) BndEditModel(aQute.bnd.build.model.BndEditModel)

Aggregations

Resource (org.osgi.resource.Resource)201 Capability (org.osgi.resource.Capability)62 Requirement (org.osgi.resource.Requirement)62 ArrayList (java.util.ArrayList)57 List (java.util.List)39 HashMap (java.util.HashMap)32 File (java.io.File)27 Collection (java.util.Collection)24 Wire (org.osgi.resource.Wire)24 ResourceBuilder (aQute.bnd.osgi.resource.ResourceBuilder)23 Map (java.util.Map)22 CapReqBuilder (aQute.bnd.osgi.resource.CapReqBuilder)18 URI (java.net.URI)18 HashSet (java.util.HashSet)18 BndEditModel (aQute.bnd.build.model.BndEditModel)15 MockRegistry (test.lib.MockRegistry)15 Version (org.osgi.framework.Version)14 BundleRevision (org.osgi.framework.wiring.BundleRevision)14 Repository (org.osgi.service.repository.Repository)14 IdentityCapability (aQute.bnd.osgi.resource.ResourceUtils.IdentityCapability)13