Search in sources :

Example 36 with Resource

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();
    }
}
Also used : DigraphHelper(org.apache.karaf.features.internal.region.DigraphHelper) ResolverHookFactory(org.osgi.framework.hooks.resolver.ResolverHookFactory) Region(org.eclipse.equinox.region.Region) RegionFilterBuilder(org.eclipse.equinox.region.RegionFilterBuilder) ExecutionEnvironmentNamespace(org.osgi.framework.namespace.ExecutionEnvironmentNamespace) LoggerFactory(org.slf4j.LoggerFactory) HashMap(java.util.HashMap) FeaturesService(org.apache.karaf.features.FeaturesService) FrameworkListener(org.osgi.framework.FrameworkListener) HashSet(java.util.HashSet) BundleCapability(org.osgi.framework.wiring.BundleCapability) Map(java.util.Map) Bundle(org.osgi.framework.Bundle) BundleRevision(org.osgi.framework.wiring.BundleRevision) BundleException(org.osgi.framework.BundleException) ServiceRegistration(org.osgi.framework.ServiceRegistration) Logger(org.slf4j.Logger) Iterator(java.util.Iterator) InvalidSyntaxException(org.osgi.framework.InvalidSyntaxException) Resource(org.osgi.resource.Resource) Collection(java.util.Collection) FrameworkStartLevel(org.osgi.framework.startlevel.FrameworkStartLevel) Feature(org.apache.karaf.features.Feature) FrameworkEvent(org.osgi.framework.FrameworkEvent) BundleRequirement(org.osgi.framework.wiring.BundleRequirement) Set(java.util.Set) IOException(java.io.IOException) ResolverHook(org.osgi.framework.hooks.resolver.ResolverHook) FileInputStream(java.io.FileInputStream) File(java.io.File) BundleContext(org.osgi.framework.BundleContext) BundleStartLevel(org.osgi.framework.startlevel.BundleStartLevel) CountDownLatch(java.util.concurrent.CountDownLatch) List(java.util.List) RegionDigraph(org.eclipse.equinox.region.RegionDigraph) FrameworkWiring(org.osgi.framework.wiring.FrameworkWiring) Wire(org.osgi.resource.Wire) BundleUtils(org.apache.karaf.util.bundles.BundleUtils) HostNamespace(org.osgi.framework.namespace.HostNamespace) InputStream(java.io.InputStream) ResolverHookFactory(org.osgi.framework.hooks.resolver.ResolverHookFactory) HashMap(java.util.HashMap) ResolverHook(org.osgi.framework.hooks.resolver.ResolverHook) Bundle(org.osgi.framework.Bundle) Resource(org.osgi.resource.Resource) FrameworkWiring(org.osgi.framework.wiring.FrameworkWiring) Wire(org.osgi.resource.Wire) BundleRequirement(org.osgi.framework.wiring.BundleRequirement) BundleRevision(org.osgi.framework.wiring.BundleRevision) Collection(java.util.Collection) BundleCapability(org.osgi.framework.wiring.BundleCapability) HashSet(java.util.HashSet)

Example 37 with Resource

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;
}
Also used : Resource(org.osgi.resource.Resource) BundleCapability(org.osgi.framework.wiring.BundleCapability)

Example 38 with Resource

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;
}
Also used : BundleInfo(org.apache.karaf.features.BundleInfo) Resource(org.osgi.resource.Resource) Dependency(org.apache.karaf.features.Dependency)

Example 39 with 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;
                    }
                });
            }
        }
    }
}
Also used : Requirement(org.osgi.resource.Requirement) Capability(org.osgi.resource.Capability) SimpleFilter(org.apache.karaf.features.internal.resolver.SimpleFilter) Resource(org.osgi.resource.Resource) Wire(org.osgi.resource.Wire) DictionaryAsMap(org.apache.felix.utils.collections.DictionaryAsMap)

Example 40 with 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;
}
Also used : SimpleFilter(org.apache.karaf.features.internal.resolver.SimpleFilter) Resource(org.osgi.resource.Resource) Wire(org.osgi.resource.Wire)

Aggregations

Resource (org.osgi.resource.Resource)201 Capability (org.osgi.resource.Capability)62 Requirement (org.osgi.resource.Requirement)62 ArrayList (java.util.ArrayList)57 List (java.util.List)39 HashMap (java.util.HashMap)32 File (java.io.File)27 Collection (java.util.Collection)24 Wire (org.osgi.resource.Wire)24 ResourceBuilder (aQute.bnd.osgi.resource.ResourceBuilder)23 Map (java.util.Map)22 CapReqBuilder (aQute.bnd.osgi.resource.CapReqBuilder)18 URI (java.net.URI)18 HashSet (java.util.HashSet)18 BndEditModel (aQute.bnd.build.model.BndEditModel)15 MockRegistry (test.lib.MockRegistry)15 Version (org.osgi.framework.Version)14 BundleRevision (org.osgi.framework.wiring.BundleRevision)14 Repository (org.osgi.service.repository.Repository)14 IdentityCapability (aQute.bnd.osgi.resource.ResourceUtils.IdentityCapability)13