use of org.osgi.resource.Resource in project karaf by apache.
the class SubsystemResolveContext method computeDistances.
private Map<Resource, Integer> computeDistances(Resource root) {
Map<Resource, Integer> distance = new HashMap<>();
Set<Resource> settledNodes = new HashSet<>();
distance.put(root, 0);
List<Resource> unSettledNodes = new ArrayList<>();
unSettledNodes.add(root);
while (!unSettledNodes.isEmpty()) {
unSettledNodes.sort(Comparator.comparingInt(r -> distance.getOrDefault(r, Integer.MAX_VALUE)));
Resource node = unSettledNodes.remove(0);
if (settledNodes.add(node)) {
Map<Resource, Integer> edge = computeEdges(node);
for (Resource target : edge.keySet()) {
int d = distance.getOrDefault(node, Integer.MAX_VALUE) + edge.get(target);
distance.merge(target, d, Math::min);
if (!settledNodes.contains(target)) {
unSettledNodes.add(target);
}
}
}
}
return distance;
}
use of org.osgi.resource.Resource in project karaf by apache.
the class SubsystemResolveContext method findProviders.
@Override
public List<Capability> findProviders(Requirement requirement) {
List<Capability> caps = new ArrayList<>();
Region requirerRegion = getRegion(requirement.getResource());
if (requirerRegion != null) {
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);
}
}
}
// Use the digraph to prune non visible capabilities
Visitor visitor = new Visitor(caps);
requirerRegion.visitSubgraph(visitor);
Collection<Capability> allowed = visitor.getAllowed();
caps.retainAll(allowed);
// the parent one
if (caps.size() > 1) {
Set<Resource> providers = new HashSet<>();
for (Capability cap : caps) {
Resource resource = cap.getResource();
String id = ResolverUtil.getSymbolicName(resource) + "|" + ResolverUtil.getVersion(resource);
if (!providers.contains(resource)) {
Set<Resource> oldRes = new HashSet<>(providers);
providers.clear();
String r1 = getRegion(resource).getName();
boolean superceded = false;
for (Resource r : oldRes) {
String id2 = ResolverUtil.getSymbolicName(r) + "|" + ResolverUtil.getVersion(r);
if (id.equals(id2)) {
String r2 = getRegion(r).getName();
if (r1.equals(r2)) {
if (r instanceof BundleRevision) {
providers.add(r);
superceded = true;
} else if (resource instanceof BundleRevision) {
providers.add(resource);
} else {
throw new InternalError();
}
} else if (r1.startsWith(r2)) {
providers.add(r);
superceded = true;
} else if (r2.startsWith(r1)) {
providers.add(resource);
} else {
providers.add(r);
}
} else {
providers.add(r);
}
}
if (!superceded) {
providers.add(resource);
}
}
}
caps.removeIf(cap -> !providers.contains(cap.getResource()));
}
// Sort caps
if (distance != null) {
caps.sort(candidateComparator);
}
}
return caps;
}
use of org.osgi.resource.Resource in project karaf by apache.
the class SubsystemResolver method toJson.
private Object toJson(Map<Resource, List<Wire>> wiring) {
Map<String, List<Map<String, Object>>> wires = new HashMap<>();
for (Map.Entry<Resource, List<Wire>> reswiring : wiring.entrySet()) {
Resource resource = reswiring.getKey();
String id = toString(resource);
List<Map<String, Object>> reswires = new ArrayList<>();
for (Wire w : reswiring.getValue()) {
Map<String, Object> rw = new LinkedHashMap<>();
rw.put("requirement", toString(w.getRequirement()));
rw.put("capability", toString(w.getCapability()));
rw.put("requirer", toString(w.getRequirer()));
rw.put("provider", toString(w.getProvider()));
reswires.add(rw);
}
wires.put(id, reswires);
}
return wires;
}
use of org.osgi.resource.Resource 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.resource.Resource in project karaf by apache.
the class OfflineResolver method readRepository.
private static Repository readRepository(Object repository) throws BundleException {
List<Resource> resources = new ArrayList<>();
Collection<Map<String, List<String>>> metadatas;
if (repository instanceof Map) {
metadatas = ((Map<String, Map<String, List<String>>>) repository).values();
} else {
metadatas = (Collection<Map<String, List<String>>>) repository;
}
for (Map<String, List<String>> metadata : metadatas) {
ResourceImpl res = new ResourceImpl();
for (String cap : metadata.get("capabilities")) {
res.addCapabilities(ResourceBuilder.parseCapability(res, cap));
}
if (metadata.containsKey("requirements")) {
for (String req : metadata.get("requirements")) {
res.addRequirements(ResourceBuilder.parseRequirement(res, req));
}
}
resources.add(res);
}
return new BaseRepository(resources);
}
Aggregations