diff options
author | Thomas Watson | 2015-09-29 21:33:17 +0000 |
---|---|---|
committer | Thomas Watson | 2015-09-29 21:34:47 +0000 |
commit | 62e50f08890d7a52a95f0a18bcb227baea8434d3 (patch) | |
tree | 7a043920d17f30fc5a4bbebae7f112403e683c51 | |
parent | 582504f8895753d81ddcc79fbd758b1a48a8fd31 (diff) | |
download | rt.equinox.bundles-62e50f08890d7a52a95f0a18bcb227baea8434d3.tar.gz rt.equinox.bundles-62e50f08890d7a52a95f0a18bcb227baea8434d3.tar.xz rt.equinox.bundles-62e50f08890d7a52a95f0a18bcb227baea8434d3.zip |
Bug 478674 - Deprecate RegionFilter.VISIBLE_SERVICE_NAMESPACEI20151013-0800I20151006-0800
4 files changed, 124 insertions, 11 deletions
diff --git a/bundles/org.eclipse.equinox.region.tests/src/org/eclipse/equinox/region/internal/tests/StandardRegionFilterTests.java b/bundles/org.eclipse.equinox.region.tests/src/org/eclipse/equinox/region/internal/tests/StandardRegionFilterTests.java index df70ede36..3a1ff2e04 100644 --- a/bundles/org.eclipse.equinox.region.tests/src/org/eclipse/equinox/region/internal/tests/StandardRegionFilterTests.java +++ b/bundles/org.eclipse.equinox.region.tests/src/org/eclipse/equinox/region/internal/tests/StandardRegionFilterTests.java @@ -13,6 +13,7 @@ package org.eclipse.equinox.region.internal.tests; 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.*; @@ -42,8 +43,10 @@ public class StandardRegionFilterTests { private BundleCapability barPackage; + private BundleCapability fooServiceCapability; private ServiceRegistration<Object> fooService; + private BundleCapability barServiceCapability; private ServiceRegistration<Object> barService; @Before @@ -59,12 +62,26 @@ public class StandardRegionFilterTests { EasyMock.expect(fooPackage.getAttributes()).andReturn(fooAttrs).anyTimes(); EasyMock.replay(fooPackage); + this.fooServiceCapability = EasyMock.createMock(BundleCapability.class); + Map<String, Object> fooServiceAttrs = new HashMap<String, Object>(); + fooServiceAttrs.put(Constants.OBJECTCLASS, "foo.Service"); + EasyMock.expect(fooServiceCapability.getNamespace()).andReturn(RegionFilter.VISIBLE_OSGI_SERVICE_NAMESPACE).anyTimes(); + EasyMock.expect(fooServiceCapability.getAttributes()).andReturn(fooServiceAttrs).anyTimes(); + EasyMock.replay(fooServiceCapability); + 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); + + this.barServiceCapability = EasyMock.createMock(BundleCapability.class); + Map<String, Object> barServiceAttrs = new HashMap<String, Object>(); + barServiceAttrs.put(Constants.OBJECTCLASS, "bar.Service"); + EasyMock.expect(barServiceCapability.getNamespace()).andReturn(RegionFilter.VISIBLE_OSGI_SERVICE_NAMESPACE).anyTimes(); + EasyMock.expect(barServiceCapability.getAttributes()).andReturn(barServiceAttrs).anyTimes(); + EasyMock.replay(barServiceCapability); } private RegionFilter createBundleFilter(String bundleSymbolicName, Version bundleVersion) throws InvalidSyntaxException { @@ -130,30 +147,76 @@ public class StandardRegionFilterTests { assertEquals(Arrays.asList(this.packageImportPolicy), regionFilter.getSharingPolicy().get(RegionFilter.VISIBLE_PACKAGE_NAMESPACE)); } + @SuppressWarnings("deprecation") @Test public void testServiceAllowed() throws InvalidSyntaxException { RegionFilter regionFilter = createRegionFilter(RegionFilter.VISIBLE_SERVICE_NAMESPACE, Arrays.asList(serviceImportPolicy)); assertTrue(regionFilter.isAllowed(fooService.getReference())); + assertTrue(regionFilter.isAllowed(fooServiceCapability)); assertEquals(Arrays.asList(serviceImportPolicy), regionFilter.getSharingPolicy().get(RegionFilter.VISIBLE_SERVICE_NAMESPACE)); + + regionFilter = createRegionFilter(RegionFilter.VISIBLE_OSGI_SERVICE_NAMESPACE, Arrays.asList(serviceImportPolicy)); + assertTrue(regionFilter.isAllowed(fooService.getReference())); + assertTrue(regionFilter.isAllowed(fooServiceCapability)); + assertNull(regionFilter.getSharingPolicy().get(RegionFilter.VISIBLE_SERVICE_NAMESPACE)); + assertEquals(Arrays.asList(serviceImportPolicy), regionFilter.getSharingPolicy().get(RegionFilter.VISIBLE_OSGI_SERVICE_NAMESPACE)); } @Test public void testServiceAllNotAllowed() { RegionFilter regionFilter = RegionReflectionUtils.newStandardRegionFilterBuilder().build(); assertFalse(regionFilter.isAllowed(fooService.getReference())); + assertFalse(regionFilter.isAllowed(fooServiceCapability)); } @Test public void testServiceAllAllowed() { + @SuppressWarnings("deprecation") RegionFilter regionFilter = RegionReflectionUtils.newStandardRegionFilterBuilder().allowAll(RegionFilter.VISIBLE_SERVICE_NAMESPACE).build(); assertTrue(regionFilter.isAllowed(fooService.getReference())); + assertTrue(regionFilter.isAllowed(fooServiceCapability)); + + regionFilter = RegionReflectionUtils.newStandardRegionFilterBuilder().allowAll(RegionFilter.VISIBLE_OSGI_SERVICE_NAMESPACE).build(); + assertTrue(regionFilter.isAllowed(fooService.getReference())); + assertTrue(regionFilter.isAllowed(fooServiceCapability)); } + @SuppressWarnings("deprecation") @Test public void testServiceNotAllowed() throws InvalidSyntaxException { RegionFilter regionFilter = createRegionFilter(RegionFilter.VISIBLE_SERVICE_NAMESPACE, Arrays.asList(serviceImportPolicy)); assertFalse(regionFilter.isAllowed(barService.getReference())); + assertFalse(regionFilter.isAllowed(barServiceCapability)); assertEquals(Arrays.asList(serviceImportPolicy), regionFilter.getSharingPolicy().get(RegionFilter.VISIBLE_SERVICE_NAMESPACE)); + assertEquals(Arrays.asList(serviceImportPolicy), regionFilter.getSharingPolicy().get(RegionFilter.VISIBLE_OSGI_SERVICE_NAMESPACE)); + + regionFilter = createRegionFilter(RegionFilter.VISIBLE_OSGI_SERVICE_NAMESPACE, Arrays.asList(serviceImportPolicy)); + assertFalse(regionFilter.isAllowed(barService.getReference())); + assertFalse(regionFilter.isAllowed(barServiceCapability)); + assertNull(regionFilter.getSharingPolicy().get(RegionFilter.VISIBLE_SERVICE_NAMESPACE)); + assertEquals(Arrays.asList(serviceImportPolicy), regionFilter.getSharingPolicy().get(RegionFilter.VISIBLE_OSGI_SERVICE_NAMESPACE)); + } + + @SuppressWarnings("deprecation") + @Test + public void testAllNamespaceForService() throws InvalidSyntaxException { + RegionFilter negateNonServices = RegionReflectionUtils.newStandardRegionFilterBuilder().allow(RegionFilter.VISIBLE_ALL_NAMESPACE, "(" + RegionFilter.VISIBLE_ALL_NAMESPACE_ATTRIBUTE + "=" + RegionFilter.VISIBLE_SERVICE_NAMESPACE + ")").build(); + assertFalse(negateNonServices.isAllowed(stubBundle)); + assertFalse(negateNonServices.isAllowed(fooPackage)); + assertFalse(negateNonServices.isAllowed(barPackage)); + assertTrue(negateNonServices.isAllowed(fooService.getReference())); + assertTrue(negateNonServices.isAllowed(fooServiceCapability)); + assertTrue(negateNonServices.isAllowed(barService.getReference())); + assertTrue(negateNonServices.isAllowed(barServiceCapability)); + + negateNonServices = RegionReflectionUtils.newStandardRegionFilterBuilder().allow(RegionFilter.VISIBLE_ALL_NAMESPACE, "(" + RegionFilter.VISIBLE_ALL_NAMESPACE_ATTRIBUTE + "=" + RegionFilter.VISIBLE_OSGI_SERVICE_NAMESPACE + ")").build(); + assertFalse(negateNonServices.isAllowed(stubBundle)); + assertFalse(negateNonServices.isAllowed(fooPackage)); + assertFalse(negateNonServices.isAllowed(barPackage)); + assertTrue(negateNonServices.isAllowed(fooService.getReference())); + assertTrue(negateNonServices.isAllowed(fooServiceCapability)); + assertTrue(negateNonServices.isAllowed(barService.getReference())); + assertTrue(negateNonServices.isAllowed(barServiceCapability)); } @Test @@ -163,16 +226,21 @@ public class StandardRegionFilterTests { assertFalse(regionFilterNotAllowed.isAllowed(fooPackage)); assertFalse(regionFilterNotAllowed.isAllowed(barPackage)); assertFalse(regionFilterNotAllowed.isAllowed(fooService.getReference())); + assertFalse(regionFilterNotAllowed.isAllowed(fooServiceCapability)); assertFalse(regionFilterNotAllowed.isAllowed(barService.getReference())); + assertFalse(regionFilterNotAllowed.isAllowed(barServiceCapability)); RegionFilter regionFilterAllAllowed = RegionReflectionUtils.newStandardRegionFilterBuilder().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())); + assertFalse(regionFilterNotAllowed.isAllowed(fooService.getReference())); + assertFalse(regionFilterNotAllowed.isAllowed(fooServiceCapability)); + assertFalse(regionFilterNotAllowed.isAllowed(barService.getReference())); + assertFalse(regionFilterNotAllowed.isAllowed(barServiceCapability)); } + @SuppressWarnings("deprecation") @Test public void testNegativeAllNamespace() throws InvalidSyntaxException { RegionFilter negateServices = RegionReflectionUtils.newStandardRegionFilterBuilder().allow(RegionFilter.VISIBLE_ALL_NAMESPACE, "(!(" + RegionFilter.VISIBLE_ALL_NAMESPACE_ATTRIBUTE + "=" + RegionFilter.VISIBLE_SERVICE_NAMESPACE + "))").build(); @@ -180,6 +248,17 @@ public class StandardRegionFilterTests { assertTrue(negateServices.isAllowed(fooPackage)); assertTrue(negateServices.isAllowed(barPackage)); assertFalse(negateServices.isAllowed(fooService.getReference())); + assertFalse(negateServices.isAllowed(fooServiceCapability)); + assertFalse(negateServices.isAllowed(barService.getReference())); + assertFalse(negateServices.isAllowed(barServiceCapability)); + + negateServices = RegionReflectionUtils.newStandardRegionFilterBuilder().allow(RegionFilter.VISIBLE_ALL_NAMESPACE, "(!(" + RegionFilter.VISIBLE_ALL_NAMESPACE_ATTRIBUTE + "=" + RegionFilter.VISIBLE_OSGI_SERVICE_NAMESPACE + "))").build(); + assertTrue(negateServices.isAllowed(stubBundle)); + assertTrue(negateServices.isAllowed(fooPackage)); + assertTrue(negateServices.isAllowed(barPackage)); + assertFalse(negateServices.isAllowed(fooService.getReference())); + assertFalse(negateServices.isAllowed(fooServiceCapability)); assertFalse(negateServices.isAllowed(barService.getReference())); + assertFalse(negateServices.isAllowed(barServiceCapability)); } } diff --git a/bundles/org.eclipse.equinox.region/src/org/eclipse/equinox/internal/region/StandardRegionFilter.java b/bundles/org.eclipse.equinox.region/src/org/eclipse/equinox/internal/region/StandardRegionFilter.java index 7554c4eab..9b99faf6f 100644 --- a/bundles/org.eclipse.equinox.region/src/org/eclipse/equinox/internal/region/StandardRegionFilter.java +++ b/bundles/org.eclipse.equinox.region/src/org/eclipse/equinox/internal/region/StandardRegionFilter.java @@ -89,9 +89,9 @@ public final class StandardRegionFilter implements RegionFilter { } public boolean isAllowed(ServiceReference<?> service) { - if (match(filters.get(VISIBLE_SERVICE_NAMESPACE), service)) + if (match(filters.get(VISIBLE_OSGI_SERVICE_NAMESPACE), service)) return true; - return matchAll(VISIBLE_SERVICE_NAMESPACE, service); + return matchAll(VISIBLE_OSGI_SERVICE_NAMESPACE, service); } public boolean isAllowed(BundleCapability capability) { @@ -108,15 +108,22 @@ public final class StandardRegionFilter implements RegionFilter { return matchAll(namespace, attributes); } + @SuppressWarnings("deprecation") + static final String[] serviceNamespaces = new String[] {VISIBLE_OSGI_SERVICE_NAMESPACE, VISIBLE_SERVICE_NAMESPACE}; + private boolean matchAll(final String namespace, final Map<String, ?> attributes) { Collection<Filter> allMatching = filters.get(VISIBLE_ALL_NAMESPACE); if (allMatching == null) { return false; } return match(allMatching, new AbstractMap<String, Object>() { + @SuppressWarnings("deprecation") @Override public Object get(Object key) { if (RegionFilter.VISIBLE_ALL_NAMESPACE_ATTRIBUTE.equals(key)) { + if (VISIBLE_SERVICE_NAMESPACE.equals(namespace) || VISIBLE_OSGI_SERVICE_NAMESPACE.equals(namespace)) { + return serviceNamespaces; + } return namespace; } return attributes.get(key); @@ -135,9 +142,13 @@ public final class StandardRegionFilter implements RegionFilter { return false; } return match(allMatching, new AbstractMap<String, Object>() { + @SuppressWarnings("deprecation") @Override public Object get(Object key) { if (RegionFilter.VISIBLE_ALL_NAMESPACE_ATTRIBUTE.equals(key)) { + if (VISIBLE_SERVICE_NAMESPACE.equals(namespace) || VISIBLE_OSGI_SERVICE_NAMESPACE.equals(namespace)) { + return serviceNamespaces; + } return namespace; } return service.getProperty((String) key); diff --git a/bundles/org.eclipse.equinox.region/src/org/eclipse/equinox/internal/region/StandardRegionFilterBuilder.java b/bundles/org.eclipse.equinox.region/src/org/eclipse/equinox/internal/region/StandardRegionFilterBuilder.java index aa6ff4889..d0315406e 100644 --- a/bundles/org.eclipse.equinox.region/src/org/eclipse/equinox/internal/region/StandardRegionFilterBuilder.java +++ b/bundles/org.eclipse.equinox.region/src/org/eclipse/equinox/internal/region/StandardRegionFilterBuilder.java @@ -11,6 +11,9 @@ package org.eclipse.equinox.internal.region; +import static org.eclipse.equinox.region.RegionFilter.VISIBLE_OSGI_SERVICE_NAMESPACE; +import static org.eclipse.equinox.region.RegionFilter.VISIBLE_SERVICE_NAMESPACE; + import java.util.*; import org.eclipse.equinox.region.RegionFilter; import org.eclipse.equinox.region.RegionFilterBuilder; @@ -21,6 +24,7 @@ public final class StandardRegionFilterBuilder implements RegionFilterBuilder { private final static String ALL_SPEC = "(|(!(all=*))(all=*))"; //$NON-NLS-1$ private final static Filter ALL; + static { try { ALL = FrameworkUtil.createFilter(ALL_SPEC); @@ -35,6 +39,7 @@ public final class StandardRegionFilterBuilder implements RegionFilterBuilder { private final Map<String, Collection<Filter>> policy = new HashMap<String, Collection<Filter>>(); + @SuppressWarnings("deprecation") public RegionFilterBuilder allow(String namespace, String filter) throws InvalidSyntaxException { if (namespace == null) throw new IllegalArgumentException("The namespace must not be null."); //$NON-NLS-1$ @@ -43,28 +48,39 @@ public final class StandardRegionFilterBuilder implements RegionFilterBuilder { synchronized (this.monitor) { Collection<Filter> namespaceFilters = policy.get(namespace); if (namespaceFilters == null) { - namespaceFilters = new ArrayList<Filter>(); + // use set to avoid duplicates + namespaceFilters = new LinkedHashSet<Filter>(); policy.put(namespace, namespaceFilters); } // TODO need to use BundleContext.createFilter here namespaceFilters.add(FrameworkUtil.createFilter(filter)); } + if (VISIBLE_SERVICE_NAMESPACE.equals(namespace)) { + // alias the deprecated namespace to osgi.service + allow(VISIBLE_OSGI_SERVICE_NAMESPACE, filter); + } return this; } + @SuppressWarnings("deprecation") public RegionFilterBuilder allowAll(String namespace) { if (namespace == null) throw new IllegalArgumentException("The namespace must not be null."); //$NON-NLS-1$ synchronized (this.monitor) { Collection<Filter> namespaceFilters = policy.get(namespace); if (namespaceFilters == null) { - namespaceFilters = new ArrayList<Filter>(); + // use set to avoid duplicates + namespaceFilters = new LinkedHashSet<Filter>(); policy.put(namespace, namespaceFilters); } // remove any other filters since this will override them all. namespaceFilters.clear(); namespaceFilters.add(ALL); } + if (VISIBLE_SERVICE_NAMESPACE.equals(namespace)) { + // alias the deprecated namespace to osgi.service + allowAll(VISIBLE_OSGI_SERVICE_NAMESPACE); + } return this; } diff --git a/bundles/org.eclipse.equinox.region/src/org/eclipse/equinox/region/RegionFilter.java b/bundles/org.eclipse.equinox.region/src/org/eclipse/equinox/region/RegionFilter.java index 7b514de5e..21370791e 100644 --- a/bundles/org.eclipse.equinox.region/src/org/eclipse/equinox/region/RegionFilter.java +++ b/bundles/org.eclipse.equinox.region/src/org/eclipse/equinox/region/RegionFilter.java @@ -55,10 +55,17 @@ public interface RegionFilter { /** * Name space for sharing services. The filters specified in this name space will be used to match * {@link ServiceReference services}. + * @deprecated use the {@link RegionFilter#VISIBLE_OSGI_SERVICE_NAMESPACE osgi.service} namespace instead. */ public static final String VISIBLE_SERVICE_NAMESPACE = "org.eclipse.equinox.allow.service"; //$NON-NLS-1$ /** + * Name space for sharing services. The filters specified in this name space will be used to match + * {@link ServiceReference services} as well as {@code osgi.service} capabilities. + */ + public static final String VISIBLE_OSGI_SERVICE_NAMESPACE = "osgi.service"; //$NON-NLS-1$ + + /** * 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 attributes {@link Constants#BUNDLE_SYMBOLICNAME_ATTRIBUTE bundle-symbolic-name} * and org.eclipse.equinox.allow.bundle are used for the symbolic name and the attribute @@ -87,7 +94,7 @@ public interface RegionFilter { /** * An attribute used to hold the namespace for which the attributes belong to when using the - * {@link #VISIBLE_ALL_NAMESPACE} namespace to mach all capabilities. This can be useful + * {@link #VISIBLE_ALL_NAMESPACE} namespace to match all capabilities. This can be useful * for excluding some namespaces from the all namespace. For example, * <q>(!(org.eclipse.equinox.allow.all.namespace=osgi.wiring.package))</q> will allow everything * except package capabilities. @@ -120,7 +127,7 @@ public interface RegionFilter { * Determines whether this filter allows the given service reference. A * service is allowed if its service properties successfully matches one * or more filters specified using the - * {@link RegionFilter#VISIBLE_SERVICE_NAMESPACE} name space. + * {@link RegionFilter#VISIBLE_OSGI_SERVICE_NAMESPACE} name space. * * @param service the service reference of the service * @return <code>true</code> if the service is allowed and <code>false</code>otherwise @@ -149,9 +156,9 @@ public interface RegionFilter { * The name space can be any generic name space including but not limited to the following: * {@link #VISIBLE_BUNDLE_NAMESPACE}, {@link #VISIBLE_PACKAGE_NAMESPACE}, * {@link #VISIBLE_HOST_NAMESPACE}, {@link #VISIBLE_REQUIRE_NAMESPACE} and - * {@link #VISIBLE_SERVICE_NAMESPACE}. Any other generic capability - * {@link RegionFilterBuilder#allow(String, String) name spaces} can also - * be allowed by the region filter. + * {@link #VISIBLE_OSGI_SERVICE_NAMESPACE}. + * Any other generic capability {@link RegionFilterBuilder#allow(String, String) name spaces} + * can also be allowed by the region filter. * * @param namespace the name space * @param attributes the attributes to check if they are allowed |