You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@aries.apache.org by cs...@apache.org on 2018/02/08 16:41:04 UTC

[4/9] aries-rsa git commit: [ARIES-1774] Remove PublishingEndpointListenerFactory

[ARIES-1774] Remove PublishingEndpointListenerFactory


Project: http://git-wip-us.apache.org/repos/asf/aries-rsa/repo
Commit: http://git-wip-us.apache.org/repos/asf/aries-rsa/commit/ed5adf8f
Tree: http://git-wip-us.apache.org/repos/asf/aries-rsa/tree/ed5adf8f
Diff: http://git-wip-us.apache.org/repos/asf/aries-rsa/diff/ed5adf8f

Branch: refs/heads/master
Commit: ed5adf8fc57ea2f10d0d73d97a47bdaa013b1b2f
Parents: 44943bc
Author: Christian Schneider <cs...@adobe.com>
Authored: Wed Feb 7 10:45:30 2018 +0100
Committer: Christian Schneider <cs...@adobe.com>
Committed: Thu Feb 8 17:40:34 2018 +0100

----------------------------------------------------------------------
 .../discovery/zookeeper/ZooKeeperDiscovery.java |  13 ++-
 .../publish/PublishingEndpointListener.java     |  42 +++++--
 .../PublishingEndpointListenerFactory.java      | 110 -------------------
 .../PublishingEndpointListenerFactoryTest.java  | 103 -----------------
 4 files changed, 41 insertions(+), 227 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/aries-rsa/blob/ed5adf8f/discovery/zookeeper/src/main/java/org/apache/aries/rsa/discovery/zookeeper/ZooKeeperDiscovery.java
----------------------------------------------------------------------
diff --git a/discovery/zookeeper/src/main/java/org/apache/aries/rsa/discovery/zookeeper/ZooKeeperDiscovery.java b/discovery/zookeeper/src/main/java/org/apache/aries/rsa/discovery/zookeeper/ZooKeeperDiscovery.java
index 13dadad..c8b020d 100644
--- a/discovery/zookeeper/src/main/java/org/apache/aries/rsa/discovery/zookeeper/ZooKeeperDiscovery.java
+++ b/discovery/zookeeper/src/main/java/org/apache/aries/rsa/discovery/zookeeper/ZooKeeperDiscovery.java
@@ -25,7 +25,7 @@ import java.util.Enumeration;
 import java.util.HashMap;
 import java.util.Map;
 
-import org.apache.aries.rsa.discovery.zookeeper.publish.PublishingEndpointListenerFactory;
+import org.apache.aries.rsa.discovery.zookeeper.publish.PublishingEndpointListener;
 import org.apache.aries.rsa.discovery.zookeeper.subscribe.EndpointListenerTracker;
 import org.apache.aries.rsa.discovery.zookeeper.subscribe.InterfaceMonitorManager;
 import org.apache.zookeeper.WatchedEvent;
