Search in sources :

Example 56 with Capability

use of org.osgi.resource.Capability in project bndtools by bndtools.

the class BundleSorter method compare.

@Override
public int compare(Viewer viewer, Object e1, Object e2) {
    Resource r1 = (Resource) e1;
    Resource r2 = (Resource) e2;
    Capability id1 = ResourceUtils.getIdentityCapability(r1);
    Capability id2 = ResourceUtils.getIdentityCapability(r2);
    String name1 = ResourceUtils.getIdentity(id1);
    if (name1 == null) {
        name1 = "";
    }
    String name2 = ResourceUtils.getIdentity(id2);
    if (name2 == null) {
        name2 = "";
    }
    int ret = name1.compareTo(name2);
    if (ret != 0) {
        return ret;
    }
    Version ver1 = ResourceUtils.getVersion(id1);
    if (ver1 == null) {
        ver1 = Version.emptyVersion;
    }
    Version ver2 = ResourceUtils.getVersion(id2);
    if (ver2 == null) {
        ver2 = Version.emptyVersion;
    }
    // This comparison is the reverse of what you would expect
    return ver2.compareTo(ver1);
}
Also used : Capability(org.osgi.resource.Capability) Version(org.osgi.framework.Version) Resource(org.osgi.resource.Resource)

Example 57 with Capability

use of org.osgi.resource.Capability in project bndtools by bndtools.

the class ResolutionChoiceSelectionDialog method updateSavePreferenceText.

private void updateSavePreferenceText() {
    Resource resource = candidates.get(0).getResource();
    Capability identity = ResourceUtils.getIdentityCapability(resource);
    String name = ResourceUtils.getIdentity(identity);
    StyledString label = new StyledString("Save top candidate (");
    label.append(name, BoldStyler.INSTANCE_DEFAULT);
    label.append(") as a ");
    label.append("preferred resource.", ItalicStyler.INSTANCE_DEFAULT);
    txtSavePreference.setText(label.getString());
    txtSavePreference.setStyleRanges(label.getStyleRanges());
}
Also used : Capability(org.osgi.resource.Capability) Resource(org.osgi.resource.Resource) StyledString(org.eclipse.jface.viewers.StyledString) StyledString(org.eclipse.jface.viewers.StyledString)

Example 58 with Capability

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

the class ResourceTest method getResources.

private Set<Resource> getResources(String locations) throws MalformedURLException, URISyntaxException {
    FixedIndexedRepo repo = new FixedIndexedRepo();
    repo.setLocations(locations);
    Requirement wildcard = ResourceUtils.createWildcardRequirement();
    Collection<Capability> caps = repo.findProviders(Collections.singleton(wildcard)).get(wildcard);
    return ResourceUtils.getResources(caps);
}
Also used : Requirement(org.osgi.resource.Requirement) Capability(org.osgi.resource.Capability) FixedIndexedRepo(aQute.bnd.deployer.repository.FixedIndexedRepo)

Example 59 with Capability

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

the class P2Indexer method readRepository.

