Search in sources :

Example 1 with Repository

use of org.osgi.service.repository.Repository in project aries by apache.

the class RepositoryServiceRepositoryTest method testFindProviders.

@Test
public void testFindProviders() throws Exception {
    BundleContext bc = EasyMock.createNiceMock(BundleContext.class);
    RepositoryServiceRepository rsr = new RepositoryServiceRepository(bc);
    @SuppressWarnings("unchecked") ServiceReference<Object> sr = EasyMock.createMock(ServiceReference.class);
    @SuppressWarnings("unchecked") ServiceReference<Object> sr2 = EasyMock.createMock(ServiceReference.class);
    @SuppressWarnings("unchecked") ServiceReference<Object> sr3 = EasyMock.createMock(ServiceReference.class);
    EasyMock.expect(bc.getAllServiceReferences("org.osgi.service.repository.Repository", null)).andReturn(new ServiceReference[] { sr, sr2, sr3 }).anyTimes();
    TestRepository tr = new TestRepository();
    EasyMock.expect(bc.getService(sr)).andReturn(tr).anyTimes();
    ToastRepository tr2 = new ToastRepository();
    EasyMock.expect(bc.getService(sr2)).andReturn(tr2).anyTimes();
    Repository tr3 = Creator.create();
    EasyMock.expect(bc.getService(sr3)).andReturn(tr3).anyTimes();
    EasyMock.replay(bc);
    Map<String, String> dirs = Collections.singletonMap("filter", "(org.foo=bar)");
    Requirement req = new TestRequirement("org.foo", dirs);
    Collection<Capability> res = rsr.findProviders(req);
    assertEquals(1, res.size());
    Capability cap = res.iterator().next();
    assertEquals("org.foo", cap.getNamespace());
    assertEquals(1, cap.getAttributes().size());
    assertEquals("bar", cap.getAttributes().get("org.foo"));
    Map<String, String> dirs2 = Collections.singletonMap("filter", "(org.foo=b)");
    Requirement req2 = new TestRequirement("poing", dirs2);
    Collection<Capability> res2 = rsr.findProviders(req2);
    assertEquals(1, res2.size());
    Capability cap2 = res2.iterator().next();
    assertEquals("poing", cap2.getNamespace());
    assertEquals(1, cap2.getAttributes().size());
    assertEquals("b", cap2.getAttributes().get("org.foo"));
    Map<String, String> dirs3 = Collections.singletonMap("filter", "(x=y)");
    Requirement req3 = new TestRequirement("ns1", dirs3);
    Collection<Capability> res3 = rsr.findProviders(req3);
    assertEquals(1, res3.size());
    Capability cap3 = res3.iterator().next();
    assertEquals("ns1", cap3.getNamespace());
    assertEquals(1, cap3.getAttributes().size());
    assertEquals("y", cap3.getAttributes().get("x"));
}
Also used : Capability(org.osgi.resource.Capability) ServiceReference(org.osgi.framework.ServiceReference) Requirement(org.osgi.resource.Requirement) Repository(org.osgi.service.repository.Repository) BundleContext(org.osgi.framework.BundleContext) Test(org.junit.Test)

Example 2 with Repository

use of org.osgi.service.repository.Repository in project karaf by apache.

the class AggregateRepository method findProviders.

@Override
public Map<Requirement, Collection<Capability>> findProviders(Collection<? extends Requirement> requirements) {
    Map<Requirement, Collection<Capability>> result = new HashMap<>();
    for (Requirement requirement : requirements) {
        List<Capability> caps = new ArrayList<>();
        for (Repository repository : repositories) {
            Map<Requirement, Collection<Capability>> resMap = repository.findProviders(Collections.singleton(requirement));
            Collection<Capability> res = resMap != null ? resMap.get(requirement) : null;
            if (res != null) {
                caps.addAll(res);
            }
        }
        result.put(requirement, caps);
    }
    return result;
}
Also used : Requirement(org.osgi.resource.Requirement) Repository(org.osgi.service.repository.Repository) Capability(org.osgi.resource.Capability) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Collection(java.util.Collection)

Example 3 with Repository

use of org.osgi.service.repository.Repository in project karaf by apache.

the class OfflineResolver method resolve.

