use of org.osgi.service.resolver.HostedCapability in project felix by apache.
the class IterativeResolver method resolve.
public Map<Resource, List<Wire>> resolve(final ResolveContext context) throws ResolutionException {
final Map<Resource, List<Wire>> wires = new HashMap<Resource, List<Wire>>();
final Map<Resource, List<Wire>> invertedWires = new HashMap<Resource, List<Wire>>();
Collection<Resource> resources = context.getMandatoryResources();
for (final Resource resource : resources) {
// Build wiring
invertedWires.clear();
for (Resource res : wires.keySet()) {
for (Wire wire : wires.get(res)) {
List<Wire> w = invertedWires.get(wire.getProvider());
if (w == null) {
w = new ArrayList<Wire>();
invertedWires.put(wire.getProvider(), w);
}
w.add(wire);
}
}
final Map<Resource, Wiring> wiring = new HashMap<Resource, Wiring>();
for (Resource res : wires.keySet()) {
wiring.put(res, new SimpleWiring(res, wires, invertedWires));
}
// Resolve the new resource
Map<Resource, List<Wire>> tempWires = resolver.resolve(new ResolveContext() {
@Override
public Collection<Resource> getMandatoryResources() {
return Collections.singleton(resource);
}
@Override
public Collection<Resource> getOptionalResources() {
return context.getOptionalResources();
}
@Override
public List<Capability> findProviders(Requirement requirement) {
return context.findProviders(requirement);
}
@Override
public int insertHostedCapability(List<Capability> capabilities, HostedCapability hostedCapability) {
return context.insertHostedCapability(capabilities, hostedCapability);
}
@Override
public boolean isEffective(Requirement requirement) {
return context.isEffective(requirement);
}
@Override
public Map<Resource, Wiring> getWirings() {
return wiring;
}
});
// Merge wiring
wires.putAll(tempWires);
}
return wires;
}
use of org.osgi.service.resolver.HostedCapability 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();
}
};
}
use of org.osgi.service.resolver.HostedCapability in project bnd by bndtools.
the class AbstractResolveContext method insertHostedCapability.
@Override
public int insertHostedCapability(List<Capability> caps, HostedCapability hc) {
init();
Integer prioObj = resourcePriorities.get(hc.getResource());
int priority = prioObj != null ? prioObj.intValue() : Integer.MAX_VALUE;
for (int i = 0; i < caps.size(); i++) {
Capability c = caps.get(i);
Integer otherPrioObj = resourcePriorities.get(c.getResource());
int otherPriority = otherPrioObj != null ? otherPrioObj.intValue() : 0;
if (otherPriority > priority) {
caps.add(i, hc);
return i;
}
}
int newIndex = caps.size();
// the List passed by Felix does not support the
// single-arg version of add()... it throws
// UnsupportedOperationException
caps.add(newIndex, hc);
return newIndex;
}
use of org.osgi.service.resolver.HostedCapability 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();
}
});
}
use of org.osgi.service.resolver.HostedCapability in project aries by apache.
the class ResolveContextTest method testInsertHostedCapability.
@Test
public void testInsertHostedCapability() throws Exception {
Activator prev = getActivator();
try {
Activator activator = createActivator();
setActivator(activator);
SubsystemResource res = new SubsystemResource(new File("."));
ResolveContext rc = new ResolveContext(res);
HostedCapability hc = EasyMock.createNiceMock(HostedCapability.class);
List<Capability> caps = new ArrayList<Capability>() {
// Must use add(idx, obj), get the other add() overloads to complain
@Override
public boolean add(Capability e) {
throw new UnsupportedOperationException();
}
@Override
public boolean addAll(Collection<? extends Capability> c) {
throw new UnsupportedOperationException();
}
@Override
public boolean addAll(int index, Collection<? extends Capability> c) {
throw new UnsupportedOperationException();
}
};
caps.add(0, EasyMock.createNiceMock(HostedCapability.class));
assertEquals(1, rc.insertHostedCapability(caps, hc));
assertSame(hc, caps.get(1));
} finally {
setActivator(prev);
}
}
Aggregations