use of org.osgi.resource.Capability 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.Capability in project karaf by apache.
the class XmlRepository method addResource.
@Override
protected void addResource(Resource resource) {
List<Capability> identities = resource.getCapabilities(IDENTITY_NAMESPACE);
if (identities.isEmpty()) {
throw new IllegalStateException("Invalid resource: a capability with 'osgi.identity' namespace is required");
} else if (identities.size() > 1) {
throw new IllegalStateException("Invalid resource: multiple 'osgi.identity' capabilities found");
}
Capability identity = identities.get(0);
Object name = identity.getAttributes().get(IDENTITY_NAMESPACE);
Object type = identity.getAttributes().get(CAPABILITY_TYPE_ATTRIBUTE);
Object vers = identity.getAttributes().get(CAPABILITY_VERSION_ATTRIBUTE);
if (!String.class.isInstance(name) || !String.class.isInstance(type) || !Version.class.isInstance(vers)) {
throw new IllegalStateException("Invalid osgi.identity capability: " + identity);
}
if (!hasResource((String) type, (String) name, (Version) vers)) {
super.addResource(resource);
}
}
use of org.osgi.resource.Capability in project karaf by apache.
the class CapabilitySet method dump.
public void dump() {
for (Entry<String, Map<Object, Set<Capability>>> entry : indices.entrySet()) {
boolean header1 = false;
for (Entry<Object, Set<Capability>> entry2 : entry.getValue().entrySet()) {
boolean header2 = false;
for (Capability cap : entry2.getValue()) {
if (!header1) {
System.out.println(entry.getKey() + ":");
header1 = true;
}
if (!header2) {
System.out.println(" " + entry2.getKey());
header2 = true;
}
System.out.println(" " + cap);
}
}
}
}
use of org.osgi.resource.Capability in project karaf by apache.
the class SubsystemResolver method associateFragments.
private void associateFragments() {
SimpleFilter sf = createFilter(IDENTITY_NAMESPACE, "*", CAPABILITY_TYPE_ATTRIBUTE, TYPE_SUBSYSTEM);
for (Map.Entry<Resource, List<Wire>> entry : wiring.entrySet()) {
final Resource resource = entry.getKey();
final Requirement requirement = getSubsystemRequirement(resource);
if (ResolverUtil.isFragment(resource) && requirement != null) {
List<Wire> wires = entry.getValue();
final Resource host = wires.get(0).getProvider();
final Wire wire = findMatchingWire(sf, wiring.get(host));
if (wire != null) {
wires.add(new Wire() {
@Override
public Capability getCapability() {
return wire.getCapability();
}
@Override
public Requirement getRequirement() {
return requirement;
}
@Override
public Resource getProvider() {
return wire.getProvider();
}
@Override
public Resource getRequirer() {
return resource;
}
});
}
}
}
}
use of org.osgi.resource.Capability in project aries by apache.
the class SubsystemResource method findContent.
private Resource findContent(Requirement requirement) throws BundleException, IOException, InvalidSyntaxException, URISyntaxException {
Map<Requirement, Collection<Capability>> map;
// the case of a persisted subsystem.
if (isUnscoped()) {
map = Activator.getInstance().getSystemRepository().findProviders(Collections.singleton(requirement));
if (map.containsKey(requirement)) {
Collection<Capability> capabilities = map.get(requirement);
for (Capability capability : capabilities) {
Resource provider = capability.getResource();
if (provider instanceof BundleRevision) {
if (getRegion().contains(((BundleRevision) provider).getBundle())) {
return provider;
}
} else if (provider instanceof BasicSubsystem) {
if (getRegion().equals(((BasicSubsystem) provider).getRegion())) {
return provider;
}
}
}
}
}
// First search the local repository.
map = resource.getLocalRepository().findProviders(Collections.singleton(requirement));
Collection<Capability> capabilities = map.get(requirement);
if (capabilities.isEmpty()) {
// Nothing found in the local repository so search the repository services.
capabilities = new RepositoryServiceRepository().findProviders(requirement);
}
if (capabilities.isEmpty()) {
// Nothing found period.
return null;
}
for (Capability capability : capabilities) {
if (!IdentityNamespace.TYPE_FRAGMENT.equals(capability.getAttributes().get(IdentityNamespace.CAPABILITY_TYPE_ATTRIBUTE))) {
// See ARIES-1425.
return capability.getResource();
}
}
// Nothing here but fragment bundles. Return the first one.
return capabilities.iterator().next().getResource();
}
Aggregations