Skip to main content
aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGlyn Normington2011-03-11 15:19:31 +0000
committerGlyn Normington2011-03-11 15:19:31 +0000
commit8470ddbeb58417696b07c24efb5b1d646c9b097e (patch)
treece0e3acf9bfb66e83a2b1f8ca42448c1690d7f34
parent90dec138da1f88daae06392d7a7d33872a9df606 (diff)
downloadorg.eclipse.virgo.kernel-8470ddbeb58417696b07c24efb5b1d646c9b097e.tar.gz
org.eclipse.virgo.kernel-8470ddbeb58417696b07c24efb5b1d646c9b097e.tar.xz
org.eclipse.virgo.kernel-8470ddbeb58417696b07c24efb5b1d646c9b097e.zip
bug 336941: reuse the abstract base class in the other hooks
-rw-r--r--org.eclipse.virgo.kernel.osgi/src/main/java/org/eclipse/virgo/kernel/osgi/region/hook/RegionBundleFindHook.java107
-rw-r--r--org.eclipse.virgo.kernel.osgi/src/main/java/org/eclipse/virgo/kernel/osgi/region/hook/RegionDigraphVisitorBase.java14
-rw-r--r--org.eclipse.virgo.kernel.osgi/src/main/java/org/eclipse/virgo/kernel/osgi/region/hook/RegionResolverHook.java99
-rw-r--r--org.eclipse.virgo.kernel.osgi/src/main/java/org/eclipse/virgo/kernel/osgi/region/hook/RegionServiceFindHook.java69
4 files changed, 86 insertions, 203 deletions
diff --git a/org.eclipse.virgo.kernel.osgi/src/main/java/org/eclipse/virgo/kernel/osgi/region/hook/RegionBundleFindHook.java b/org.eclipse.virgo.kernel.osgi/src/main/java/org/eclipse/virgo/kernel/osgi/region/hook/RegionBundleFindHook.java
index 7e1c9b05..9d27abc1 100644
--- a/org.eclipse.virgo.kernel.osgi/src/main/java/org/eclipse/virgo/kernel/osgi/region/hook/RegionBundleFindHook.java
+++ b/org.eclipse.virgo.kernel.osgi/src/main/java/org/eclipse/virgo/kernel/osgi/region/hook/RegionBundleFindHook.java
@@ -12,14 +12,10 @@
package org.eclipse.virgo.kernel.osgi.region.hook;
import java.util.Collection;
-import java.util.HashSet;
-import java.util.Iterator;
import java.util.Set;
-import java.util.Stack;
import org.eclipse.virgo.kernel.osgi.region.Region;
import org.eclipse.virgo.kernel.osgi.region.RegionDigraph;
-import org.eclipse.virgo.kernel.osgi.region.RegionDigraphVisitor;
import org.eclipse.virgo.kernel.osgi.region.RegionFilter;
import org.osgi.framework.Bundle;
import org.osgi.framework.BundleContext;
@@ -61,107 +57,36 @@ public final class RegionBundleFindHook implements FindHook {
bundles.retainAll(allowed);
}
-
- private class Visitor implements RegionDigraphVisitor {
-
- private final Collection<Bundle> bundles;
-
- private final Stack<Set<Bundle>> allowedStack = new Stack<Set<Bundle>>();
-
- private Object monitor = new Object();
-
- private Set<Bundle> allowed;
-
- private Visitor(Collection<Bundle> bundles) {
- this.bundles = bundles;
- synchronized (this.monitor) {
- this.allowed = new HashSet<Bundle>();
- }
- }
-
- private Set<Bundle> getAllowed() {
- synchronized (this.monitor) {
- return this.allowed;
- }
- }
-
- private void allow(Bundle b) {
- synchronized (this.monitor) {
- this.allowed.add(b);
- }
- }
-
+
+ private class Visitor extends RegionDigraphVisitorBase<Bundle> {
- private void allow(Set<Bundle> a) {
- synchronized (this.monitor) {
- this.allowed.addAll(a);
- }
- }
-
- private void pushAllowed() {
- synchronized (this.monitor) {
- this.allowedStack.push(this.allowed);
- this.allowed = new HashSet<Bundle>();
- }
+ private Visitor(Collection<Bundle> candidates) {
+ super(candidates);
}
- private Set<Bundle> popAllowed() {
- synchronized (this.monitor) {
- Set<Bundle> a = this.allowed;
- this.allowed = this.allowedStack.pop();
- return a;
- }
- }
-
- /**
+ /**
* {@inheritDoc}
*/
- public boolean visit(Region r) {
- for (Bundle b : this.bundles) {
- if (r.contains(b)) {
- allow(b);
- }
- }
- return true;
+ @Override
+ protected boolean contains(Region region, Bundle candidate) {
+ return region.contains(candidate);
}
- /**
+ /**
* {@inheritDoc}
*/
- public boolean preEdgeTraverse(RegionFilter regionFilter) {
- pushAllowed();
- return true;
- }
-
- /**
+ /**
* {@inheritDoc}
*/
- public void postEdgeTraverse(RegionFilter regionFilter) {
- Set<Bundle> a = popAllowed();
- filter(a, regionFilter);
- allow(a);
+ @Override
+ protected boolean isAllowed(Bundle candidate, RegionFilter filter) {
+ return filter.isBundleAllowed(candidate.getSymbolicName(), candidate.getVersion());
}
-
- private void filter(Set<Bundle> bundles, RegionFilter filter) {
- Iterator<Bundle> i = bundles.iterator();
- while (i.hasNext()) {
- Bundle b = i.next();
- if (!filter.isBundleAllowed(b.getSymbolicName(), b.getVersion())) {
- i.remove();
- }
- }
- }
-
+
}
-
+
private Region getRegion(BundleContext context) {
- Bundle b = context.getBundle();
- for (Region r : this.regionDigraph) {
- if (r.contains(b)) {
- return r;
- }
- }
- return null;
+ return this.regionDigraph.getRegion(context.getBundle());
}
}
diff --git a/org.eclipse.virgo.kernel.osgi/src/main/java/org/eclipse/virgo/kernel/osgi/region/hook/RegionDigraphVisitorBase.java b/org.eclipse.virgo.kernel.osgi/src/main/java/org/eclipse/virgo/kernel/osgi/region/hook/RegionDigraphVisitorBase.java
index c04d2ac5..84b8b7ff 100644
--- a/org.eclipse.virgo.kernel.osgi/src/main/java/org/eclipse/virgo/kernel/osgi/region/hook/RegionDigraphVisitorBase.java
+++ b/org.eclipse.virgo.kernel.osgi/src/main/java/org/eclipse/virgo/kernel/osgi/region/hook/RegionDigraphVisitorBase.java
@@ -93,6 +93,13 @@ abstract class RegionDigraphVisitorBase<C> implements RegionDigraphVisitor {
return true;
}
+ /**
+ * Determines whether the given region contains the given candidate.
+ *
+ * @param region the {@link Region}
+ * @param candidate the candidate
+ * @return <code>true</code> if and only if the given region contains the given candidate
+ */
protected abstract boolean contains(Region region, C candidate);
/**
@@ -122,6 +129,13 @@ abstract class RegionDigraphVisitorBase<C> implements RegionDigraphVisitor {
}
}
+ /**
+ * Determines whether the given candidate is allowed by the given {@link RegionFilter}.
+ *
+ * @param candidate the candidate
+ * @param filter the filter
+ * @return <code>true</code> if and only if the given candidate is allowed by the given filter
+ */
protected abstract boolean isAllowed(C candidate, RegionFilter filter);
} \ No newline at end of file
diff --git a/org.eclipse.virgo.kernel.osgi/src/main/java/org/eclipse/virgo/kernel/osgi/region/hook/RegionResolverHook.java b/org.eclipse.virgo.kernel.osgi/src/main/java/org/eclipse/virgo/kernel/osgi/region/hook/RegionResolverHook.java
index b1ee9563..a0ab1cf0 100644
--- a/org.eclipse.virgo.kernel.osgi/src/main/java/org/eclipse/virgo/kernel/osgi/region/hook/RegionResolverHook.java
+++ b/org.eclipse.virgo.kernel.osgi/src/main/java/org/eclipse/virgo/kernel/osgi/region/hook/RegionResolverHook.java
@@ -12,14 +12,12 @@
package org.eclipse.virgo.kernel.osgi.region.hook;
import java.util.Collection;
-import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
import org.eclipse.osgi.service.resolver.BundleDescription;
import org.eclipse.virgo.kernel.osgi.region.Region;
import org.eclipse.virgo.kernel.osgi.region.RegionDigraph;
-import org.eclipse.virgo.kernel.osgi.region.RegionDigraph.FilteredRegion;
import org.eclipse.virgo.kernel.osgi.region.RegionFilter;
import org.eclipse.virgo.kernel.osgi.region.RegionPackageImportPolicy;
import org.eclipse.virgo.kernel.serviceability.Assert;
@@ -65,8 +63,10 @@ final class RegionResolverHook implements ResolverHook {
candidates.clear();
return;
}
-
- Set<BundleCapability> allowed = getAllowed(requirerRegion, candidates, new HashSet<Region>());
+
+ Visitor visitor = new Visitor(candidates);
+ requirerRegion.visitSubgraph(visitor);
+ Set<BundleCapability> allowed = visitor.getAllowed();
candidates.retainAll(allowed);
} finally {
@@ -75,6 +75,37 @@ final class RegionResolverHook implements ResolverHook {
}
}
}
+
+ private class Visitor extends RegionDigraphVisitorBase<BundleCapability> {
+
+ private Visitor(Collection<BundleCapability> candidates) {
+ super(candidates);
+ }
+
+ @Override
+ protected boolean contains(Region region, BundleCapability candidate) {
+ return region.equals(getRegion(candidate.getRevision()));
+ }
+
+ @Override
+ protected boolean isAllowed(BundleCapability candidate, RegionFilter filter) {
+ RegionPackageImportPolicy packageImportPolicy = filter.getPackageImportPolicy();
+ BundleRevision providerRevision = candidate.getRevision();
+ if (!filter.isBundleAllowed(providerRevision.getSymbolicName(), providerRevision.getVersion())) {
+ String namespace = candidate.getNamespace();
+ if (BundleRevision.PACKAGE_NAMESPACE.equals(namespace)) {
+ if (!packageImportPolicy.isImported((String) candidate.getAttributes().get(BundleRevision.PACKAGE_NAMESPACE), candidate.getAttributes(),
+ candidate.getDirectives())) {
+ return false;
+ }
+ } else {
+ return false;
+ }
+ }
+ return true;
+ }
+
+ }
private Region getRegion(BundleRevision bundleRevision) {
Bundle bundle = bundleRevision.getBundle();
@@ -103,66 +134,8 @@ final class RegionResolverHook implements ResolverHook {
return INVALID_BUNDLE_ID;
}
- private Set<BundleCapability> getAllowed(Region r, Collection<BundleCapability> candidates, Set<Region> path) {
- Set<BundleCapability> allowed = new HashSet<BundleCapability>();
-
- if (!path.contains(r)) {
- allowPackagesInRegion(allowed, r, candidates);
- allowImportedPackages(allowed, r, candidates, path);
- }
-
- return allowed;
- }
-
- private void allowImportedPackages(Set<BundleCapability> allowed, Region r, Collection<BundleCapability> candidates, Set<Region> path) {
- for (FilteredRegion fr : this.regionDigraph.getEdges(r)) {
- Set<BundleCapability> a = getAllowed(fr.getRegion(), candidates, extendPath(r, path));
- filter(a, fr.getFilter());
- allowed.addAll(a);
- }
- }
-
- private void allowPackagesInRegion(Set<BundleCapability> allowed, Region r, Collection<BundleCapability> candidates) {
- for (BundleCapability b : candidates) {
- if (r.equals(getRegion(b.getRevision()))) {
- allowed.add(b);
- }
- }
- }
-
- private Set<Region> extendPath(Region r, Set<Region> path) {
- Set<Region> newPath = new HashSet<Region>(path);
- newPath.add(r);
- return newPath;
- }
-
- private void filter(Set<BundleCapability> capabilities, RegionFilter filter) {
- RegionPackageImportPolicy packageImportPolicy = filter.getPackageImportPolicy();
- Iterator<BundleCapability> i = capabilities.iterator();
- while (i.hasNext()) {
- BundleCapability c = i.next();
- BundleRevision providerRevision = c.getRevision();
- if (!filter.isBundleAllowed(providerRevision.getSymbolicName(), providerRevision.getVersion())) {
- String namespace = c.getNamespace();
- if (BundleRevision.PACKAGE_NAMESPACE.equals(namespace)) {
- if (!packageImportPolicy.isImported((String) c.getAttributes().get(BundleRevision.PACKAGE_NAMESPACE), c.getAttributes(),
- c.getDirectives())) {
- i.remove();
- }
- } else {
- i.remove();
- }
- }
- }
- }
-
private Region getRegion(Bundle bundle) {
- for (Region r : this.regionDigraph) {
- if (r.contains(bundle)) {
- return r;
- }
- }
- return null;
+ return this.regionDigraph.getRegion(bundle);
}
@Override
diff --git a/org.eclipse.virgo.kernel.osgi/src/main/java/org/eclipse/virgo/kernel/osgi/region/hook/RegionServiceFindHook.java b/org.eclipse.virgo.kernel.osgi/src/main/java/org/eclipse/virgo/kernel/osgi/region/hook/RegionServiceFindHook.java
index e6388684..65d80c0b 100644
--- a/org.eclipse.virgo.kernel.osgi/src/main/java/org/eclipse/virgo/kernel/osgi/region/hook/RegionServiceFindHook.java
+++ b/org.eclipse.virgo.kernel.osgi/src/main/java/org/eclipse/virgo/kernel/osgi/region/hook/RegionServiceFindHook.java
@@ -12,17 +12,12 @@
package org.eclipse.virgo.kernel.osgi.region.hook;
import java.util.Collection;
-import java.util.HashSet;
-import java.util.Iterator;
import java.util.Set;
import org.eclipse.virgo.kernel.osgi.region.Region;
import org.eclipse.virgo.kernel.osgi.region.RegionDigraph;
import org.eclipse.virgo.kernel.osgi.region.RegionFilter;
-import org.eclipse.virgo.kernel.osgi.region.RegionDigraph.FilteredRegion;
-import org.osgi.framework.Bundle;
import org.osgi.framework.BundleContext;
-import org.osgi.framework.Filter;
import org.osgi.framework.ServiceReference;
import org.osgi.framework.hooks.service.FindHook;
@@ -57,63 +52,39 @@ public final class RegionServiceFindHook implements FindHook {
return;
}
- Set<ServiceReference<?>> allowed = getAllowed(finderRegion, references, new HashSet<Region>());
+ Visitor visitor = new Visitor(references);
+ finderRegion.visitSubgraph(visitor);
+ Set<ServiceReference<?>> allowed = visitor.getAllowed();
references.retainAll(allowed);
}
-
- private Set<ServiceReference<?>> getAllowed(Region r, Collection<ServiceReference<?>> references, Set<Region> path) {
- Set<ServiceReference<?>> allowed = new HashSet<ServiceReference<?>>();
- if (!path.contains(r)) {
- allowServiceReferencesInRegion(allowed, r, references);
- allowImportedBundles(allowed, r, references, path);
- }
-
- return allowed;
- }
+ private class Visitor extends RegionDigraphVisitorBase<ServiceReference<?>> {
- private void allowImportedBundles(Set<ServiceReference<?>> allowed, Region r, Collection<ServiceReference<?>> references, Set<Region> path) {
- for (FilteredRegion fr : this.regionDigraph.getEdges(r)) {
- Set<ServiceReference<?>> a = getAllowed(fr.getRegion(), references, extendPath(r, path));
- filter(a, fr.getFilter());
- allowed.addAll(a);
+ private Visitor(Collection<ServiceReference<?>> candidates) {
+ super(candidates);
}
- }
- private void allowServiceReferencesInRegion(Set<ServiceReference<?>> allowed, Region r, Collection<ServiceReference<?>> references) {
- for (ServiceReference<?> b : references) {
- if (r.contains(b.getBundle())) {
- allowed.add(b);
- }
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ protected boolean contains(Region region, ServiceReference<?> candidate) {
+ return region.contains(candidate.getBundle());
}
- }
- private Set<Region> extendPath(Region r, Set<Region> path) {
- Set<Region> newPath = new HashSet<Region>(path);
- newPath.add(r);
- return newPath;
- }
-
- private void filter(Set<ServiceReference<?>> references, RegionFilter filter) {
- Filter serviceFilter = filter.getServiceFilter();
- Iterator<ServiceReference<?>> i = references.iterator();
- while (i.hasNext()) {
- ServiceReference<?> sr = i.next();
- if (!serviceFilter.match(sr)) {
- i.remove();
- }
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ protected boolean isAllowed(ServiceReference<?> candidate, RegionFilter filter) {
+ return filter.getServiceFilter().match(candidate);
}
+
}
private Region getRegion(BundleContext context) {
- Bundle b = context.getBundle();
- for (Region r : this.regionDigraph) {
- if (r.contains(b)) {
- return r;
- }
- }
- return null;
+ return this.regionDigraph.getRegion(context.getBundle());
}
}

Back to the top