use of org.osgi.service.resolver.ResolveContext 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();
}
});
}
Aggregations