Skip to main content
aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGlyn Normington2011-03-14 06:27:38 -0400
committerGlyn Normington2011-03-14 06:27:38 -0400
commit2b9b7ffd7937333ae8d8207f30b9faf8ef5a3ca9 (patch)
treef0d22460b626b3e6efdcb8a5599be4ef4a576bf8
parent7550f35b2b4f285c569e6406d1d9871055bf3363 (diff)
parentca96d4114a7d45ea05f4176b518817eed70007bf (diff)
downloadorg.eclipse.virgo.kernel-2b9b7ffd7937333ae8d8207f30b9faf8ef5a3ca9.tar.gz
org.eclipse.virgo.kernel-2b9b7ffd7937333ae8d8207f30b9faf8ef5a3ca9.tar.xz
org.eclipse.virgo.kernel-2b9b7ffd7937333ae8d8207f30b9faf8ef5a3ca9.zip
Merge branch 'region_api'
-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.java153
-rw-r--r--org.eclipse.virgo.kernel.osgi/src/main/java/org/eclipse/virgo/kernel/osgi/region/RegionFilterBuilder.java49
-rw-r--r--org.eclipse.virgo.kernel.osgi/src/main/java/org/eclipse/virgo/kernel/osgi/region/RegionPackageImportPolicy.java40
-rw-r--r--org.eclipse.virgo.kernel.osgi/src/main/java/org/eclipse/virgo/kernel/osgi/region/StandardRegionFilter.java102
-rw-r--r--org.eclipse.virgo.kernel.osgi/src/main/java/org/eclipse/virgo/kernel/osgi/region/hook/RegionBundleFindHook.java18
-rw-r--r--org.eclipse.virgo.kernel.osgi/src/main/java/org/eclipse/virgo/kernel/osgi/region/hook/RegionResolverHook.java24
-rw-r--r--org.eclipse.virgo.kernel.osgi/src/main/java/org/eclipse/virgo/kernel/osgi/region/hook/RegionServiceFindHook.java6
-rw-r--r--org.eclipse.virgo.kernel.osgi/src/main/java/org/eclipse/virgo/kernel/osgi/region/internal/StandardRegionDigraph.java22
-rw-r--r--org.eclipse.virgo.kernel.osgi/src/main/java/org/eclipse/virgo/kernel/osgi/region/internal/StandardRegionFilter.java145
-rw-r--r--org.eclipse.virgo.kernel.osgi/src/main/java/org/eclipse/virgo/kernel/osgi/region/internal/StandardRegionFilterBuilder.java84
-rw-r--r--org.eclipse.virgo.kernel.osgi/src/test/java/org/eclipse/virgo/kernel/osgi/region/hook/RegionBundleFindHookTests.java27
-rw-r--r--org.eclipse.virgo.kernel.osgi/src/test/java/org/eclipse/virgo/kernel/osgi/region/hook/RegionResolverHookTests.java62
-rw-r--r--org.eclipse.virgo.kernel.osgi/src/test/java/org/eclipse/virgo/kernel/osgi/region/hook/RegionServiceFindHookTests.java63
-rw-r--r--org.eclipse.virgo.kernel.osgi/src/test/java/org/eclipse/virgo/kernel/osgi/region/internal/BundleIdBasedRegionTests.java7
-rw-r--r--org.eclipse.virgo.kernel.osgi/src/test/java/org/eclipse/virgo/kernel/osgi/region/internal/StandardRegionDigraphTests.java74
-rw-r--r--org.eclipse.virgo.kernel.osgi/src/test/java/org/eclipse/virgo/kernel/osgi/region/internal/StandardRegionFilterTests.java157
-rw-r--r--org.eclipse.virgo.kernel.userregion/src/main/java/org/eclipse/virgo/kernel/userregion/internal/quasi/DependencyCalculator.java4
-rw-r--r--org.eclipse.virgo.kernel.userregion/src/main/java/org/eclipse/virgo/kernel/userregion/internal/quasi/StandardQuasiFramework.java9
-rw-r--r--org.eclipse.virgo.kernel.userregionfactory/src/main/java/org/eclipse/virgo/kernel/userregionfactory/Activator.java147
-rw-r--r--org.eclipse.virgo.kernel.userregionfactory/src/main/java/org/eclipse/virgo/kernel/userregionfactory/UserRegionPackageImportPolicy.java128
-rw-r--r--org.eclipse.virgo.kernel.userregionfactory/src/test/java/org/eclipse/virgo/kernel/userregionfactory/UserRegionPackageImportPolicyTests.java115
22 files changed, 737 insertions, 712 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 3cd41908..77ccc199 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
@@ -56,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
@@ -66,13 +66,20 @@ public interface RegionDigraph extends Iterable<Region> {
Region createRegion(String regionName) throws BundleException;
/**
+ * Create a {@link RegionFilterBuilder} instance.
+ *
+ * @return a region filter builder
+ */
+ RegionFilterBuilder createRegionFilterBuilder();
+
+ /**
* 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.
*
* @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 994dac12..95d80bd6 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,17 +13,20 @@
package org.eclipse.virgo.kernel.osgi.region;
-import java.util.Dictionary;
+import java.util.Collection;
import java.util.Map;
-import org.eclipse.virgo.util.osgi.VersionRange;
-import org.osgi.framework.Filter;
+import org.osgi.framework.Bundle;
+import org.osgi.framework.Constants;
import org.osgi.framework.ServiceReference;
-import org.osgi.framework.Version;
+import org.osgi.framework.wiring.BundleCapability;
+import org.osgi.framework.wiring.BundleRevision;
/**
* A {@link RegionFilter} is associated with a connection from one region to another and determines the bundles,
- * packages, and services which are visible across the connection.
+ * packages, services and other capabilities which are visible across the connection. A region filter is constant; its
+ * sharing policy cannot be changed after construction. Instances of region filters can be created with a
+ * {@link RegionFilterBuilder}.
* <p />
*
* <strong>Concurrent Semantics</strong><br />
@@ -33,117 +36,85 @@ import org.osgi.framework.Version;
*/
public interface RegionFilter {
- public static final RegionPackageImportPolicy ALL_PACKAGES = new RegionPackageImportPolicy() {
-
- @Override
- public boolean isImported(String packageName, Map<String, Object> attributes, Map<String, String> directives) {
- return true;
- }
- };
-
- public static final Filter ALL_SERVICES = new Filter() {
-
- @Override
- public boolean match(ServiceReference<?> reference) {
- return true;
- }
-
- @Override
- public boolean match(Dictionary<String, ?> dictionary) {
- return true;
- }
-
- @Override
- public boolean matchCase(Dictionary<String, ?> dictionary) {
- return true;
- }
-
- @Override
- public boolean matches(Map<String, ?> map) {
- return true;
- }
- };
-
- public static final RegionFilter TOP = new RegionFilter() {
-
- @Override
- public RegionFilter allowBundle(String bundleSymbolicName, VersionRange versionRange) {
- return this;
- }
-
- @Override
- public boolean isBundleAllowed(String bundleSymbolicName, Version bundleVersion) {
- return true;
- }
+ /**
+ * Name space for sharing package capabilities.
+ *
+ * @see BundleRevision#PACKAGE_NAMESPACE
+ */
+ public static final String VISIBLE_PACKAGE_NAMESPACE = BundleRevision.PACKAGE_NAMESPACE;
- @Override
- public RegionFilter setPackageImportPolicy(RegionPackageImportPolicy packageImportPolicy) {
- throw new UnsupportedOperationException("TOP is immutable");
- }
+ /**
+ * Name space for sharing bundle capabilities for require bundle constraints.
+ *
+ * @see BundleRevision#BUNDLE_NAMESPACE
+ */
+ public static final String VISIBLE_REQUIRE_NAMESPACE = BundleRevision.BUNDLE_NAMESPACE;
- @Override
- public RegionPackageImportPolicy getPackageImportPolicy() {
- return ALL_PACKAGES;
- }
+ /**
+ * Name space for sharing host capabilities.
+ *
+ * @see BundleRevision#HOST_NAMESPACE
+ */
+ public static final String VISIBLE_HOST_NAMESPACE = BundleRevision.HOST_NAMESPACE;
- @Override
- public RegionFilter setServiceFilter(Filter serviceFilter) {
- throw new UnsupportedOperationException("TOP is immutable");
- }
+ /**
+ * 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";
- @Override
- public Filter getServiceFilter() {
- return ALL_SERVICES;
- }
- };
+ /**
+ * 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";
/**
- * Allows bundles with the given bundle symbolic name and bundle version in the given range to be imported.
- *
- * Note that the system bundle has the symbolic name "org.eclipse.osgi".
- *
- * @param bundleSymbolicName
- * @param versionRange
- * @return this {@link RegionFilter} for chaining purposes
+ * Name space for matching against all capabilities. The filters specified in this name space will be used to match
+ * all capabilities.
*/
- RegionFilter allowBundle(String bundleSymbolicName, VersionRange versionRange);
+ public static final String VISIBLE_ALL_NAMESPACE = "org.eclipse.equinox.allow.all";
/**
- * Determines whether this filter allows the bundle with the given symbolic name and version
+ * Determines whether this filter allows the given bundle
*
- * @param bundleSymbolicName the symbolic name of the bundle
- * @param bundleVersion the {@link Version} of the bundle
+ * @param bundle the bundle
* @return <code>true</code> if the bundle is allowed and <code>false</code>otherwise
*/
- boolean isBundleAllowed(String bundleSymbolicName, Version bundleVersion);
+ public boolean isAllowed(Bundle bundle);
/**
- * Sets the package import policy of this filter.
+ * Determines whether this filter allows the given bundle
*
- * @param packageImportPolicy
- * @return this {@link RegionFilter} for chaining purposes
+ * @param bundle the bundle revision
+ * @return <code>true</code> if the bundle is allowed and <code>false</code>otherwise
*/
- RegionFilter setPackageImportPolicy(RegionPackageImportPolicy packageImportPolicy);
+ public boolean isAllowed(BundleRevision bundle);
/**
- * Gets the package import policy of this filter.
+ * Determines whether this filter allows the given service reference.
*
- * @return the package import policy or <code>null</code> if this has not been set
+ * @param service the service reference of the service
+ * @return <code>true</code> if the service is allowed and <code>false</code>otherwise
*/
- RegionPackageImportPolicy getPackageImportPolicy();
+ public boolean isAllowed(ServiceReference<?> service);
/**
- * @param serviceFilter
- * @return this {@link RegionFilter} for chaining purposes
- * @see org.osgi.framework.Filter more information about service filters
+ * 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
*/
- RegionFilter setServiceFilter(Filter serviceFilter);
+ public boolean isAllowed(BundleCapability capability);
/**
- * Gets the service filter of this filter.
+ * Returns a map of the filters used by each name space for this region filter. The may key is the name space and
+ * the value is a collection of filters for the name space. The returned map is a snapshot of the sharing policy.
+ * Changes made to the returned map have no affect on this region filter.
*
- * @return the service filter or <code>null</code> if this has not been set
+ * @return a map containing the sharing policy used by this region filter
*/
- Filter getServiceFilter();
-
+ public Map<String, Collection<String>> getSharingPolicy();
}
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 00000000..92152aba
--- /dev/null
+++ b/org.eclipse.virgo.kernel.osgi/src/main/java/org/eclipse/virgo/kernel/osgi/region/RegionFilterBuilder.java
@@ -0,0 +1,49 @@
+/*******************************************************************************
+ * 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 name space matching the given filter.
+ *
+ * @param namespace the name space 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;
+
+ /**
+ * Allow all capabilities with the given name space.
+ *
+ * @param namespace the name space of the capabilities to be allowed
+ * @return this builder (for method chaining)
+ */
+ RegionFilterBuilder allowAll(String namespace);
+
+ /**
+ * 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/RegionPackageImportPolicy.java b/org.eclipse.virgo.kernel.osgi/src/main/java/org/eclipse/virgo/kernel/osgi/region/RegionPackageImportPolicy.java
deleted file mode 100644
index ff1abc83..00000000
--- a/org.eclipse.virgo.kernel.osgi/src/main/java/org/eclipse/virgo/kernel/osgi/region/RegionPackageImportPolicy.java
+++ /dev/null
@@ -1,40 +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.osgi.region;
-
-import java.util.Map;
-
-/**
- * {@link RegionPackageImportPolicy} determines the package names that are imported into a region.
- * <p />
- *
- * <strong>Concurrent Semantics</strong><br />
- *
- * Implementations of this interface must be thread safe.
- *
- */
-public interface RegionPackageImportPolicy {
-
- /**
- * Returns <code>true</code> if and only if the package with the specified name exported with the specified
- * attributes and directives is imported into the region.
- *
- * @param packageName the name of the package
- * @param attributes the package's export attributes
- * @param directives the package's export directives
- * @return <code>true</code> if and only if the package is imported
- */
- boolean isImported(String packageName, Map<String, Object> attributes, Map<String, String> directives);
-
-}
diff --git a/org.eclipse.virgo.kernel.osgi/src/main/java/org/eclipse/virgo/kernel/osgi/region/StandardRegionFilter.java b/org.eclipse.virgo.kernel.osgi/src/main/java/org/eclipse/virgo/kernel/osgi/region/StandardRegionFilter.java
deleted file mode 100644
index d18af633..00000000
--- a/org.eclipse.virgo.kernel.osgi/src/main/java/org/eclipse/virgo/kernel/osgi/region/StandardRegionFilter.java
+++ /dev/null
@@ -1,102 +0,0 @@
-/*******************************************************************************
- * 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;
-
-import java.util.HashSet;
-import java.util.Set;
-
-import org.eclipse.virgo.util.math.OrderedPair;
-import org.eclipse.virgo.util.osgi.VersionRange;
-import org.osgi.framework.Filter;
-import org.osgi.framework.Version;
-
-/**
- * {@link StandardRegionFilter} is the default implementation of {@link RegionFilter}.
- * <p />
- *
- * <strong>Concurrent Semantics</strong><br />
- * Thread safe.
- */
-public final class StandardRegionFilter implements RegionFilter {
-
- private final Object monitor = new Object();
-
- private final Set<OrderedPair<String, VersionRange>> allowedBundles = new HashSet<OrderedPair<String, VersionRange>>();
-
- private RegionPackageImportPolicy packageImportPolicy;
-
- private Filter serviceFilter;
-
- /**
- * {@inheritDoc}
- */
- @Override
- public RegionFilter allowBundle(String bundleSymbolicName, VersionRange versionRange) {
- synchronized (this.monitor) {
- this.allowedBundles.add(createPair(bundleSymbolicName, versionRange));
- }
- return this;
- }
-
- private OrderedPair<String, VersionRange> createPair(String bundleSymbolicName, VersionRange versionRange) {
- return new OrderedPair<String, VersionRange>(bundleSymbolicName, versionRange);
- }
-
- /**
- * {@inheritDoc}
- */
- @Override
- public RegionFilter setServiceFilter(Filter serviceFilter) {
- synchronized (this.monitor) {
- this.serviceFilter = serviceFilter;
- }
- return this;
- }
-
- /**
- * {@inheritDoc}
- */
- @Override
- public Filter getServiceFilter() {
- synchronized (this.monitor) {
- return this.serviceFilter;
- }
- }
-
- @Override
- public RegionPackageImportPolicy getPackageImportPolicy() {
- synchronized (this.monitor) {
- return this.packageImportPolicy;
- }
- }
-
- @Override
- public RegionFilter setPackageImportPolicy(RegionPackageImportPolicy packageImportPolicy) {
- synchronized (this.monitor) {
- this.packageImportPolicy = packageImportPolicy;
- }
- return this;
- }
-
- @Override
- public boolean isBundleAllowed(String bundleSymbolicName, Version bundleVersion) {
- synchronized (this.monitor) {
- for (OrderedPair<String, VersionRange> allowedBundle : this.allowedBundles) {
- if (allowedBundle.getFirst().equals(bundleSymbolicName) && allowedBundle.getSecond().includes(bundleVersion)) {
- return true;
- }
- }
- return false;
- }
- }
-
-}
diff --git a/org.eclipse.virgo.kernel.osgi/src/main/java/org/eclipse/virgo/kernel/osgi/region/hook/RegionBundleFindHook.java b/org.eclipse.virgo.kernel.osgi/src/main/java/org/eclipse/virgo/kernel/osgi/region/hook/RegionBundleFindHook.java
index 9d27abc1..016de4bf 100644
--- a/org.eclipse.virgo.kernel.osgi/src/main/java/org/eclipse/virgo/kernel/osgi/region/hook/RegionBundleFindHook.java
+++ b/org.eclipse.virgo.kernel.osgi/src/main/java/org/eclipse/virgo/kernel/osgi/region/hook/RegionBundleFindHook.java
@@ -57,14 +57,14 @@ public final class RegionBundleFindHook implements FindHook {
bundles.retainAll(allowed);
}
-
+
private class Visitor extends RegionDigraphVisitorBase<Bundle> {
-
+
private Visitor(Collection<Bundle> candidates) {
super(candidates);
}
- /**
+ /**
* {@inheritDoc}
*/
@Override
@@ -72,21 +72,17 @@ public final class RegionBundleFindHook implements FindHook {
return region.contains(candidate);
}
- /**
- * {@inheritDoc}
- */
- /**
+ /**
* {@inheritDoc}
*/
@Override
protected boolean isAllowed(Bundle candidate, RegionFilter filter) {
- return filter.isBundleAllowed(candidate.getSymbolicName(), candidate.getVersion());
+ return filter.isAllowed(candidate);
}
-
+
}
-
+
private Region getRegion(BundleContext context) {
return this.regionDigraph.getRegion(context.getBundle());
}
-
}
diff --git a/org.eclipse.virgo.kernel.osgi/src/main/java/org/eclipse/virgo/kernel/osgi/region/hook/RegionResolverHook.java b/org.eclipse.virgo.kernel.osgi/src/main/java/org/eclipse/virgo/kernel/osgi/region/hook/RegionResolverHook.java
index a0ab1cf0..8adedb56 100644
--- a/org.eclipse.virgo.kernel.osgi/src/main/java/org/eclipse/virgo/kernel/osgi/region/hook/RegionResolverHook.java
+++ b/org.eclipse.virgo.kernel.osgi/src/main/java/org/eclipse/virgo/kernel/osgi/region/hook/RegionResolverHook.java
@@ -19,7 +19,6 @@ import org.eclipse.osgi.service.resolver.BundleDescription;
import org.eclipse.virgo.kernel.osgi.region.Region;
import org.eclipse.virgo.kernel.osgi.region.RegionDigraph;
import org.eclipse.virgo.kernel.osgi.region.RegionFilter;
-import org.eclipse.virgo.kernel.osgi.region.RegionPackageImportPolicy;
import org.eclipse.virgo.kernel.serviceability.Assert;
import org.osgi.framework.Bundle;
import org.osgi.framework.hooks.resolver.ResolverHook;
@@ -63,7 +62,7 @@ final class RegionResolverHook implements ResolverHook {
candidates.clear();
return;
}
-
+
Visitor visitor = new Visitor(candidates);
requirerRegion.visitSubgraph(visitor);
Set<BundleCapability> allowed = visitor.getAllowed();
@@ -75,9 +74,9 @@ final class RegionResolverHook implements ResolverHook {
}
}
}
-
+
private class Visitor extends RegionDigraphVisitorBase<BundleCapability> {
-
+
private Visitor(Collection<BundleCapability> candidates) {
super(candidates);
}
@@ -89,22 +88,9 @@ final class RegionResolverHook implements ResolverHook {
@Override
protected boolean isAllowed(BundleCapability candidate, RegionFilter filter) {
- RegionPackageImportPolicy packageImportPolicy = filter.getPackageImportPolicy();
- BundleRevision providerRevision = candidate.getRevision();
- if (!filter.isBundleAllowed(providerRevision.getSymbolicName(), providerRevision.getVersion())) {
- String namespace = candidate.getNamespace();
- if (BundleRevision.PACKAGE_NAMESPACE.equals(namespace)) {
- if (!packageImportPolicy.isImported((String) candidate.getAttributes().get(BundleRevision.PACKAGE_NAMESPACE), candidate.getAttributes(),
- candidate.getDirectives())) {
- return false;
- }
- } else {
- return false;
- }
- }
- return true;
+ return filter.isAllowed(candidate) || filter.isAllowed(candidate.getRevision());
}
-
+
}
private Region getRegion(BundleRevision bundleRevision) {
diff --git a/org.eclipse.virgo.kernel.osgi/src/main/java/org/eclipse/virgo/kernel/osgi/region/hook/RegionServiceFindHook.java b/org.eclipse.virgo.kernel.osgi/src/main/java/org/eclipse/virgo/kernel/osgi/region/hook/RegionServiceFindHook.java
index 65d80c0b..a027c33c 100644
--- a/org.eclipse.virgo.kernel.osgi/src/main/java/org/eclipse/virgo/kernel/osgi/region/hook/RegionServiceFindHook.java
+++ b/org.eclipse.virgo.kernel.osgi/src/main/java/org/eclipse/virgo/kernel/osgi/region/hook/RegionServiceFindHook.java
@@ -65,7 +65,7 @@ public final class RegionServiceFindHook implements FindHook {
super(candidates);
}
- /**
+ /**
* {@inheritDoc}
*/
@Override
@@ -73,12 +73,12 @@ public final class RegionServiceFindHook implements FindHook {
return region.contains(candidate.getBundle());
}
- /**
+ /**
* {@inheritDoc}
*/
@Override
protected boolean isAllowed(ServiceReference<?> candidate, RegionFilter filter) {
- return filter.getServiceFilter().match(candidate);
+ return filter.isAllowed(candidate) || filter.isAllowed(candidate.getBundle());
}
}
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 90caa684..92adc276 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
@@ -24,6 +24,7 @@ import org.eclipse.virgo.kernel.osgi.region.Region;
import org.eclipse.virgo.kernel.osgi.region.RegionDigraph;
import org.eclipse.virgo.kernel.osgi.region.RegionDigraphVisitor;
import org.eclipse.virgo.kernel.osgi.region.RegionFilter;
+import org.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;
@@ -267,6 +268,11 @@ public final class StandardRegionDigraph implements RegionDigraph {
return result;
}
+ @Override
+ public RegionFilterBuilder createRegionFilterBuilder() {
+ return new StandardRegionFilterBuilder();
+ }
+
private void notifyAdded(Region region) {
Set<RegionLifecycleListener> listeners = getListeners();
for (RegionLifecycleListener listener : listeners) {
@@ -298,7 +304,7 @@ public final class StandardRegionDigraph implements RegionDigraph {
return listeners;
}
- /**
+ /**
* {@inheritDoc}
*/
@Override
@@ -306,4 +312,18 @@ public final class StandardRegionDigraph implements RegionDigraph {
this.subgraphTraverser.visitSubgraph(startingRegion, visitor);
}
+ /**
+ * Returns a snapshot of filtered regions
+ *
+ * @return a snapshot of filtered regions
+ */
+ Map<Region, Set<FilteredRegion>> getFilteredRegions() {
+ Map<Region, Set<FilteredRegion>> result = new HashMap<Region, Set<FilteredRegion>>();
+ synchronized (this.monitor) {
+ for (Region region : regions) {
+ result.put(region, getEdges(region));
+ }
+ }
+ return result;
+ }
}
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 00000000..f81b2ff4
--- /dev/null
+++ b/org.eclipse.virgo.kernel.osgi/src/main/java/org/eclipse/virgo/kernel/osgi/region/internal/StandardRegionFilter.java
@@ -0,0 +1,145 @@
+/*******************************************************************************
+ * 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.ServiceReference;
+import org.osgi.framework.wiring.BundleCapability;
+import org.osgi.framework.wiring.BundleRevision;
+
+final class StandardRegionFilter implements RegionFilter {
+
+ private final Map<String, Collection<Filter>> filters;
+
+ 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);
+ }
+ }
+
+ /**
+ * 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 isAllowed(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 isAllowed(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 isAllowed(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 isAllowed(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;
+ }
+}
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 00000000..421429fd
--- /dev/null
+++ b/org.eclipse.virgo.kernel.osgi/src/main/java/org/eclipse/virgo/kernel/osgi/region/internal/StandardRegionFilterBuilder.java
@@ -0,0 +1,84 @@
+/*******************************************************************************
+ * 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;
+
+final class StandardRegionFilterBuilder implements RegionFilterBuilder {
+
+ private final static String ALL_SPEC = "(|(!(all=*))(all=*))";
+
+ private final static Filter ALL;
+ static {
+ try {
+ ALL = FrameworkUtil.createFilter(ALL_SPEC);
+ } catch (InvalidSyntaxException e) {
+ // should never happen!
+ e.printStackTrace();
+ throw new RuntimeException(e);
+ }
+ }
+
+ 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;
+ }
+
+ public RegionFilterBuilder allowAll(String namespace) {
+ if (namespace == null)
+ throw new IllegalArgumentException("The namespace must not be null.");
+ synchronized (this.monitor) {
+ Collection<Filter> namespaceFilters = policy.get(namespace);
+ if (namespaceFilters == null) {
+ namespaceFilters = new ArrayList<Filter>();
+ policy.put(namespace, namespaceFilters);
+ }
+ // remove any other filters since this will override them all.
+ namespaceFilters.clear();
+ namespaceFilters.add(ALL);
+ }
+ 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 26a5071e..823fe4fd 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
@@ -15,6 +15,7 @@ import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
+import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
@@ -22,17 +23,17 @@ import java.util.Map;
import org.eclipse.virgo.kernel.osgi.region.Region;
import org.eclipse.virgo.kernel.osgi.region.RegionFilter;
-import org.eclipse.virgo.kernel.osgi.region.StandardRegionFilter;
+import org.eclipse.virgo.kernel.osgi.region.RegionFilterBuilder;
import org.eclipse.virgo.kernel.osgi.region.internal.StandardRegionDigraph;
import org.eclipse.virgo.teststubs.osgi.framework.StubBundle;
import org.eclipse.virgo.teststubs.osgi.framework.StubBundleContext;
-import org.eclipse.virgo.util.osgi.VersionRange;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.osgi.framework.Bundle;
import org.osgi.framework.BundleContext;
import org.osgi.framework.BundleException;
+import org.osgi.framework.InvalidSyntaxException;
import org.osgi.framework.Version;
import org.osgi.framework.hooks.bundle.FindHook;
@@ -77,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);
@@ -115,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);
@@ -125,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);
@@ -138,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));
@@ -154,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));
@@ -232,12 +233,16 @@ public class RegionBundleFindHookTests {
return this.regions.get(regionName);
}
- private RegionFilter createFilter(String... bundleSymbolicNames) {
- RegionFilter filter = new StandardRegionFilter();
+ private RegionFilter createFilter(String... bundleSymbolicNames) throws InvalidSyntaxException {
+ Collection<String> filters = new ArrayList<String>(bundleSymbolicNames.length);
for (String bundleSymbolicName : bundleSymbolicNames) {
- filter.allowBundle(bundleSymbolicName, new VersionRange(BUNDLE_VERSION.toString()));
+ filters.add('(' + RegionFilter.VISIBLE_BUNDLE_NAMESPACE + '=' + bundleSymbolicName + ')');
+ }
+ RegionFilterBuilder builder = digraph.createRegionFilterBuilder();
+ for (String filter : filters) {
+ builder.allow(RegionFilter.VISIBLE_BUNDLE_NAMESPACE, filter);
}
- return filter;
+ 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 0e1d5dbd..df294de3 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
@@ -15,6 +15,7 @@ import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
+import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
@@ -23,17 +24,17 @@ import java.util.Map;
import org.eclipse.virgo.kernel.osgi.region.Region;
import org.eclipse.virgo.kernel.osgi.region.RegionFilter;
-import org.eclipse.virgo.kernel.osgi.region.RegionPackageImportPolicy;
-import org.eclipse.virgo.kernel.osgi.region.StandardRegionFilter;
+import org.eclipse.virgo.kernel.osgi.region.RegionFilterBuilder;
import org.eclipse.virgo.kernel.osgi.region.internal.StandardRegionDigraph;
import org.eclipse.virgo.teststubs.osgi.framework.StubBundle;
import org.eclipse.virgo.teststubs.osgi.framework.StubBundleContext;
-import org.eclipse.virgo.util.osgi.VersionRange;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.osgi.framework.Bundle;
import org.osgi.framework.BundleException;
+import org.osgi.framework.Constants;
+import org.osgi.framework.InvalidSyntaxException;
import org.osgi.framework.Version;
import org.osgi.framework.hooks.resolver.ResolverHook;
import org.osgi.framework.wiring.BundleCapability;
@@ -129,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);
@@ -139,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);
@@ -149,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);
@@ -162,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);
@@ -176,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));
@@ -192,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));
@@ -284,27 +285,23 @@ public class RegionResolverHookTests {
return this.regions.get(regionName);
}
- private RegionFilter createFilter(final String... packageNames) {
- RegionFilter filter = new StandardRegionFilter();
- filter.setPackageImportPolicy(new RegionPackageImportPolicy() {
-
- @Override
- public boolean isImported(String packageName, Map<String, Object> attributes, Map<String, String> directives) {
- for (String pkg : packageNames) {
- if (packageName.equals(pkg)) {
- return true;
- }
- }
- return false;
- }
- });
- return filter;
+ 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 + ')');
+ }
+ 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) {
- RegionFilter filter = new StandardRegionFilter();
- filter.allowBundle(bundleSymbolicName, new VersionRange(bundleVersion.toString()));
- return filter;
+ 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) {
@@ -312,7 +309,7 @@ public class RegionResolverHookTests {
this.bundles.put(bundleSymbolicName, stubBundle);
return stubBundle;
}
-
+
private BundleRequirement bundleRequirement(String bundleSymbolicName) {
return new StubBundleRequirement(bundle(bundleSymbolicName));
}
@@ -424,6 +421,7 @@ public class RegionResolverHookTests {
@Override
public Map<String, Object> getAttributes() {
HashMap<String, Object> attributes = new HashMap<String, Object>();
+ attributes.put(BundleRevision.BUNDLE_NAMESPACE, bundleSymbolicName);
return attributes;
}
@@ -465,9 +463,9 @@ public class RegionResolverHookTests {
}
}
-
+
private final class StubBundleRequirement implements BundleRequirement {
-
+
private final StubBundleRevision bundleRevision;
private StubBundleRequirement(Bundle bundle) {
@@ -498,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 c6602f84..46e72882 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
@@ -15,15 +15,15 @@ import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
+import java.util.ArrayList;
import java.util.Collection;
-import java.util.Dictionary;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import org.eclipse.virgo.kernel.osgi.region.Region;
import org.eclipse.virgo.kernel.osgi.region.RegionFilter;
-import org.eclipse.virgo.kernel.osgi.region.StandardRegionFilter;
+import org.eclipse.virgo.kernel.osgi.region.RegionFilterBuilder;
import org.eclipse.virgo.kernel.osgi.region.internal.StandardRegionDigraph;
import org.eclipse.virgo.teststubs.osgi.framework.StubBundle;
import org.eclipse.virgo.teststubs.osgi.framework.StubBundleContext;
@@ -35,7 +35,8 @@ import org.junit.Test;
import org.osgi.framework.Bundle;
import org.osgi.framework.BundleContext;
import org.osgi.framework.BundleException;
-import org.osgi.framework.Filter;
+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.service.FindHook;
@@ -87,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);
@@ -125,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);
@@ -135,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);
@@ -148,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));
@@ -163,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));
@@ -241,40 +242,16 @@ public class RegionServiceFindHookTests {
return this.regions.get(regionName);
}
- private RegionFilter createFilter(final String... referenceNames) {
- RegionFilter filter = new StandardRegionFilter();
- Filter f = new Filter() {
-
- @Override
- public boolean match(ServiceReference<?> reference) {
- for (String referenceName : referenceNames) {
- if (reference.getBundle().getSymbolicName().equals(referenceName)) {
- return true;
- }
- }
- return false;
- }
-
- @Override
- public boolean match(Dictionary<String, ?> dictionary) {
- // TODO Auto-generated method stub
- return false;
- }
-
- @Override
- public boolean matchCase(Dictionary<String, ?> dictionary) {
- // TODO Auto-generated method stub
- return false;
- }
-
- @Override
- public boolean matches(Map<String, ?> map) {
- // TODO Auto-generated method stub
- return false;
- }
- };
- filter.setServiceFilter(f);
- return filter;
+ 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 + ')');
+ }
+ RegionFilterBuilder builder = digraph.createRegionFilterBuilder();
+ for (String filter : filters) {
+ builder.allow(RegionFilter.VISIBLE_SERVICE_NAMESPACE, filter);
+ }
+ return builder.build();
}
private Bundle createBundle(String bundleSymbolicName) {
@@ -286,7 +263,7 @@ public class RegionServiceFindHookTests {
private StubServiceReference<Object> createServiceReference(Bundle stubBundle, String referenceName) {
StubServiceRegistration<Object> stubServiceRegistration = new StubServiceRegistration<Object>(
- (StubBundleContext) stubBundle.getBundleContext(), Object.class.getName());
+ (StubBundleContext) stubBundle.getBundleContext(), referenceName);
StubServiceReference<Object> stubServiceReference = new StubServiceReference<Object>(stubServiceRegistration);
this.serviceReferences.put(referenceName, stubServiceReference);
return stubServiceReference;
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 a868ba23..3af2eb7e 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
@@ -11,7 +11,10 @@
package org.eclipse.virgo.kernel.osgi.region.internal;
-import static org.junit.Assert.*;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertNull;
+import static org.junit.Assert.assertTrue;
import java.util.HashSet;
import java.util.Iterator;
@@ -20,8 +23,8 @@ import java.util.Set;
import org.easymock.EasyMock;
import org.eclipse.virgo.kernel.osgi.region.Region;
import org.eclipse.virgo.kernel.osgi.region.RegionDigraph;
-import org.eclipse.virgo.kernel.osgi.region.RegionFilter;
import org.eclipse.virgo.kernel.osgi.region.RegionDigraph.FilteredRegion;
+import org.eclipse.virgo.kernel.osgi.region.RegionFilter;
import org.eclipse.virgo.util.math.OrderedPair;
import org.junit.After;
import org.junit.Before;
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 83d9e262..64de3fec 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
@@ -24,8 +24,8 @@ import java.util.Set;
import org.easymock.EasyMock;
import org.eclipse.virgo.kernel.osgi.region.Region;
import org.eclipse.virgo.kernel.osgi.region.RegionDigraph;
-import org.eclipse.virgo.kernel.osgi.region.RegionFilter;
import org.eclipse.virgo.kernel.osgi.region.RegionDigraph.FilteredRegion;
+import org.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.util.math.OrderedPair;
@@ -34,6 +34,8 @@ import org.junit.Before;
import org.junit.Test;
import org.osgi.framework.Bundle;
import org.osgi.framework.BundleException;
+import org.osgi.framework.Constants;
+import org.osgi.framework.InvalidSyntaxException;
import org.osgi.framework.Version;
public class StandardRegionDigraphTests {
@@ -58,62 +60,58 @@ 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();
this.mockRegion3 = EasyMock.createMock(Region.class);
EasyMock.expect(this.mockRegion3.getName()).andReturn("mockRegion3").anyTimes();
- this.regionFilter1 = EasyMock.createMock(RegionFilter.class);
- this.regionFilter2 = EasyMock.createMock(RegionFilter.class);
this.mockBundle = EasyMock.createMock(Bundle.class);
}
- private void setDefaultMockFilters() {
- setMockFilterAllowedBundles(this.regionFilter1);
- setMockFilterAllowedBundles(this.regionFilter2);
+ private void setDefaultFilters() throws InvalidSyntaxException {
+ this.regionFilter1 = digraph.createRegionFilterBuilder().build();
+ this.regionFilter2 = digraph.createRegionFilterBuilder().build();
}
- private void setMockFilterAllowedBundles(RegionFilter regionFilter) {
- EasyMock.expect(regionFilter.isBundleAllowed(EasyMock.isA(String.class), EasyMock.isA(Version.class))).andReturn(false).anyTimes();
- }
+ 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() + "))";
+ 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 setMockFilterAllowedBundle(RegionFilter regionFilter, OrderedPair<String, Version> bundle) {
- EasyMock.expect(regionFilter.isBundleAllowed(EasyMock.eq(bundle.getFirst()), EasyMock.eq(bundle.getSecond()))).andReturn(true).anyTimes();
- EasyMock.expect(regionFilter.isBundleAllowed(EasyMock.not(EasyMock.eq(bundle.getFirst())), EasyMock.eq(bundle.getSecond()))).andReturn(false).anyTimes();
- EasyMock.expect(regionFilter.isBundleAllowed(EasyMock.eq(bundle.getFirst()), EasyMock.not(EasyMock.eq(bundle.getSecond())))).andReturn(false).anyTimes();
- EasyMock.expect(regionFilter.isBundleAllowed(EasyMock.not(EasyMock.eq(bundle.getFirst())), EasyMock.not(EasyMock.eq(bundle.getSecond())))).andReturn(false).anyTimes();
}
private void replayMocks() {
- EasyMock.replay(this.mockRegion1, this.mockRegion2, this.mockRegion3, this.regionFilter1, this.regionFilter2, this.mockBundle);
+ EasyMock.replay(this.mockRegion1, this.mockRegion2, this.mockRegion3, this.mockBundle);
}
@After
public void tearDown() throws Exception {
- EasyMock.verify(this.mockRegion1, this.mockRegion2, this.mockRegion3, this.regionFilter1, this.regionFilter2, this.mockBundle);
+ EasyMock.verify(this.mockRegion1, this.mockRegion2, this.mockRegion3, this.mockBundle);
}
@Test
- public void testConnect() throws BundleException {
- setDefaultMockFilters();
+ public void testConnect() throws BundleException, InvalidSyntaxException {
+ setDefaultFilters();
replayMocks();
this.digraph.connect(this.mockRegion1, this.regionFilter1, this.mockRegion2);
}
@Test
- public void testConnectWithFilterContents() throws BundleException {
+ 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();
@@ -123,16 +121,16 @@ public class StandardRegionDigraphTests {
}
@Test(expected = BundleException.class)
- public void testConnectLoop() throws BundleException {
- setDefaultMockFilters();
+ public void testConnectLoop() throws BundleException, InvalidSyntaxException {
+ setDefaultFilters();
replayMocks();
this.digraph.connect(this.mockRegion1, this.regionFilter1, this.mockRegion1);
}
@Test(expected = BundleException.class)
- public void testDuplicateConnection() throws BundleException {
- setDefaultMockFilters();
+ public void testDuplicateConnection() throws BundleException, InvalidSyntaxException {
+ setDefaultFilters();
replayMocks();
this.digraph.connect(this.mockRegion1, this.regionFilter1, this.mockRegion2);
@@ -140,8 +138,8 @@ public class StandardRegionDigraphTests {
}
@Test
- public void testGetEdges() throws BundleException {
- setDefaultMockFilters();
+ public void testGetEdges() throws BundleException, InvalidSyntaxException {
+ setDefaultFilters();
replayMocks();
this.digraph.connect(this.mockRegion1, this.regionFilter1, this.mockRegion2);
@@ -164,8 +162,8 @@ public class StandardRegionDigraphTests {
}
@Test
- public void testRemoveRegion() throws BundleException {
- setDefaultMockFilters();
+ public void testRemoveRegion() throws BundleException, InvalidSyntaxException {
+ setDefaultFilters();
replayMocks();
this.digraph.connect(this.mockRegion1, this.regionFilter1, this.mockRegion2);
@@ -176,17 +174,17 @@ public class StandardRegionDigraphTests {
assertNull(this.digraph.getRegion("mockRegion1"));
assertNotNull(this.digraph.getRegion("mockRegion2"));
}
-
+
@Test
- public void testGetRegions() throws BundleException {
- setDefaultMockFilters();
+ 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 47768ab6..5945f7da 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
@@ -17,16 +17,25 @@ import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
+import java.util.Arrays;
+import java.util.Collection;
+import java.util.HashMap;
+import java.util.Map;
+
import org.easymock.EasyMock;
import org.eclipse.virgo.kernel.osgi.region.RegionFilter;
-import org.eclipse.virgo.kernel.osgi.region.RegionPackageImportPolicy;
-import org.eclipse.virgo.kernel.osgi.region.StandardRegionFilter;
-import org.eclipse.virgo.util.osgi.VersionRange;
+import org.eclipse.virgo.teststubs.osgi.framework.StubBundle;
+import org.eclipse.virgo.teststubs.osgi.framework.StubBundleContext;
+import org.eclipse.virgo.teststubs.osgi.framework.StubServiceRegistration;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
-import org.osgi.framework.Filter;
+import org.osgi.framework.Constants;
+import org.osgi.framework.InvalidSyntaxException;
+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 {
@@ -34,51 +43,149 @@ public class StandardRegionFilterTests {
private static final Version BUNDLE_VERSION = new Version("0");
- private RegionFilter regionFilter;
+ private StubBundle stubBundle;
+
+ private String packageImportPolicy = "(" + BundleRevision.PACKAGE_NAMESPACE + "=foo)";
+
+ private String serviceImportPolicy = "(" + Constants.OBJECTCLASS + "=foo.Service)";
+
+ private BundleCapability fooPackage;
- private Filter mockFilter;
+ private BundleCapability barPackage;
- private RegionPackageImportPolicy packageImportPolicy;
+ private ServiceRegistration<Object> fooService;
+
+ private ServiceRegistration<Object> barService;
@Before
public void setUp() throws Exception {
- this.regionFilter = new StandardRegionFilter();
- this.mockFilter = EasyMock.createMock(Filter.class);
- this.packageImportPolicy = EasyMock.createMock(RegionPackageImportPolicy.class);
+ this.stubBundle = new StubBundle(BUNDLE_SYMBOLIC_NAME, BUNDLE_VERSION);
+ this.fooService = new StubServiceRegistration<Object>(new StubBundleContext(), "foo.Service");
+ this.barService = new StubServiceRegistration<Object>(new StubBundleContext(), "bar.Service");
+
+ this.fooPackage = EasyMock.createMock(BundleCapability.class);
+ Map<String, Object> fooAttrs = new HashMap<String, Object>();
+ fooAttrs.put(BundleRevision.PACKAGE_NAMESPACE, "foo");
+ EasyMock.expect(fooPackage.getNamespace()).andReturn(BundleRevision.PACKAGE_NAMESPACE).anyTimes();
+ EasyMock.expect(fooPackage.getAttributes()).andReturn(fooAttrs).anyTimes();
+ EasyMock.replay(fooPackage);
+
+ this.barPackage = EasyMock.createMock(BundleCapability.class);
+ Map<String, Object> barAttrs = new HashMap<String, Object>();
+ barAttrs.put(BundleRevision.PACKAGE_NAMESPACE, "bar");
+ EasyMock.expect(barPackage.getNamespace()).andReturn(BundleRevision.PACKAGE_NAMESPACE).anyTimes();
+ EasyMock.expect(barPackage.getAttributes()).andReturn(barAttrs).anyTimes();
+ EasyMock.replay(barPackage);
}
@After
public void tearDown() throws Exception {
}
+ private RegionFilter createBundleFilter(String bundleSymbolicName, Version bundleVersion) throws InvalidSyntaxException {
+ 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 {
+ StandardRegionFilterBuilder builder = new StandardRegionFilterBuilder();
+ for (String filter : filters) {
+ builder.allow(namespace, filter);
+ }
+ return builder.build();
+ }
+
+ @Test
+ public void testBundleAllow() throws InvalidSyntaxException {
+ RegionFilter regionFilter = createBundleFilter(BUNDLE_SYMBOLIC_NAME, BUNDLE_VERSION);
+ assertTrue(regionFilter.isAllowed(stubBundle));
+ }
+
+ @Test
+ public void testBundleAllNotAllowed() {
+ RegionFilter regionFilter = new StandardRegionFilterBuilder().build();
+ assertFalse(regionFilter.isAllowed(stubBundle));
+ }
+
+ @Test
+ public void testBundleAllAllowed() {
+ RegionFilter regionFilter = new StandardRegionFilterBuilder().allowAll(RegionFilter.VISIBLE_BUNDLE_NAMESPACE).build();
+ assertTrue(regionFilter.isAllowed(stubBundle));
+ }
+
+ @Test
+ public void testBundleNotAllowedInRange() throws InvalidSyntaxException {
+ RegionFilter regionFilter = createBundleFilter(BUNDLE_SYMBOLIC_NAME, new Version(1, 0, 0));
+ assertFalse(regionFilter.isAllowed(stubBundle));
+ }
+
+ @Test
+ public void testCapabilityAllowed() throws InvalidSyntaxException {
+ RegionFilter regionFilter = createRegionFilter(RegionFilter.VISIBLE_PACKAGE_NAMESPACE, Arrays.asList(packageImportPolicy));
+ assertTrue(regionFilter.isAllowed(fooPackage));
+ assertEquals(Arrays.asList(this.packageImportPolicy), regionFilter.getSharingPolicy().get(RegionFilter.VISIBLE_PACKAGE_NAMESPACE));
+ }
+
@Test
- public void testAllowBundle() {
- this.regionFilter.allowBundle(BUNDLE_SYMBOLIC_NAME, VersionRange.NATURAL_NUMBER_RANGE);
- assertTrue(this.regionFilter.isBundleAllowed(BUNDLE_SYMBOLIC_NAME, BUNDLE_VERSION));
+ public void testCapabilityAllNotAllowed() {
+ RegionFilter regionFilter = new StandardRegionFilterBuilder().build();
+ assertFalse(regionFilter.isAllowed(barPackage));
}
-
+
+ @Test
+ public void testCapabilityAllAllowed() {
+ RegionFilter regionFilter = new StandardRegionFilterBuilder().allowAll(RegionFilter.VISIBLE_PACKAGE_NAMESPACE).build();
+ assertTrue(regionFilter.isAllowed(barPackage));
+ }
+
@Test
- public void testBundleNotAllowed() {
- assertFalse(this.regionFilter.isBundleAllowed(BUNDLE_SYMBOLIC_NAME, BUNDLE_VERSION));
+ public void testCapabilityNotAllowed() throws InvalidSyntaxException {
+ RegionFilter regionFilter = createRegionFilter(RegionFilter.VISIBLE_PACKAGE_NAMESPACE, Arrays.asList(packageImportPolicy));
+ assertFalse(regionFilter.isAllowed(barPackage));
+ assertEquals(Arrays.asList(this.packageImportPolicy), regionFilter.getSharingPolicy().get(RegionFilter.VISIBLE_PACKAGE_NAMESPACE));
}
@Test
- public void testBundleNotAllowedInRange() {
- this.regionFilter.allowBundle(BUNDLE_SYMBOLIC_NAME, new VersionRange("1"));
+ public void testServiceAllowed() throws InvalidSyntaxException {
+ RegionFilter regionFilter = createRegionFilter(RegionFilter.VISIBLE_SERVICE_NAMESPACE, Arrays.asList(serviceImportPolicy));
+ assertTrue(regionFilter.isAllowed(fooService.getReference()));
+ assertEquals(Arrays.asList(serviceImportPolicy), regionFilter.getSharingPolicy().get(RegionFilter.VISIBLE_SERVICE_NAMESPACE));
+ }
- assertFalse(this.regionFilter.isBundleAllowed(BUNDLE_SYMBOLIC_NAME, BUNDLE_VERSION));
+ @Test
+ public void testServiceAllNotAllowed() {
+ RegionFilter regionFilter = new StandardRegionFilterBuilder().build();
+ assertFalse(regionFilter.isAllowed(fooService.getReference()));
}
@Test
- public void testSetPackageImportPolicy() {
- this.regionFilter.setPackageImportPolicy(this.packageImportPolicy);
- assertEquals(this.packageImportPolicy, this.regionFilter.getPackageImportPolicy());
+ public void testServiceAllAllowed() {
+ RegionFilter regionFilter = new StandardRegionFilterBuilder().allowAll(RegionFilter.VISIBLE_SERVICE_NAMESPACE).build();
+ assertTrue(regionFilter.isAllowed(fooService.getReference()));
}
@Test
- public void testSetServiceFilter() {
- this.regionFilter.setServiceFilter(this.mockFilter);
- assertEquals(this.mockFilter, this.regionFilter.getServiceFilter());
+ public void testServiceNotAllowed() throws InvalidSyntaxException {
+ RegionFilter regionFilter = createRegionFilter(RegionFilter.VISIBLE_SERVICE_NAMESPACE, Arrays.asList(serviceImportPolicy));
+ assertFalse(regionFilter.isAllowed(barService.getReference()));
+ assertEquals(Arrays.asList(serviceImportPolicy), regionFilter.getSharingPolicy().get(RegionFilter.VISIBLE_SERVICE_NAMESPACE));
}
+ @Test
+ public void testAllNamespace() throws InvalidSyntaxException {
+ RegionFilter regionFilterNotAllowed = new StandardRegionFilterBuilder().allow(RegionFilter.VISIBLE_ALL_NAMESPACE, "(all=namespace)").build();
+ assertFalse(regionFilterNotAllowed.isAllowed(stubBundle));
+ assertFalse(regionFilterNotAllowed.isAllowed(fooPackage));
+ assertFalse(regionFilterNotAllowed.isAllowed(barPackage));
+ assertFalse(regionFilterNotAllowed.isAllowed(fooService.getReference()));
+ assertFalse(regionFilterNotAllowed.isAllowed(barService.getReference()));
+
+ RegionFilter regionFilterAllAllowed = new StandardRegionFilterBuilder().allowAll(RegionFilter.VISIBLE_ALL_NAMESPACE).build();
+ assertTrue(regionFilterAllAllowed.isAllowed(stubBundle));
+ assertTrue(regionFilterAllAllowed.isAllowed(fooPackage));
+ assertTrue(regionFilterAllAllowed.isAllowed(barPackage));
+ assertTrue(regionFilterAllAllowed.isAllowed(fooService.getReference()));
+ assertTrue(regionFilterAllAllowed.isAllowed(barService.getReference()));
+ }
}
diff --git a/org.eclipse.virgo.kernel.userregion/src/main/java/org/eclipse/virgo/kernel/userregion/internal/quasi/DependencyCalculator.java b/org.eclipse.virgo.kernel.userregion/src/main/java/org/eclipse/virgo/kernel/userregion/internal/quasi/DependencyCalculator.java
index d5356490..7c747bcd 100644
--- a/org.eclipse.virgo.kernel.userregion/src/main/java/org/eclipse/virgo/kernel/userregion/internal/quasi/DependencyCalculator.java
+++ b/org.eclipse.virgo.kernel.userregion/src/main/java/org/eclipse/virgo/kernel/userregion/internal/quasi/DependencyCalculator.java
@@ -355,13 +355,13 @@ public final class DependencyCalculator {
BundleDescription[] bundleDescriptions = state.getBundles(bundleSymbolicName);
for (BundleDescription bundleDescription : bundleDescriptions) {
if (bundleDescription.getVersion().equals(version)) {
- //XXX Refactoring required here. This temporary code only traverses the coregion and user region.
+ // XXX Refactoring required here. This temporary code only traverses the coregion and user region.
Set<FilteredRegion> edges = this.coregion.getEdges();
FilteredRegion edge = edges.iterator().next();
Region userRegion = edge.getRegion();
RegionFilter filter = edge.getFilter();
long bundleId = bundleDescription.getBundleId();
- if ((bundleId == 0L || this.coregion.contains(bundleId) || (filter.isBundleAllowed(bundleSymbolicName, version) && userRegion.contains(bundleId)))) {
+ if ((bundleId == 0L || this.coregion.contains(bundleId) || (filter.isAllowed(bundleDescription) && userRegion.contains(bundleId)))) {
return true;
}
}
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 987b1fcd..64965ea1 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
@@ -69,6 +69,10 @@ import org.slf4j.LoggerFactory;
*/
final class StandardQuasiFramework implements QuasiFramework {
+ private final RegionFilter TOP;
+
+ private final String TOP_FILTER = "(|(!(x=*))(x=*))";
+
private static final String REGION_LOCATION_DELIMITER = "@";
private static final String COREGION_SUFFIX = ".coregion";
@@ -110,6 +114,7 @@ final class StandardQuasiFramework implements QuasiFramework {
StandardQuasiFramework(BundleContext bundleContext, State state, PlatformAdmin platformAdmin, ResolutionFailureDetective detective,
Repository repository, TransformedManifestProvidingBundleFileWrapper bundleTransformationHandler, RegionDigraph regionDigraph) {
+ TOP = regionDigraph.createRegionFilterBuilder().allowAll(RegionFilter.VISIBLE_ALL_NAMESPACE).build();
this.bundleContext = bundleContext;
this.state = state;
this.stateObjectFactory = platformAdmin.getFactory();
@@ -156,8 +161,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 d2c42074..46d56cfd 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
@@ -15,6 +15,8 @@ package org.eclipse.virgo.kernel.userregionfactory;
import java.net.URI;
import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Collections;
import java.util.Dictionary;
import java.util.HashMap;
import java.util.Hashtable;
@@ -29,21 +31,24 @@ 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.StandardRegionFilter;
+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;
import org.eclipse.virgo.util.osgi.ServiceRegistrationTracker;
+import org.eclipse.virgo.util.osgi.VersionRange;
import org.eclipse.virgo.util.osgi.manifest.BundleManifest;
import org.eclipse.virgo.util.osgi.manifest.BundleManifestFactory;
+import org.eclipse.virgo.util.osgi.manifest.ImportedPackage;
import org.eclipse.virgo.util.osgi.manifest.RequireBundle;
import org.eclipse.virgo.util.osgi.manifest.RequiredBundle;
import org.osgi.framework.Bundle;
import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;
import org.osgi.framework.BundleException;
-import org.osgi.framework.Filter;
+import org.osgi.framework.Constants;
import org.osgi.framework.InvalidSyntaxException;
+import org.osgi.framework.Version;
import org.osgi.service.cm.Configuration;
import org.osgi.service.cm.ConfigurationAdmin;
import org.osgi.service.event.Event;
@@ -86,6 +91,8 @@ public final class Activator implements BundleActivator {
private static final String EVENT_PROPERTY_REGION_BUNDLECONTEXT = "region.bundleContext";
+ private static final String WILDCARD = "*";
+
private EventAdmin eventAdmin;
private String regionBundles;
@@ -143,7 +150,7 @@ public final class Activator implements BundleActivator {
}
}
- private void createUserRegion(RegionDigraph regionDigraph, EventLogger eventLogger) throws BundleException {
+ private void createUserRegion(RegionDigraph regionDigraph, EventLogger eventLogger) throws BundleException, InvalidSyntaxException {
BundleContext systemBundleContext = getSystemBundleContext();
Bundle userRegionFactoryBundle = this.bundleContext.getBundle();
@@ -154,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(this.bundleContext);
@@ -168,48 +175,43 @@ public final class Activator implements BundleActivator {
publishUserRegionBundleContext(this.bundleContext);
}
- private RegionFilter createUserRegionFilter() throws BundleException {
- RegionFilter userRegionFilter = new StandardRegionFilter();
- Filter serviceFilter;
- try {
- serviceFilter = this.bundleContext.createFilter(classesToFilter(this.regionServiceExports));
- } catch (InvalidSyntaxException e) {
- throw new BundleException("Invalid " + USER_REGION_SERVICE_EXPORTS_PROPERTY + "in user region configuration: '"
- + this.regionServiceExports + "'", e);
+ private RegionFilter createUserRegionFilter(RegionDigraph digraph) throws InvalidSyntaxException {
+ Collection<String> serviceFilters = classesToFilter(this.regionServiceExports);
+ RegionFilterBuilder builder = digraph.createRegionFilterBuilder();
+ for (String filter : serviceFilters) {
+ builder.allow(RegionFilter.VISIBLE_SERVICE_NAMESPACE, filter);
}
- userRegionFilter.setServiceFilter(serviceFilter);
-
- return userRegionFilter;
+ return builder.build();
}
private Region getKernelRegion(RegionDigraph regionDigraph) {
return regionDigraph.iterator().next();
}
- private RegionFilter createKernelFilter(BundleContext systemBundleContext, EventLogger eventLogger) throws BundleException {
- RegionFilter kernelFilter = new StandardRegionFilter();
- allowImportedBundles(kernelFilter, eventLogger);
- kernelFilter.setPackageImportPolicy(createUserRegionPackageImportPolicy(systemBundleContext, eventLogger));
- Filter serviceFilter;
- try {
- serviceFilter = this.bundleContext.createFilter(classesToFilter(this.regionServiceImports));
- } catch (InvalidSyntaxException e) {
- throw new BundleException("Invalid " + USER_REGION_SERVICE_IMPORTS_PROPERTY + "in user region configuration: '"
- + this.regionServiceImports + "'", e);
+ 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);
}
- kernelFilter.setServiceFilter(serviceFilter);
- return kernelFilter;
+ Collection<String> allowedPackages = createUserRegionPackageImportPolicy(systemBundleContext, eventLogger);
+ 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(RegionFilter kernelFilter, 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);
- List<RequiredBundle> importedBundles = bundleImportsAsRequireBundle.getRequiredBundles();
- for (RequiredBundle importedBundle : importedBundles) {
- kernelFilter.allowBundle(importedBundle.getBundleSymbolicName(), importedBundle.getBundleVersion());
- }
+ return importBundleToFilter(bundleImportsAsRequireBundle.getRequiredBundles());
}
private RequireBundle representBundleImportsAsRequireBundle(String userRegionBundleImportsProperty, EventLogger eventLogger) {
@@ -219,24 +221,82 @@ public final class Activator implements BundleActivator {
return manifest.getRequireBundle();
}
- private String classesToFilter(String classList) {
+ 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;
+ }
+
+ private static Collection<String> importPackageToFilter(String importList) {
+ if (importList == null || importList.isEmpty()) {
+ return Collections.emptyList();
+ }
+ if (importList.contains(WILDCARD)) {
+ 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();
+ 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;
+ }
+
+ 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 + "))");
+ }
+ }
+ }
+
+ private static Collection<String> classesToFilter(String classList) {
if (classList == null) {
- return "";
+ return Collections.emptyList();
}
String[] classes = classList.split(CLASS_LIST_SEPARATOR);
if (classes.length == 0) {
- return "";
+ return Collections.emptyList();
}
- StringBuffer filter = new StringBuffer();
- filter.append("(|");
+ Collection<String> result = new ArrayList<String>(classes.length);
for (String className : classes) {
- filter.append("(objectClass=").append(className).append(")");
+ result.add("(objectClass=" + className + ")");
}
- filter.append(")");
- return filter.toString();
+ return result;
}
- private UserRegionPackageImportPolicy createUserRegionPackageImportPolicy(BundleContext systemBundleContext, EventLogger eventLogger) {
+ private Collection<String> createUserRegionPackageImportPolicy(BundleContext systemBundleContext, EventLogger eventLogger) {
String userRegionImportsProperty = this.regionPackageImports != null ? this.regionPackageImports
: this.bundleContext.getProperty(USER_REGION_PACKAGE_IMPORTS_PROPERTY);
String expandedUserRegionImportsProperty = null;
@@ -244,8 +304,7 @@ public final class Activator implements BundleActivator {
expandedUserRegionImportsProperty = PackageImportWildcardExpander.expandPackageImportsWildcards(userRegionImportsProperty,
systemBundleContext, eventLogger);
}
-
- return new UserRegionPackageImportPolicy(expandedUserRegionImportsProperty);
+ return importPackageToFilter(expandedUserRegionImportsProperty);
}
private BundleContext getSystemBundleContext() {
diff --git a/org.eclipse.virgo.kernel.userregionfactory/src/main/java/org/eclipse/virgo/kernel/userregionfactory/UserRegionPackageImportPolicy.java b/org.eclipse.virgo.kernel.userregionfactory/src/main/java/org/eclipse/virgo/kernel/userregionfactory/UserRegionPackageImportPolicy.java
deleted file mode 100644
index 06a85b13..00000000
--- a/org.eclipse.virgo.kernel.userregionfactory/src/main/java/org/eclipse/virgo/kernel/userregionfactory/UserRegionPackageImportPolicy.java
+++ /dev/null
@@ -1,128 +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.List;
-import java.util.Map;
-import java.util.Set;
-
-import org.eclipse.osgi.service.resolver.VersionRange;
-import org.eclipse.virgo.kernel.osgi.region.RegionPackageImportPolicy;
-import org.eclipse.virgo.util.osgi.manifest.BundleManifest;
-import org.eclipse.virgo.util.osgi.manifest.BundleManifestFactory;
-import org.eclipse.virgo.util.osgi.manifest.ImportedPackage;
-import org.osgi.framework.Version;
-
-/**
- * {@link UserRegionPackageImportPolicy} is a {@link RegionPackageImportPolicy} for a user region.
- * <p />
- *
- * <strong>Concurrent Semantics</strong><br />
- *
- * Thread safe.
- *
- */
-class UserRegionPackageImportPolicy implements RegionPackageImportPolicy {
-
- private static final String MANDATORY_ATTRIBUTE_NAME_SEPARATOR = ",";
-
- private static final String MANDATORY_DIRECTIVE_NAME = "mandatory";
-
- private static final String VERSION_ATTRIBUTE_NAME = "version";
-
- private static final String WILDCARD = "*";
-
- private final Map<String, ImportedPackage> importedPackages = new HashMap<String, ImportedPackage>();
-
- /**
- * Construct a {@link UserRegionPackageImportPolicy} for the specified import package list which must not contain
- * wildcards.
- *
- * @param regionImports a string representing a list of imported packages
- */
- UserRegionPackageImportPolicy(String regionImports) {
- if (regionImports != null && !regionImports.isEmpty()) {
- if (regionImports.contains(WILDCARD)) {
- throw new IllegalArgumentException("Wildcards not supported in region imports: '" + regionImports + "'");
- }
- BundleManifest manifest = BundleManifestFactory.createBundleManifest();
- manifest.setHeader("Import-Package", regionImports);
- List<ImportedPackage> list = manifest.getImportPackage().getImportedPackages();
- for (ImportedPackage importedPackage : list) {
- String packageName = importedPackage.getPackageName();
- this.importedPackages.put(packageName, importedPackage);
- }
- }
- }
-
- /**
- * {@inheritDoc}
- */
- @Override
- public boolean isImported(String packageName, Map<String, Object> exportAttributes, Map<String, String> exportDirectives) {
- ImportedPackage importedPackage = this.importedPackages.get(packageName);
- if (importedPackage != null) {
- Map<String, String> importAttributes = importedPackage.getAttributes();
- Set<String> importAttributeNames = importAttributes.keySet();
-
- // Check any attribute values match.
- for (String importAttributeName : importAttributeNames) {
- if (exportAttributes == null) {
- return false;
- }
- Object exportAttributeValue = exportAttributes.get(importAttributeName);
- if (importAttributeName.equals(VERSION_ATTRIBUTE_NAME)) {
- if (exportAttributeValue != null && exportAttributeValue instanceof Version) {
- Version exportVersion = (Version) exportAttributeValue;
- String importAttributeValue = importAttributes.get(importAttributeName);
- VersionRange importVersion = new VersionRange(importAttributeValue);
- if (!importVersion.isIncluded(exportVersion)) {
- return false;
- }
- } else {
- return false;
- }
- } else {
- if (exportAttributeValue != null && exportAttributeValue instanceof String) {
- String exportAttributeValueString = (String) exportAttributeValue;
- String importAttributeValue = importAttributes.get(importAttributeName);
- if (!exportAttributeValueString.equals(importAttributeValue)) {
- return false;
- }
- } else {
- return false;
- }
- }
-
- }
-
- // Check mandatory attributes are present.
- if (exportDirectives != null) {
- String mandatoryDirectiveValue = exportDirectives.get(MANDATORY_DIRECTIVE_NAME);
- if (mandatoryDirectiveValue != null) {
- for (String mandatoryAttribute : mandatoryDirectiveValue.split(MANDATORY_ATTRIBUTE_NAME_SEPARATOR)) {
- if (!importAttributeNames.contains(mandatoryAttribute)) {
- return false;
- }
- }
- }
- }
- return true;
- }
-
- return false;
- }
-
-}
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 308470db..00000000
--- a/org.eclipse.virgo.kernel.userregionfactory/src/test/java/org/eclipse/virgo/kernel/userregionfactory/UserRegionPackageImportPolicyTests.java
+++ /dev/null
@@ -1,115 +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.eclipse.virgo.kernel.osgi.region.RegionPackageImportPolicy;
-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));
- }
-
-}

Back to the top