use of org.osgi.resource.Resource in project karaf by apache.
the class BundleInstallSupportImpl method resolveBundles.
/* (non-Javadoc)
* @see org.apache.karaf.features.internal.service.Regions#resolveBundles(java.util.Set, java.util.Map, java.util.Map)
*/
@Override
public void resolveBundles(Set<Bundle> bundles, final Map<Resource, List<Wire>> wiring, Map<Resource, Bundle> resToBnd) {
// Make sure it's only used for us
final Thread thread = Thread.currentThread();
// Translate wiring
final Map<Bundle, Resource> bndToRes = new HashMap<>();
for (Resource res : resToBnd.keySet()) {
bndToRes.put(resToBnd.get(res), res);
}
// Hook
final ResolverHook hook = new ResolverHook() {
@Override
public void filterResolvable(Collection<BundleRevision> candidates) {
}
@Override
public void filterSingletonCollisions(BundleCapability singleton, Collection<BundleCapability> collisionCandidates) {
}
@Override
public void filterMatches(BundleRequirement requirement, Collection<BundleCapability> candidates) {
if (Thread.currentThread() == thread) {
// osgi.ee capabilities are provided by the system bundle, so just ignore those
if (ExecutionEnvironmentNamespace.EXECUTION_ENVIRONMENT_NAMESPACE.equals(requirement.getNamespace())) {
return;
}
Bundle sourceBundle = requirement.getRevision().getBundle();
Resource sourceResource = bndToRes.get(sourceBundle);
Set<Resource> wired = new HashSet<>();
// Get a list of allowed wired resources
wired.add(sourceResource);
for (Wire wire : wiring.get(sourceResource)) {
wired.add(wire.getProvider());
if (HostNamespace.HOST_NAMESPACE.equals(wire.getRequirement().getNamespace())) {
for (Wire hostWire : wiring.get(wire.getProvider())) {
wired.add(hostWire.getProvider());
}
}
}
// Remove candidates that are not allowed
for (Iterator<BundleCapability> candIter = candidates.iterator(); candIter.hasNext(); ) {
BundleCapability cand = candIter.next();
BundleRevision br = cand.getRevision();
if ((br.getTypes() & BundleRevision.TYPE_FRAGMENT) != 0) {
br = br.getWiring().getRequiredWires(null).get(0).getProvider();
}
Resource res = bndToRes.get(br.getBundle());
if (!wired.contains(br) && !wired.contains(res)) {
candIter.remove();
}
}
}
}
@Override
public void end() {
}
};
ResolverHookFactory factory = triggers -> hook;
ServiceRegistration<ResolverHookFactory> registration = systemBundleContext.registerService(ResolverHookFactory.class, factory, null);
try {
FrameworkWiring frameworkWiring = systemBundleContext.getBundle().adapt(FrameworkWiring.class);
frameworkWiring.resolveBundles(bundles);
} finally {
registration.unregister();
}
}
use of org.osgi.resource.Resource in project karaf by apache.
the class CandidateComparator method compare.
public int compare(Capability cap1, Capability cap2) {
int c = 0;
// Always prefer system bundle
if (cap1 instanceof BundleCapability && !(cap2 instanceof BundleCapability)) {
c = -1;
} else if (!(cap1 instanceof BundleCapability) && cap2 instanceof BundleCapability) {
c = 1;
}
// Always prefer mandatory resources
if (c == 0) {
int c1 = cost.applyAsInt(cap1.getResource());
int c2 = cost.applyAsInt(cap2.getResource());
c = Integer.compare(c1, c2);
}
// Compare revision capabilities.
if ((c == 0) && cap1.getNamespace().equals(BundleNamespace.BUNDLE_NAMESPACE)) {
c = compareNames(cap1, cap2, BundleNamespace.BUNDLE_NAMESPACE);
if (c == 0) {
c = compareVersions(cap1, cap2, BundleNamespace.CAPABILITY_BUNDLE_VERSION_ATTRIBUTE);
}
// Compare package capabilities.
} else if ((c == 0) && cap1.getNamespace().equals(PackageNamespace.PACKAGE_NAMESPACE)) {
c = compareNames(cap1, cap2, PackageNamespace.PACKAGE_NAMESPACE);
if (c == 0) {
c = compareVersions(cap1, cap2, PackageNamespace.CAPABILITY_VERSION_ATTRIBUTE);
// if same version, rather compare on the bundle version
if (c == 0) {
c = compareVersions(cap1, cap2, BundleNamespace.CAPABILITY_BUNDLE_VERSION_ATTRIBUTE);
}
}
// Compare feature capabilities
} else if ((c == 0) && cap1.getNamespace().equals(IdentityNamespace.IDENTITY_NAMESPACE)) {
c = compareNames(cap1, cap2, IdentityNamespace.IDENTITY_NAMESPACE);
if (c == 0) {
c = compareVersions(cap1, cap2, IdentityNamespace.CAPABILITY_VERSION_ATTRIBUTE);
}
}
if (c == 0) {
// We just want to have a deterministic heuristic
final Resource resource1 = cap1.getResource();
final Resource resource2 = cap2.getResource();
String n1 = ResolverUtil.getSymbolicName(resource1);
String n2 = ResolverUtil.getSymbolicName(resource2);
c = n1.compareTo(n2);
// so use this difference for deterministic heuristic
if (c == 0) {
String o1 = ResolverUtil.getOwnerName(resource1);
String o2 = ResolverUtil.getOwnerName(resource2);
if (o1 != null && o2 != null) {
// In case the owners are the same but with different version, prefer the latest one
// TODO: this may not be fully correct, as we'd need to separate names/versions
// TODO: and do a real version comparison
c = -o1.compareTo(o2);
}
}
}
return c;
}
use of org.osgi.resource.Resource in project karaf by apache.
the class FeatureResource method build.
public static FeatureResource build(Feature feature, String featureRange, Map<String, ? extends Resource> locToRes) throws BundleException {
FeatureResource resource = new FeatureResource(feature);
for (BundleInfo info : feature.getBundles()) {
if (!info.isDependency()) {
Resource res = locToRes.get(info.getLocation());
if (res == null) {
throw new IllegalStateException("Resource not found for url " + info.getLocation());
}
addIdentityRequirement(resource, res);
}
}
for (Dependency dep : feature.getDependencies()) {
if (!dep.isDependency()) {
addDependency(resource, dep, featureRange);
}
}
for (org.apache.karaf.features.Capability cap : feature.getCapabilities()) {
resource.addCapabilities(ResourceBuilder.parseCapability(resource, cap.getValue()));
}
for (org.apache.karaf.features.Requirement req : feature.getRequirements()) {
resource.addRequirements(ResourceBuilder.parseRequirement(resource, req.getValue()));
}
return resource;
}
use of org.osgi.resource.Resource 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.Resource in project karaf by apache.
the class SubsystemResolver method getResourceMapping.
/**
*
* @param resourceFilter
* @return map from resource to region name
*/
private Map<Resource, String> getResourceMapping(SimpleFilter resourceFilter) {
Map<String, String> flats = getFlatSubsystemsMap();
Map<Resource, List<Wire>> wiring = getWiring();
Map<Resource, String> resources = new HashMap<>();
SimpleFilter sf = createFilter(IDENTITY_NAMESPACE, "*", CAPABILITY_TYPE_ATTRIBUTE, TYPE_SUBSYSTEM);
if (wiring != null) {
for (Resource resource : wiring.keySet()) {
if (findMatchingCapability(resourceFilter, resource.getCapabilities(null)) != null) {
// Find the subsystem where this feature is installed
Wire wire = findMatchingWire(sf, wiring.get(resource));
if (wire != null) {
String region = (String) wire.getCapability().getAttributes().get(IDENTITY_NAMESPACE);
region = flats.get(region);
resources.put(resource, region);
}
}
}
}
return resources;
}
Aggregations