use of org.apache.felix.resolver.test.util.CandidateComparator in project felix by apache.
the class BigResolutionTest method buildResolutionContext.
private ResolveContext buildResolutionContext() throws IOException, BundleException {
Object resolution;
InputStream is = getClass().getClassLoader().getResourceAsStream("resolution.json");
try {
resolution = JsonReader.read(is);
} finally {
is.close();
}
List<Resource> resources = new ArrayList<Resource>();
ResourceImpl system = new ResourceImpl("system-bundle");
parseCapability(system, "osgi.ee; osgi.ee=JavaSE; version=1.5");
parseCapability(system, "osgi.ee; osgi.ee=JavaSE; version=1.6");
parseCapability(system, "osgi.ee; osgi.ee=JavaSE; version=1.7");
resources.add(system);
for (Object r : (Collection) ((Map) resolution).get("resources")) {
resources.add(parseResource(r));
}
final List<Resource> mandatory = new ArrayList<Resource>();
for (Object r : (Collection) ((Map) resolution).get("mandatory")) {
mandatory.add(parseResource(r));
}
final Map<String, CapabilitySet> capSets = new HashMap<String, CapabilitySet>();
CapabilitySet svcSet = new CapabilitySet(Collections.singletonList("objectClass"));
capSets.put("osgi.service", svcSet);
for (Resource resource : resources) {
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);
}
}
return new ResolveContext() {
@Override
public Collection<Resource> getMandatoryResources() {
return mandatory;
}
@Override
public List<Capability> findProviders(Requirement requirement) {
SimpleFilter sf;
if (requirement.getDirectives().containsKey("filter")) {
sf = SimpleFilter.parse(requirement.getDirectives().get("filter"));
} else {
sf = SimpleFilter.convert(requirement.getAttributes());
}
CapabilitySet set = capSets.get(requirement.getNamespace());
List<Capability> caps = new ArrayList<Capability>(set.match(sf, true));
Collections.sort(caps, new CandidateComparator());
return caps;
}
@Override
public int insertHostedCapability(List<Capability> capabilities, HostedCapability hostedCapability) {
capabilities.add(hostedCapability);
return capabilities.size() - 1;
}
@Override
public boolean isEffective(Requirement requirement) {
return true;
}
@Override
public Map<Resource, Wiring> getWirings() {
return Collections.emptyMap();
}
};
}
Aggregations