private Repository readRepository() throws Exception {
    P2Impl p2 = new P2Impl(client, this.url, Processor.getExecutor());
    List<Artifact> artifacts = p2.getArtifacts();
    List<Promise<Resource>> fetched = new ArrayList<>(artifacts.size());
    Set<URI> visitedURIs = new HashSet<>(artifacts.size());
    Set<ArtifactID> visitedArtifacts = new HashSet<>(artifacts.size());
    Map<ArtifactID, Resource> knownResources = new HashMap<>();
    if (getBridge() != null) {
        for (Capability capability : getBridge().getRepository().findProviders(singleton(MD5_REQUIREMENT)).get(MD5_REQUIREMENT)) {
            Resource resource = capability.getResource();
            IdentityCapability identity = ResourceUtils.getIdentityCapability(resource);
            ArtifactID artifact = new ArtifactID(identity.osgi_identity(), identity.version(), (String) capability.getAttributes().get(MD5_ATTRIBUTE));
            knownResources.put(artifact, resource);
        }
    }
    for (final Artifact a : artifacts) {
        if (!visitedURIs.add(a.uri))
            continue;
        if (a.md5 != null) {
            ArtifactID id = new ArtifactID(a.id, toVersion(a.version), a.md5);
            if (!visitedArtifacts.add(id))
                continue;
            if (knownResources.containsKey(id)) {
                fetched.add(Promises.resolved(knownResources.get(id)));
                continue;
            }
        }
        Promise<Resource> promise = client.build().useCache(MAX_STALE).async(a.uri.toURL()).map(new Function<File, Resource>() {

            @Override
            public Resource apply(File file) {
                try {
                    ResourceBuilder rb = new ResourceBuilder();
                    rb.addFile(file, a.uri);
                    if (a.md5 != null)
                        rb.addCapability(new CapabilityBuilder(P2_CAPABILITY_NAMESPACE).addAttribute(MD5_ATTRIBUTE, a.md5));
                    return rb.build();
                } catch (Exception e) {
                    logger.debug("{}: Failed to create resource for %s from {}", name, a, file, e);
                    return RECOVERY;
                }
            }
        }).recover(new Function<Promise<?>, Resource>() {

            @Override
            public Resource apply(Promise<?> failed) {
                try {
                    logger.debug("{}: Failed to create resource for {}", name, a, failed.getFailure());
                } catch (InterruptedException e) {
                // impossible
                }
                return RECOVERY;
            }
        });
        fetched.add(promise);
    }
    Promise<List<Resource>> all = Promises.all(fetched);
    return all.map(new Function<List<Resource>, ResourcesRepository>() {

        @Override
        public ResourcesRepository apply(List<Resource> resources) {
            ResourcesRepository rr = new ResourcesRepository();
            for (Resource resource : resources) {
                if (resource != RECOVERY) {
                    rr.add(resource);
                }
            }
            return rr;
        }
    }).getValue();
}
Also used : P2Impl(aQute.p2.provider.P2Impl) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) CapabilityBuilder(aQute.bnd.osgi.resource.CapabilityBuilder) URI(java.net.URI) Function(org.osgi.util.function.Function) ArrayList(java.util.ArrayList) List(java.util.List) HashSet(java.util.HashSet) ResourceBuilder(aQute.bnd.osgi.resource.ResourceBuilder) Capability(org.osgi.resource.Capability) ContentCapability(aQute.bnd.osgi.resource.ResourceUtils.ContentCapability) IdentityCapability(aQute.bnd.osgi.resource.ResourceUtils.IdentityCapability) Resource(org.osgi.resource.Resource) Artifact(aQute.p2.api.Artifact) IdentityCapability(aQute.bnd.osgi.resource.ResourceUtils.IdentityCapability) IOException(java.io.IOException) DownloadListenerPromise(aQute.bnd.util.repository.DownloadListenerPromise) Promise(org.osgi.util.promise.Promise) File(java.io.File) ResourcesRepository(aQute.bnd.osgi.repository.ResourcesRepository)

Example 60 with Capability

use of org.osgi.resource.Capability in project bndtools by bndtools.

the class ResourceCapReqLoader method loadCapabilities.

@Override
public Map<String, List<Capability>> loadCapabilities() throws Exception {
    Map<String, List<Capability>> result = new HashMap<String, List<Capability>>();
    List<Capability> caps = resource.getCapabilities(null);
    for (Capability cap : caps) {
        String ns = cap.getNamespace();
        List<Capability> listForNamespace = result.get(ns);
        if (listForNamespace == null) {
            listForNamespace = new LinkedList<Capability>();
            result.put(ns, listForNamespace);
        }
        listForNamespace.add(cap);
    }
    return result;
}
Also used : Capability(org.osgi.resource.Capability) HashMap(java.util.HashMap) List(java.util.List) LinkedList(java.util.LinkedList)

Aggregations

Capability (org.osgi.resource.Capability)197 Requirement (org.osgi.resource.Requirement)104 Resource (org.osgi.resource.Resource)61 ArrayList (java.util.ArrayList)40 HashMap (java.util.HashMap)39 IdentityCapability (aQute.bnd.osgi.resource.ResourceUtils.IdentityCapability)37 CapReqBuilder (aQute.bnd.osgi.resource.CapReqBuilder)35 Collection (java.util.Collection)32 MockRegistry (test.lib.MockRegistry)26 BndEditModel (aQute.bnd.build.model.BndEditModel)25 Version (org.osgi.framework.Version)20 List (java.util.List)19 Repository (org.osgi.service.repository.Repository)17 Test (org.junit.Test)16 File (java.io.File)15 HostedCapability (org.osgi.service.resolver.HostedCapability)13 HashSet (java.util.HashSet)12 Map (java.util.Map)12 LinkedList (java.util.LinkedList)11 RequirementBuilder (aQute.bnd.osgi.resource.RequirementBuilder)8