Repository: aries-rsa Updated Branches: refs/heads/master fc5cd4108 -> 55809ab28
Refactor discovery local for simpler testing Project: http://git-wip-us.apache.org/repos/asf/aries-rsa/repo Commit: http://git-wip-us.apache.org/repos/asf/aries-rsa/commit/bb50834e Tree: http://git-wip-us.apache.org/repos/asf/aries-rsa/tree/bb50834e Diff: http://git-wip-us.apache.org/repos/asf/aries-rsa/diff/bb50834e Branch: refs/heads/master Commit: bb50834e418dbc331a5c314276bc0756fd51a86b Parents: fc5cd41 Author: Christian Schneider <[email protected]> Authored: Tue Mar 22 11:06:20 2016 +0100 Committer: Christian Schneider <[email protected]> Committed: Tue Mar 22 11:06:20 2016 +0100 ---------------------------------------------------------------------- .../aries/rsa/discovery/local/Activator.java | 59 +++++-- .../rsa/discovery/local/LocalDiscovery.java | 56 +------ .../rsa/discovery/local/ActivatorTest.java | 27 ---- .../rsa/discovery/local/LocalDiscoveryTest.java | 152 +++++-------------- .../publish/PublishingEndpointListener.java | 4 +- 5 files changed, 93 insertions(+), 205 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/aries-rsa/blob/bb50834e/discovery/local/src/main/java/org/apache/aries/rsa/discovery/local/Activator.java ---------------------------------------------------------------------- diff --git a/discovery/local/src/main/java/org/apache/aries/rsa/discovery/local/Activator.java b/discovery/local/src/main/java/org/apache/aries/rsa/discovery/local/Activator.java index 7ac9ee0..b1d6cd3 100644 --- a/discovery/local/src/main/java/org/apache/aries/rsa/discovery/local/Activator.java +++ b/discovery/local/src/main/java/org/apache/aries/rsa/discovery/local/Activator.java @@ -20,22 +20,59 @@ package org.apache.aries.rsa.discovery.local; import org.osgi.framework.BundleActivator; import org.osgi.framework.BundleContext; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; +import org.osgi.framework.ServiceReference; +import org.osgi.service.remoteserviceadmin.EndpointListener; +import org.osgi.util.tracker.ServiceTracker; public class Activator implements BundleActivator { + private ServiceTracker<EndpointListener, EndpointListener> listenerTracker; + private LocalDiscovery localDiscovery; - private static final Logger LOG = LoggerFactory.getLogger(Activator.class); - - LocalDiscovery localDiscovery; + public void start(BundleContext context) { + localDiscovery = new LocalDiscovery(); + listenerTracker = new EPListenerTracker(context, localDiscovery); + listenerTracker.open(); + localDiscovery.processExistingBundles(context.getBundles()); + context.addBundleListener(localDiscovery); + } - public synchronized void start(BundleContext context) { - localDiscovery = new LocalDiscovery(context); - LOG.info("Local D-OSGi service discovery started"); + public void stop(BundleContext context) { + listenerTracker.close(); + context.removeBundleListener(localDiscovery); } - public synchronized void stop(BundleContext context) { - localDiscovery.shutDown(); - LOG.info("Local D-OSGi service discovery stopped"); + private final class EPListenerTracker extends ServiceTracker<EndpointListener, EndpointListener> { + private final LocalDiscovery localDiscovery; + + private EPListenerTracker(BundleContext context, LocalDiscovery localDiscovery) { + super(context, EndpointListener.class, null); + this.localDiscovery = localDiscovery; + } + + @Override + public EndpointListener addingService(ServiceReference<EndpointListener> reference) { + EndpointListener service = super.addingService(reference); + localDiscovery.addListener(reference, service); + return service; + } + + @Override + public void modifiedService(ServiceReference<EndpointListener> reference, EndpointListener service) { + super.modifiedService(reference, service); + localDiscovery.removeListener(service); + + // This may cause duplicate registrations of remote services, + // but that's fine and should be filtered out on another level. + // See Remote Service Admin spec section 122.6.3 + localDiscovery.addListener(reference, service); + } + + @Override + public void removedService(ServiceReference<EndpointListener> reference, EndpointListener service) { + super.removedService(reference, service); + localDiscovery.removeListener(service); + } } + + } http://git-wip-us.apache.org/repos/asf/aries-rsa/blob/bb50834e/discovery/local/src/main/java/org/apache/aries/rsa/discovery/local/LocalDiscovery.java ---------------------------------------------------------------------- diff --git a/discovery/local/src/main/java/org/apache/aries/rsa/discovery/local/LocalDiscovery.java b/discovery/local/src/main/java/org/apache/aries/rsa/discovery/local/LocalDiscovery.java index ecb30de..823d736 100644 --- a/discovery/local/src/main/java/org/apache/aries/rsa/discovery/local/LocalDiscovery.java +++ b/discovery/local/src/main/java/org/apache/aries/rsa/discovery/local/LocalDiscovery.java @@ -32,14 +32,13 @@ import java.util.concurrent.ConcurrentHashMap; import org.apache.aries.rsa.discovery.endpoint.EndpointDescriptionBundleParser; import org.apache.aries.rsa.util.StringPlus; import org.osgi.framework.Bundle; -import org.osgi.framework.BundleContext; import org.osgi.framework.BundleEvent; import org.osgi.framework.BundleListener; import org.osgi.framework.Filter; +import org.osgi.framework.FrameworkUtil; import org.osgi.framework.ServiceReference; import org.osgi.service.remoteserviceadmin.EndpointDescription; import org.osgi.service.remoteserviceadmin.EndpointListener; -import org.osgi.util.tracker.ServiceTracker; public class LocalDiscovery implements BundleListener { @@ -51,50 +50,14 @@ public class LocalDiscovery implements BundleListener { new HashMap<EndpointListener, Collection<String>>(); Map<String, Collection<EndpointListener>> filterToListeners = new HashMap<String, Collection<EndpointListener>>(); - final BundleContext bundleContext; EndpointDescriptionBundleParser bundleParser; - ServiceTracker<EndpointListener, EndpointListener> listenerTracker; - public LocalDiscovery(BundleContext bc) { + public LocalDiscovery() { this.bundleParser = new EndpointDescriptionBundleParser(); - bundleContext = bc; - - listenerTracker = new ServiceTracker<EndpointListener, EndpointListener>(bundleContext, - EndpointListener.class, null) { - - @Override - public EndpointListener addingService(ServiceReference<EndpointListener> reference) { - EndpointListener service = super.addingService(reference); - addListener(reference, service); - return service; - } - - @Override - public void modifiedService(ServiceReference<EndpointListener> reference, EndpointListener service) { - super.modifiedService(reference, service); - removeListener(service); - - // This may cause duplicate registrations of remote services, - // but that's fine and should be filtered out on another level. - // See Remote Service Admin spec section 122.6.3 - addListener(reference, service); - } - - @Override - public void removedService(ServiceReference<EndpointListener> reference, EndpointListener service) { - super.removedService(reference, service); - removeListener(service); - } - }; - listenerTracker.open(); - - bundleContext.addBundleListener(this); - processExistingBundles(); } - private void processExistingBundles() { - Bundle[] bundles = bundleContext.getBundles(); + public void processExistingBundles(Bundle[] bundles) { if (bundles == null) { return; } @@ -158,7 +121,7 @@ public class LocalDiscovery implements BundleListener { synchronized (listenerToFilters) { for (Entry<String, Collection<EndpointListener>> entry : filterToListeners.entrySet()) { String filter = entry.getKey(); - if (LocalDiscovery.matchFilter(bundleContext, filter, endpoint)) { + if (LocalDiscovery.matchFilter(filter, endpoint)) { matched.put(filter, new ArrayList<EndpointListener>(entry.getValue())); } } @@ -166,11 +129,6 @@ public class LocalDiscovery implements BundleListener { return matched; } - public void shutDown() { - bundleContext.removeBundleListener(this); - listenerTracker.close(); - } - // BundleListener method public void bundleChanged(BundleEvent be) { switch (be.getType()) { @@ -222,7 +180,7 @@ public class LocalDiscovery implements BundleListener { private void triggerCallbacks(EndpointListener endpointListener, String filter, EndpointDescription endpoint, boolean added) { - if (!LocalDiscovery.matchFilter(bundleContext, filter, endpoint)) { + if (!LocalDiscovery.matchFilter(filter, endpoint)) { return; } @@ -241,13 +199,13 @@ public class LocalDiscovery implements BundleListener { } } - private static boolean matchFilter(BundleContext bctx, String filter, EndpointDescription endpoint) { + private static boolean matchFilter(String filter, EndpointDescription endpoint) { if (filter == null) { return false; } try { - Filter f = bctx.createFilter(filter); + Filter f = FrameworkUtil.createFilter(filter); Dictionary<String, Object> dict = new Hashtable<String, Object>(endpoint.getProperties()); return f.match(dict); } catch (Exception e) { http://git-wip-us.apache.org/repos/asf/aries-rsa/blob/bb50834e/discovery/local/src/test/java/org/apache/aries/rsa/discovery/local/ActivatorTest.java ---------------------------------------------------------------------- diff --git a/discovery/local/src/test/java/org/apache/aries/rsa/discovery/local/ActivatorTest.java b/discovery/local/src/test/java/org/apache/aries/rsa/discovery/local/ActivatorTest.java index a69e7e6..478e41b 100644 --- a/discovery/local/src/test/java/org/apache/aries/rsa/discovery/local/ActivatorTest.java +++ b/discovery/local/src/test/java/org/apache/aries/rsa/discovery/local/ActivatorTest.java @@ -20,35 +20,8 @@ package org.apache.aries.rsa.discovery.local; import junit.framework.TestCase; -import org.apache.aries.rsa.discovery.local.Activator; -import org.apache.aries.rsa.discovery.local.LocalDiscovery; -import org.easymock.EasyMock; -import org.easymock.IAnswer; -import org.osgi.framework.BundleContext; -import org.osgi.framework.Filter; -import org.osgi.framework.FrameworkUtil; - public class ActivatorTest extends TestCase { public void testActivator() throws Exception { - BundleContext bc = EasyMock.createNiceMock(BundleContext.class); - EasyMock.expect(bc.createFilter((String) EasyMock.anyObject())).andAnswer(new IAnswer<Filter>() { - public Filter answer() throws Throwable { - return FrameworkUtil.createFilter((String) EasyMock.getCurrentArguments()[0]); - } - }).anyTimes(); - EasyMock.replay(bc); - - Activator a = new Activator(); - a.start(bc); - assertNotNull(a.localDiscovery); - - a.localDiscovery = EasyMock.createMock(LocalDiscovery.class); - a.localDiscovery.shutDown(); - EasyMock.expectLastCall(); - EasyMock.replay(a.localDiscovery); - a.stop(bc); - - EasyMock.verify(a.localDiscovery); } } http://git-wip-us.apache.org/repos/asf/aries-rsa/blob/bb50834e/discovery/local/src/test/java/org/apache/aries/rsa/discovery/local/LocalDiscoveryTest.java ---------------------------------------------------------------------- diff --git a/discovery/local/src/test/java/org/apache/aries/rsa/discovery/local/LocalDiscoveryTest.java b/discovery/local/src/test/java/org/apache/aries/rsa/discovery/local/LocalDiscoveryTest.java index c33c23f..f503236 100644 --- a/discovery/local/src/test/java/org/apache/aries/rsa/discovery/local/LocalDiscoveryTest.java +++ b/discovery/local/src/test/java/org/apache/aries/rsa/discovery/local/LocalDiscoveryTest.java @@ -18,6 +18,9 @@ */ package org.apache.aries.rsa.discovery.local; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertSame; + import java.net.URL; import java.util.ArrayList; import java.util.Arrays; @@ -29,79 +32,19 @@ import java.util.List; import java.util.Map; import java.util.Set; -import junit.framework.TestCase; - -import org.apache.aries.rsa.discovery.local.LocalDiscovery; import org.easymock.EasyMock; import org.easymock.IAnswer; +import org.junit.Test; import org.osgi.framework.Bundle; -import org.osgi.framework.BundleContext; import org.osgi.framework.BundleEvent; -import org.osgi.framework.BundleListener; -import org.osgi.framework.Filter; -import org.osgi.framework.FrameworkUtil; -import org.osgi.framework.InvalidSyntaxException; -import org.osgi.framework.ServiceListener; import org.osgi.framework.ServiceReference; import org.osgi.service.remoteserviceadmin.EndpointDescription; import org.osgi.service.remoteserviceadmin.EndpointListener; -public class LocalDiscoveryTest extends TestCase { - - public void testLocalDiscovery() throws Exception { - Filter filter = EasyMock.createMock(Filter.class); - EasyMock.replay(filter); - - BundleContext bc = EasyMock.createMock(BundleContext.class); - EasyMock.expect(bc.createFilter("(objectClass=org.osgi.service.remoteserviceadmin.EndpointListener)")) - .andReturn(filter); - bc.addServiceListener((ServiceListener) EasyMock.anyObject(), - EasyMock.eq("(objectClass=org.osgi.service.remoteserviceadmin.EndpointListener)")); - EasyMock.expectLastCall(); - EasyMock.expect(bc.getServiceReferences("org.osgi.service.remoteserviceadmin.EndpointListener", null)) - .andReturn(null); - bc.addBundleListener((BundleListener) EasyMock.anyObject()); - EasyMock.expectLastCall().andAnswer(new IAnswer<Object>() { - public Object answer() throws Throwable { - assertEquals(LocalDiscovery.class, EasyMock.getCurrentArguments()[0].getClass()); - return null; - } - }); - EasyMock.expect(bc.getBundles()).andReturn(null); - EasyMock.replay(bc); - - LocalDiscovery ld = new LocalDiscovery(bc); - assertSame(bc, ld.bundleContext); - assertNotNull(ld.listenerTracker); - - EasyMock.verify(bc); - - EasyMock.reset(bc); - bc.removeBundleListener(ld); - EasyMock.expectLastCall(); - bc.removeServiceListener((ServiceListener) EasyMock.anyObject()); - EasyMock.expectLastCall(); - EasyMock.replay(bc); - - ld.shutDown(); - EasyMock.verify(bc); - } +public class LocalDiscoveryTest { + @Test public void testPreExistingBundles() throws Exception { - Filter filter = EasyMock.createMock(Filter.class); - EasyMock.replay(filter); - - BundleContext bc = EasyMock.createMock(BundleContext.class); - EasyMock.expect(bc.createFilter("(objectClass=org.osgi.service.remoteserviceadmin.EndpointListener)")) - .andReturn(filter); - bc.addServiceListener((ServiceListener) EasyMock.anyObject(), - EasyMock.eq("(objectClass=org.osgi.service.remoteserviceadmin.EndpointListener)")); - EasyMock.expectLastCall(); - EasyMock.expect(bc.getServiceReferences("org.osgi.service.remoteserviceadmin.EndpointListener", null)) - .andReturn(null); - bc.addBundleListener((BundleListener) EasyMock.anyObject()); - EasyMock.expectLastCall(); - Bundle b1 = EasyMock.createMock(Bundle.class); EasyMock.expect(b1.getState()).andReturn(Bundle.RESOLVED); EasyMock.replay(b1); @@ -118,10 +61,10 @@ public class LocalDiscoveryTest extends TestCase { .andReturn(Collections.enumeration(urls)); EasyMock.replay(b2); - EasyMock.expect(bc.getBundles()).andReturn(new Bundle[] {b1, b2}); - EasyMock.replay(bc); + Bundle[] bundles = new Bundle[] {b1, b2}; - LocalDiscovery ld = new LocalDiscovery(bc); + LocalDiscovery ld = new LocalDiscovery(); + ld.processExistingBundles(bundles); assertEquals(3, ld.endpointDescriptions.size()); Set<String> expected = new HashSet<String>( @@ -134,8 +77,9 @@ public class LocalDiscoveryTest extends TestCase { assertEquals(expected, actual); } + @Test public void testBundleChanged() throws Exception { - LocalDiscovery ld = getLocalDiscovery(); + LocalDiscovery ld = new LocalDiscovery(); Bundle bundle = EasyMock.createMock(Bundle.class); EasyMock.expect(bundle.getSymbolicName()).andReturn("testing.bundle").anyTimes(); @@ -194,21 +138,13 @@ public class LocalDiscoveryTest extends TestCase { EasyMock.verify(endpointListener); } + @Test public void testEndpointListenerService() throws Exception { - LocalDiscovery ld = getLocalDiscovery(); - - Bundle bundle = EasyMock.createMock(Bundle.class); - EasyMock.expect(bundle.getState()).andReturn(Bundle.ACTIVE); - Dictionary<String, String> headers = new Hashtable<String, String>(); - headers.put("Remote-Service", "OSGI-INF/rsa/ed4.xml"); - EasyMock.expect(bundle.getHeaders()).andReturn(headers); - EasyMock.expect(bundle.findEntries("OSGI-INF/rsa", "ed4.xml", false)) - .andReturn(Collections.enumeration( - Collections.singleton(getClass().getResource("/ed4.xml")))); - EasyMock.replay(bundle); + LocalDiscovery ld = new LocalDiscovery(); - BundleEvent be = new BundleEvent(BundleEvent.STARTED, bundle); - ld.bundleChanged(be); + Bundle bundle = createBundle(); + BundleEvent event = new BundleEvent(BundleEvent.STARTED, bundle); + ld.bundleChanged(event); assertEquals(2, ld.endpointDescriptions.size()); final Map<String, Object> props = new Hashtable<String, Object>(); @@ -224,17 +160,7 @@ public class LocalDiscoveryTest extends TestCase { EasyMock.replay(sr); - EasyMock.reset(ld.bundleContext); EndpointListener el = EasyMock.createMock(EndpointListener.class); - EasyMock.expect(ld.bundleContext.getService(sr)).andReturn(el); - EasyMock.expect(ld.bundleContext.ungetService(sr)).andReturn(true); - EasyMock.expect(ld.bundleContext.createFilter((String) EasyMock.anyObject())).andAnswer(new IAnswer<Filter>() { - public Filter answer() throws Throwable { - return FrameworkUtil.createFilter((String) EasyMock.getCurrentArguments()[0]); - } - }).anyTimes(); - EasyMock.replay(ld.bundleContext); - el.endpointAdded((EndpointDescription) EasyMock.anyObject(), EasyMock.eq("(objectClass=org.example.ClassA)")); EasyMock.expectLastCall(); @@ -243,7 +169,7 @@ public class LocalDiscoveryTest extends TestCase { // Add the EndpointListener Service assertEquals("Precondition failed", 0, ld.listenerToFilters.size()); assertEquals("Precondition failed", 0, ld.filterToListeners.size()); - assertSame(el, ld.listenerTracker.addingService(sr)); + ld.addListener(sr, el); assertEquals(1, ld.listenerToFilters.size()); assertEquals(Collections.singletonList("(objectClass=org.example.ClassA)"), ld.listenerToFilters.get(el)); @@ -270,7 +196,8 @@ public class LocalDiscoveryTest extends TestCase { }).times(2); EasyMock.replay(el); - ld.listenerTracker.modifiedService(sr, el); + ld.removeListener(el); + ld.addListener(sr, el); assertEquals(1, ld.listenerToFilters.size()); assertEquals(Arrays.asList("(|(objectClass=org.example.ClassA)(objectClass=org.example.ClassB))"), ld.listenerToFilters.get(el)); @@ -283,13 +210,27 @@ public class LocalDiscoveryTest extends TestCase { assertEquals(expectedEndpoints, actualEndpoints); // Remove the EndpointListener Service - ld.listenerTracker.removedService(sr, el); + ld.removeListener(el); assertEquals(0, ld.listenerToFilters.size()); assertEquals(0, ld.filterToListeners.size()); } + private Bundle createBundle() { + Bundle bundle = EasyMock.createMock(Bundle.class); + EasyMock.expect(bundle.getState()).andReturn(Bundle.ACTIVE); + Dictionary<String, String> headers = new Hashtable<String, String>(); + headers.put("Remote-Service", "OSGI-INF/rsa/ed4.xml"); + EasyMock.expect(bundle.getHeaders()).andReturn(headers); + EasyMock.expect(bundle.findEntries("OSGI-INF/rsa", "ed4.xml", false)) + .andReturn(Collections.enumeration( + Collections.singleton(getClass().getResource("/ed4.xml")))); + EasyMock.replay(bundle); + return bundle; + } + + @Test public void testRegisterTracker() throws Exception { - LocalDiscovery ld = getLocalDiscovery(); + LocalDiscovery ld = new LocalDiscovery(); final Map<String, Object> props = new Hashtable<String, Object>(); props.put(EndpointListener.ENDPOINT_LISTENER_SCOPE, "(objectClass=Aaaa)"); @@ -366,8 +307,9 @@ public class LocalDiscoveryTest extends TestCase { assertEquals(Collections.singletonList(endpointListener3), ld.filterToListeners.get("(objectClass=Y)")); } + @Test public void testClearTracker() throws Exception { - LocalDiscovery ld = getLocalDiscovery(); + LocalDiscovery ld = new LocalDiscovery(); EndpointListener endpointListener = EasyMock.createMock(EndpointListener.class); ld.listenerToFilters.put(endpointListener, @@ -387,24 +329,4 @@ public class LocalDiscoveryTest extends TestCase { assertEquals(0, ld.listenerToFilters.size()); assertEquals(0, ld.filterToListeners.size()); } - - private LocalDiscovery getLocalDiscovery() throws InvalidSyntaxException { - BundleContext bc = EasyMock.createMock(BundleContext.class); - EasyMock.expect(bc.createFilter((String) EasyMock.anyObject())).andAnswer(new IAnswer<Filter>() { - public Filter answer() throws Throwable { - return FrameworkUtil.createFilter((String) EasyMock.getCurrentArguments()[0]); - } - }).anyTimes(); - bc.addServiceListener((ServiceListener) EasyMock.anyObject(), - EasyMock.eq("(objectClass=org.osgi.service.remoteserviceadmin.EndpointListener)")); - EasyMock.expectLastCall(); - EasyMock.expect(bc.getServiceReferences("org.osgi.service.remoteserviceadmin.EndpointListener", null)) - .andReturn(null); - bc.addBundleListener((BundleListener) EasyMock.anyObject()); - EasyMock.expectLastCall(); - EasyMock.expect(bc.getBundles()).andReturn(null); - EasyMock.replay(bc); - - return new LocalDiscovery(bc); - } } http://git-wip-us.apache.org/repos/asf/aries-rsa/blob/bb50834e/discovery/zookeeper/src/main/java/org/apache/aries/rsa/discovery/zookeeper/publish/PublishingEndpointListener.java ---------------------------------------------------------------------- diff --git a/discovery/zookeeper/src/main/java/org/apache/aries/rsa/discovery/zookeeper/publish/PublishingEndpointListener.java b/discovery/zookeeper/src/main/java/org/apache/aries/rsa/discovery/zookeeper/publish/PublishingEndpointListener.java index 75efbd3..cda76bc 100644 --- a/discovery/zookeeper/src/main/java/org/apache/aries/rsa/discovery/zookeeper/publish/PublishingEndpointListener.java +++ b/discovery/zookeeper/src/main/java/org/apache/aries/rsa/discovery/zookeeper/publish/PublishingEndpointListener.java @@ -70,8 +70,6 @@ public class PublishingEndpointListener implements EndpointListener { } public void endpointAdded(EndpointDescription endpoint, String matchedFilter) { - LOG.info("Local EndpointDescription added: {}", endpoint); - synchronized (endpoints) { if (closed) { return; @@ -105,7 +103,7 @@ public class PublishingEndpointListener implements EndpointListener { } } } - + LOG.info("Exporting endpoint to zookeeper: {}", endpoint); for (String name : interfaces) { String path = Utils.getZooKeeperPath(name); String fullPath = path + '/' + endpointKey;
