summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorThomas Watson2011-03-09 14:50:42 (EST)
committer Glyn Normington2011-03-09 14:50:42 (EST)
commit6eea00d3678f950a25489423201db92a1576e3e5 (patch)
treef348f75affcfcf3094d96c4cc003fb31facd2460
parentff162a9d83339f67d5dfa38049c72e6bca0d561d (diff)
downloadorg.eclipse.virgo.kernel-6eea00d3678f950a25489423201db92a1576e3e5.zip
org.eclipse.virgo.kernel-6eea00d3678f950a25489423201db92a1576e3e5.tar.gz
org.eclipse.virgo.kernel-6eea00d3678f950a25489423201db92a1576e3e5.tar.bz2
- use builder pattern for RegionFilters
- code formating
-rw-r--r--org.eclipse.virgo.kernel.osgi/src/main/java/org/eclipse/virgo/kernel/osgi/region/RegionDigraph.java20
-rw-r--r--org.eclipse.virgo.kernel.osgi/src/main/java/org/eclipse/virgo/kernel/osgi/region/RegionFilter.java103
-rw-r--r--org.eclipse.virgo.kernel.osgi/src/main/java/org/eclipse/virgo/kernel/osgi/region/RegionFilterBuilder.java41
-rw-r--r--org.eclipse.virgo.kernel.osgi/src/main/java/org/eclipse/virgo/kernel/osgi/region/internal/StandardRegionDigraph.java9
-rw-r--r--org.eclipse.virgo.kernel.osgi/src/main/java/org/eclipse/virgo/kernel/osgi/region/internal/StandardRegionFilter.java130
-rw-r--r--org.eclipse.virgo.kernel.osgi/src/main/java/org/eclipse/virgo/kernel/osgi/region/internal/StandardRegionFilterBuilder.java55
-rw-r--r--org.eclipse.virgo.kernel.osgi/src/test/java/org/eclipse/virgo/kernel/osgi/region/hook/RegionBundleFindHookTests.java31
-rw-r--r--org.eclipse.virgo.kernel.osgi/src/test/java/org/eclipse/virgo/kernel/osgi/region/hook/RegionResolverHookTests.java63
-rw-r--r--org.eclipse.virgo.kernel.osgi/src/test/java/org/eclipse/virgo/kernel/osgi/region/hook/RegionServiceFindHookTests.java31
-rw-r--r--org.eclipse.virgo.kernel.osgi/src/test/java/org/eclipse/virgo/kernel/osgi/region/internal/StandardRegionDigraphTests.java41
-rw-r--r--org.eclipse.virgo.kernel.osgi/src/test/java/org/eclipse/virgo/kernel/osgi/region/internal/StandardRegionFilterTests.java64
-rw-r--r--org.eclipse.virgo.kernel.userregion/src/main/java/org/eclipse/virgo/kernel/userregion/internal/quasi/StandardQuasiFramework.java23
-rw-r--r--org.eclipse.virgo.kernel.userregionfactory/src/main/java/org/eclipse/virgo/kernel/userregionfactory/Activator.java126
13 files changed, 373 insertions, 364 deletions
diff --git a/org.eclipse.virgo.kernel.osgi/src/main/java/org/eclipse/virgo/kernel/osgi/region/RegionDigraph.java b/org.eclipse.virgo.kernel.osgi/src/main/java/org/eclipse/virgo/kernel/osgi/region/RegionDigraph.java
index 77e0a9a..d0f0d56 100644
--- a/org.eclipse.virgo.kernel.osgi/src/main/java/org/eclipse/virgo/kernel/osgi/region/RegionDigraph.java
+++ b/org.eclipse.virgo.kernel.osgi/src/main/java/org/eclipse/virgo/kernel/osgi/region/RegionDigraph.java
@@ -13,13 +13,10 @@
package org.eclipse.virgo.kernel.osgi.region;
-import java.util.Collection;
-import java.util.Map;
import java.util.Set;
import org.osgi.framework.Bundle;
import org.osgi.framework.BundleException;
-import org.osgi.framework.InvalidSyntaxException;
/**
* {@link RegionDigraph} is a <a href="http://en.wikipedia.org/wiki/Directed_graph">directed graph</a>, or
@@ -59,8 +56,8 @@ public interface RegionDigraph extends Iterable<Region> {
}
/**
- * Create a {@link Region} with the given name. If a region with the given name already exists, then BundleException with
- * exception type UNSUPPORTED_OPERATION is thrown.
+ * Create a {@link Region} with the given name. If a region with the given name already exists, then BundleException
+ * with exception type UNSUPPORTED_OPERATION is thrown.
*
* @param regionName the name of the region
* @return the {@link Region} created
@@ -69,14 +66,11 @@ public interface RegionDigraph extends Iterable<Region> {
Region createRegion(String regionName) throws BundleException;
/**
- * Create a {@link RegionFilter} with the given sharing policy. The sharing
- * policy map uses capability name spaces as the key and a collection of
- * filter strings as the value.
- * @param sharingPolicy the sharing policy filters use to match shared capabilities.
- * @return the {@link RegionFilter} created
- * @throws InvalidSyntaxException if a specified sharing policy filter is invalid
+ * Create a {@link RegionFilterBuilder} instance.
+ *
+ * @return a region filter builder
*/
- RegionFilter createRegionFilter(Map<String, Collection<String>> sharingPolicy) throws InvalidSyntaxException;
+ RegionFilterBuilder createRegionFilterBuilder();
/**
* Removes the given {@link Region} from the digraph along with any edges which have the given region as head or
@@ -85,7 +79,7 @@ public interface RegionDigraph extends Iterable<Region> {
* @param region the {@link Region} to be removed
*/
void removeRegion(Region region);
-
+
/**
* Gets all the {@link Region Regions} in the digraph.
*
diff --git a/org.eclipse.virgo.kernel.osgi/src/main/java/org/eclipse/virgo/kernel/osgi/region/RegionFilter.java b/org.eclipse.virgo.kernel.osgi/src/main/java/org/eclipse/virgo/kernel/osgi/region/RegionFilter.java
index 082f3f7..5fa872c 100644
--- a/org.eclipse.virgo.kernel.osgi/src/main/java/org/eclipse/virgo/kernel/osgi/region/RegionFilter.java
+++ b/org.eclipse.virgo.kernel.osgi/src/main/java/org/eclipse/virgo/kernel/osgi/region/RegionFilter.java
@@ -13,16 +13,11 @@
package org.eclipse.virgo.kernel.osgi.region;
-import java.util.ArrayList;
import java.util.Collection;
-import java.util.HashMap;
import java.util.Map;
import org.osgi.framework.Bundle;
import org.osgi.framework.Constants;
-import org.osgi.framework.Filter;
-import org.osgi.framework.FrameworkUtil;
-import org.osgi.framework.InvalidSyntaxException;
import org.osgi.framework.ServiceReference;
import org.osgi.framework.wiring.BundleCapability;
import org.osgi.framework.wiring.BundleRevision;
@@ -38,81 +33,47 @@ import org.osgi.framework.wiring.BundleRevision;
*
*/
public interface RegionFilter {
- /**
- * Name space for sharing package capabilities.
- * @see BundleRevision#PACKAGE_NAMESPACE
- */
- public static final String VISIBLE_PACKAGE_NAMESPACE = BundleRevision.PACKAGE_NAMESPACE;
- /**
- * Name space for sharing bundle capabilities for require bundle constraints.
- * @see BundleRevision#BUNDLE_NAMESPACE
- */
- public static final String VISIBLE_REQUIRE_NAMESPACE = BundleRevision.BUNDLE_NAMESPACE;
-
- /**
- * Name space for sharing host capabilities.
- * @see BundleRevision#HOST_NAMESPACE
- */
- public static final String VISIBLE_HOST_NAMESPACE = BundleRevision.HOST_NAMESPACE;
-
- /**
- * Name space for sharing services. The filters specified in this name space will
- * be used to match {@link ServiceReference services}.
- */
- public static final String VISIBLE_SERVICE_NAMESPACE = "org.eclipse.equinox.allow.service";
-
- /**
- * Name space for sharing bundles. The filters specified in this name space will
- * be use to match against a bundle's symbolic name and version. The attribute
- * {@link Constants#BUNDLE_SYMBOLICNAME_ATTRIBUTE bundle-symbolic-name} is used
- * for the symbolic name and the attribute {@link Constants#BUNDLE_VERSION_ATTRIBUTE
- * bundle-version} is used for the bundle version.
- */
- public static final String VISIBLE_BUNDLE_NAMESPACE = "org.eclipse.equinox.allow.bundle";
-
- /**
- * Name space for matching against all capabilities. The filters specified in this
- * name space will be used to match all capabilities.
- */
- public static final String VISIBLE_ALL_NAMESPACE = "org.eclipse.equinox.allow.all";
-
- /**
- * A filter specification that matches all package capabilities in the
- * {@link VISIBLE_PACKAGE_NAMESPACE} name space.
- */
- public static final String ALL_PACKAGES = '(' + VISIBLE_PACKAGE_NAMESPACE + "=*)";
+ /**
+ * Name space for sharing package capabilities.
+ *
+ * @see BundleRevision#PACKAGE_NAMESPACE
+ */
+ public static final String VISIBLE_PACKAGE_NAMESPACE = BundleRevision.PACKAGE_NAMESPACE;
/**
- * A filter specification that matches all require bundle capabilities in the
- * {@link VISIBLE_REQUIRE_NAMESPACE} name space.
- */
- public static final String ALL_REQUIRES = '(' + VISIBLE_REQUIRE_NAMESPACE + "=*)";
+ * Name space for sharing bundle capabilities for require bundle constraints.
+ *
+ * @see BundleRevision#BUNDLE_NAMESPACE
+ */
+ public static final String VISIBLE_REQUIRE_NAMESPACE = BundleRevision.BUNDLE_NAMESPACE;
- /**
- * A filter specification that matches all host capabilities in the
- * {@link VISIBLE_HOST_NAMESPACE} name space.
- */
- public static final String ALL_HOSTS = '(' + VISIBLE_HOST_NAMESPACE + "=*)";
+ /**
+ * Name space for sharing host capabilities.
+ *
+ * @see BundleRevision#HOST_NAMESPACE
+ */
+ public static final String VISIBLE_HOST_NAMESPACE = BundleRevision.HOST_NAMESPACE;
- /**
- * A filter specification that matches all service in the
- * {@link VISIBLE_SERVICE_NAMESPACE} name space.
- */
- public static final String ALL_SERVICES = '(' + org.osgi.framework.Constants.SERVICE_ID + "=*)";
+ /**
+ * Name space for sharing services. The filters specified in this name space will be used to match
+ * {@link ServiceReference services}.
+ */
+ public static final String VISIBLE_SERVICE_NAMESPACE = "org.eclipse.equinox.allow.service";
- /**
- * A filter specification that matches all bundles in the
- * {@link VISIBLE_BUNDLE_NAMESPACE} name space.
- */
- public static final String ALL_BUNDLES = '(' + org.osgi.framework.Constants.BUNDLE_SYMBOLICNAME_ATTRIBUTE + "=*)";
+ /**
+ * Name space for sharing bundles. The filters specified in this name space will be use to match against a bundle's
+ * symbolic name and version. The attribute {@link Constants#BUNDLE_SYMBOLICNAME_ATTRIBUTE bundle-symbolic-name} is
+ * used for the symbolic name and the attribute {@link Constants#BUNDLE_VERSION_ATTRIBUTE bundle-version} is used
+ * for the bundle version.
+ */
+ public static final String VISIBLE_BUNDLE_NAMESPACE = "org.eclipse.equinox.allow.bundle";
/**
- * A filter specification that matches all capabilities in all name spaces.
- * This is useful if you want to use the {@link VISIBLE_ALL_NAMESPACE} to
- * match all capabilities.
+ * Name space for matching against all capabilities. The filters specified in this name space will be used to match
+ * all capabilities.
*/
- public static final String ALL = "(|(x=*)(!(x=*)))";
+ public static final String VISIBLE_ALL_NAMESPACE = "org.eclipse.equinox.allow.all";
/**
* Determines whether this filter allows the given bundle
diff --git a/org.eclipse.virgo.kernel.osgi/src/main/java/org/eclipse/virgo/kernel/osgi/region/RegionFilterBuilder.java b/org.eclipse.virgo.kernel.osgi/src/main/java/org/eclipse/virgo/kernel/osgi/region/RegionFilterBuilder.java
new file mode 100644
index 0000000..8c03214
--- /dev/null
+++ b/org.eclipse.virgo.kernel.osgi/src/main/java/org/eclipse/virgo/kernel/osgi/region/RegionFilterBuilder.java
@@ -0,0 +1,41 @@
+/*******************************************************************************
+ * Copyright (c) 2011 IBM Corporation and others.
+ * 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:
+ * IBM Corporation - initial API and implementation
+ *******************************************************************************/
+
+package org.eclipse.virgo.kernel.osgi.region;
+
+import org.osgi.framework.InvalidSyntaxException;
+
+/**
+ * A builder for creating {@link RegionFilter} instances. A builder instance can be obtained from the
+ * {@link RegionDigraph#createRegionFilterBuilder()} method.
+ *
+ * <strong>Concurrent Semantics</strong><br />
+ *
+ * Implementations of this interface must be thread safe.
+ */
+public interface RegionFilterBuilder {
+
+ /**
+ * Allow capabilities with the given namespace matching the given filter.
+ *
+ * @param namespace the namespace of the capabilities to be allowed
+ * @param filter the filter matching the capabilities to be allowed
+ * @return this builder (for method chaining)
+ */
+ RegionFilterBuilder allow(String namespace, String filter) throws InvalidSyntaxException;
+
+ /**
+ * Build a {@link RegionFilter} from the current state of this builder.
+ *
+ * @return the {@link RegionFilter} built
+ */
+ RegionFilter build();
+}
diff --git a/org.eclipse.virgo.kernel.osgi/src/main/java/org/eclipse/virgo/kernel/osgi/region/internal/StandardRegionDigraph.java b/org.eclipse.virgo.kernel.osgi/src/main/java/org/eclipse/virgo/kernel/osgi/region/internal/StandardRegionDigraph.java
index 3224975..a2612d4 100644
--- a/org.eclipse.virgo.kernel.osgi/src/main/java/org/eclipse/virgo/kernel/osgi/region/internal/StandardRegionDigraph.java
+++ b/org.eclipse.virgo.kernel.osgi/src/main/java/org/eclipse/virgo/kernel/osgi/region/internal/StandardRegionDigraph.java
@@ -23,6 +23,7 @@ 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.RegionFilterBuilder;
import org.eclipse.virgo.kernel.osgi.region.RegionLifecycleListener;
import org.eclipse.virgo.kernel.serviceability.NonNull;
import org.eclipse.virgo.util.math.OrderedPair;
@@ -263,10 +264,10 @@ public final class StandardRegionDigraph implements RegionDigraph {
return result;
}
- @Override
- public RegionFilter createRegionFilter(Map<String, Collection<String>> sharingPolicy) throws InvalidSyntaxException {
- return new StandardRegionFilter(sharingPolicy);
- }
+ @Override
+ public RegionFilterBuilder createRegionFilterBuilder() {
+ return new StandardRegionFilterBuilder();
+ }
private void notifyAdded(Region region) {
Set<RegionLifecycleListener> listeners = getListeners();
diff --git a/org.eclipse.virgo.kernel.osgi/src/main/java/org/eclipse/virgo/kernel/osgi/region/internal/StandardRegionFilter.java b/org.eclipse.virgo.kernel.osgi/src/main/java/org/eclipse/virgo/kernel/osgi/region/internal/StandardRegionFilter.java
index 5888c69..29f790f 100644
--- a/org.eclipse.virgo.kernel.osgi/src/main/java/org/eclipse/virgo/kernel/osgi/region/internal/StandardRegionFilter.java
+++ b/org.eclipse.virgo.kernel.osgi/src/main/java/org/eclipse/virgo/kernel/osgi/region/internal/StandardRegionFilter.java
@@ -8,6 +8,7 @@
* Contributors:
* VMware Inc. - initial contribution
*******************************************************************************/
+
package org.eclipse.virgo.kernel.osgi.region.internal;
import java.util.ArrayList;
@@ -18,23 +19,24 @@ import java.util.Map;
import org.eclipse.virgo.kernel.osgi.region.RegionFilter;
import org.osgi.framework.Bundle;
import org.osgi.framework.Filter;
-import org.osgi.framework.FrameworkUtil;
-import org.osgi.framework.InvalidSyntaxException;
import org.osgi.framework.ServiceReference;
import org.osgi.framework.wiring.BundleCapability;
import org.osgi.framework.wiring.BundleRevision;
-public class StandardRegionFilter implements RegionFilter{
+public class StandardRegionFilter implements RegionFilter {
+
private final Map<String, Collection<Filter>> filters;
- public StandardRegionFilter(Map<String, Collection<String>> filters) throws InvalidSyntaxException {
- if (filters == null) {
- throw new IllegalArgumentException("filters must not be null.");
- }
- this.filters = new HashMap<String, Collection<Filter>>((int) ((filters.size() / 0.75) + 1));
- for (Map.Entry<String, Collection<String>> namespace : filters.entrySet()) {
- this.filters.put(namespace.getKey(), createFilters(namespace.getValue()));
- }
+ public StandardRegionFilter(Map<String, Collection<Filter>> filters) {
+ if (filters == null) {
+ throw new IllegalArgumentException("filters must not be null.");
+ }
+ // must perform deep copy to avoid external changes
+ this.filters = new HashMap<String, Collection<Filter>>((int) ((filters.size() / 0.75) + 1));
+ for (Map.Entry<String, Collection<Filter>> namespace : filters.entrySet()) {
+ Collection<Filter> namespaceFilters = new ArrayList<Filter>(namespace.getValue());
+ this.filters.put(namespace.getKey(), namespaceFilters);
+ }
}
/**
@@ -44,12 +46,12 @@ public class StandardRegionFilter implements RegionFilter{
* @return <code>true</code> if the bundle is allowed and <code>false</code>otherwise
*/
public boolean isBundleAllowed(Bundle bundle) {
- HashMap<String, Object> attrs = new HashMap<String, Object>(3);
- String bsn = bundle.getSymbolicName();
- if (bsn != null)
- attrs.put(VISIBLE_BUNDLE_NAMESPACE, bsn);
- attrs.put(org.osgi.framework.Constants.BUNDLE_VERSION_ATTRIBUTE, bundle.getVersion());
- return isBundleAllowed(attrs);
+ HashMap<String, Object> attrs = new HashMap<String, Object>(3);
+ String bsn = bundle.getSymbolicName();
+ if (bsn != null)
+ attrs.put(VISIBLE_BUNDLE_NAMESPACE, bsn);
+ attrs.put(org.osgi.framework.Constants.BUNDLE_VERSION_ATTRIBUTE, bundle.getVersion());
+ return isBundleAllowed(attrs);
}
/**
@@ -59,12 +61,12 @@ public class StandardRegionFilter implements RegionFilter{
* @return <code>true</code> if the bundle is allowed and <code>false</code>otherwise
*/
public boolean isBundleAllowed(BundleRevision bundle) {
- HashMap<String, Object> attrs = new HashMap<String, Object>(3);
- String bsn = bundle.getSymbolicName();
- if (bsn != null)
- attrs.put(VISIBLE_BUNDLE_NAMESPACE, bsn);
- attrs.put(org.osgi.framework.Constants.BUNDLE_VERSION_ATTRIBUTE, bundle.getVersion());
- return isBundleAllowed(attrs);
+ HashMap<String, Object> attrs = new HashMap<String, Object>(3);
+ String bsn = bundle.getSymbolicName();
+ if (bsn != null)
+ attrs.put(VISIBLE_BUNDLE_NAMESPACE, bsn);
+ attrs.put(org.osgi.framework.Constants.BUNDLE_VERSION_ATTRIBUTE, bundle.getVersion());
+ return isBundleAllowed(attrs);
}
/**
@@ -74,29 +76,29 @@ public class StandardRegionFilter implements RegionFilter{
* @return <code>true</code> if the bundle is allowed and <code>false</code>otherwise
*/
private boolean isBundleAllowed(Map<String, ?> bundleAttributes) {
- if (match(filters.get(VISIBLE_BUNDLE_NAMESPACE), bundleAttributes))
- return true;
- return match(filters.get(VISIBLE_ALL_NAMESPACE), bundleAttributes);
+ if (match(filters.get(VISIBLE_BUNDLE_NAMESPACE), bundleAttributes))
+ return true;
+ return match(filters.get(VISIBLE_ALL_NAMESPACE), bundleAttributes);
}
private boolean match(Collection<Filter> filters, Map<String, ?> attrs) {
- if (filters == null)
- return false;
- for (Filter filter : filters) {
- if (filter.matches(attrs))
- return true;
- }
- return false;
+ if (filters == null)
+ return false;
+ for (Filter filter : filters) {
+ if (filter.matches(attrs))
+ return true;
+ }
+ return false;
}
private boolean match(Collection<Filter> filters, ServiceReference<?> service) {
- if (filters == null)
- return false;
- for (Filter filter : filters) {
- if (filter.match(service))
- return true;
- }
- return false;
+ if (filters == null)
+ return false;
+ for (Filter filter : filters) {
+ if (filter.match(service))
+ return true;
+ }
+ return false;
}
/**
@@ -106,9 +108,9 @@ public class StandardRegionFilter implements RegionFilter{
* @return <code>true</code> if the service is allowed and <code>false</code>otherwise
*/
public boolean isServiceAllowed(ServiceReference<?> service) {
- if (match(filters.get(VISIBLE_SERVICE_NAMESPACE), service))
- return true;
- return match(filters.get(VISIBLE_ALL_NAMESPACE), service);
+ if (match(filters.get(VISIBLE_SERVICE_NAMESPACE), service))
+ return true;
+ return match(filters.get(VISIBLE_ALL_NAMESPACE), service);
}
/**
@@ -118,38 +120,26 @@ public class StandardRegionFilter implements RegionFilter{
* @return <code>true</code> if the capability is allowed and <code>false</code>otherwise
*/
public boolean isCapabilityAllowed(BundleCapability capability) {
- String namespace = capability.getNamespace();
- Map<String, ?> attrs = capability.getAttributes();
- if (match(filters.get(namespace), attrs))
- return true;
- return match(filters.get(VISIBLE_ALL_NAMESPACE), attrs);
+ String namespace = capability.getNamespace();
+ Map<String, ?> attrs = capability.getAttributes();
+ if (match(filters.get(namespace), attrs))
+ return true;
+ return match(filters.get(VISIBLE_ALL_NAMESPACE), attrs);
}
-
public Map<String, Collection<String>> getSharingPolicy() {
- Map<String, Collection<String>> result = new HashMap<String, Collection<String>>((int) ((filters.size() / 0.75) + 1));
- for (Map.Entry<String, Collection<Filter>> namespace : filters.entrySet()) {
- result.put(namespace.getKey(), getFilters(namespace.getValue()));
- }
- return result;
+ Map<String, Collection<String>> result = new HashMap<String, Collection<String>>((int) ((filters.size() / 0.75) + 1));
+ for (Map.Entry<String, Collection<Filter>> namespace : filters.entrySet()) {
+ result.put(namespace.getKey(), getFilters(namespace.getValue()));
+ }
+ return result;
}
private static Collection<String> getFilters(Collection<Filter> filters) {
- Collection<String> result = new ArrayList<String>(filters.size());
- for (Filter filter : filters) {
- result.add(filter.toString());
- }
- return result;
- }
-
- private static Collection<Filter> createFilters(Collection<String> filters) throws InvalidSyntaxException {
- if (filters == null)
- return null;
- Collection<Filter> result = new ArrayList<Filter>(filters.size());
- //TODO Using FrameworkUtil for now; Should move to using BundleContext
- for (String filter : filters) {
- result.add(FrameworkUtil.createFilter(filter));
- }
- return result;
+ Collection<String> result = new ArrayList<String>(filters.size());
+ for (Filter filter : filters) {
+ result.add(filter.toString());
+ }
+ return result;
}
}
diff --git a/org.eclipse.virgo.kernel.osgi/src/main/java/org/eclipse/virgo/kernel/osgi/region/internal/StandardRegionFilterBuilder.java b/org.eclipse.virgo.kernel.osgi/src/main/java/org/eclipse/virgo/kernel/osgi/region/internal/StandardRegionFilterBuilder.java
new file mode 100644
index 0000000..b7c03ab
--- /dev/null
+++ b/org.eclipse.virgo.kernel.osgi/src/main/java/org/eclipse/virgo/kernel/osgi/region/internal/StandardRegionFilterBuilder.java
@@ -0,0 +1,55 @@
+/*******************************************************************************
+ * Copyright (c) 2011 IBM Corporation and others.
+ * 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:
+ * IBM Corporation - initial API and implementation
+ *******************************************************************************/
+
+package org.eclipse.virgo.kernel.osgi.region.internal;
+
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.HashMap;
+import java.util.Map;
+
+import org.eclipse.virgo.kernel.osgi.region.RegionFilter;
+import org.eclipse.virgo.kernel.osgi.region.RegionFilterBuilder;
+import org.osgi.framework.Filter;
+import org.osgi.framework.FrameworkUtil;
+import org.osgi.framework.InvalidSyntaxException;
+
+public class StandardRegionFilterBuilder implements RegionFilterBuilder {
+
+ private final Object monitor = new Object();
+
+ private final Map<String, Collection<Filter>> policy = new HashMap<String, Collection<Filter>>();
+
+ @Override
+ public RegionFilterBuilder allow(String namespace, String filter) throws InvalidSyntaxException {
+ if (namespace == null)
+ throw new IllegalArgumentException("The namespace must not be null.");
+ if (filter == null)
+ throw new IllegalArgumentException("The filter must not be null.");
+ synchronized (this.monitor) {
+ Collection<Filter> namespaceFilters = policy.get(namespace);
+ if (namespaceFilters == null) {
+ namespaceFilters = new ArrayList<Filter>();
+ policy.put(namespace, namespaceFilters);
+ }
+ // TODO need to use BundleContext.createFilter here
+ namespaceFilters.add(FrameworkUtil.createFilter(filter));
+ }
+ return this;
+ }
+
+ @Override
+ public RegionFilter build() {
+ synchronized (this.monitor) {
+ return new StandardRegionFilter(policy);
+ }
+ }
+}
diff --git a/org.eclipse.virgo.kernel.osgi/src/test/java/org/eclipse/virgo/kernel/osgi/region/hook/RegionBundleFindHookTests.java b/org.eclipse.virgo.kernel.osgi/src/test/java/org/eclipse/virgo/kernel/osgi/region/hook/RegionBundleFindHookTests.java
index 5370e5f..823fe4f 100644
--- a/org.eclipse.virgo.kernel.osgi/src/test/java/org/eclipse/virgo/kernel/osgi/region/hook/RegionBundleFindHookTests.java
+++ b/org.eclipse.virgo.kernel.osgi/src/test/java/org/eclipse/virgo/kernel/osgi/region/hook/RegionBundleFindHookTests.java
@@ -21,12 +21,10 @@ import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
-import junit.framework.Assert;
-
import org.eclipse.virgo.kernel.osgi.region.Region;
import org.eclipse.virgo.kernel.osgi.region.RegionFilter;
+import org.eclipse.virgo.kernel.osgi.region.RegionFilterBuilder;
import org.eclipse.virgo.kernel.osgi.region.internal.StandardRegionDigraph;
-import org.eclipse.virgo.kernel.osgi.region.internal.StandardRegionFilter;
import org.eclipse.virgo.teststubs.osgi.framework.StubBundle;
import org.eclipse.virgo.teststubs.osgi.framework.StubBundleContext;
import org.junit.After;
@@ -80,7 +78,7 @@ public class RegionBundleFindHookTests {
this.bundleId = 1L;
this.regions = new HashMap<String, Region>();
this.bundles = new HashMap<String, Bundle>();
-
+
StubBundle stubSystemBundle = new StubBundle(0L, "osgi.framework", new Version("0"), "loc");
StubBundleContext stubBundleContext = new StubBundleContext();
stubBundleContext.addInstalledBundle(stubSystemBundle);
@@ -118,7 +116,7 @@ public class RegionBundleFindHookTests {
}
@Test
- public void testFindConnectedRegionAllowed() throws BundleException {
+ public void testFindConnectedRegionAllowed() throws BundleException, InvalidSyntaxException {
RegionFilter filter = createFilter(BUNDLE_B);
region(REGION_A).connectRegion(region(REGION_B), filter);
@@ -128,7 +126,7 @@ public class RegionBundleFindHookTests {
}
@Test
- public void testFindConnectedRegionFiltering() throws BundleException {
+ public void testFindConnectedRegionFiltering() throws BundleException, InvalidSyntaxException {
region(REGION_A).connectRegion(region(REGION_B), createFilter(BUNDLE_B));
Bundle x = createBundle(BUNDLE_X);
region(REGION_B).addBundle(x);
@@ -141,7 +139,7 @@ public class RegionBundleFindHookTests {
}
@Test
- public void testFindTransitive() throws BundleException {
+ public void testFindTransitive() throws BundleException, InvalidSyntaxException {
region(REGION_A).connectRegion(region(REGION_B), createFilter(BUNDLE_C));
region(REGION_B).connectRegion(region(REGION_C), createFilter(BUNDLE_C));
region(REGION_C).addBundle(bundle(BUNDLE_X));
@@ -157,7 +155,7 @@ public class RegionBundleFindHookTests {
}
@Test
- public void testFindInCyclicGraph() throws BundleException {
+ public void testFindInCyclicGraph() throws BundleException, InvalidSyntaxException {
region(REGION_D).addBundle(bundle(BUNDLE_X));
region(REGION_A).connectRegion(region(REGION_B), createFilter(BUNDLE_D, BUNDLE_X));
@@ -235,21 +233,16 @@ public class RegionBundleFindHookTests {
return this.regions.get(regionName);
}
- private RegionFilter createFilter(String... bundleSymbolicNames) {
+ private RegionFilter createFilter(String... bundleSymbolicNames) throws InvalidSyntaxException {
Collection<String> filters = new ArrayList<String>(bundleSymbolicNames.length);
for (String bundleSymbolicName : bundleSymbolicNames) {
- filters.add('(' + RegionFilter.VISIBLE_BUNDLE_NAMESPACE + '=' + bundleSymbolicName + ')');
+ filters.add('(' + RegionFilter.VISIBLE_BUNDLE_NAMESPACE + '=' + bundleSymbolicName + ')');
}
- Map<String, Collection<String>> policy = new HashMap<String, Collection<String>>();
- if (!filters.isEmpty()) {
- policy.put(RegionFilter.VISIBLE_BUNDLE_NAMESPACE, filters);
+ RegionFilterBuilder builder = digraph.createRegionFilterBuilder();
+ for (String filter : filters) {
+ builder.allow(RegionFilter.VISIBLE_BUNDLE_NAMESPACE, filter);
}
- try {
- return new StandardRegionFilter(policy);
- } catch (InvalidSyntaxException e) {
- Assert.fail(e.getMessage());
- }
- return null; // only for compiling; should not happen
+ return builder.build();
}
private Bundle createBundle(String bundleSymbolicName) {
diff --git a/org.eclipse.virgo.kernel.osgi/src/test/java/org/eclipse/virgo/kernel/osgi/region/hook/RegionResolverHookTests.java b/org.eclipse.virgo.kernel.osgi/src/test/java/org/eclipse/virgo/kernel/osgi/region/hook/RegionResolverHookTests.java
index 73631b9..df294de 100644
--- a/org.eclipse.virgo.kernel.osgi/src/test/java/org/eclipse/virgo/kernel/osgi/region/hook/RegionResolverHookTests.java
+++ b/org.eclipse.virgo.kernel.osgi/src/test/java/org/eclipse/virgo/kernel/osgi/region/hook/RegionResolverHookTests.java
@@ -16,19 +16,16 @@ import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import java.util.ArrayList;
-import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
-import junit.framework.Assert;
-
import org.eclipse.virgo.kernel.osgi.region.Region;
import org.eclipse.virgo.kernel.osgi.region.RegionFilter;
+import org.eclipse.virgo.kernel.osgi.region.RegionFilterBuilder;
import org.eclipse.virgo.kernel.osgi.region.internal.StandardRegionDigraph;
-import org.eclipse.virgo.kernel.osgi.region.internal.StandardRegionFilter;
import org.eclipse.virgo.teststubs.osgi.framework.StubBundle;
import org.eclipse.virgo.teststubs.osgi.framework.StubBundleContext;
import org.junit.After;
@@ -133,7 +130,7 @@ public class RegionResolverHookTests {
}
@Test
- public void testResolveConnectedRegionAllowed() throws BundleException {
+ public void testResolveConnectedRegionAllowed() throws BundleException, InvalidSyntaxException {
RegionFilter filter = createFilter(PACKAGE_B);
region(REGION_A).connectRegion(region(REGION_B), filter);
@@ -143,7 +140,7 @@ public class RegionResolverHookTests {
}
@Test
- public void testResolveBundleCapabilityConnectedRegionAllowed() throws BundleException {
+ public void testResolveBundleCapabilityConnectedRegionAllowed() throws BundleException, InvalidSyntaxException {
RegionFilter filter = createBundleFilter(BUNDLE_B, BUNDLE_VERSION);
region(REGION_A).connectRegion(region(REGION_B), filter);
@@ -153,7 +150,7 @@ public class RegionResolverHookTests {
}
@Test
- public void testResolveConnectedRegionFiltering() throws BundleException {
+ public void testResolveConnectedRegionFiltering() throws BundleException, InvalidSyntaxException {
region(REGION_A).connectRegion(region(REGION_B), createFilter(PACKAGE_B));
Bundle x = createBundle(BUNDLE_X);
region(REGION_B).addBundle(x);
@@ -166,7 +163,7 @@ public class RegionResolverHookTests {
}
@Test
- public void testResolveBundleConnectedRegionFiltering() throws BundleException {
+ public void testResolveBundleConnectedRegionFiltering() throws BundleException, InvalidSyntaxException {
RegionFilter filter = createBundleFilter(BUNDLE_B, BUNDLE_VERSION);
region(REGION_A).connectRegion(region(REGION_B), filter);
Bundle x = createBundle(BUNDLE_X);
@@ -180,7 +177,7 @@ public class RegionResolverHookTests {
}
@Test
- public void testResolveTransitive() throws BundleException {
+ public void testResolveTransitive() throws BundleException, InvalidSyntaxException {
region(REGION_A).connectRegion(region(REGION_B), createFilter(PACKAGE_C));
region(REGION_B).connectRegion(region(REGION_C), createFilter(PACKAGE_C));
region(REGION_C).addBundle(bundle(BUNDLE_X));
@@ -196,7 +193,7 @@ public class RegionResolverHookTests {
}
@Test
- public void testResolveInCyclicGraph() throws BundleException {
+ public void testResolveInCyclicGraph() throws BundleException, InvalidSyntaxException {
region(REGION_D).addBundle(bundle(BUNDLE_X));
region(REGION_A).connectRegion(region(REGION_B), createFilter(PACKAGE_D, PACKAGE_X));
@@ -288,35 +285,23 @@ public class RegionResolverHookTests {
return this.regions.get(regionName);
}
- private RegionFilter createFilter(final String... packageNames) {
+ private RegionFilter createFilter(final String... packageNames) throws InvalidSyntaxException {
Collection<String> filters = new ArrayList<String>(packageNames.length);
for (String pkg : packageNames) {
- filters.add('(' + RegionFilter.VISIBLE_PACKAGE_NAMESPACE + '=' + pkg + ')');
- }
- Map<String, Collection<String>> policy = new HashMap<String, Collection<String>>();
- if (!filters.isEmpty()) {
- policy.put(RegionFilter.VISIBLE_PACKAGE_NAMESPACE, filters);
- }
- try {
- return new StandardRegionFilter(policy);
- } catch (InvalidSyntaxException e) {
- Assert.fail(e.getMessage());
- }
- return null; // only for compiling; should not happen
+ filters.add('(' + RegionFilter.VISIBLE_PACKAGE_NAMESPACE + '=' + pkg + ')');
+ }
+ RegionFilterBuilder builder = digraph.createRegionFilterBuilder();
+ for (String filter : filters) {
+ builder.allow(RegionFilter.VISIBLE_PACKAGE_NAMESPACE, filter);
+ }
+ return builder.build();
}
- private RegionFilter createBundleFilter(String bundleSymbolicName, Version bundleVersion) {
- String bundleFilter = "(&(" + RegionFilter.VISIBLE_BUNDLE_NAMESPACE + '=' + bundleSymbolicName + ')' +
- '(' + Constants.BUNDLE_VERSION_ATTRIBUTE + ">=" + (bundleVersion == null ? "0" : bundleVersion.toString()) + "))";
-
- try {
- Map<String, Collection<String>> policy = new HashMap<String, Collection<String>>();
- policy.put(RegionFilter.VISIBLE_BUNDLE_NAMESPACE, Arrays.asList(bundleFilter));
- return new StandardRegionFilter(policy);
- } catch (InvalidSyntaxException e) {
- Assert.fail(e.getMessage());
- }
- return null; // only for compiling; should not happen
+ private RegionFilter createBundleFilter(String bundleSymbolicName, Version bundleVersion) throws InvalidSyntaxException {
+ String bundleFilter = "(&(" + RegionFilter.VISIBLE_BUNDLE_NAMESPACE + '=' + bundleSymbolicName + ')' + '('
+ + Constants.BUNDLE_VERSION_ATTRIBUTE + ">=" + (bundleVersion == null ? "0" : bundleVersion.toString()) + "))";
+ RegionFilterBuilder builder = digraph.createRegionFilterBuilder();
+ return builder.allow(RegionFilter.VISIBLE_BUNDLE_NAMESPACE, bundleFilter).build();
}
private Bundle createBundle(String bundleSymbolicName) {
@@ -324,7 +309,7 @@ public class RegionResolverHookTests {
this.bundles.put(bundleSymbolicName, stubBundle);
return stubBundle;
}
-
+
private BundleRequirement bundleRequirement(String bundleSymbolicName) {
return new StubBundleRequirement(bundle(bundleSymbolicName));
}
@@ -478,9 +463,9 @@ public class RegionResolverHookTests {
}
}
-
+
private final class StubBundleRequirement implements BundleRequirement {
-
+
private final StubBundleRevision bundleRevision;
private StubBundleRequirement(Bundle bundle) {
@@ -511,7 +496,7 @@ public class RegionResolverHookTests {
public boolean matches(BundleCapability capability) {
throw new UnsupportedOperationException();
}
-
+
}
private final class StubBundleRevision implements BundleRevision {
diff --git a/org.eclipse.virgo.kernel.osgi/src/test/java/org/eclipse/virgo/kernel/osgi/region/hook/RegionServiceFindHookTests.java b/org.eclipse.virgo.kernel.osgi/src/test/java/org/eclipse/virgo/kernel/osgi/region/hook/RegionServiceFindHookTests.java
index 7f4fa28..46e7288 100644
--- a/org.eclipse.virgo.kernel.osgi/src/test/java/org/eclipse/virgo/kernel/osgi/region/hook/RegionServiceFindHookTests.java
+++ b/org.eclipse.virgo.kernel.osgi/src/test/java/org/eclipse/virgo/kernel/osgi/region/hook/RegionServiceFindHookTests.java
@@ -21,12 +21,10 @@ import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
-import junit.framework.Assert;
-
import org.eclipse.virgo.kernel.osgi.region.Region;
import org.eclipse.virgo.kernel.osgi.region.RegionFilter;
+import org.eclipse.virgo.kernel.osgi.region.RegionFilterBuilder;
import org.eclipse.virgo.kernel.osgi.region.internal.StandardRegionDigraph;
-import org.eclipse.virgo.kernel.osgi.region.internal.StandardRegionFilter;
import org.eclipse.virgo.teststubs.osgi.framework.StubBundle;
import org.eclipse.virgo.teststubs.osgi.framework.StubBundleContext;
import org.eclipse.virgo.teststubs.osgi.framework.StubServiceReference;
@@ -90,7 +88,7 @@ public class RegionServiceFindHookTests {
this.regions = new HashMap<String, Region>();
this.bundles = new HashMap<String, Bundle>();
this.serviceReferences = new HashMap<String, ServiceReference<Object>>();
-
+
StubBundle stubSystemBundle = new StubBundle(0L, "osgi.framework", new Version("0"), "loc");
StubBundleContext stubBundleContext = new StubBundleContext();
stubBundleContext.addInstalledBundle(stubSystemBundle);
@@ -128,7 +126,7 @@ public class RegionServiceFindHookTests {
}
@Test
- public void testFindConnectedRegionAllowed() throws BundleException {
+ public void testFindConnectedRegionAllowed() throws BundleException, InvalidSyntaxException {
RegionFilter filter = createFilter(BUNDLE_B);
region(REGION_A).connectRegion(region(REGION_B), filter);
@@ -138,7 +136,7 @@ public class RegionServiceFindHookTests {
}
@Test
- public void testFindConnectedRegionFiltering() throws BundleException {
+ public void testFindConnectedRegionFiltering() throws BundleException, InvalidSyntaxException {
region(REGION_A).connectRegion(region(REGION_B), createFilter(BUNDLE_B));
Bundle x = createBundle(BUNDLE_X);
region(REGION_B).addBundle(x);
@@ -151,7 +149,7 @@ public class RegionServiceFindHookTests {
}
@Test
- public void testFindTransitive() throws BundleException {
+ public void testFindTransitive() throws BundleException, InvalidSyntaxException {
region(REGION_A).connectRegion(region(REGION_B), createFilter(BUNDLE_C));
region(REGION_B).connectRegion(region(REGION_C), createFilter(BUNDLE_C));
region(REGION_C).addBundle(bundle(BUNDLE_X));
@@ -166,7 +164,7 @@ public class RegionServiceFindHookTests {
}
@Test
- public void testFindInCyclicGraph() throws BundleException {
+ public void testFindInCyclicGraph() throws BundleException, InvalidSyntaxException {
region(REGION_D).addBundle(bundle(BUNDLE_X));
region(REGION_A).connectRegion(region(REGION_B), createFilter(BUNDLE_D, BUNDLE_X));
@@ -244,21 +242,16 @@ public class RegionServiceFindHookTests {
return this.regions.get(regionName);
}
- private RegionFilter createFilter(final String... referenceNames) {
+ private RegionFilter createFilter(final String... referenceNames) throws InvalidSyntaxException {
Collection<String> filters = new ArrayList<String>(referenceNames.length);
for (String referenceName : referenceNames) {
- filters.add('(' + Constants.OBJECTCLASS + '=' + referenceName + ')');
+ filters.add('(' + Constants.OBJECTCLASS + '=' + referenceName + ')');
}
- Map<String, Collection<String>> policy = new HashMap<String, Collection<String>>();
- if (!filters.isEmpty()) {
- policy.put(RegionFilter.VISIBLE_SERVICE_NAMESPACE, filters);
+ RegionFilterBuilder builder = digraph.createRegionFilterBuilder();
+ for (String filter : filters) {
+ builder.allow(RegionFilter.VISIBLE_SERVICE_NAMESPACE, filter);
}
- try {
- return new StandardRegionFilter(policy);
- } catch (InvalidSyntaxException e) {
- Assert.fail(e.getMessage());
- }
- return null; // only for compiling; should not happen
+ return builder.build();
}
private Bundle createBundle(String bundleSymbolicName) {
diff --git a/org.eclipse.virgo.kernel.osgi/src/test/java/org/eclipse/virgo/kernel/osgi/region/internal/StandardRegionDigraphTests.java b/org.eclipse.virgo.kernel.osgi/src/test/java/org/eclipse/virgo/kernel/osgi/region/internal/StandardRegionDigraphTests.java
index 3d20c56..64de3fe 100644
--- a/org.eclipse.virgo.kernel.osgi/src/test/java/org/eclipse/virgo/kernel/osgi/region/internal/StandardRegionDigraphTests.java
+++ b/org.eclipse.virgo.kernel.osgi/src/test/java/org/eclipse/virgo/kernel/osgi/region/internal/StandardRegionDigraphTests.java
@@ -19,11 +19,6 @@ import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
-import java.util.Arrays;
-import java.util.Collection;
-import java.util.Collections;
-import java.util.HashMap;
-import java.util.Map;
import java.util.Set;
import org.easymock.EasyMock;
@@ -65,10 +60,10 @@ public class StandardRegionDigraphTests {
StubBundleContext stubBundleContext = new StubBundleContext();
stubBundleContext.addInstalledBundle(stubSystemBundle);
this.digraph = new StandardRegionDigraph(stubBundleContext, new ThreadLocal<Region>());
-
+
this.mockRegion1 = EasyMock.createMock(Region.class);
EasyMock.expect(this.mockRegion1.getName()).andReturn("mockRegion1").anyTimes();
-
+
this.mockRegion2 = EasyMock.createMock(Region.class);
EasyMock.expect(this.mockRegion2.getName()).andReturn("mockRegion2").anyTimes();
@@ -79,25 +74,19 @@ public class StandardRegionDigraphTests {
}
private void setDefaultFilters() throws InvalidSyntaxException {
- this.regionFilter1 = new StandardRegionFilter(Collections.EMPTY_MAP);
- this.regionFilter2 = new StandardRegionFilter(Collections.EMPTY_MAP);
+ this.regionFilter1 = digraph.createRegionFilterBuilder().build();
+ this.regionFilter2 = digraph.createRegionFilterBuilder().build();
}
-
private void setAllowedFilters(OrderedPair<String, Version> b1, OrderedPair<String, Version> b2) throws InvalidSyntaxException {
- String filter1 = "(&(" +
- RegionFilter.VISIBLE_BUNDLE_NAMESPACE + "=" + b1.getFirst() + ")(" +
- Constants.BUNDLE_VERSION_ATTRIBUTE + "=" + b1.getSecond() + "))";
- Map<String, Collection<String>> policy1 = new HashMap<String, Collection<String>>();
- policy1.put(RegionFilter.VISIBLE_BUNDLE_NAMESPACE, Arrays.asList(filter1));
- regionFilter1 = new StandardRegionFilter(policy1);
-
- String filter2 = "(&(" +
- RegionFilter.VISIBLE_BUNDLE_NAMESPACE + "=" + b1.getFirst() + ")(" +
- Constants.BUNDLE_VERSION_ATTRIBUTE + "=" + b1.getSecond() + "))";
- Map<String, Collection<String>> policy2 = new HashMap<String, Collection<String>>();
- policy2.put(RegionFilter.VISIBLE_BUNDLE_NAMESPACE, Arrays.asList(filter2));
- regionFilter2 = new StandardRegionFilter(policy2);
+ String filter1 = "(&(" + RegionFilter.VISIBLE_BUNDLE_NAMESPACE + "=" + b1.getFirst() + ")(" + Constants.BUNDLE_VERSION_ATTRIBUTE + "="
+ + b1.getSecond() + "))";
+ regionFilter1 = digraph.createRegionFilterBuilder().allow(RegionFilter.VISIBLE_BUNDLE_NAMESPACE, filter1).build();
+
+ String filter2 = "(&(" + RegionFilter.VISIBLE_BUNDLE_NAMESPACE + "=" + b1.getFirst() + ")(" + Constants.BUNDLE_VERSION_ATTRIBUTE + "="
+ + b1.getSecond() + "))";
+ regionFilter2 = digraph.createRegionFilterBuilder().allow(RegionFilter.VISIBLE_BUNDLE_NAMESPACE, filter2).build();
+
}
private void replayMocks() {
@@ -185,17 +174,17 @@ public class StandardRegionDigraphTests {
assertNull(this.digraph.getRegion("mockRegion1"));
assertNotNull(this.digraph.getRegion("mockRegion2"));
}
-
+
@Test
public void testGetRegions() throws BundleException, InvalidSyntaxException {
setDefaultFilters();
replayMocks();
-
+
this.digraph.connect(this.mockRegion1, this.regionFilter1, this.mockRegion2);
Set<Region> regions = this.digraph.getRegions();
assertEquals(2, regions.size());
assertTrue(regions.contains(this.mockRegion1));
assertTrue(regions.contains(this.mockRegion2));
}
-
+
}
diff --git a/org.eclipse.virgo.kernel.osgi/src/test/java/org/eclipse/virgo/kernel/osgi/region/internal/StandardRegionFilterTests.java b/org.eclipse.virgo.kernel.osgi/src/test/java/org/eclipse/virgo/kernel/osgi/region/internal/StandardRegionFilterTests.java
index 1a020c3..97858ef 100644
--- a/org.eclipse.virgo.kernel.osgi/src/test/java/org/eclipse/virgo/kernel/osgi/region/internal/StandardRegionFilterTests.java
+++ b/org.eclipse.virgo.kernel.osgi/src/test/java/org/eclipse/virgo/kernel/osgi/region/internal/StandardRegionFilterTests.java
@@ -27,15 +27,12 @@ import org.easymock.EasyMock;
import org.eclipse.virgo.kernel.osgi.region.RegionFilter;
import org.eclipse.virgo.teststubs.osgi.framework.StubBundle;
import org.eclipse.virgo.teststubs.osgi.framework.StubBundleContext;
-import org.eclipse.virgo.teststubs.osgi.framework.StubServiceReference;
import org.eclipse.virgo.teststubs.osgi.framework.StubServiceRegistration;
import org.junit.After;
-import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.osgi.framework.Constants;
import org.osgi.framework.InvalidSyntaxException;
-import org.osgi.framework.ServiceReference;
import org.osgi.framework.ServiceRegistration;
import org.osgi.framework.Version;
import org.osgi.framework.wiring.BundleCapability;
@@ -48,7 +45,9 @@ public class StandardRegionFilterTests {
private static final Version BUNDLE_VERSION = new Version("0");
private StubBundle stubBundle;
+
private String packageImportPolicy = "(" + BundleRevision.PACKAGE_NAMESPACE + "=foo)";
+
private String serviceImportPolicy = "(" + Constants.OBJECTCLASS + "=foo.Service)";
@Before
@@ -61,18 +60,17 @@ public class StandardRegionFilterTests {
}
private RegionFilter createBundleFilter(String bundleSymbolicName, Version bundleVersion) throws InvalidSyntaxException {
- String filter = "(&(" +
- RegionFilter.VISIBLE_BUNDLE_NAMESPACE + "=" + bundleSymbolicName + ")(" +
- Constants.BUNDLE_VERSION_ATTRIBUTE + ">=" + bundleVersion + "))";
- Map<String, Collection<String>> policy = new HashMap<String, Collection<String>>();
- policy.put(RegionFilter.VISIBLE_BUNDLE_NAMESPACE, Arrays.asList(filter));
- return new StandardRegionFilter(policy);
- }
+ String filter = "(&(" + RegionFilter.VISIBLE_BUNDLE_NAMESPACE + "=" + bundleSymbolicName + ")(" + Constants.BUNDLE_VERSION_ATTRIBUTE + ">="
+ + bundleVersion + "))";
+ return new StandardRegionFilterBuilder().allow(RegionFilter.VISIBLE_BUNDLE_NAMESPACE, filter).build();
+ }
private RegionFilter createRegionFilter(String namespace, Collection<String> filters) throws InvalidSyntaxException {
- Map<String, Collection<String>> policy = new HashMap<String, Collection<String>>();
- policy.put(namespace, filters);
- return new StandardRegionFilter(policy);
+ StandardRegionFilterBuilder builder = new StandardRegionFilterBuilder();
+ for (String filter : filters) {
+ builder.allow(namespace, filter);
+ }
+ return builder.build();
}
@Test
@@ -81,41 +79,41 @@ public class StandardRegionFilterTests {
assertTrue(regionFilter.isBundleAllowed(stubBundle));
}
- @Test
+ @Test
public void testBundleNotAllowed() throws InvalidSyntaxException {
- RegionFilter regionFilter = new StandardRegionFilter(Collections.EMPTY_MAP);
+ RegionFilter regionFilter = new StandardRegionFilter(Collections.EMPTY_MAP);
assertFalse(regionFilter.isBundleAllowed(stubBundle));
}
@Test
public void testBundleNotAllowedInRange() throws InvalidSyntaxException {
- RegionFilter regionFilter = createBundleFilter(BUNDLE_SYMBOLIC_NAME, new Version(1,0,0));
+ RegionFilter regionFilter = createBundleFilter(BUNDLE_SYMBOLIC_NAME, new Version(1, 0, 0));
assertFalse(regionFilter.isBundleAllowed(stubBundle));
}
@Test
public void testPackageImportAllowed() throws InvalidSyntaxException {
- RegionFilter regionFilter = createRegionFilter(RegionFilter.VISIBLE_PACKAGE_NAMESPACE, Arrays.asList(packageImportPolicy));
- BundleCapability packageCapability = EasyMock.createMock(BundleCapability.class);
- Map<String, Object> attrs = new HashMap<String, Object>();
- attrs.put(BundleRevision.PACKAGE_NAMESPACE, "foo");
- EasyMock.expect(packageCapability.getNamespace()).andReturn(BundleRevision.PACKAGE_NAMESPACE).anyTimes();
- EasyMock.expect(packageCapability.getAttributes()).andReturn(attrs).anyTimes();
- EasyMock.replay(packageCapability);
- assertTrue(regionFilter.isCapabilityAllowed(packageCapability));
+ RegionFilter regionFilter = createRegionFilter(RegionFilter.VISIBLE_PACKAGE_NAMESPACE, Arrays.asList(packageImportPolicy));
+ BundleCapability packageCapability = EasyMock.createMock(BundleCapability.class);
+ Map<String, Object> attrs = new HashMap<String, Object>();
+ attrs.put(BundleRevision.PACKAGE_NAMESPACE, "foo");
+ EasyMock.expect(packageCapability.getNamespace()).andReturn(BundleRevision.PACKAGE_NAMESPACE).anyTimes();
+ EasyMock.expect(packageCapability.getAttributes()).andReturn(attrs).anyTimes();
+ EasyMock.replay(packageCapability);
+ assertTrue(regionFilter.isCapabilityAllowed(packageCapability));
assertEquals(Arrays.asList(this.packageImportPolicy), regionFilter.getSharingPolicy().get(RegionFilter.VISIBLE_PACKAGE_NAMESPACE));
}
@Test
public void testPackageImportNotAllowed() throws InvalidSyntaxException {
- RegionFilter regionFilter = createRegionFilter(RegionFilter.VISIBLE_PACKAGE_NAMESPACE, Arrays.asList(packageImportPolicy));
- BundleCapability packageCapability = EasyMock.createMock(BundleCapability.class);
- Map<String, Object> attrs = new HashMap<String, Object>();
- attrs.put(BundleRevision.PACKAGE_NAMESPACE, "bar");
- EasyMock.expect(packageCapability.getNamespace()).andReturn(BundleRevision.PACKAGE_NAMESPACE).anyTimes();
- EasyMock.expect(packageCapability.getAttributes()).andReturn(attrs).anyTimes();
- EasyMock.replay(packageCapability);
- assertFalse(regionFilter.isCapabilityAllowed(packageCapability));
+ RegionFilter regionFilter = createRegionFilter(RegionFilter.VISIBLE_PACKAGE_NAMESPACE, Arrays.asList(packageImportPolicy));
+ BundleCapability packageCapability = EasyMock.createMock(BundleCapability.class);
+ Map<String, Object> attrs = new HashMap<String, Object>();
+ attrs.put(BundleRevision.PACKAGE_NAMESPACE, "bar");
+ EasyMock.expect(packageCapability.getNamespace()).andReturn(BundleRevision.PACKAGE_NAMESPACE).anyTimes();
+ EasyMock.expect(packageCapability.getAttributes()).andReturn(attrs).anyTimes();
+ EasyMock.replay(packageCapability);
+ assertFalse(regionFilter.isCapabilityAllowed(packageCapability));
assertEquals(Arrays.asList(this.packageImportPolicy), regionFilter.getSharingPolicy().get(RegionFilter.VISIBLE_PACKAGE_NAMESPACE));
}
@@ -126,7 +124,7 @@ public class StandardRegionFilterTests {
assertTrue(regionFilter.isServiceAllowed(reg.getReference()));
assertEquals(Arrays.asList(serviceImportPolicy), regionFilter.getSharingPolicy().get(RegionFilter.VISIBLE_SERVICE_NAMESPACE));
}
-
+
@Test
public void testServiceImportNotAllowed() throws InvalidSyntaxException {
RegionFilter regionFilter = createRegionFilter(RegionFilter.VISIBLE_SERVICE_NAMESPACE, Arrays.asList(serviceImportPolicy));
diff --git a/org.eclipse.virgo.kernel.userregion/src/main/java/org/eclipse/virgo/kernel/userregion/internal/quasi/StandardQuasiFramework.java b/org.eclipse.virgo.kernel.userregion/src/main/java/org/eclipse/virgo/kernel/userregion/internal/quasi/StandardQuasiFramework.java
index 607cba2..ac9d3f5 100644
--- a/org.eclipse.virgo.kernel.userregion/src/main/java/org/eclipse/virgo/kernel/userregion/internal/quasi/StandardQuasiFramework.java
+++ b/org.eclipse.virgo.kernel.userregion/src/main/java/org/eclipse/virgo/kernel/userregion/internal/quasi/StandardQuasiFramework.java
@@ -18,14 +18,10 @@ import java.net.MalformedURLException;
import java.net.URI;
import java.net.URL;
import java.util.ArrayList;
-import java.util.Arrays;
-import java.util.Collection;
import java.util.Collections;
import java.util.Dictionary;
-import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
-import java.util.Map;
import java.util.Set;
import org.eclipse.osgi.internal.resolver.StateImpl;
@@ -73,7 +69,10 @@ import org.slf4j.LoggerFactory;
*
*/
final class StandardQuasiFramework implements QuasiFramework {
- public final RegionFilter TOP;
+
+ private final RegionFilter TOP;
+
+ private final String TOP_FILTER = "(|(!(x=*))(x=*))";
private static final String REGION_LOCATION_DELIMITER = "@";
@@ -116,14 +115,12 @@ final class StandardQuasiFramework implements QuasiFramework {
StandardQuasiFramework(BundleContext bundleContext, State state, PlatformAdmin platformAdmin, ResolutionFailureDetective detective,
Repository repository, TransformedManifestProvidingBundleFileWrapper bundleTransformationHandler, RegionDigraph regionDigraph) {
- Map<String, Collection<String>> all = new HashMap<String, Collection<String>>();
- all.put(RegionFilter.VISIBLE_ALL_NAMESPACE, Arrays.asList(RegionFilter.ALL));
- try {
- TOP = regionDigraph.createRegionFilter(all);
- } catch (InvalidSyntaxException e) {
- throw new RuntimeException(e);
- }
- this.bundleContext = bundleContext;
+ try {
+ TOP = regionDigraph.createRegionFilterBuilder().allow(RegionFilter.VISIBLE_ALL_NAMESPACE, TOP_FILTER).build();
+ } catch (InvalidSyntaxException e) {
+ throw new RuntimeException(e);
+ }
+ this.bundleContext = bundleContext;
this.state = state;
this.stateObjectFactory = platformAdmin.getFactory();
this.detective = detective;
diff --git a/org.eclipse.virgo.kernel.userregionfactory/src/main/java/org/eclipse/virgo/kernel/userregionfactory/Activator.java b/org.eclipse.virgo.kernel.userregionfactory/src/main/java/org/eclipse/virgo/kernel/userregionfactory/Activator.java
index 8107a18..26efcb9 100644
--- a/org.eclipse.virgo.kernel.userregionfactory/src/main/java/org/eclipse/virgo/kernel/userregionfactory/Activator.java
+++ b/org.eclipse.virgo.kernel.userregionfactory/src/main/java/org/eclipse/virgo/kernel/userregionfactory/Activator.java
@@ -32,6 +32,7 @@ import org.eclipse.virgo.kernel.osgi.framework.OsgiServiceHolder;
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.RegionFilterBuilder;
import org.eclipse.virgo.medic.eventlog.EventLogger;
import org.eclipse.virgo.osgi.launcher.parser.ArgumentParser;
import org.eclipse.virgo.osgi.launcher.parser.BundleEntry;
@@ -150,7 +151,8 @@ public final class Activator implements BundleActivator {
}
}
- private void createUserRegion(BundleContext userRegionBundleContext, RegionDigraph regionDigraph, EventLogger eventLogger) throws BundleException, InvalidSyntaxException {
+ private void createUserRegion(BundleContext userRegionBundleContext, RegionDigraph regionDigraph, EventLogger eventLogger)
+ throws BundleException, InvalidSyntaxException {
BundleContext systemBundleContext = getSystemBundleContext();
Bundle userRegionFactoryBundle = userRegionBundleContext.getBundle();
@@ -176,33 +178,43 @@ public final class Activator implements BundleActivator {
}
private RegionFilter createUserRegionFilter(RegionDigraph digraph) throws InvalidSyntaxException {
- Map<String, Collection<String>> policy = new HashMap<String, Collection<String>>();
- Collection<String> serviceFilter = classesToFilter(this.regionServiceExports);
- policy.put(RegionFilter.VISIBLE_SERVICE_NAMESPACE, serviceFilter);
- return digraph.createRegionFilter(policy);
+ Collection<String> serviceFilters = classesToFilter(this.regionServiceExports);
+ RegionFilterBuilder builder = digraph.createRegionFilterBuilder();
+ for (String filter : serviceFilters) {
+ builder.allow(RegionFilter.VISIBLE_SERVICE_NAMESPACE, filter);
+ }
+ return builder.build();
}
private Region getKernelRegion(RegionDigraph regionDigraph) {
return regionDigraph.iterator().next();
}
- private RegionFilter createKernelFilter(RegionDigraph digraph, BundleContext systemBundleContext, EventLogger eventLogger) throws BundleException, InvalidSyntaxException {
- Map<String, Collection<String>> policy = new HashMap<String, Collection<String>>();
- allowImportedBundles(policy, eventLogger);
- policy.put(RegionFilter.VISIBLE_PACKAGE_NAMESPACE, createUserRegionPackageImportPolicy(systemBundleContext));
- Collection<String> serviceFilter = classesToFilter(this.regionServiceImports);
- policy.put(RegionFilter.VISIBLE_SERVICE_NAMESPACE, serviceFilter);
- return digraph.createRegionFilter(policy);
+ private RegionFilter createKernelFilter(RegionDigraph digraph, BundleContext systemBundleContext, EventLogger eventLogger)
+ throws BundleException, InvalidSyntaxException {
+ RegionFilterBuilder builder = digraph.createRegionFilterBuilder();
+ Collection<String> allowedBundles = allowImportedBundles(eventLogger);
+ for (String filter : allowedBundles) {
+ builder.allow(RegionFilter.VISIBLE_BUNDLE_NAMESPACE, filter);
+ }
+ Collection<String> allowedPackages = createUserRegionPackageImportPolicy(systemBundleContext);
+ for (String filter : allowedPackages) {
+ builder.allow(RegionFilter.VISIBLE_PACKAGE_NAMESPACE, filter);
+ }
+ Collection<String> allowedServices = classesToFilter(this.regionServiceImports);
+ for (String filter : allowedServices) {
+ builder.allow(RegionFilter.VISIBLE_SERVICE_NAMESPACE, filter);
+ }
+ return builder.build();
}
- private void allowImportedBundles(Map<String, Collection<String>> policy, EventLogger eventLogger) {
+ private Collection<String> allowImportedBundles(EventLogger eventLogger) {
String userRegionBundleImports = this.regionBundleImports != null ? this.regionBundleImports
: this.bundleContext.getProperty(USER_REGION_BUNDLE_IMPORTS_PROPERTY);
RequireBundle bundleImportsAsRequireBundle = representBundleImportsAsRequireBundle(userRegionBundleImports, eventLogger);
- Collection<String> importedBundles = importBundleToFilter(bundleImportsAsRequireBundle.getRequiredBundles());
- policy.put(RegionFilter.VISIBLE_BUNDLE_NAMESPACE, importedBundles);
- }
+ return importBundleToFilter(bundleImportsAsRequireBundle.getRequiredBundles());
+ }
private RequireBundle representBundleImportsAsRequireBundle(String userRegionBundleImportsProperty, EventLogger eventLogger) {
Dictionary<String, String> headers = new Hashtable<String, String>();
@@ -212,63 +224,63 @@ public final class Activator implements BundleActivator {
}
private static Collection<String> importBundleToFilter(List<RequiredBundle> importedBundles) {
- if (importedBundles == null || importedBundles.isEmpty())
- return Collections.emptyList();
- Collection<String> result = new ArrayList<String>(importedBundles.size());
- for (RequiredBundle importedBundle : importedBundles) {
- StringBuilder f = new StringBuilder();
- f.append("(&(").append(RegionFilter.VISIBLE_BUNDLE_NAMESPACE).append('=').append(importedBundle.getBundleSymbolicName()).append(')');
- addRange(Constants.BUNDLE_VERSION_ATTRIBUTE, importedBundle.getBundleVersion(), f);
- f.append(')');
- result.add(f.toString());
- }
- return result;
+ if (importedBundles == null || importedBundles.isEmpty())
+ return Collections.emptyList();
+ Collection<String> result = new ArrayList<String>(importedBundles.size());
+ for (RequiredBundle importedBundle : importedBundles) {
+ StringBuilder f = new StringBuilder();
+ f.append("(&(").append(RegionFilter.VISIBLE_BUNDLE_NAMESPACE).append('=').append(importedBundle.getBundleSymbolicName()).append(')');
+ addRange(Constants.BUNDLE_VERSION_ATTRIBUTE, importedBundle.getBundleVersion(), f);
+ f.append(')');
+ result.add(f.toString());
+ }
+ return result;
}
private static Collection<String> importPackageToFilter(String importList) {
if (importList == null || importList.isEmpty()) {
- return Collections.emptyList();
+ return Collections.emptyList();
}
if (importList.contains(WILDCARD)) {
- throw new IllegalArgumentException("Wildcards not supported in region imports: '" + importList + "'");
+ throw new IllegalArgumentException("Wildcards not supported in region imports: '" + importList + "'");
}
BundleManifest manifest = BundleManifestFactory.createBundleManifest();
manifest.setHeader("Import-Package", importList);
List<ImportedPackage> list = manifest.getImportPackage().getImportedPackages();
if (list.isEmpty())
- return Collections.emptyList();
+ return Collections.emptyList();
Collection<String> filters = new ArrayList<String>(list.size());
for (ImportedPackage importedPackage : list) {
- StringBuilder f = new StringBuilder();
- f.append("(&(").append(RegionFilter.VISIBLE_PACKAGE_NAMESPACE).append('=').append(importedPackage.getPackageName()).append(')');
- Map<String, String> attrs = importedPackage.getAttributes();
- for (Map.Entry<String, String> attr : attrs.entrySet()) {
- if (Constants.VERSION_ATTRIBUTE.equals(attr.getKey()) || Constants.BUNDLE_VERSION_ATTRIBUTE.equals(attr.getKey())) {
- addRange(attr.getKey(), new VersionRange(attr.getValue()), f);
- } else {
- f.append('(').append(attr.getKey()).append('=').append(attr.getValue()).append(')');
- }
- }
- f.append(')');
- filters.add(f.toString());
- }
- return filters;
+ StringBuilder f = new StringBuilder();
+ f.append("(&(").append(RegionFilter.VISIBLE_PACKAGE_NAMESPACE).append('=').append(importedPackage.getPackageName()).append(')');
+ Map<String, String> attrs = importedPackage.getAttributes();
+ for (Map.Entry<String, String> attr : attrs.entrySet()) {
+ if (Constants.VERSION_ATTRIBUTE.equals(attr.getKey()) || Constants.BUNDLE_VERSION_ATTRIBUTE.equals(attr.getKey())) {
+ addRange(attr.getKey(), new VersionRange(attr.getValue()), f);
+ } else {
+ f.append('(').append(attr.getKey()).append('=').append(attr.getValue()).append(')');
+ }
+ }
+ f.append(')');
+ filters.add(f.toString());
+ }
+ return filters;
}
private static void addRange(String key, VersionRange range, StringBuilder f) {
- if (range.isFloorInclusive()) {
- f.append('(' + key + ">=" + range.getFloor() + ')');
- } else {
- f.append("(!(" + key + "<=" + range.getFloor() +"))");
- }
- Version ceiling = range.getCeiling();
- if (ceiling != null) {
- if (range.isCeilingInclusive()) {
- f.append('(' + key + "<=" + ceiling + ')');
- } else {
- f.append("(!(" + key + ">=" + ceiling + "))");
- }
- }
+ if (range.isFloorInclusive()) {
+ f.append('(' + key + ">=" + range.getFloor() + ')');
+ } else {
+ f.append("(!(" + key + "<=" + range.getFloor() + "))");
+ }
+ Version ceiling = range.getCeiling();
+ if (ceiling != null) {
+ if (range.isCeilingInclusive()) {
+ f.append('(' + key + "<=" + ceiling + ')');
+ } else {
+ f.append("(!(" + key + ">=" + ceiling + "))");
+ }
+ }
}
private static Collection<String> classesToFilter(String classList) {