Search in sources :

Example 1 with RegionDigraphVisitor

use of org.eclipse.equinox.region.RegionDigraphVisitor in project aries by apache.

the class WovenClassListener method addSharingPolicyUpdates.

private void addSharingPolicyUpdates(final DynamicImportPackageRequirement requirement, final BasicSubsystem scopedSubsystem, final Collection<BundleCapability> providers, Map<Region, RegionUpdaterInfo> updates) {
    final List<BasicSubsystem> subsystems = new ArrayList<BasicSubsystem>();
    final Map<Region, BasicSubsystem> regionToSubsystem = new HashMap<Region, BasicSubsystem>();
    regionToSubsystem(scopedSubsystem, regionToSubsystem);
    scopedSubsystem.getRegion().visitSubgraph(new RegionDigraphVisitor() {

        private final List<BasicSubsystem> visited = new ArrayList<BasicSubsystem>();

        @Override
        public void postEdgeTraverse(RegionFilter filter) {
        // Nothing.
        }

        @Override
        public boolean preEdgeTraverse(RegionFilter filter) {
            return true;
        }

        @Override
        public boolean visit(Region region) {
            BasicSubsystem subsystem = regionToSubsystem.get(region);
            if (subsystem == null || subsystem.isRoot()) {
                // sharing policy.
                return false;
            }
            if (!visited.isEmpty() && !subsystem.equals(scopedParent(visited.get(visited.size() - 1)))) {
                // We're only interested in walking up the scoped parent tree.
                return false;
            }
            visited.add(subsystem);
            if (!requirement.getPackageName().contains("*")) {
                for (BundleCapability provider : providers) {
                    BundleRevision br = provider.getResource();
                    if (region.contains(br.getBundle())) {
                        // satisfied.
                        return false;
                    }
                }
            }
            boolean allowed = false;
            Set<FilteredRegion> filters = region.getEdges();
            for (FilteredRegion filteredRegion : filters) {
                RegionFilter filter = filteredRegion.getFilter();
                if (filter.isAllowed(providers.iterator().next())) {
                    // The region already allows matching capabilities
                    // through so there is no need to update the sharing
                    // policy.
                    allowed = true;
                    break;
                }
            }
            if (!allowed) {
                // The subsystem region requires a sharing policy update.
                subsystems.add(subsystem);
            }
            // Visit the next region.
            return true;
        }
    });
    // Collect the information for the necessary sharing policy updates.
    for (BasicSubsystem subsystem : subsystems) {
        Region tail = subsystem.getRegion();
        Region head = scopedParent(subsystem).getRegion();
        RegionUpdaterInfo info = updates.get(tail);
        if (info == null) {
            info = new RegionUpdaterInfo(tail, head);
            updates.put(tail, info);
        }
        info.requirement(requirement);
    }
}
Also used : EnumSet(java.util.EnumSet) Set(java.util.Set) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) RegionDigraphVisitor(org.eclipse.equinox.region.RegionDigraphVisitor) FilteredRegion(org.eclipse.equinox.region.RegionDigraph.FilteredRegion) RegionFilter(org.eclipse.equinox.region.RegionFilter) 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)

Aggregations

ArrayList (java.util.ArrayList)1 EnumSet (java.util.EnumSet)1 HashMap (java.util.HashMap)1 Set (java.util.Set)1 Region (org.eclipse.equinox.region.Region)1 FilteredRegion (org.eclipse.equinox.region.RegionDigraph.FilteredRegion)1 RegionDigraphVisitor (org.eclipse.equinox.region.RegionDigraphVisitor)1 RegionFilter (org.eclipse.equinox.region.RegionFilter)1 BundleCapability (org.osgi.framework.wiring.BundleCapability)1 BundleRevision (org.osgi.framework.wiring.BundleRevision)1