use of org.osgi.resource.Requirement in project karaf by apache.
the class AggregateRepository method findProviders.
@Override
public Map<Requirement, Collection<Capability>> findProviders(Collection<? extends Requirement> requirements) {
Map<Requirement, Collection<Capability>> result = new HashMap<>();
for (Requirement requirement : requirements) {
List<Capability> caps = new ArrayList<>();
for (Repository repository : repositories) {
Map<Requirement, Collection<Capability>> resMap = repository.findProviders(Collections.singleton(requirement));
Collection<Capability> res = resMap != null ? resMap.get(requirement) : null;
if (res != null) {
caps.addAll(res);
}
}
result.put(requirement, caps);
}
return result;
}
use of org.osgi.resource.Requirement 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.Requirement in project karaf by apache.
the class Subsystem method doBuild.
private void doBuild(Collection<Feature> features, boolean mandatory) throws Exception {
for (Subsystem child : children) {
child.doBuild(features, true);
}
if (feature != null) {
for (Dependency dep : feature.getDependencies()) {
Subsystem ss = this;
while (!ss.isAcceptDependencies()) {
ss = ss.getParent();
}
ss.requireFeature(dep.getName(), dep.getVersion(), false);
}
for (Conditional cond : feature.getConditional()) {
Feature fcond = cond.asFeature();
String ssName = this.name + "#" + (fcond.hasVersion() ? fcond.getName() + "-" + fcond.getVersion() : fcond.getName());
Subsystem fs = getChild(ssName);
if (fs == null) {
fs = new Subsystem(ssName, fcond, this, true);
fs.doBuild(features, false);
installable.add(fs);
children.add(fs);
}
}
}
List<Requirement> processed = new ArrayList<>();
while (true) {
List<Requirement> requirements = getRequirements(IDENTITY_NAMESPACE);
requirements.addAll(dependentFeatures);
requirements.removeAll(processed);
if (requirements.isEmpty()) {
break;
}
for (Requirement requirement : requirements) {
String name = (String) requirement.getAttributes().get(IDENTITY_NAMESPACE);
String type = (String) requirement.getAttributes().get(CAPABILITY_TYPE_ATTRIBUTE);
VersionRange range = (VersionRange) requirement.getAttributes().get(CAPABILITY_VERSION_ATTRIBUTE);
if (TYPE_FEATURE.equals(type)) {
for (Feature feature : features) {
if (feature.getName().equals(name) && (range == null || range.contains(VersionTable.getVersion(feature.getVersion())))) {
if (feature != this.feature) {
String ssName = this.name + "#" + (feature.hasVersion() ? feature.getName() + "-" + feature.getVersion() : feature.getName());
Subsystem fs = getChild(ssName);
if (fs == null) {
fs = new Subsystem(ssName, feature, this, mandatory && !SubsystemResolveContext.isOptional(requirement));
fs.build(features);
installable.add(fs);
children.add(fs);
}
}
}
}
}
processed.add(requirement);
}
}
}
use of org.osgi.resource.Requirement in project karaf by apache.
the class FeatureResource method build.
public static FeatureResource build(Feature feature, Conditional conditional, String featureRange, Map<String, ? extends Resource> locToRes) throws BundleException {
Feature fcond = conditional.asFeature();
FeatureResource resource = build(fcond, featureRange, locToRes);
for (String cond : conditional.getCondition()) {
if (cond.startsWith("req:")) {
cond = cond.substring("req:".length());
List<Requirement> reqs = ResourceBuilder.parseRequirement(resource, cond);
resource.addRequirements(reqs);
} else {
org.apache.karaf.features.internal.model.Dependency dep = new org.apache.karaf.features.internal.model.Dependency();
String[] p = cond.split("/");
dep.setName(p[0]);
if (p.length > 1) {
dep.setVersion(p[1]);
}
addDependency(resource, dep, featureRange, true);
}
}
org.apache.karaf.features.internal.model.Dependency dep = new org.apache.karaf.features.internal.model.Dependency();
dep.setName(feature.getName());
dep.setVersion(feature.getVersion());
addDependency(resource, dep, featureRange, true);
return resource;
}
use of org.osgi.resource.Requirement in project karaf by apache.
the class ResourceBuilder method convertImportService.
private static List<Requirement> convertImportService(List<ParsedHeaderClause> clauses, Resource resource) throws BundleException {
try {
List<Requirement> reqList = new ArrayList<>();
for (ParsedHeaderClause clause : clauses) {
for (String path : clause.paths) {
String multiple = clause.dirs.get("multiple");
String avail = clause.dirs.get("availability");
String filter = (String) clause.attrs.get("filter");
Map<String, String> dirs = new LinkedHashMap<>();
dirs.put(ServiceNamespace.REQUIREMENT_EFFECTIVE_DIRECTIVE, ServiceNamespace.EFFECTIVE_ACTIVE);
if ("optional".equals(avail)) {
dirs.put(ServiceNamespace.REQUIREMENT_RESOLUTION_DIRECTIVE, ServiceNamespace.RESOLUTION_OPTIONAL);
}
if ("true".equals(multiple)) {
dirs.put(ServiceNamespace.REQUIREMENT_CARDINALITY_DIRECTIVE, ServiceNamespace.CARDINALITY_MULTIPLE);
}
if (filter == null) {
filter = "(" + Constants.OBJECTCLASS + "=" + path + ")";
} else if (!filter.startsWith("(") && !filter.endsWith(")")) {
filter = "(&(" + Constants.OBJECTCLASS + "=" + path + ")(" + filter + "))";
} else {
filter = "(&(" + Constants.OBJECTCLASS + "=" + path + ")" + filter + ")";
}
dirs.put(ServiceNamespace.REQUIREMENT_FILTER_DIRECTIVE, filter);
reqList.add(new RequirementImpl(resource, ServiceNamespace.SERVICE_NAMESPACE, dirs, Collections.emptyMap(), SimpleFilter.parse(filter)));
}
}
return reqList;
} catch (Exception ex) {
throw new BundleException("Error creating requirement: " + ex, ex);
}
}
Aggregations