use of org.apache.felix.resolver.test.util.CapabilitySet 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.apache.felix.resolver.test.util.CapabilitySet 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