public static void resolve(String resolutionFile) throws Exception {
    Map<String, Object> resolution;
    try (BufferedReader reader = Files.newBufferedReader(Paths.get(resolutionFile), StandardCharsets.UTF_8)) {
        resolution = (Map<String, Object>) JsonReader.read(reader);
    }
    final Repository globalRepository;
    if (resolution.containsKey("globalRepository")) {
        globalRepository = readRepository(resolution.get("globalRepository"));
    } else {
        globalRepository = null;
    }
    final Repository repository = readRepository(resolution.get("repository"));
    Resolver resolver = new ResolverImpl(new Logger(Logger.LOG_ERROR));
    Map<Resource, List<Wire>> wiring = resolver.resolve(new ResolveContext() {

        private final CandidateComparator candidateComparator = new CandidateComparator(r -> 0);

        @Override
        public Collection<Resource> getMandatoryResources() {
            List<Resource> resources = new ArrayList<>();
            Requirement req = new RequirementImpl(null, IDENTITY_NAMESPACE, Collections.emptyMap(), Collections.emptyMap(), SimpleFilter.parse("(" + IDENTITY_NAMESPACE + "=root)"));
            Collection<Capability> identities = repository.findProviders(Collections.singleton(req)).get(req);
            for (Capability identity : identities) {
                resources.add(identity.getResource());
            }
            return resources;
        }

        @Override
        public List<Capability> findProviders(Requirement requirement) {
            List<Capability> caps = new ArrayList<>();
            Map<Requirement, Collection<Capability>> resMap = repository.findProviders(Collections.singleton(requirement));
            Collection<Capability> res = resMap != null ? resMap.get(requirement) : null;
            if (res != null && !res.isEmpty()) {
                caps.addAll(res);
            } else if (globalRepository != null) {
                // Only bring in external resources for non optional requirements
                if (!RESOLUTION_OPTIONAL.equals(requirement.getDirectives().get(RESOLUTION_DIRECTIVE))) {
                    resMap = globalRepository.findProviders(Collections.singleton(requirement));
                    res = resMap != null ? resMap.get(requirement) : null;
                    if (res != null && !res.isEmpty()) {
                        caps.addAll(res);
                    }
                }
            }
            // Sort caps
            Collections.sort(caps, candidateComparator);
            return caps;
        }

        @Override
        public int insertHostedCapability(List<Capability> capabilities, HostedCapability hostedCapability) {
            int idx = Collections.binarySearch(capabilities, hostedCapability, candidateComparator);
            if (idx < 0) {
                idx = Math.abs(idx + 1);
            }
            capabilities.add(idx, hostedCapability);
            return idx;
        }

        @Override
        public boolean isEffective(Requirement requirement) {
            return true;
        }

        @Override
        public Map<Resource, Wiring> getWirings() {
            return Collections.emptyMap();
        }
    });
}
Also used : ResolveContext(org.osgi.service.resolver.ResolveContext) RequirementImpl(org.apache.karaf.features.internal.resolver.RequirementImpl) Resolver(org.osgi.service.resolver.Resolver) HostedCapability(org.osgi.service.resolver.HostedCapability) Capability(org.osgi.resource.Capability) Resource(org.osgi.resource.Resource) ResolverImpl(org.apache.felix.resolver.ResolverImpl) Logger(org.apache.felix.resolver.Logger) HostedCapability(org.osgi.service.resolver.HostedCapability) Requirement(org.osgi.resource.Requirement) BaseRepository(org.apache.karaf.features.internal.repository.BaseRepository) Repository(org.osgi.service.repository.Repository) BufferedReader(java.io.BufferedReader) Collection(java.util.Collection) ArrayList(java.util.ArrayList) List(java.util.List) Map(java.util.Map)

Example 4 with Repository

use of org.osgi.service.repository.Repository in project bnd by bndtools.

the class ResolveCommand method _repos.

public void _repos(RepoOptions options) throws Exception {
    Workspace ws = bnd.getWorkspace(options.workspace());
    if (ws == null) {
        error("No workspace");
        return;
    }
    List<Repository> plugins = ws.getPlugins(Repository.class);
    bnd.out.println(Strings.join("\n", plugins));
}
Also used : Repository(org.osgi.service.repository.Repository) Workspace(aQute.bnd.build.Workspace)

Example 5 with Repository

use of org.osgi.service.repository.Repository in project bnd by bndtools.

the class Project method getBundleByHash.

