Skip to main content
aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGlyn Normington2011-03-11 09:39:00 -0500
committerGlyn Normington2011-03-11 09:39:00 -0500
commit90dec138da1f88daae06392d7a7d33872a9df606 (patch)
tree588ffba96821383845619ef468d711d4e2848883
parent40b067213f1753b5c899d0eaf7e5b82349843379 (diff)
downloadorg.eclipse.virgo.kernel-90dec138da1f88daae06392d7a7d33872a9df606.tar.gz
org.eclipse.virgo.kernel-90dec138da1f88daae06392d7a7d33872a9df606.tar.xz
org.eclipse.virgo.kernel-90dec138da1f88daae06392d7a7d33872a9df606.zip
bug 336941: Factor out abstract base visitor
-rw-r--r--org.eclipse.virgo.kernel.osgi/src/main/java/org/eclipse/virgo/kernel/osgi/region/hook/RegionBundleFindHook.java64
-rw-r--r--org.eclipse.virgo.kernel.osgi/src/main/java/org/eclipse/virgo/kernel/osgi/region/hook/RegionDigraphVisitorBase.java127
-rw-r--r--org.eclipse.virgo.kernel.osgi/src/main/java/org/eclipse/virgo/kernel/osgi/region/hook/RegionServiceFindHook.java2
-rw-r--r--org.eclipse.virgo.kernel.osgi/src/test/java/org/eclipse/virgo/kernel/osgi/region/hook/RegionServiceEventHookTests.java1
4 files changed, 170 insertions, 24 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 906d9322..7e1c9b05 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
@@ -64,16 +64,19 @@ public final class RegionBundleFindHook implements FindHook {
private class Visitor implements RegionDigraphVisitor {
- private Object monitor = new Object();
-
private final Collection<Bundle> bundles;
- private Set<Bundle> allowed = new HashSet<Bundle>();
-
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() {
@@ -82,20 +85,47 @@ public final class RegionBundleFindHook implements FindHook {
}
}
+ private void allow(Bundle b) {
+ synchronized (this.monitor) {
+ this.allowed.add(b);
+ }
+ }
+
+
+ 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 void popAllowed() {
+
+ 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;
+ }
+
+ /**
* {@inheritDoc}
*/
public boolean preEdgeTraverse(RegionFilter regionFilter) {
@@ -103,14 +133,13 @@ public final class RegionBundleFindHook implements FindHook {
return true;
}
- /**
+ /**
* {@inheritDoc}
*/
public void postEdgeTraverse(RegionFilter regionFilter) {
- Set<Bundle> a = getAllowed();
- popAllowed();
+ Set<Bundle> a = popAllowed();
filter(a, regionFilter);
- getAllowed().addAll(a);
+ allow(a);
}
private void filter(Set<Bundle> bundles, RegionFilter filter) {
@@ -123,19 +152,8 @@ public final class RegionBundleFindHook implements FindHook {
}
}
- /**
- * {@inheritDoc}
- */
- public boolean visit(Region r) {
- for (Bundle b : this.bundles) {
- if (r.contains(b)) {
- getAllowed().add(b);
- }
- }
- return true;
- }
}
-
+
private Region getRegion(BundleContext context) {
Bundle b = context.getBundle();
for (Region r : this.regionDigraph) {
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
new file mode 100644
index 00000000..c04d2ac5
--- /dev/null
+++ b/org.eclipse.virgo.kernel.osgi/src/main/java/org/eclipse/virgo/kernel/osgi/region/hook/RegionDigraphVisitorBase.java
@@ -0,0 +1,127 @@
+/*******************************************************************************
+ * This file is part of the Virgo Web Server.
+ *
+ * Copyright (c) 2011 VMware Inc.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ * SpringSource, a division of VMware - initial API and implementation and/or initial documentation
+ *******************************************************************************/
+
+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.RegionDigraphVisitor;
+import org.eclipse.virgo.kernel.osgi.region.RegionFilter;
+
+/**
+ * {@link RegionDigraphVisitorBase} is an abstract base class for {@link RegionDigraphVisitor} implementations in the
+ * framework hooks.
+ * <p />
+ *
+ * <strong>Concurrent Semantics</strong><br />
+ * This class is thread safe.
+ */
+abstract class RegionDigraphVisitorBase<C> implements RegionDigraphVisitor {
+
+ private final Collection<C> allCandidates;
+
+ private final Stack<Set<C>> allowedStack = new Stack<Set<C>>();
+
+ private Object monitor = new Object();
+
+ private Set<C> allowed;
+
+ protected RegionDigraphVisitorBase(Collection<C> candidates) {
+ this.allCandidates = candidates;
+ synchronized (this.monitor) {
+ this.allowed = new HashSet<C>();
+ }
+ }
+
+ Set<C> getAllowed() {
+ synchronized (this.monitor) {
+ return this.allowed;
+ }
+ }
+
+ private void allow(C candidate) {
+ synchronized (this.monitor) {
+ this.allowed.add(candidate);
+ }
+ }
+
+ private void allow(Set<C> candidates) {
+ synchronized (this.monitor) {
+ this.allowed.addAll(candidates);
+ }
+ }
+
+ private void pushAllowed() {
+ synchronized (this.monitor) {
+ this.allowedStack.push(this.allowed);
+ this.allowed = new HashSet<C>();
+ }
+ }
+
+ private Set<C> popAllowed() {
+ synchronized (this.monitor) {
+ Set<C> a = this.allowed;
+ this.allowed = this.allowedStack.pop();
+ return a;
+ }
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public boolean visit(Region region) {
+ for (C candidate : this.allCandidates) {
+ if (contains(region, candidate)) {
+ allow(candidate);
+ }
+ }
+ return true;
+ }
+
+ protected abstract boolean contains(Region region, C candidate);
+
+ /**
+ * {@inheritDoc}
+ */
+ public boolean preEdgeTraverse(RegionFilter regionFilter) {
+ pushAllowed();
+ return true;
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public void postEdgeTraverse(RegionFilter regionFilter) {
+ Set<C> candidates = popAllowed();
+ filter(candidates, regionFilter);
+ allow(candidates);
+ }
+
+ private void filter(Set<C> candidates, RegionFilter filter) {
+ Iterator<C> i = candidates.iterator();
+ while (i.hasNext()) {
+ C candidate = i.next();
+ if (!isAllowed(candidate, filter)) {
+ i.remove();
+ }
+ }
+ }
+
+ 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/RegionServiceFindHook.java b/org.eclipse.virgo.kernel.osgi/src/main/java/org/eclipse/virgo/kernel/osgi/region/hook/RegionServiceFindHook.java
index 30cf3e18..e6388684 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
@@ -61,7 +61,7 @@ public final class RegionServiceFindHook implements FindHook {
references.retainAll(allowed);
}
-
+
private Set<ServiceReference<?>> getAllowed(Region r, Collection<ServiceReference<?>> references, Set<Region> path) {
Set<ServiceReference<?>> allowed = new HashSet<ServiceReference<?>>();
diff --git a/org.eclipse.virgo.kernel.osgi/src/test/java/org/eclipse/virgo/kernel/osgi/region/hook/RegionServiceEventHookTests.java b/org.eclipse.virgo.kernel.osgi/src/test/java/org/eclipse/virgo/kernel/osgi/region/hook/RegionServiceEventHookTests.java
index f8b0402c..40553936 100644
--- a/org.eclipse.virgo.kernel.osgi/src/test/java/org/eclipse/virgo/kernel/osgi/region/hook/RegionServiceEventHookTests.java
+++ b/org.eclipse.virgo.kernel.osgi/src/test/java/org/eclipse/virgo/kernel/osgi/region/hook/RegionServiceEventHookTests.java
@@ -34,6 +34,7 @@ import org.osgi.framework.ServiceReference;
import org.osgi.framework.hooks.service.EventHook;
import org.osgi.framework.hooks.service.FindHook;
+@SuppressWarnings("deprecation")
public class RegionServiceEventHookTests {
private FindHook mockFindHook;

Back to the top