Search in sources :

Example 6 with BundleCapability

use of org.osgi.framework.wiring.BundleCapability in project aries by apache.

the class BundleWiringState method getRevisionWiring.

private CompositeData getRevisionWiring(BundleRevision revision, int revisionID, String namespace, Map<BundleRevision, Integer> revisionIDMap) {
    BundleWiring wiring = revision.getWiring();
    List<BundleCapability> capabilities = wiring.getCapabilities(namespace);
    List<BundleRequirement> requirements = wiring.getRequirements(namespace);
    List<BundleWire> providedWires = wiring.getProvidedWires(namespace);
    List<BundleWire> requiredWires = wiring.getRequiredWires(namespace);
    BundleWiringData data = new BundleWiringData(wiring.getBundle().getBundleId(), revisionID, capabilities, requirements, providedWires, requiredWires, revisionIDMap);
    return data.toCompositeData();
}
Also used : BundleWiring(org.osgi.framework.wiring.BundleWiring) BundleWiringData(org.apache.aries.jmx.codec.BundleWiringData) BundleCapability(org.osgi.framework.wiring.BundleCapability) BundleWire(org.osgi.framework.wiring.BundleWire) BundleRequirement(org.osgi.framework.wiring.BundleRequirement)

Example 7 with BundleCapability

use of org.osgi.framework.wiring.BundleCapability in project aries by apache.

the class Aries1383Test method test11.

/*
	 * (11) Subsystem with apache-aries-provision-dependencies:=resolve undergoes 
	 * the following state transitions when starting fails due to a runtime 
	 * resolution failure: INSTALLING -> INSTALLED -> RESOLVING -> INSTALLED.
	 */
@Test
public void test11() throws Exception {
    Subsystem root = getRootSubsystem();
    subsystemEvents.clear();
    Subsystem subsystem = root.install(APPLICATION_DEPENDENCY_IN_ARCHIVE, applicationDependencyInArchive());
    ServiceRegistration<ResolverHookFactory> registration = bundleContext.registerService(ResolverHookFactory.class, new ResolverHookFactory() {

        @Override
        public ResolverHook begin(Collection<BundleRevision> triggers) {
            return new ResolverHook() {

                @Override
                public void filterResolvable(Collection<BundleRevision> candidates) {
                    for (Iterator<BundleRevision> i = candidates.iterator(); i.hasNext(); ) {
                        BundleRevision revision = i.next();
                        if (revision.getSymbolicName().equals(BUNDLE_B)) {
                            i.remove();
                        }
                    }
                }

                @Override
                public void filterSingletonCollisions(BundleCapability singleton, Collection<BundleCapability> collisionCandidates) {
                // Nothing.
                }

                @Override
                public void filterMatches(BundleRequirement requirement, Collection<BundleCapability> candidates) {
                // Nothing.
                }

                @Override
                public void end() {
                // Nothing.
                }
            };
        }
    }, null);
    try {
        subsystem.start();
        stopSubsystemSilently(subsystem);
        fail("Subsystem should not have started");
    } catch (SubsystemException e) {
        e.printStackTrace();
        long id = lastSubsystemId();
        assertEvent(id, APPLICATION_DEPENDENCY_IN_ARCHIVE, Version.emptyVersion, SubsystemConstants.SUBSYSTEM_TYPE_APPLICATION, State.INSTALLING, subsystemEvents.poll(id, 5000), ServiceEvent.REGISTERED);
        assertEvent(id, APPLICATION_DEPENDENCY_IN_ARCHIVE, Version.emptyVersion, SubsystemConstants.SUBSYSTEM_TYPE_APPLICATION, State.INSTALLED, subsystemEvents.poll(id, 5000), ServiceEvent.MODIFIED);
        assertEvent(id, APPLICATION_DEPENDENCY_IN_ARCHIVE, Version.emptyVersion, SubsystemConstants.SUBSYSTEM_TYPE_APPLICATION, State.RESOLVING, subsystemEvents.poll(id, 5000), ServiceEvent.MODIFIED);
        assertEvent(id, APPLICATION_DEPENDENCY_IN_ARCHIVE, Version.emptyVersion, SubsystemConstants.SUBSYSTEM_TYPE_APPLICATION, State.INSTALLED, subsystemEvents.poll(id, 5000), ServiceEvent.MODIFIED);
    } finally {
        registration.unregister();
        uninstallSubsystemSilently(subsystem);
    }
}
Also used : ResolverHookFactory(org.osgi.framework.hooks.resolver.ResolverHookFactory) ResolverHook(org.osgi.framework.hooks.resolver.ResolverHook) AriesSubsystem(org.apache.aries.subsystem.AriesSubsystem) Subsystem(org.osgi.service.subsystem.Subsystem) SubsystemException(org.osgi.service.subsystem.SubsystemException) BundleRevision(org.osgi.framework.wiring.BundleRevision) Iterator(java.util.Iterator) BundleCapability(org.osgi.framework.wiring.BundleCapability) BundleRequirement(org.osgi.framework.wiring.BundleRequirement) SubsystemTest(org.apache.aries.subsystem.itests.SubsystemTest) Test(org.junit.Test)

Example 8 with BundleCapability

use of org.osgi.framework.wiring.BundleCapability in project aries by apache.

the class WovenClassListener method modified.