@@ -46,7 +46,7 @@ public class ZooKeeperDiscovery implements Watcher, ManagedService {
 
     private final BundleContext bctx;
 
-    private PublishingEndpointListenerFactory endpointListenerFactory;
+    private PublishingEndpointListener endpointListener;
     private ServiceTracker<?, ?> endpointListenerTracker;
     private InterfaceMonitorManager imManager;
     private ZooKeeper zkClient;
@@ -92,8 +92,8 @@ public class ZooKeeperDiscovery implements Watcher, ManagedService {
             return;
         }
         LOG.debug("starting ZookeeperDiscovery");
-        endpointListenerFactory = new PublishingEndpointListenerFactory(zkClient, bctx);
-        endpointListenerFactory.start();
+        endpointListener = new PublishingEndpointListener(zkClient, bctx);
+        endpointListener.start(bctx);
         imManager = new InterfaceMonitorManager(bctx, zkClient);
         endpointListenerTracker = new EndpointListenerTracker(bctx, imManager);
         endpointListenerTracker.open();
@@ -106,8 +106,9 @@ public class ZooKeeperDiscovery implements Watcher, ManagedService {
         }
         started = false;
         closed |= close;
-        if (endpointListenerFactory != null) {
-            endpointListenerFactory.stop();
+        if (endpointListener != null) {
+            endpointListener.stop();
+            endpointListener.close();
         }
         if (endpointListenerTracker != null) {
             endpointListenerTracker.close();

http://git-wip-us.apache.org/repos/asf/aries-rsa/blob/ed5adf8f/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 d5fe7a6..6dc0a08 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
@@ -26,11 +26,14 @@ import java.net.UnknownHostException;
 import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.Collection;
+import java.util.Dictionary;
 import java.util.HashMap;
+import java.util.Hashtable;
 import java.util.List;
 import java.util.Map;
 
 import org.apache.aries.rsa.discovery.endpoint.EndpointDescriptionParser;
+import org.apache.aries.rsa.discovery.zookeeper.ZooKeeperDiscovery;
 import org.apache.aries.rsa.discovery.zookeeper.util.Utils;
 import org.apache.zookeeper.CreateMode;
 import org.apache.zookeeper.KeeperException;
@@ -38,12 +41,14 @@ import org.apache.zookeeper.KeeperException.NoNodeException;
 import org.apache.zookeeper.KeeperException.NodeExistsException;
 import org.apache.zookeeper.ZooDefs.Ids;
 import org.apache.zookeeper.ZooKeeper;
-import org.apache.zookeeper.data.Stat;
 import org.osgi.framework.BundleContext;
+import org.osgi.framework.Constants;
+import org.osgi.framework.ServiceRegistration;
 import org.osgi.service.remoteserviceadmin.EndpointDescription;
 import org.osgi.service.remoteserviceadmin.EndpointEvent;
 import org.osgi.service.remoteserviceadmin.EndpointEventListener;
 import org.osgi.service.remoteserviceadmin.EndpointListener;
+import org.osgi.service.remoteserviceadmin.RemoteConstants;
 import org.osgi.util.tracker.ServiceTracker;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
@@ -57,19 +62,38 @@ public class PublishingEndpointListener implements EndpointEventListener, Endpoi
     private static final Logger LOG = LoggerFactory.getLogger(PublishingEndpointListener.class);
 
     private final ZooKeeper zk;
-    private final ServiceTracker<DiscoveryPlugin, DiscoveryPlugin> discoveryPluginTracker;
     private final List<EndpointDescription> endpoints = new ArrayList<EndpointDescription>();
     private boolean closed;
-
     private final EndpointDescriptionParser endpointDescriptionParser;
 
+    private ServiceTracker<DiscoveryPlugin, DiscoveryPlugin> discoveryPluginTracker;
+    private ServiceRegistration<?> listenerReg;
+
     public PublishingEndpointListener(ZooKeeper zk, BundleContext bctx) {
         this.zk = zk;
-        discoveryPluginTracker = new ServiceTracker<DiscoveryPlugin, DiscoveryPlugin>(bctx, 
-            DiscoveryPlugin.class, null);
-        discoveryPluginTracker.open();
         endpointDescriptionParser = new EndpointDescriptionParser();
     }
+    
+    public void start(BundleContext bctx) {
+        discoveryPluginTracker = new ServiceTracker<DiscoveryPlugin, DiscoveryPlugin>(bctx, 
+                DiscoveryPlugin.class, null);
+            discoveryPluginTracker.open();
+        Dictionary<String, String> props = new Hashtable<String, String>();
+        String uuid = bctx.getProperty(Constants.FRAMEWORK_UUID);
+        props.put(EndpointEventListener.ENDPOINT_LISTENER_SCOPE, 
+                  String.format("(&(%s=*)(%s=%s))", Constants.OBJECTCLASS, 
+                                RemoteConstants.ENDPOINT_FRAMEWORK_UUID, uuid));
+        props.put(ZooKeeperDiscovery.DISCOVERY_ZOOKEEPER_ID, "true");
+        String[] ifAr = {EndpointEventListener.class.getName(), EndpointListener.class.getName()};
+        listenerReg = bctx.registerService(ifAr, this, props);
+    }
+    
+    public void stop() {
+        if (listenerReg != null) {
+            listenerReg.unregister();
+            listenerReg = null;
+        }
+    }
 
     @Override
     public void endpointChanged(EndpointEvent event, String filter) {
@@ -152,7 +176,7 @@ public class PublishingEndpointListener implements EndpointEventListener, Endpoi
         Map<String, Object> props = new HashMap<String, Object>(endpoint.getProperties());
 
         // process plugins
-        Object[] plugins = discoveryPluginTracker.getServices();
+        Object[] plugins = discoveryPluginTracker != null ? discoveryPluginTracker.getServices() : null;
         if (plugins != null) {
             for (Object plugin : plugins) {
                 if (plugin instanceof DiscoveryPlugin) {
@@ -259,6 +283,8 @@ public class PublishingEndpointListener implements EndpointEventListener, Endpoi
             }
             endpoints.clear();
         }
-        discoveryPluginTracker.close();
+        if (discoveryPluginTracker != null) {
+            discoveryPluginTracker.close();
+        }
     }
 }

http://git-wip-us.apache.org/repos/asf/aries-rsa/blob/ed5adf8f/discovery/zookeeper/src/main/java/org/apache/aries/rsa/discovery/zookeeper/publish/PublishingEndpointListenerFactory.java
----------------------------------------------------------------------
diff --git a/discovery/zookeeper/src/main/java/org/apache/aries/rsa/discovery/zookeeper/publish/PublishingEndpointListenerFactory.java b/discovery/zookeeper/src/main/java/org/apache/aries/rsa/discovery/zookeeper/publish/PublishingEndpointListenerFactory.java
deleted file mode 100644
index 444f7bb..0000000
--- a/discovery/zookeeper/src/main/java/org/apache/aries/rsa/discovery/zookeeper/publish/PublishingEndpointListenerFactory.java
+++ /dev/null
@@ -1,110 +0,0 @@
-/**
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements. See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership. The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied. See the License for the
- * specific language governing permissions and limitations
- * under the License.
- */
-package org.apache.aries.rsa.discovery.zookeeper.publish;
-
-import java.util.ArrayList;
-import java.util.Dictionary;
-import java.util.Hashtable;
-import java.util.List;
-
-import org.apache.aries.rsa.discovery.zookeeper.ZooKeeperDiscovery;
-import org.apache.zookeeper.ZooKeeper;
-import org.osgi.framework.Bundle;
-import org.osgi.framework.BundleContext;
-import org.osgi.framework.Constants;
-import org.osgi.framework.ServiceFactory;
-import org.osgi.framework.ServiceRegistration;
-import org.osgi.service.remoteserviceadmin.EndpointEventListener;
-import org.osgi.service.remoteserviceadmin.EndpointListener;
-import org.osgi.service.remoteserviceadmin.RemoteConstants;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-
-/**
- * Creates local EndpointListeners that publish to ZooKeeper.
- */
-@SuppressWarnings("deprecation")
-public class PublishingEndpointListenerFactory implements ServiceFactory<PublishingEndpointListener> {
-
-    private static final Logger LOG = LoggerFactory.getLogger(PublishingEndpointListenerFactory.class);
-
-    private final BundleContext bctx;
-    private final ZooKeeper zk;
-    private final List<PublishingEndpointListener> listeners = new ArrayList<PublishingEndpointListener>();
-    private ServiceRegistration<?> serviceRegistration;
-
-    public PublishingEndpointListenerFactory(ZooKeeper zk, BundleContext bctx) {
-        this.bctx = bctx;
-        this.zk = zk;
-    }
-
-    public PublishingEndpointListener getService(Bundle b, ServiceRegistration<PublishingEndpointListener> sr) {
-        LOG.debug("new EndpointListener from factory");
-        synchronized (listeners) {
-            PublishingEndpointListener pel = new PublishingEndpointListener(zk, bctx);
-            listeners.add(pel);
-            return pel;
-        }
-    }
-
-    public void ungetService(Bundle b, ServiceRegistration<PublishingEndpointListener> sr, 
-                             PublishingEndpointListener pel) {
-        LOG.debug("remove EndpointListener");
-        synchronized (listeners) {
-            if (listeners.remove(pel)) {
-                pel.close();
-            }
-        }
-    }
-
-    public synchronized void start() {
-        Dictionary<String, String> props = new Hashtable<String, String>();
-        String uuid = bctx.getProperty(Constants.FRAMEWORK_UUID);
-        props.put(EndpointEventListener.ENDPOINT_LISTENER_SCOPE, 
-                  String.format("(&(%s=*)(%s=%s))", Constants.OBJECTCLASS, 
-                                RemoteConstants.ENDPOINT_FRAMEWORK_UUID, uuid));
-        props.put(ZooKeeperDiscovery.DISCOVERY_ZOOKEEPER_ID, "true");
-        String[] ifAr = {EndpointEventListener.class.getName(), EndpointListener.class.getName()};
-        serviceRegistration = bctx.registerService(ifAr, this, props);
-    }
-    
-    public synchronized void stop() {
-        if (serviceRegistration != null) {
-            serviceRegistration.unregister();
-            serviceRegistration = null;
-        }
-        synchronized (listeners) {
-            for (PublishingEndpointListener pel : listeners) {
-                pel.close();
-            }
-            listeners.clear();
-        }
-    }
-
-    /**
-     * Only for the test case!
-     * 
-     * @return 
-     */
-    protected List<PublishingEndpointListener> getListeners() {
-        synchronized (listeners) {
-            return listeners;
-        }
-    }
-}

http://git-wip-us.apache.org/repos/asf/aries-rsa/blob/ed5adf8f/discovery/zookeeper/src/test/java/org/apache/aries/rsa/discovery/zookeeper/publish/PublishingEndpointListenerFactoryTest.java
----------------------------------------------------------------------
diff --git a/discovery/zookeeper/src/test/java/org/apache/aries/rsa/discovery/zookeeper/publish/PublishingEndpointListenerFactoryTest.java b/discovery/zookeeper/src/test/java/org/apache/aries/rsa/discovery/zookeeper/publish/PublishingEndpointListenerFactoryTest.java
deleted file mode 100644
index 381ec9d..0000000
--- a/discovery/zookeeper/src/test/java/org/apache/aries/rsa/discovery/zookeeper/publish/PublishingEndpointListenerFactoryTest.java
+++ /dev/null
@@ -1,103 +0,0 @@
-/**
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements. See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership. The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied. See the License for the
- * specific language governing permissions and limitations
- * under the License.
- */
-package org.apache.aries.rsa.discovery.zookeeper.publish;
-
-import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertTrue;
-
-import java.util.Dictionary;
-import java.util.List;
-
-import org.apache.zookeeper.ZooKeeper;
-import org.easymock.EasyMock;
-import org.easymock.IMocksControl;
-import org.junit.Before;
-import org.junit.Test;
-import org.osgi.framework.BundleContext;
-import org.osgi.framework.ServiceRegistration;
-import org.osgi.service.remoteserviceadmin.EndpointEventListener;
-import org.osgi.service.remoteserviceadmin.EndpointListener;
-
-@SuppressWarnings("deprecation")
-public class PublishingEndpointListenerFactoryTest {
-
-    private IMocksControl c;
-    private BundleContext ctx;
-    private ZooKeeper zk;
-
-    @Before
-    public void before() {
-        c = EasyMock.createNiceControl();
-        zk = c.createMock(ZooKeeper.class);
-        ctx = createBundleContext();
-    }
-    
-    @Test
-    public void testScope() {
-        PublishingEndpointListenerFactory eplf = new PublishingEndpointListenerFactory(zk, ctx);
-
-        c.replay();
-        eplf.start();
-        c.verify();
-
-    }
-
-    @Test
-    public void testServiceFactory() {
-        PublishingEndpointListenerFactory eplf = new PublishingEndpointListenerFactory(zk, ctx);
-
-        PublishingEndpointListener eli = c.createMock(PublishingEndpointListener.class);
-        eli.close();
-        EasyMock.expectLastCall().once();
-
-        c.replay();
-        eplf.start();
-
-        PublishingEndpointListener service = eplf.getService(null, null);
-        assertTrue(service instanceof EndpointEventListener);
-
-        List<PublishingEndpointListener> listeners = eplf.getListeners();
-        assertEquals(1, listeners.size());
-        assertEquals(service, listeners.get(0));
-
-        eplf.ungetService(null, null, service);
-        listeners = eplf.getListeners();
-        assertEquals(0, listeners.size());
-
-        eplf.ungetService(null, null, eli); // no call to close
-        listeners.add(eli);
-        eplf.ungetService(null, null, eli); // call to close
-        listeners = eplf.getListeners();
-        assertEquals(0, listeners.size());
-
-        c.verify();
-    }
-
-    @SuppressWarnings({ "rawtypes", "unchecked" })
-    private BundleContext createBundleContext() {
-        BundleContext ctx = c.createMock(BundleContext.class);
-        ServiceRegistration sreg = c.createMock(ServiceRegistration.class);
-        String[] ifAr = {EndpointEventListener.class.getName(), EndpointListener.class.getName()};
-        EasyMock.expect(ctx.registerService(EasyMock.aryEq(ifAr), EasyMock.anyObject(),
-                                            (Dictionary<String, String>)EasyMock.anyObject())).andReturn(sreg).once();
-    
-        EasyMock.expect(ctx.getProperty(EasyMock.eq("org.osgi.framework.uuid"))).andReturn("myUUID").anyTimes();
-        return ctx;
-    }
-}