Package org.osgi.framework.wiring

Examples of org.osgi.framework.wiring.BundleRevision


            matchBundleWithOptionalImport(bundle, toRefresh);
        }
    }

    private void matchBundleWithOptionalImport(Bundle bundle, Set<Bundle> toRefresh) {
        BundleRevision revision = bundle.adapt(BundleRevision.class);
        for (BundleRequirement req : revision.getDeclaredRequirements(PackageNamespace.PACKAGE_NAMESPACE)) {
            if (PackageNamespace.RESOLUTION_OPTIONAL.equals(req.getDirectives().get(PackageNamespace.REQUIREMENT_RESOLUTION_DIRECTIVE))) {

                // Find the wire for this optional package import
                BundleWiring wiring = bundle.adapt(BundleWiring.class);
                BundleWire reqwire = null;
                if (wiring != null) {
                    for (BundleWire wire : wiring.getRequiredWires(PackageNamespace.PACKAGE_NAMESPACE)) {
                        if (req.equals(wire.getRequirement())) {
                            BundleCapability cap = wire.getCapability();
                            BundleRevision provider = wire.getProvider();
                            LOGGER.debug("Optional requirement {} from {} wires to capability {} provided by {}", req, bundle, cap, provider);
                            reqwire = wire;
                            break;
                        }
                    }
                }

                // If the requirement is already wired we don't need to do anything
                // the refresh algorithm will compute the transitive graph of refresh candidates
                if (reqwire == null) {

                    // Compute the set of possible providers
                    for (Bundle provider : toRefresh) {
                        BundleRevision providerRev = provider.adapt(BundleRevision.class);
                        if (providerRev != null) {
                            for (BundleCapability cap : providerRev.getDeclaredCapabilities(PackageNamespace.PACKAGE_NAMESPACE)) {
                                if (req.matches(cap)) {
                                    LOGGER.info("Found possible provider for unwired optional requirement {} from {}: {}", req, bundle, provider);
                                    toRefresh.add(bundle);
                                    return;
                                }
View Full Code Here


    /* (non-Javadoc)
     * @see org.osgi.jmx.framework.BundleRevisionsStateMBean#getCurrentRevisionDeclaredRequirements(long, java.lang.String)
     */
    public CompositeData[] getCurrentRevisionDeclaredRequirements(long bundleId, String namespace) throws IOException {
        Bundle bundle = FrameworkUtils.resolveBundle(bundleContext, bundleId);
        BundleRevision revision = bundle.adapt(BundleRevision.class);

        return BundleWiringData.getRequirementsCompositeData(revision.getDeclaredRequirements(namespace));
    }
View Full Code Here

    /* (non-Javadoc)
     * @see org.osgi.jmx.framework.BundleRevisionsStateMBean#getCurrentRevisionDeclaredCapabilities(long, java.lang.String)
     */
    public CompositeData[] getCurrentRevisionDeclaredCapabilities(long bundleId, String namespace) throws IOException {
        Bundle bundle = FrameworkUtils.resolveBundle(bundleContext, bundleId);
        BundleRevision revision = bundle.adapt(BundleRevision.class);

        return BundleWiringData.getCapabilitiesCompositeData(revision.getDeclaredCapabilities(namespace));
    }
View Full Code Here

    /* (non-Javadoc)
     * @see org.osgi.jmx.framework.BundleRevisionsStateMBean#getCurrentWiring(long, java.lang.String)
     */
    public CompositeData getCurrentWiring(long bundleId, String namespace) throws IOException {
        Bundle bundle = FrameworkUtils.resolveBundle(bundleContext, bundleId);
        BundleRevision currentRevision = bundle.adapt(BundleRevision.class);
        Map<BundleRevision, Integer> revisionIDMap = getCurrentRevisionTransitiveRevisionsClosure(bundleId, namespace);
        return getRevisionWiring(currentRevision, 0, namespace, revisionIDMap);
    }
View Full Code Here

    // The current revision being passed in always gets assigned revision ID 0
    // All the other revision IDs unique, but don't increase monotonous.
    private Map<BundleRevision, Integer> getCurrentRevisionTransitiveRevisionsClosure(long rootBundleId, String namespace) throws IOException {
        Bundle rootBundle = FrameworkUtils.resolveBundle(bundleContext, rootBundleId);
        BundleRevision rootRevision = rootBundle.adapt(BundleRevision.class);
        return getRevisionTransitiveClosure(rootRevision, namespace);
    }
View Full Code Here

        if (wiring == null)
            return;

        List<BundleWire> requiredWires = wiring.getRequiredWires(namespace);
        for (BundleWire wire : requiredWires) {
            BundleRevision revision = wire.getCapability().getRevision();
            if (!allRevisions.containsKey(revision)) {
                populateTransitiveRevisions(namespace, revision, allRevisions);
            }
        }

        List<BundleWire> providedWires = wiring.getProvidedWires(namespace);
        for (BundleWire wire : providedWires) {
            BundleRevision revision = wire.getRequirement().getRevision();
            if (!allRevisions.containsKey(revision)) {
                populateTransitiveRevisions(namespace, revision, allRevisions);
            }
        }
    }
View Full Code Here

            populateTransitiveRevisions(namespace, revision, revisionIDMap);
        }

        // Set the root current revision ID to 0,
        // TODO check if there is already a revision with ID 0 and if so swap them. Quite a small chance that this will be needed
        BundleRevision revision = bundle.adapt(BundleRevision.class);
        revisionIDMap.put(revision, 0);

        TabularData td = new TabularDataSupport(BundleWiringStateMBean.BUNDLES_WIRING_TYPE);
        for (Map.Entry<BundleRevision, Integer> entry : revisionIDMap.entrySet()) {
            td.put(getRevisionWiring(entry.getKey(), entry.getValue(), namespace, revisionIDMap));
View Full Code Here

                // Populate mandatory revisions; since these are mandatory
                // revisions, failure throws a resolve exception.
                for (Iterator<BundleRevision> it = mandatoryRevisions.iterator();
                    it.hasNext(); )
                {
                    BundleRevision br = it.next();
                    if (Util.isFragment(br) || (br.getWiring() == null))
                    {
                        allCandidates.populate(state, br, Candidates.MANDATORY);
                    }
                    else
                    {
                        it.remove();
                    }
                }

                // Populate optional revisions; since these are optional
                // revisions, failure does not throw a resolve exception.
                for (BundleRevision br : optionalRevisions)
                {
                    boolean isFragment = Util.isFragment(br);
                    if (isFragment || (br.getWiring() == null))
                    {
                        allCandidates.populate(state, br, Candidates.OPTIONAL);
                    }
                }

                // Populate ondemand fragments; since these are optional
                // revisions, failure does not throw a resolve exception.
                for (BundleRevision br : ondemandFragments)
                {
                    boolean isFragment = Util.isFragment(br);
                    if (isFragment)
                    {
                        allCandidates.populate(state, br, Candidates.ON_DEMAND);
                    }
                }

                // Merge any fragments into hosts.
                allCandidates.prepare(getResolvedSingletons(state));

                // Create a combined list of populated revisions; for
                // optional revisions. We do not need to consider ondemand
                // fragments, since they will only be pulled in if their
                // host is already present.
                Set<BundleRevision> allRevisions =
                    new HashSet<BundleRevision>(mandatoryRevisions);
                for (BundleRevision br : optionalRevisions)
                {
                    if (allCandidates.isPopulated(br))
                    {
                        allRevisions.add(br);
                    }
                }

                // Record the initial candidate permutation.
                m_usesPermutations.add(allCandidates);

                ResolveException rethrow = null;

                // If a populated revision is a fragment, then its host
                // must ultimately be verified, so store its host requirement
                // to use for package space calculation.
                Map<BundleRevision, List<BundleRequirement>> hostReqs =
                    new HashMap<BundleRevision, List<BundleRequirement>>();
                for (BundleRevision br : allRevisions)
                {
                    if (Util.isFragment(br))
                    {
                        hostReqs.put(
                            br,
                            br.getDeclaredRequirements(BundleRevision.HOST_NAMESPACE));
                    }
                }

                do
                {
                    rethrow = null;

                    revisionPkgMap.clear();
                    m_packageSourcesCache.clear();

                    allCandidates = (m_usesPermutations.size() > 0)
                        ? m_usesPermutations.remove(0)
                        : m_importPermutations.remove(0);
//allCandidates.dump();

                    for (BundleRevision br : allRevisions)
                    {
                        BundleRevision target = br;

                        // If we are resolving a fragment, then get its
                        // host candidate and verify it instead.
                        List<BundleRequirement> hostReq = hostReqs.get(br);
                        if (hostReq != null)
                        {
                            target = allCandidates.getCandidates(hostReq.get(0))
                                .iterator().next().getRevision();
                        }

                        calculatePackageSpaces(
                            allCandidates.getWrappedHost(target), allCandidates, revisionPkgMap,
                            new HashMap<BundleCapability, List<BundleRevision>>(), new HashSet<BundleRevision>());
//System.out.println("+++ PACKAGE SPACES START +++");
//dumpRevisionPkgMap(revisionPkgMap);
//System.out.println("+++ PACKAGE SPACES END +++");

                        try
                        {
                            checkPackageSpaceConsistency(
                                false, allCandidates.getWrappedHost(target),
                                allCandidates, revisionPkgMap, new HashMap<BundleRevision, Object>());
                        }
                        catch (ResolveException ex)
                        {
                            rethrow = ex;
                        }
                    }
                }
                while ((rethrow != null)
                    && ((m_usesPermutations.size() > 0) || (m_importPermutations.size() > 0)));

                // If there is a resolve exception, then determine if an
                // optionally resolved revision is to blame (typically a fragment).
                // If so, then remove the optionally resolved resolved and try
                // again; otherwise, rethrow the resolve exception.
                if (rethrow != null)
                {
                    BundleRevision faultyRevision =
                        getActualBundleRevision(rethrow.getRevision());
                    if (rethrow.getRequirement() instanceof HostedRequirement)
                    {
                        faultyRevision =
                            ((HostedRequirement) rethrow.getRequirement())
                                .getOriginalRequirement().getRevision();
                    }
                    if (optionalRevisions.remove(faultyRevision))
                    {
                        retry = true;
                    }
                    else if (ondemandFragments.remove(faultyRevision))
                    {
                        retry = true;
                    }
                    else
                    {
                        throw rethrow;
                    }
                }
                // If there is no exception to rethrow, then this was a clean
                // resolve, so populate the wire map.
                else
                {
                    for (BundleRevision br : allRevisions)
                    {
                        BundleRevision target = br;

                        // If we are resolving a fragment, then we
                        // actually want to populate its host's wires.
                        List<BundleRequirement> hostReq = hostReqs.get(br);
                        if (hostReq != null)
View Full Code Here

                    // optionally resolved revision is to blame (typically a fragment).
                    // If so, then remove the optionally resolved revision and try
                    // again; otherwise, rethrow the resolve exception.
                    if (rethrow != null)
                    {
                        BundleRevision faultyRevision =
                            getActualBundleRevision(rethrow.getRevision());
                        if (rethrow.getRequirement() instanceof HostedRequirement)
                        {
                            faultyRevision =
                                ((HostedRequirement) rethrow.getRequirement())
View Full Code Here

    private static Map<BundleRevision, List<ResolverWire>> populateWireMap(
        BundleRevision revision, Map<BundleRevision, Packages> revisionPkgMap,
        Map<BundleRevision, List<ResolverWire>> wireMap,
        Candidates allCandidates)
    {
        BundleRevision unwrappedRevision = getActualBundleRevision(revision);
        if ((unwrappedRevision.getWiring() == null)
            && !wireMap.containsKey(unwrappedRevision))
        {
            wireMap.put(unwrappedRevision, (List<ResolverWire>) Collections.EMPTY_LIST);

            List<ResolverWire> packageWires = new ArrayList<ResolverWire>();
            List<ResolverWire> bundleWires = new ArrayList<ResolverWire>();
            List<ResolverWire> capabilityWires = new ArrayList<ResolverWire>();

            for (BundleRequirement req : revision.getDeclaredRequirements(null))
            {
                SortedSet<BundleCapability> cands = allCandidates.getCandidates(req);
                if ((cands != null) && (cands.size() > 0))
                {
                    BundleCapability cand = cands.iterator().next();
                    // Ignore revisions that import themselves.
                    if (!revision.equals(cand.getRevision()))
                    {
                        if (cand.getRevision().getWiring() == null)
                        {
                            populateWireMap(cand.getRevision(),
                                revisionPkgMap, wireMap, allCandidates);
                        }
                        @SuppressWarnings("unused")
                        Packages candPkgs = revisionPkgMap.get(cand.getRevision());
                        ResolverWire wire = new ResolverWireImpl(
                            unwrappedRevision,
                            getActualRequirement(req),
                            getActualBundleRevision(cand.getRevision()),
                            getActualCapability(cand));
                        if (req.getNamespace().equals(BundleRevision.PACKAGE_NAMESPACE))
                        {
                            packageWires.add(wire);
                        }
                        else if (req.getNamespace().equals(BundleRevision.BUNDLE_NAMESPACE))
                        {
                            bundleWires.add(wire);
                        }
                        else
                        {
                            capabilityWires.add(wire);
                        }
                    }
                }
            }

            // Combine package wires with require wires last.
            packageWires.addAll(bundleWires);
            packageWires.addAll(capabilityWires);
            wireMap.put(unwrappedRevision, packageWires);

            // Add host wire for any fragments.
            if (revision instanceof HostBundleRevision)
            {
                List<BundleRevision> fragments = ((HostBundleRevision) revision).getFragments();
                for (BundleRevision fragment : fragments)
                {
                    List<ResolverWire> hostWires = wireMap.get(fragment);
                    if (hostWires == null)
                    {
                        hostWires = new ArrayList<ResolverWire>();
                        wireMap.put(fragment, hostWires);
                    }
                    hostWires.add(
                        new ResolverWireImpl(
                            getActualBundleRevision(fragment),
                            fragment.getDeclaredRequirements(
                                BundleRevision.HOST_NAMESPACE).get(0),
                            unwrappedRevision,
                            unwrappedRevision.getDeclaredCapabilities(
                                BundleRevision.HOST_NAMESPACE).get(0)));
                }
            }
        }
View Full Code Here

TOP

Related Classes of org.osgi.framework.wiring.BundleRevision

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.