@Override
public void modified(WovenClass wovenClass) {
    if (wovenClass.getState() != WovenClass.TRANSFORMED) {
        // the defined state is reached.
        return;
    }
    List<String> dynamicImports = wovenClass.getDynamicImports();
    if (dynamicImports.isEmpty()) {
        // Nothing to do if there are no dynamic imports.
        return;
    }
    BundleWiring wiring = wovenClass.getBundleWiring();
    Bundle bundle = wiring.getBundle();
    BundleRevision revision = bundle.adapt(BundleRevision.class);
    BundleConstituent constituent = new BundleConstituent(null, revision);
    Collection<BasicSubsystem> basicSubsystems = subsystems.getSubsystemsByConstituent(constituent);
    BasicSubsystem subsystem = basicSubsystems.iterator().next();
    // Find the scoped subsystem in the region.
    subsystem = scopedSubsystem(subsystem);
    if (subsystem.getSubsystemId() == 0) {
        // The root subsystem needs no sharing policy.
        return;
    }
    if (EnumSet.of(Subsystem.State.INSTALLING, Subsystem.State.INSTALLED).contains(subsystem.getState())) {
        // The scoped subsystem must be resolved before adding dynamic 
        // package imports to the sharing policy in order to minimize 
        // unpredictable wirings. Resolving the scoped subsystem will also
        // resolve all of the unscoped subsystems in the region.
        AccessController.doPrivileged(new StartAction(subsystem, subsystem, subsystem, Restriction.RESOLVE_ONLY));
    }
    Bundle systemBundle = context.getBundle(org.osgi.framework.Constants.SYSTEM_BUNDLE_LOCATION);
    FrameworkWiring frameworkWiring = systemBundle.adapt(FrameworkWiring.class);
    // The following map tracks all of the necessary updates as each dynamic
    // import is processed. The key is the tail region of the connection 
    // whose filter needs updating.
    Map<Region, RegionUpdaterInfo> updates = new HashMap<Region, RegionUpdaterInfo>();
    for (String dynamicImport : dynamicImports) {
        // For each dynamic import, collect the necessary update information.
        DynamicImportPackageHeader header = new DynamicImportPackageHeader(dynamicImport);
        List<DynamicImportPackageRequirement> requirements = header.toRequirements(revision);
        for (DynamicImportPackageRequirement requirement : requirements) {
            Collection<BundleCapability> providers = frameworkWiring.findProviders(requirement);
            if (providers.isEmpty()) {
                // import, no updates are made.
                continue;
            }
            addSharingPolicyUpdates(requirement, subsystem, providers, updates);
        }
    }
    // Now update each sharing policy only once.
    for (RegionUpdaterInfo update : updates.values()) {
        RegionUpdater updater = new RegionUpdater(update.tail(), update.head());
        try {
            updater.addRequirements(update.requirements());
        } catch (IllegalStateException e) {
        // Something outside of the subsystems implementation has
        // deleted the edge between the parent and child subsystems.
        // Assume the dynamic import sharing policy is being handled
        // elsewhere. See ARIES-1429.
        } catch (Exception e) {
            throw new SubsystemException(e);
        }
    }
}
Also used : HashMap(java.util.HashMap) Bundle(org.osgi.framework.Bundle) BundleWiring(org.osgi.framework.wiring.BundleWiring) SubsystemException(org.osgi.service.subsystem.SubsystemException) FrameworkWiring(org.osgi.framework.wiring.FrameworkWiring) SubsystemException(org.osgi.service.subsystem.SubsystemException) DynamicImportPackageHeader(org.apache.aries.subsystem.core.archive.DynamicImportPackageHeader) BundleConstituent(org.apache.aries.subsystem.core.internal.BundleResourceInstaller.BundleConstituent) DynamicImportPackageRequirement(org.apache.aries.subsystem.core.archive.DynamicImportPackageRequirement) BundleRevision(org.osgi.framework.wiring.BundleRevision) Region(org.eclipse.equinox.region.Region) FilteredRegion(org.eclipse.equinox.region.RegionDigraph.FilteredRegion) BundleCapability(org.osgi.framework.wiring.BundleCapability)

Example 9 with BundleCapability

use of org.osgi.framework.wiring.BundleCapability 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 10 with BundleCapability

use of org.osgi.framework.wiring.BundleCapability 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)

Aggregations

BundleCapability (org.osgi.framework.wiring.BundleCapability)27 Bundle (org.osgi.framework.Bundle)11 BundleRevision (org.osgi.framework.wiring.BundleRevision)10 BundleWiring (org.osgi.framework.wiring.BundleWiring)9 HashMap (java.util.HashMap)8 ArrayList (java.util.ArrayList)7 BundleRequirement (org.osgi.framework.wiring.BundleRequirement)7 CompositeData (javax.management.openmbean.CompositeData)6 BundleWire (org.osgi.framework.wiring.BundleWire)6 List (java.util.List)4 Map (java.util.Map)4 Region (org.eclipse.equinox.region.Region)3 Test (org.junit.Test)3 HashSet (java.util.HashSet)2 Iterator (java.util.Iterator)2 Set (java.util.Set)2 TreeMap (java.util.TreeMap)2 AbstractIntegrationTest (org.apache.aries.jmx.AbstractIntegrationTest)2 VersionRange (org.apache.felix.utils.version.VersionRange)2 PackageVersion (org.apache.karaf.packages.core.PackageVersion)2