use of org.osgi.service.resolver.Resolver in project felix by apache.
the class FELIX_4914_Test method testResolution.
@Test
public void testResolution() throws Exception {
BufferedReader reader = new BufferedReader(new InputStreamReader(getClass().getResourceAsStream("/felix-4914.json")));
Map<String, Object> resolution = (Map<String, Object>) JsonReader.read(reader);
List<Resource> repository = readRepository(resolution.get("repository"));
Map<String, CapabilitySet> capSets = new HashMap<String, CapabilitySet>();
for (Resource resource : repository) {
for (Capability cap : resource.getCapabilities(null)) {
String ns = cap.getNamespace();
CapabilitySet set = capSets.get(ns);
if (set == null) {
set = new CapabilitySet(Collections.singletonList(ns));
capSets.put(ns, set);
}
set.addCapability(cap);
}
}
Resource root = null;
for (Resource resource : repository) {
if ("root".equals(Util.getSymbolicName(resource))) {
root = resource;
break;
}
}
List<Resource> mandatory = new ArrayList<Resource>();
mandatory.add(root);
Map<Requirement, List<Capability>> candidates = new HashMap<Requirement, List<Capability>>();
for (Resource resource : repository) {
for (Requirement requirement : resource.getRequirements(null)) {
CapabilitySet set = capSets.get(requirement.getNamespace());
if (set != null) {
String filter = requirement.getDirectives().get(Constants.FILTER_DIRECTIVE);
SimpleFilter sf = (filter != null) ? SimpleFilter.parse(filter) : SimpleFilter.convert(requirement.getAttributes());
candidates.put(requirement, new ArrayList<Capability>(set.match(sf, true)));
} else {
candidates.put(requirement, Collections.<Capability>emptyList());
}
}
}
ResolveContextImpl rci = new ResolveContextImpl(Collections.<Resource, Wiring>emptyMap(), candidates, mandatory, Collections.EMPTY_LIST);
Resolver resolver = new ResolverImpl(new Logger(Logger.LOG_DEBUG));
Map<Resource, List<Wire>> wireMap = resolver.resolve(rci);
}
use of org.osgi.service.resolver.Resolver in project felix by apache.
the class ResolverTest method testScenario6.
@Test
public void testScenario6() throws Exception {
Resolver resolver = new ResolverImpl(new Logger(Logger.LOG_DEBUG));
Map<Resource, Wiring> wirings = new HashMap<Resource, Wiring>();
Map<Requirement, List<Capability>> candMap = new HashMap<Requirement, List<Capability>>();
List<Resource> mandatory = populateScenario6(wirings, candMap);
ResolveContextImpl rci = new ResolveContextImpl(wirings, candMap, mandatory, Collections.<Resource>emptyList());
Map<Resource, List<Wire>> wireMap = resolver.resolve(rci);
int aResources = 0;
for (Resource r : wireMap.keySet()) {
if ("A".equals(getResourceName(r))) {
aResources++;
List<Wire> wires = wireMap.get(r);
assertEquals(4, wires.size());
List<String> providers = new ArrayList<String>();
for (Wire w : wires) {
providers.add(getResourceName(w.getProvider()));
}
Collections.sort(providers);
assertEquals(Arrays.asList("B", "C", "D", "D"), providers);
}
}
assertEquals("Should have found two resolved resources named 'A'", 2, aResources);
}
use of org.osgi.service.resolver.Resolver in project felix by apache.
the class ResolverTest method testScenario9.
@Test
public void testScenario9() throws Exception {
Resolver resolver = new ResolverImpl(new Logger(Logger.LOG_DEBUG), 1);
Map<Resource, Wiring> wirings = new HashMap<Resource, Wiring>();
Map<Requirement, List<Capability>> candMap = new HashMap<Requirement, List<Capability>>();
List<Resource> mandatory = populateScenario9(wirings, candMap);
ResolveContextImpl rci = new ResolveContextImpl(wirings, candMap, mandatory, Collections.<Resource>emptyList());
Map<Resource, List<Wire>> wireMap = resolver.resolve(rci);
Resource resB = findResource("B", wireMap.keySet());
Resource resA = findResource("A", wirings.keySet());
Resource resC1 = findResource("C1", wirings.keySet());
assertNotNull(resB);
assertNotNull(resC1);
assertEquals(1, wireMap.size());
List<Wire> wiresB = wireMap.get(resB);
assertEquals(2, wiresB.size());
// should be wired to A and C1
assertEquals(resA, wiresB.get(0).getProvider());
assertEquals(resC1, wiresB.get(1).getProvider());
}
use of org.osgi.service.resolver.Resolver in project felix by apache.
the class ResolverTest method testScenario7.
@Test
public void testScenario7() throws Exception {
Resolver resolver = new ResolverImpl(new Logger(Logger.LOG_DEBUG));
Map<Resource, Wiring> wirings = new HashMap<Resource, Wiring>();
Map<Requirement, List<Capability>> candMap = new HashMap<Requirement, List<Capability>>();
List<Resource> mandatory = populateScenario7(wirings, candMap);
ResolveContextImpl rci = new ResolveContextImpl(wirings, candMap, mandatory, Collections.<Resource>emptyList());
Map<Resource, List<Wire>> wireMap = resolver.resolve(rci);
assertEquals(4, wireMap.size());
Resource aRes = findResource("A", wireMap.keySet());
List<Wire> aWires = wireMap.get(aRes);
assertEquals(0, aWires.size());
Resource f1Res = findResource("F1", wireMap.keySet());
List<Wire> f1Wires = wireMap.get(f1Res);
assertEquals(1, f1Wires.size());
Wire f1Wire = f1Wires.get(0);
assertEquals(f1Res, f1Wire.getRequirer());
assertEquals(aRes, f1Wire.getProvider());
Requirement req = f1Wire.getRequirement();
assertEquals(HostNamespace.HOST_NAMESPACE, req.getNamespace());
assertEquals(0, req.getAttributes().size());
assertEquals(f1Res, req.getResource());
assertEquals(1, req.getDirectives().size());
assertEquals("(osgi.wiring.host=A)", req.getDirectives().get("filter"));
Capability cap = f1Wire.getCapability();
assertEquals(aRes, cap.getResource());
assertEquals(HostNamespace.HOST_NAMESPACE, cap.getNamespace());
assertEquals(0, cap.getDirectives().size());
assertEquals(1, cap.getAttributes().size());
assertEquals("A", cap.getAttributes().get(HostNamespace.HOST_NAMESPACE));
Resource f2Res = findResource("F2", wireMap.keySet());
List<Wire> f2Wires = wireMap.get(f2Res);
assertEquals(1, f2Wires.size());
Wire f2Wire = f2Wires.get(0);
assertEquals(f2Res, f2Wire.getRequirer());
assertEquals(aRes, f2Wire.getProvider());
Requirement req2 = f2Wire.getRequirement();
assertEquals(HostNamespace.HOST_NAMESPACE, req2.getNamespace());
assertEquals(0, req2.getAttributes().size());
assertEquals(f2Res, req2.getResource());
assertEquals(1, req2.getDirectives().size());
assertEquals("(osgi.wiring.host=A)", req2.getDirectives().get("filter"));
Capability cap2 = f1Wire.getCapability();
assertEquals(aRes, cap2.getResource());
assertEquals(HostNamespace.HOST_NAMESPACE, cap2.getNamespace());
assertEquals(0, cap2.getDirectives().size());
assertEquals(1, cap2.getAttributes().size());
assertEquals("A", cap2.getAttributes().get(HostNamespace.HOST_NAMESPACE));
Resource bRes = findResource("B", wireMap.keySet());
List<Wire> bWires = wireMap.get(bRes);
assertEquals(1, bWires.size());
Wire bWire = bWires.get(0);
assertEquals(bRes, bWire.getRequirer());
assertEquals(f2Res, bWire.getProvider());
Requirement bReq = bWire.getRequirement();
assertEquals(IdentityNamespace.IDENTITY_NAMESPACE, bReq.getNamespace());
assertEquals(0, bReq.getAttributes().size());
assertEquals(bRes, bReq.getResource());
assertEquals(1, bReq.getDirectives().size());
assertEquals("(osgi.identity=F2)", bReq.getDirectives().get("filter"));
}
use of org.osgi.service.resolver.Resolver in project bnd by bndtools.
the class DebugReporter method resource.
private void resource(Resource r) {
IdentityCapability id = ResourceUtils.getIdentityCapability(r);
String resolveError = null;
if (!context.isSystemResource(r) && level >= 3 && id != null)
try {
String v = id.version() == null ? null : id.version().toString();
Requirement req = CapReqBuilder.createBundleRequirement(id.osgi_identity(), v).buildSyntheticRequirement();
context.setInputRequirements(req);
Resolver resolver = new BndResolver(new ResolverLogger(4));
Map<Resource, List<Wire>> resolved = resolver.resolve(context);
} catch (Exception e) {
resolveError = e.toString();
}
String s = resolveError == null ? " " : "!";
if (id == null)
out.printf("%s %s%n", s, r);
else
out.printf("%s %-50s %-20s %s%n", s, id.osgi_identity(), noNull(id.version()), id.description(""));
if (level >= 2) {
for (Capability c : r.getCapabilities(null)) {
capability(" ", c);
}
for (Requirement rq : r.getRequirements(null)) {
requirement(" ", rq);
}
}
}
Aggregations