private Container getBundleByHash(String bsn, Map<String, String> attrs) throws Exception {
    String hashStr = attrs.get("hash");
    String algo = SHA_256;
    String hash = hashStr;
    int colonIndex = hashStr.indexOf(':');
    if (colonIndex > -1) {
        algo = hashStr.substring(0, colonIndex);
        int afterColon = colonIndex + 1;
        hash = (colonIndex < hashStr.length()) ? hashStr.substring(afterColon) : "";
    }
    for (RepositoryPlugin plugin : workspace.getRepositories()) {
        // The plugin *may* understand version=hash directly
        DownloadBlocker blocker = new DownloadBlocker(this);
        File result = plugin.get(bsn, Version.LOWEST, Collections.unmodifiableMap(attrs), blocker);
        // Service, use a capability search on the osgi.content namespace.
        if (result == null && plugin instanceof Repository) {
            Repository repo = (Repository) plugin;
            if (!SHA_256.equals(algo))
                // R5 repos only support SHA-256
                continue;
            Requirement contentReq = new CapReqBuilder(ContentNamespace.CONTENT_NAMESPACE).filter(String.format("(%s=%s)", ContentNamespace.CONTENT_NAMESPACE, hash)).buildSyntheticRequirement();
            Set<Requirement> reqs = Collections.singleton(contentReq);
            Map<Requirement, Collection<Capability>> providers = repo.findProviders(reqs);
            Collection<Capability> caps = providers != null ? providers.get(contentReq) : null;
            if (caps != null && !caps.isEmpty()) {
                Capability cap = caps.iterator().next();
                IdentityCapability idCap = ResourceUtils.getIdentityCapability(cap.getResource());
                Map<String, Object> idAttrs = idCap.getAttributes();
                String id = (String) idAttrs.get(IdentityNamespace.IDENTITY_NAMESPACE);
                Object version = idAttrs.get(IdentityNamespace.CAPABILITY_VERSION_ATTRIBUTE);
                Version bndVersion = version != null ? Version.parseVersion(version.toString()) : Version.LOWEST;
                if (!bsn.equals(id)) {
                    String error = String.format("Resource with requested hash does not match ID '%s' [hash: %s]", bsn, hashStr);
                    return new Container(this, bsn, "hash", Container.TYPE.ERROR, null, error, null, null);
                }
                result = plugin.get(id, bndVersion, null, blocker);
            }
        }
        if (result != null)
            return toContainer(bsn, "hash", attrs, result, blocker);
    }
    // If we reach this far, none of the repos found the resource.
    return new Container(this, bsn, "hash", Container.TYPE.ERROR, null, "Could not find resource by content hash " + hashStr, null, null);
}
Also used : CapReqBuilder(aQute.bnd.osgi.resource.CapReqBuilder) IdentityCapability(aQute.bnd.osgi.resource.ResourceUtils.IdentityCapability) Capability(org.osgi.resource.Capability) RepositoryPlugin(aQute.bnd.service.RepositoryPlugin) IdentityCapability(aQute.bnd.osgi.resource.ResourceUtils.IdentityCapability) Requirement(org.osgi.resource.Requirement) Repository(org.osgi.service.repository.Repository) Version(aQute.bnd.version.Version) Collection(java.util.Collection) File(java.io.File)

Aggregations

Repository (org.osgi.service.repository.Repository)35 Requirement (org.osgi.resource.Requirement)18 Capability (org.osgi.resource.Capability)16 ArrayList (java.util.ArrayList)11 Collection (java.util.Collection)11 CapReqBuilder (aQute.bnd.osgi.resource.CapReqBuilder)9 File (java.io.File)9 Resource (org.osgi.resource.Resource)8 Run (aQute.bnd.build.Run)5 OSGiRepository (aQute.bnd.repository.osgi.OSGiRepository)5 List (java.util.List)5 Resolver (org.osgi.service.resolver.Resolver)5 BndEditModel (aQute.bnd.build.model.BndEditModel)4 ResourcesRepository (aQute.bnd.osgi.repository.ResourcesRepository)4 Processor (aQute.bnd.osgi.Processor)3 IdentityCapability (aQute.bnd.osgi.resource.ResourceUtils.IdentityCapability)3 RepositoryPlugin (aQute.bnd.service.RepositoryPlugin)3 InfoRepository (aQute.bnd.service.repository.InfoRepository)3 WorkspaceR5Repository (bndtools.central.WorkspaceR5Repository)3 Map (java.util.Map)3