summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorThomas Watson2011-03-08 17:40:34 (EST)
committer Glyn Normington2011-03-08 17:40:34 (EST)
commitff162a9d83339f67d5dfa38049c72e6bca0d561d (patch)
tree82df2590644b2b772a1fa6d8e19cec3637520862
parent872a13f91cbb586989e73b7776dcf992a6f7c48b (diff)
downloadorg.eclipse.virgo.kernel-ff162a9d83339f67d5dfa38049c72e6bca0d561d.zip
org.eclipse.virgo.kernel-ff162a9d83339f67d5dfa38049c72e6bca0d561d.tar.gz
org.eclipse.virgo.kernel-ff162a9d83339f67d5dfa38049c72e6bca0d561d.tar.bz2
- make RegionFilter an inteface
- add createRegionFilter to RegionDigraph - tidy up StandardRegionFilter code
-rw-r--r--org.eclipse.virgo.kernel.osgi/src/main/java/org/eclipse/virgo/kernel/osgi/region/RegionDigraph.java13
-rw-r--r--org.eclipse.virgo.kernel.osgi/src/main/java/org/eclipse/virgo/kernel/osgi/region/RegionFilter.java280
-rw-r--r--org.eclipse.virgo.kernel.osgi/src/main/java/org/eclipse/virgo/kernel/osgi/region/internal/StandardRegionDigraph.java6
-rw-r--r--org.eclipse.virgo.kernel.osgi/src/main/java/org/eclipse/virgo/kernel/osgi/region/internal/StandardRegionFilter.java155
-rw-r--r--org.eclipse.virgo.kernel.osgi/src/test/java/org/eclipse/virgo/kernel/osgi/region/hook/RegionBundleFindHookTests.java16
-rw-r--r--org.eclipse.virgo.kernel.osgi/src/test/java/org/eclipse/virgo/kernel/osgi/region/hook/RegionResolverHookTests.java32
-rw-r--r--org.eclipse.virgo.kernel.osgi/src/test/java/org/eclipse/virgo/kernel/osgi/region/hook/RegionServiceFindHookTests.java16
-rw-r--r--org.eclipse.virgo.kernel.osgi/src/test/java/org/eclipse/virgo/kernel/osgi/region/internal/BundleIdBasedRegionTests.java6
-rw-r--r--org.eclipse.virgo.kernel.osgi/src/test/java/org/eclipse/virgo/kernel/osgi/region/internal/StandardRegionDigraphTests.java49
-rw-r--r--org.eclipse.virgo.kernel.osgi/src/test/java/org/eclipse/virgo/kernel/osgi/region/internal/StandardRegionFilterTests.java86
-rw-r--r--org.eclipse.virgo.kernel.userregion/src/main/java/org/eclipse/virgo/kernel/userregion/internal/quasi/StandardQuasiFramework.java19
-rw-r--r--org.eclipse.virgo.kernel.userregionfactory/src/main/java/org/eclipse/virgo/kernel/userregionfactory/Activator.java34
-rw-r--r--org.eclipse.virgo.kernel.userregionfactory/src/test/java/org/eclipse/virgo/kernel/userregionfactory/UserRegionPackageImportPolicyTests.java114
13 files changed, 402 insertions, 424 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 908f8ac..77e0a9a 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,10 +13,13 @@
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
@@ -66,6 +69,16 @@ 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
+ */
+ RegionFilter createRegionFilter(Map<String, Collection<String>> sharingPolicy) throws InvalidSyntaxException;
+
+ /**
* Removes the given {@link Region} from the digraph along with any edges which have the given region as head or
* tail. If the given region is not present in the digraph, this is not an error and there is no effect.
*
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 e5418a8..082f3f7 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
@@ -19,6 +19,7 @@ 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;
@@ -33,78 +34,93 @@ import org.osgi.framework.wiring.BundleRevision;
*
* <strong>Concurrent Semantics</strong><br />
*
- * This implementations is thread safe.
+ * Implementations must be thread safe.
*
*/
-public class RegionFilter {
-
+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 + "=*)";
- public static final String ALL_REQUIRES = '(' + VISIBLE_REQUIRE_NAMESPACE + "=*)";
- public static final String ALL_HOSTS = '(' + VISIBLE_HOST_NAMESPACE + "=*)";
- public static final String ALL_SERVICES = '(' + org.osgi.framework.Constants.SERVICE_ID + "=*)";
- public static final String ALL_BUNDLES = '(' + org.osgi.framework.Constants.BUNDLE_SYMBOLICNAME_ATTRIBUTE + "=*)";
- // pretty much a hack to make Filter always return true
- public static final String ALL = "(|(x=*)(!(x=*)))";
-
- public static final RegionFilter TOP = new RegionFilter() {
- @Override
- public boolean isBundleAllowed(Bundle bundle) {
- return true;
- }
+ /**
+ * 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 + "=*)";
- @Override
- public boolean isBundleAllowed(BundleRevision bundle) {
- return true;
- }
+ /**
+ * 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 + "=*)";
- @Override
- public boolean isServiceAllowed(ServiceReference<?> service) {
- return true;
- }
+ /**
+ * 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 + "=*)";
- @Override
- public boolean isCapabilityAllowed(BundleCapability capability) {
- return true;
- }
+ /**
+ * 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 + "=*)";
- @Override
- public RegionFilter setFilters(String namespace, Collection<String> filters)
- throws InvalidSyntaxException {
- throw new UnsupportedOperationException("TOP is immutable");
- }
- };
+ /**
+ * 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.
+ */
+ public static final String ALL = "(|(x=*)(!(x=*)))";
- private Object monitor = new Object();
- private Collection<Filter> packages = null;
- private Collection<Filter> requires = null;
- private Collection<Filter> hosts = null;
- private Collection<Filter> services = null;
- private Collection<Filter> bundles = null;
- private Collection<Filter> all = null;
- private Map<String, Collection<Filter>> generics = null;
-
/**
* Determines whether this filter allows the given bundle
*
* @param bundle the bundle
* @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);
- }
+ public boolean isBundleAllowed(Bundle bundle);
/**
* Determines whether this filter allows the given bundle
@@ -112,48 +128,7 @@ public class RegionFilter {
* @param bundle the bundle revision
* @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);
- }
-
- /**
- * Determines whether this filter allows the bundle with the given attributes
- *
- * @param bundleAttributes the bundle attributes
- * @return <code>true</code> if the bundle is allowed and <code>false</code>otherwise
- */
- private boolean isBundleAllowed(Map<String, ?> bundleAttributes) {
- synchronized (this.monitor) {
- if (match(bundles, bundleAttributes))
- return true;
- return match(all, 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;
- }
-
- 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;
- }
+ public boolean isBundleAllowed(BundleRevision bundle);
/**
* Determines whether this filter allows the given service reference.
@@ -161,13 +136,7 @@ public class RegionFilter {
* @param service the service reference of the service
* @return <code>true</code> if the service is allowed and <code>false</code>otherwise
*/
- public boolean isServiceAllowed(ServiceReference<?> service) {
- synchronized (this.monitor) {
- if (match(services, service))
- return true;
- return match(all, service);
- }
- }
+ public boolean isServiceAllowed(ServiceReference<?> service);
/**
* Determines whether this filter allows the given capability.
@@ -175,116 +144,7 @@ public class RegionFilter {
* @param capability the bundle capability
* @return <code>true</code> if the capability is allowed and <code>false</code>otherwise
*/
- public boolean isCapabilityAllowed(BundleCapability capability) {
- String namespace = capability.getNamespace();
- Collection<Filter> filter = null;
- synchronized (this.monitor) {
- if (VISIBLE_PACKAGE_NAMESPACE.equals(namespace))
- filter = packages;
- else if (VISIBLE_REQUIRE_NAMESPACE.equals(namespace))
- filter = requires;
- else if (VISIBLE_HOST_NAMESPACE.equals(namespace))
- filter = hosts;
- else
- filter = generics == null ? null : generics.get(namespace);
- Map<String, ?> attrs = capability.getAttributes();
- if (match(filter, attrs))
- return true;
- return match(all, attrs);
- }
- }
-
- public RegionFilter setFilters(String namespace, Collection<String> filters) throws InvalidSyntaxException {
- if (namespace == null || filters == null)
- throw new IllegalArgumentException("The namespace and filters must not be null.");
- Collection<Filter> filterImpls = createFilters(filters);
- synchronized (monitor) {
- if (VISIBLE_PACKAGE_NAMESPACE.equals(namespace))
- packages = filterImpls;
- else if (VISIBLE_REQUIRE_NAMESPACE.equals(namespace))
- requires = filterImpls;
- else if (VISIBLE_HOST_NAMESPACE.equals(namespace))
- hosts = filterImpls;
- else if (VISIBLE_SERVICE_NAMESPACE.equals(namespace))
- services = filterImpls;
- else if (VISIBLE_BUNDLE_NAMESPACE.equals(namespace))
- bundles = filterImpls;
- else if (VISIBLE_ALL_NAMESPACE.equals(namespace))
- all = filterImpls;
- else {
- if (generics == null)
- generics = new HashMap<String, Collection<Filter>>();
- generics.put(namespace, filterImpls);
- }
- }
- return this;
- }
-
- public RegionFilter removeFilters(String namespace) {
- if (namespace == null)
- throw new IllegalArgumentException("The namespace must not be null.");
- synchronized (monitor) {
- if (VISIBLE_PACKAGE_NAMESPACE.equals(namespace))
- packages = null;
- else if (VISIBLE_REQUIRE_NAMESPACE.equals(namespace))
- requires = null;
- else if (VISIBLE_HOST_NAMESPACE.equals(namespace))
- hosts = null;
- else if (VISIBLE_SERVICE_NAMESPACE.equals(namespace))
- services = null;
- else if (VISIBLE_BUNDLE_NAMESPACE.equals(namespace))
- bundles = null;
- else if (VISIBLE_ALL_NAMESPACE.equals(namespace))
- all = null;
- else {
- if (generics == null)
- generics = new HashMap<String, Collection<Filter>>();
- generics.remove(namespace);
- }
- }
- return this;
- }
-
- public Collection<String> getFilters(String namespace) {
- if (namespace == null)
- throw new IllegalArgumentException("The namespace not be null.");
- synchronized (monitor) {
- if (VISIBLE_PACKAGE_NAMESPACE.equals(namespace))
- return getFilters(packages);
- else if (VISIBLE_REQUIRE_NAMESPACE.equals(namespace))
- return getFilters(requires);
- else if (VISIBLE_HOST_NAMESPACE.equals(namespace))
- return getFilters(hosts);
- else if (VISIBLE_SERVICE_NAMESPACE.equals(namespace))
- return getFilters(services);
- else if (VISIBLE_BUNDLE_NAMESPACE.equals(namespace))
- return getFilters(bundles);
- else if (VISIBLE_ALL_NAMESPACE.equals(namespace))
- return getFilters(all);
- else {
- if (generics != null)
- return getFilters(generics.get(namespace));
- }
- return null;
- }
- }
-
- 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;
- }
+ public boolean isCapabilityAllowed(BundleCapability capability);
- 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;
- }
+ public Map<String, Collection<String>> getSharingPolicy();
}
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 c64c8a1..3224975 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
@@ -263,6 +263,11 @@ public final class StandardRegionDigraph implements RegionDigraph {
return result;
}
+ @Override
+ public RegionFilter createRegionFilter(Map<String, Collection<String>> sharingPolicy) throws InvalidSyntaxException {
+ return new StandardRegionFilter(sharingPolicy);
+ }
+
private void notifyAdded(Region region) {
Set<RegionLifecycleListener> listeners = getListeners();
for (RegionLifecycleListener listener : listeners) {
@@ -293,5 +298,4 @@ public final class StandardRegionDigraph implements RegionDigraph {
}
return listeners;
}
-
}
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
new file mode 100644
index 0000000..5888c69
--- /dev/null
+++ b/org.eclipse.virgo.kernel.osgi/src/main/java/org/eclipse/virgo/kernel/osgi/region/internal/StandardRegionFilter.java
@@ -0,0 +1,155 @@
+/*******************************************************************************
+ * 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:
+ * VMware Inc. - initial contribution
+ *******************************************************************************/
+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.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{
+ 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()));
+ }
+ }
+
+ /**
+ * Determines whether this filter allows the given bundle
+ *
+ * @param bundle the bundle
+ * @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);
+ }
+
+ /**
+ * Determines whether this filter allows the given bundle
+ *
+ * @param bundle the bundle revision
+ * @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);
+ }
+
+ /**
+ * Determines whether this filter allows the bundle with the given attributes
+ *
+ * @param bundleAttributes the bundle attributes
+ * @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);
+ }
+
+ 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;
+ }
+
+ 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;
+ }
+
+ /**
+ * Determines whether this filter allows the given service reference.
+ *
+ * @param service the service reference of the service
+ * @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);
+ }
+
+ /**
+ * Determines whether this filter allows the given capability.
+ *
+ * @param capability the bundle capability
+ * @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);
+ }
+
+
+ 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;
+ }
+
+ 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;
+ }
+}
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 27280cd..5370e5f 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
@@ -26,6 +26,7 @@ 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.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;
@@ -235,21 +236,20 @@ public class RegionBundleFindHookTests {
}
private RegionFilter createFilter(String... bundleSymbolicNames) {
- RegionFilter filter = new RegionFilter();
- if (bundleSymbolicNames.length == 0)
- return filter;
Collection<String> filters = new ArrayList<String>(bundleSymbolicNames.length);
for (String bundleSymbolicName : bundleSymbolicNames) {
- StringBuilder builder = new StringBuilder();
- builder.append('(').append(RegionFilter.VISIBLE_BUNDLE_NAMESPACE).append('=').append(bundleSymbolicName).append(')');
- filters.add(builder.toString());
+ 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);
}
try {
- filter.setFilters(RegionFilter.VISIBLE_BUNDLE_NAMESPACE, filters);
+ return new StandardRegionFilter(policy);
} catch (InvalidSyntaxException e) {
Assert.fail(e.getMessage());
}
- return filter;
+ return null; // only for compiling; should not happen
}
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 7f935b7..73631b9 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
@@ -28,6 +28,7 @@ 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.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;
@@ -288,35 +289,34 @@ public class RegionResolverHookTests {
}
private RegionFilter createFilter(final String... packageNames) {
- RegionFilter filter = new RegionFilter();
- if (packageNames.length == 0)
- return filter;
Collection<String> filters = new ArrayList<String>(packageNames.length);
for (String pkg : packageNames) {
- StringBuilder builder = new StringBuilder();
- builder.append('(').append(RegionFilter.VISIBLE_PACKAGE_NAMESPACE).append('=').append(pkg).append(')');
- filters.add(builder.toString());
+ filters.add('(' + RegionFilter.VISIBLE_PACKAGE_NAMESPACE + '=' + pkg + ')');
}
-
- try {
- filter.setFilters(RegionFilter.VISIBLE_PACKAGE_NAMESPACE, filters);
+ 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 filter;
+ return null; // only for compiling; should not happen
}
private RegionFilter createBundleFilter(String bundleSymbolicName, Version bundleVersion) {
- RegionFilter filter = new RegionFilter();
- StringBuilder builder = new StringBuilder();
- builder.append("(&(").append(RegionFilter.VISIBLE_BUNDLE_NAMESPACE).append('=').append(bundleSymbolicName).append(')');
- builder.append('(').append(Constants.BUNDLE_VERSION_ATTRIBUTE).append(">=").append(bundleVersion).append("))");
+ String bundleFilter = "(&(" + RegionFilter.VISIBLE_BUNDLE_NAMESPACE + '=' + bundleSymbolicName + ')' +
+ '(' + Constants.BUNDLE_VERSION_ATTRIBUTE + ">=" + (bundleVersion == null ? "0" : bundleVersion.toString()) + "))";
+
try {
- filter.setFilters(RegionFilter.VISIBLE_BUNDLE_NAMESPACE, Arrays.asList(builder.toString()));
+ 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 filter;
+ return null; // only for compiling; should not happen
}
private Bundle createBundle(String bundleSymbolicName) {
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 7f92bb5..7f4fa28 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
@@ -26,6 +26,7 @@ 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.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;
@@ -244,21 +245,20 @@ public class RegionServiceFindHookTests {
}
private RegionFilter createFilter(final String... referenceNames) {
- RegionFilter filter = new RegionFilter();
- if (referenceNames.length == 0)
- return filter;
Collection<String> filters = new ArrayList<String>(referenceNames.length);
for (String referenceName : referenceNames) {
- StringBuilder builder = new StringBuilder();
- builder.append('(').append(Constants.OBJECTCLASS).append('=').append(referenceName).append(')');
- filters.add(builder.toString());
+ 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);
}
try {
- filter.setFilters(RegionFilter.VISIBLE_SERVICE_NAMESPACE, filters);
+ return new StandardRegionFilter(policy);
} catch (InvalidSyntaxException e) {
Assert.fail(e.getMessage());
}
- return filter;
+ return null; // only for compiling; should not happen
}
private Bundle createBundle(String bundleSymbolicName) {
diff --git a/org.eclipse.virgo.kernel.osgi/src/test/java/org/eclipse/virgo/kernel/osgi/region/internal/BundleIdBasedRegionTests.java b/org.eclipse.virgo.kernel.osgi/src/test/java/org/eclipse/virgo/kernel/osgi/region/internal/BundleIdBasedRegionTests.java
index e81a525..a868ba2 100644
--- a/org.eclipse.virgo.kernel.osgi/src/test/java/org/eclipse/virgo/kernel/osgi/region/internal/BundleIdBasedRegionTests.java
+++ b/org.eclipse.virgo.kernel.osgi/src/test/java/org/eclipse/virgo/kernel/osgi/region/internal/BundleIdBasedRegionTests.java
@@ -79,7 +79,7 @@ public class BundleIdBasedRegionTests {
this.mockRegion = EasyMock.createMock(Region.class);
this.mockRegion2 = EasyMock.createMock(Region.class);
- this.mockRegionFilter = new RegionFilter();
+ this.mockRegionFilter = EasyMock.createMock(RegionFilter.class);
this.regionIterator = new Iterator<Region>() {
@@ -103,12 +103,12 @@ public class BundleIdBasedRegionTests {
}
private void replayMocks() {
- EasyMock.replay(this.mockBundleContext, this.mockBundle, this.mockRegion, this.mockRegion2, this.mockGraph);
+ EasyMock.replay(this.mockBundleContext, this.mockBundle, this.mockRegion, this.mockRegion2, this.mockRegionFilter, this.mockGraph);
}
@After
public void tearDown() throws Exception {
- EasyMock.verify(this.mockBundleContext, this.mockBundle, this.mockRegion, this.mockRegion2, this.mockGraph);
+ EasyMock.verify(this.mockBundleContext, this.mockBundle, this.mockRegion, this.mockRegion2, this.mockRegionFilter, this.mockGraph);
}
@Test
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 7038b57..3d20c56 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
@@ -20,6 +20,10 @@ 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;
@@ -71,22 +75,29 @@ public class StandardRegionDigraphTests {
this.mockRegion3 = EasyMock.createMock(Region.class);
EasyMock.expect(this.mockRegion3.getName()).andReturn("mockRegion3").anyTimes();
- this.regionFilter1 = new RegionFilter();
- this.regionFilter2 = new RegionFilter();
this.mockBundle = EasyMock.createMock(Bundle.class);
}
- private void setDefaultMockFilters() throws InvalidSyntaxException {
- this.regionFilter1.removeFilters(RegionFilter.VISIBLE_BUNDLE_NAMESPACE);
- this.regionFilter2.removeFilters(RegionFilter.VISIBLE_BUNDLE_NAMESPACE);
+ private void setDefaultFilters() throws InvalidSyntaxException {
+ this.regionFilter1 = new StandardRegionFilter(Collections.EMPTY_MAP);
+ this.regionFilter2 = new StandardRegionFilter(Collections.EMPTY_MAP);
}
- private void setMockFilterAllowedBundle(RegionFilter regionFilter, OrderedPair<String, Version> bundle) throws InvalidSyntaxException {
- String filter = "(&(" +
- RegionFilter.VISIBLE_BUNDLE_NAMESPACE + "=" + bundle.getFirst() + ")(" +
- Constants.BUNDLE_VERSION_ATTRIBUTE + "=" + bundle.getSecond() + "))";
- regionFilter.setFilters(RegionFilter.VISIBLE_BUNDLE_NAMESPACE, Arrays.asList(filter));
+ 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);
}
private void replayMocks() {
@@ -100,7 +111,7 @@ public class StandardRegionDigraphTests {
@Test
public void testConnect() throws BundleException, InvalidSyntaxException {
- setDefaultMockFilters();
+ setDefaultFilters();
replayMocks();
this.digraph.connect(this.mockRegion1, this.regionFilter1, this.mockRegion2);
@@ -109,11 +120,9 @@ public class StandardRegionDigraphTests {
@Test
public void testConnectWithFilterContents() throws BundleException, InvalidSyntaxException {
OrderedPair<String, Version> b1 = new OrderedPair<String, Version>("b1", new Version("0"));
- setMockFilterAllowedBundle(this.regionFilter1, b1);
- EasyMock.expect(this.mockRegion1.getBundle(b1.getFirst(), b1.getSecond())).andReturn(null).anyTimes();
-
OrderedPair<String, Version> b2 = new OrderedPair<String, Version>("b2", new Version("0"));
- setMockFilterAllowedBundle(this.regionFilter2, b2);
+ setAllowedFilters(b1, b2);
+ EasyMock.expect(this.mockRegion1.getBundle(b1.getFirst(), b1.getSecond())).andReturn(null).anyTimes();
EasyMock.expect(this.mockRegion1.getBundle(b2.getFirst(), b2.getSecond())).andReturn(null).anyTimes();
replayMocks();
@@ -124,7 +133,7 @@ public class StandardRegionDigraphTests {
@Test(expected = BundleException.class)
public void testConnectLoop() throws BundleException, InvalidSyntaxException {
- setDefaultMockFilters();
+ setDefaultFilters();
replayMocks();
this.digraph.connect(this.mockRegion1, this.regionFilter1, this.mockRegion1);
@@ -132,7 +141,7 @@ public class StandardRegionDigraphTests {
@Test(expected = BundleException.class)
public void testDuplicateConnection() throws BundleException, InvalidSyntaxException {
- setDefaultMockFilters();
+ setDefaultFilters();
replayMocks();
this.digraph.connect(this.mockRegion1, this.regionFilter1, this.mockRegion2);
@@ -141,7 +150,7 @@ public class StandardRegionDigraphTests {
@Test
public void testGetEdges() throws BundleException, InvalidSyntaxException {
- setDefaultMockFilters();
+ setDefaultFilters();
replayMocks();
this.digraph.connect(this.mockRegion1, this.regionFilter1, this.mockRegion2);
@@ -165,7 +174,7 @@ public class StandardRegionDigraphTests {
@Test
public void testRemoveRegion() throws BundleException, InvalidSyntaxException {
- setDefaultMockFilters();
+ setDefaultFilters();
replayMocks();
this.digraph.connect(this.mockRegion1, this.regionFilter1, this.mockRegion2);
@@ -179,7 +188,7 @@ public class StandardRegionDigraphTests {
@Test
public void testGetRegions() throws BundleException, InvalidSyntaxException {
- setDefaultMockFilters();
+ setDefaultFilters();
replayMocks();
this.digraph.connect(this.mockRegion1, this.regionFilter1, 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 7a065e0..1a020c3 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
@@ -18,16 +18,27 @@ import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import java.util.Arrays;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.Map;
+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;
import org.osgi.framework.wiring.BundleRevision;
public class StandardRegionFilterTests {
@@ -36,16 +47,12 @@ public class StandardRegionFilterTests {
private static final Version BUNDLE_VERSION = new Version("0");
- private RegionFilter regionFilter;
-
-
private StubBundle stubBundle;
- private String packageImportPolicy = "(" + BundleRevision.PACKAGE_NAMESPACE + "=*)";
- private String serviceImportPolicy = "(" + Constants.SERVICE_ID + "=*)";
+ private String packageImportPolicy = "(" + BundleRevision.PACKAGE_NAMESPACE + "=foo)";
+ private String serviceImportPolicy = "(" + Constants.OBJECTCLASS + "=foo.Service)";
@Before
public void setUp() throws Exception {
- this.regionFilter = new RegionFilter();
this.stubBundle = new StubBundle(BUNDLE_SYMBOLIC_NAME, BUNDLE_VERSION);
}
@@ -53,41 +60,78 @@ public class StandardRegionFilterTests {
public void tearDown() throws Exception {
}
- private void addBundleFilter(String bundleSymbolicName, Version bundleVersion) throws InvalidSyntaxException {
+ private RegionFilter createBundleFilter(String bundleSymbolicName, Version bundleVersion) throws InvalidSyntaxException {
String filter = "(&(" +
RegionFilter.VISIBLE_BUNDLE_NAMESPACE + "=" + bundleSymbolicName + ")(" +
Constants.BUNDLE_VERSION_ATTRIBUTE + ">=" + bundleVersion + "))";
- regionFilter.setFilters(RegionFilter.VISIBLE_BUNDLE_NAMESPACE, Arrays.asList(filter));
-
+ Map<String, Collection<String>> policy = new HashMap<String, Collection<String>>();
+ policy.put(RegionFilter.VISIBLE_BUNDLE_NAMESPACE, Arrays.asList(filter));
+ return new StandardRegionFilter(policy);
}
+ 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);
+ }
+
@Test
public void testAllowBundle() throws InvalidSyntaxException {
- addBundleFilter(BUNDLE_SYMBOLIC_NAME, BUNDLE_VERSION);
- assertTrue(this.regionFilter.isBundleAllowed(stubBundle));
+ RegionFilter regionFilter = createBundleFilter(BUNDLE_SYMBOLIC_NAME, BUNDLE_VERSION);
+ assertTrue(regionFilter.isBundleAllowed(stubBundle));
}
@Test
- public void testBundleNotAllowed() {
- assertFalse(this.regionFilter.isBundleAllowed(stubBundle));
+ public void testBundleNotAllowed() throws InvalidSyntaxException {
+ RegionFilter regionFilter = new StandardRegionFilter(Collections.EMPTY_MAP);
+ assertFalse(regionFilter.isBundleAllowed(stubBundle));
}
@Test
public void testBundleNotAllowedInRange() throws InvalidSyntaxException {
- addBundleFilter(BUNDLE_SYMBOLIC_NAME, new Version(1,0,0));
- assertFalse(this.regionFilter.isBundleAllowed(stubBundle));
+ RegionFilter regionFilter = createBundleFilter(BUNDLE_SYMBOLIC_NAME, new Version(1,0,0));
+ assertFalse(regionFilter.isBundleAllowed(stubBundle));
}
@Test
- public void testSetPackageImportPolicy() throws InvalidSyntaxException {
- this.regionFilter.setFilters(RegionFilter.VISIBLE_PACKAGE_NAMESPACE, Arrays.asList(packageImportPolicy));
- assertEquals(Arrays.asList(this.packageImportPolicy), this.regionFilter.getFilters(RegionFilter.VISIBLE_PACKAGE_NAMESPACE));
+ 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));
+ assertEquals(Arrays.asList(this.packageImportPolicy), regionFilter.getSharingPolicy().get(RegionFilter.VISIBLE_PACKAGE_NAMESPACE));
}
@Test
- public void testSetServiceFilter() throws InvalidSyntaxException {
- this.regionFilter.setFilters(RegionFilter.VISIBLE_SERVICE_NAMESPACE, Arrays.asList(serviceImportPolicy));
- assertEquals(Arrays.asList(serviceImportPolicy), this.regionFilter.getFilters(RegionFilter.VISIBLE_SERVICE_NAMESPACE));
+ 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));
+ assertEquals(Arrays.asList(this.packageImportPolicy), regionFilter.getSharingPolicy().get(RegionFilter.VISIBLE_PACKAGE_NAMESPACE));
}
+ @Test
+ public void testServiceImportAllowed() throws InvalidSyntaxException {
+ RegionFilter regionFilter = createRegionFilter(RegionFilter.VISIBLE_SERVICE_NAMESPACE, Arrays.asList(serviceImportPolicy));
+ ServiceRegistration<?> reg = new StubServiceRegistration<Object>(new StubBundleContext(), "foo.Service");
+ 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));
+ ServiceRegistration<?> reg = new StubServiceRegistration<Object>(new StubBundleContext(), "bar.Service");
+ assertFalse(regionFilter.isServiceAllowed(reg.getReference()));
+ assertEquals(Arrays.asList(serviceImportPolicy), regionFilter.getSharingPolicy().get(RegionFilter.VISIBLE_SERVICE_NAMESPACE));
+ }
}
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 987b1fc..607cba2 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,10 +18,14 @@ 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;
@@ -52,6 +56,7 @@ import org.osgi.framework.Bundle;
import org.osgi.framework.BundleContext;
import org.osgi.framework.BundleException;
import org.osgi.framework.Constants;
+import org.osgi.framework.InvalidSyntaxException;
import org.osgi.framework.ServiceReference;
import org.osgi.framework.Version;
import org.osgi.framework.hooks.resolver.ResolverHookFactory;
@@ -68,6 +73,7 @@ import org.slf4j.LoggerFactory;
*
*/
final class StandardQuasiFramework implements QuasiFramework {
+ public final RegionFilter TOP;
private static final String REGION_LOCATION_DELIMITER = "@";
@@ -110,7 +116,14 @@ final class StandardQuasiFramework implements QuasiFramework {
StandardQuasiFramework(BundleContext bundleContext, State state, PlatformAdmin platformAdmin, ResolutionFailureDetective detective,
Repository repository, TransformedManifestProvidingBundleFileWrapper bundleTransformationHandler, RegionDigraph regionDigraph) {
- this.bundleContext = bundleContext;
+ 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;
this.state = state;
this.stateObjectFactory = platformAdmin.getFactory();
this.detective = detective;
@@ -156,8 +169,8 @@ final class StandardQuasiFramework implements QuasiFramework {
if (this.coregion == null) {
try {
this.coregion = this.regionDigraph.createRegion(this.userRegion.getName() + COREGION_SUFFIX);
- this.userRegion.connectRegion(this.coregion, RegionFilter.TOP);
- this.coregion.connectRegion(this.userRegion, RegionFilter.TOP);
+ this.userRegion.connectRegion(this.coregion, TOP);
+ this.coregion.connectRegion(this.userRegion, TOP);
} catch (BundleException e) {
// should never happen
throw new FatalKernelException("Failed to create coregion", e);
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 5bbf0af..8107a18 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
@@ -161,10 +161,10 @@ public final class Activator implements BundleActivator {
Region userRegion = regionDigraph.createRegion(REGION_USER);
userRegion.addBundle(userRegionFactoryBundle);
- RegionFilter kernelFilter = createKernelFilter(systemBundleContext, eventLogger);
+ RegionFilter kernelFilter = createKernelFilter(regionDigraph, systemBundleContext, eventLogger);
userRegion.connectRegion(kernelRegion, kernelFilter);
- RegionFilter userRegionFilter = createUserRegionFilter();
+ RegionFilter userRegionFilter = createUserRegionFilter(regionDigraph);
kernelRegion.connectRegion(userRegion, userRegionFilter);
notifyUserRegionStarting(userRegionBundleContext);
@@ -175,39 +175,33 @@ public final class Activator implements BundleActivator {
publishUserRegionBundleContext(userRegionBundleContext);
}
- private RegionFilter createUserRegionFilter() throws BundleException {
- RegionFilter userRegionFilter = new RegionFilter();
+ private RegionFilter createUserRegionFilter(RegionDigraph digraph) throws InvalidSyntaxException {
+ Map<String, Collection<String>> policy = new HashMap<String, Collection<String>>();
Collection<String> serviceFilter = classesToFilter(this.regionServiceExports);
- try {
- userRegionFilter.setFilters(RegionFilter.VISIBLE_SERVICE_NAMESPACE, serviceFilter);
- } catch (InvalidSyntaxException e) {
- throw new BundleException("Invalid " + USER_REGION_SERVICE_EXPORTS_PROPERTY + "in user region configuration: '"
- + this.regionServiceExports + "'", e);
- }
-
- return userRegionFilter;
+ policy.put(RegionFilter.VISIBLE_SERVICE_NAMESPACE, serviceFilter);
+ return digraph.createRegionFilter(policy);
}
private Region getKernelRegion(RegionDigraph regionDigraph) {
return regionDigraph.iterator().next();
}
- private RegionFilter createKernelFilter(BundleContext systemBundleContext, EventLogger eventLogger) throws BundleException, InvalidSyntaxException {
- RegionFilter kernelFilter = new RegionFilter();
- allowImportedBundles(kernelFilter, eventLogger);
- kernelFilter.setFilters(RegionFilter.VISIBLE_PACKAGE_NAMESPACE, createUserRegionPackageImportPolicy(systemBundleContext));
+ 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);
- kernelFilter.setFilters(RegionFilter.VISIBLE_SERVICE_NAMESPACE, serviceFilter);
- return kernelFilter;
+ policy.put(RegionFilter.VISIBLE_SERVICE_NAMESPACE, serviceFilter);
+ return digraph.createRegionFilter(policy);
}
- private void allowImportedBundles(RegionFilter kernelFilter, EventLogger eventLogger) throws InvalidSyntaxException {
+ private void allowImportedBundles(Map<String, Collection<String>> policy, 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());
- kernelFilter.setFilters(RegionFilter.VISIBLE_BUNDLE_NAMESPACE, importedBundles);
+ policy.put(RegionFilter.VISIBLE_BUNDLE_NAMESPACE, importedBundles);
}
private RequireBundle representBundleImportsAsRequireBundle(String userRegionBundleImportsProperty, EventLogger eventLogger) {
diff --git a/org.eclipse.virgo.kernel.userregionfactory/src/test/java/org/eclipse/virgo/kernel/userregionfactory/UserRegionPackageImportPolicyTests.java b/org.eclipse.virgo.kernel.userregionfactory/src/test/java/org/eclipse/virgo/kernel/userregionfactory/UserRegionPackageImportPolicyTests.java
deleted file mode 100644
index 4adbca5..0000000
--- a/org.eclipse.virgo.kernel.userregionfactory/src/test/java/org/eclipse/virgo/kernel/userregionfactory/UserRegionPackageImportPolicyTests.java
+++ /dev/null
@@ -1,114 +0,0 @@
-/*******************************************************************************
- * This file is part of the Virgo Web Server.
- *
- * Copyright (c) 2010 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.userregionfactory;
-
-import java.util.HashMap;
-import java.util.Map;
-
-import org.junit.Assert;
-import org.junit.Test;
-import org.osgi.framework.Version;
-import org.osgi.framework.wiring.BundleRevision;
-
-public class UserRegionPackageImportPolicyTests {
-/*
- @Test
- public void testNullPackageString() {
- new UserRegionPackageImportPolicy(null);
- }
-
- @Test
- public void testEmptyPackageString() {
- new UserRegionPackageImportPolicy("");
- }
-
- @Test(expected = IllegalArgumentException.class)
- public void testWildcard() {
- new UserRegionPackageImportPolicy("*");
- }
-
- @Test
- public void testPackageStringWithArbitraryAttribute() {
- UserRegionPackageImportPolicy userRegionPackageImportPolicy = createUserRegionPackageImportPolicy("p;pa=pv,q");
- Assert.assertFalse(userRegionPackageImportPolicy.isImported("p", null, null));
- Map<String, Object> attributes = createAttributes("p");
- attributes.put("pa", "pv");
- Assert.assertTrue(userRegionPackageImportPolicy.isImported("p", attributes, null));
-
- }
-
- private UserRegionPackageImportPolicy createUserRegionPackageImportPolicy(String regionImports) {
- UserRegionPackageImportPolicy userRegionPackageImportPolicy = new UserRegionPackageImportPolicy(regionImports);
- return userRegionPackageImportPolicy;
- }
-
- @Test
- public void testPackageStringWithoutArbitraryAttribute() {
- RegionPackageImportPolicy userRegionPackageImportPolicy = createUserRegionPackageImportPolicy("p,q");
- Map<String, Object> attributes = createAttributes("p");
- attributes.put("pa", "pv");
- Assert.assertTrue(userRegionPackageImportPolicy.isImported("p", attributes, null));
-
- }
-
- private Map<String, Object> createAttributes(String packageName) {
- Map<String, Object> attributes = new HashMap<String, Object>();
- attributes.put(BundleRevision.PACKAGE_NAMESPACE, packageName);
- return attributes;
- }
-
- @Test
- public void testPackageStringWithMandatoryAttribute() {
- RegionPackageImportPolicy userRegionPackageImportPolicy = createUserRegionPackageImportPolicy("p;pa=pv");
- Assert.assertFalse(userRegionPackageImportPolicy.isImported("p", null, null));
- Map<String, Object> attributes = createAttributes("p");
- attributes.put("pa", "pv");
- Map<String, String> directives = createMandatoryDirective("pa");
- Assert.assertTrue(userRegionPackageImportPolicy.isImported("p", attributes, directives));
- }
-
- @Test
- public void testPackageStringWithoutMandatoryAttribute() {
- RegionPackageImportPolicy userRegionPackageImportPolicy = createUserRegionPackageImportPolicy("p");
- Map<String, Object> attributes = createAttributes("p");
- attributes.put("pa", "pv");
- Map<String, String> directives = createMandatoryDirective("pa");
- Assert.assertFalse(userRegionPackageImportPolicy.isImported("p", attributes, directives));
- }
-
- private Map<String, String> createMandatoryDirective(String attributes) {
- Map<String, String> directives = new HashMap<String, String>();
- directives.put("mandatory", attributes);
- return directives;
- }
-
- @Test
- public void testPackageStringWithVersion() {
- RegionPackageImportPolicy userRegionPackageImportPolicy = createUserRegionPackageImportPolicy("p;version=2,q");
- Assert.assertFalse(userRegionPackageImportPolicy.isImported("p", null, null));
- Map<String, Object> attributes = new HashMap<String, Object>();
- attributes.put("version", new Version("2.0.0"));
- Assert.assertTrue(userRegionPackageImportPolicy.isImported("p", attributes, null));
-
- }
-
- @Test
- public void testPackages() {
- RegionPackageImportPolicy userRegionPackageImportPolicy = createUserRegionPackageImportPolicy("p,q");
- Assert.assertTrue(userRegionPackageImportPolicy.isImported("p", null, null));
- Assert.assertTrue(userRegionPackageImportPolicy.isImported("q", null, null));
- Assert.assertFalse(userRegionPackageImportPolicy.isImported("r", null, null));
- }
-*/
-}