Repository: aries-rsa
Updated Branches:
  refs/heads/master 5cda378e7 -> 4d8cdecb0


[ARIES-1778] Use endpoint id as path in zookeeper


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

Branch: refs/heads/master
Commit: 4d8cdecb08276db0e500bcbc04f4483563d02b37
Parents: 5cda378
Author: Christian Schneider <cschn...@adobe.com>
Authored: Mon Feb 12 11:50:08 2018 +0100
Committer: Christian Schneider <cschn...@adobe.com>
Committed: Mon Feb 12 11:50:08 2018 +0100

----------------------------------------------------------------------
 .../repository/ZookeeperEndpointRepository.java | 82 +++++++-------------
 .../publish/PublishingEndpointListenerTest.java |  2 +-
 .../ZookeeperEndpointRepositoryTest.java        |  8 +-
 .../itests/felix/tcp/TestDiscoveryExport.java   |  2 +-
 4 files changed, 32 insertions(+), 62 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/aries-rsa/blob/4d8cdecb/discovery/zookeeper/src/main/java/org/apache/aries/rsa/discovery/zookeeper/repository/ZookeeperEndpointRepository.java
----------------------------------------------------------------------
diff --git 
a/discovery/zookeeper/src/main/java/org/apache/aries/rsa/discovery/zookeeper/repository/ZookeeperEndpointRepository.java
 
b/discovery/zookeeper/src/main/java/org/apache/aries/rsa/discovery/zookeeper/repository/ZookeeperEndpointRepository.java
index 271f22b..057aff4 100644
--- 
a/discovery/zookeeper/src/main/java/org/apache/aries/rsa/discovery/zookeeper/repository/ZookeeperEndpointRepository.java
+++ 
b/discovery/zookeeper/src/main/java/org/apache/aries/rsa/discovery/zookeeper/repository/ZookeeperEndpointRepository.java
@@ -4,9 +4,6 @@ import java.io.ByteArrayInputStream;
 import java.io.ByteArrayOutputStream;
 import java.io.Closeable;
 import java.io.IOException;
-import java.net.URI;
-import java.net.URISyntaxException;
-import java.net.UnknownHostException;
 import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.Collection;
@@ -62,59 +59,33 @@ public class ZookeeperEndpointRepository implements 
Closeable, Watcher {
     }
 
     /**
-     * Retrieves data from the given node and parses it into an 
EndpointDescription.
-     *
-     * @param path a node path
-     * @return endpoint found in the node or null if no endpoint was found
+     * Read current endpoint stored at a znode
+     * 
+     * @param path
+     * @return
      */
     public EndpointDescription read(String path) {
         return nodes.get(path);
     }
 
-    public void add(EndpointDescription endpoint) throws URISyntaxException, 
KeeperException,
-    InterruptedException, IOException {
-        Collection<String> interfaces = endpoint.getInterfaces();
-        String endpointKey = getKey(endpoint);
-    
-        createEphemeralNode(ZookeeperEndpointRepository.getZooKeeperPath("") + 
endpointKey, getData(endpoint));
-        
-        LOG.info("Exporting endpoint to zookeeper: {}", endpoint);
-        for (String name : interfaces) {
-            String path = ZookeeperEndpointRepository.getZooKeeperPath(name);
-            String fullPath = path + '/' + endpointKey;
-            LOG.info("Creating ZooKeeper node for service with path {}", 
fullPath);
-            createPath(path);
-            createEphemeralNode(fullPath, getData(endpoint));
-        }
+    public void add(EndpointDescription endpoint) throws KeeperException, 
InterruptedException  {
+        String path = getZooKeeperPath(endpoint.getId());
+        LOG.info("Exporting endpoint to zookeeper. Endpoint: {}, Path: {}", 
endpoint, path);
+        createBasePath();
+        createEphemeralNode(path, getData(endpoint));
     }
 
-    public void modify(EndpointDescription endpoint) throws 
URISyntaxException, KeeperException, InterruptedException {
-        Collection<String> interfaces = endpoint.getInterfaces();
-        String endpointKey = getKey(endpoint);
-
-        LOG.info("Changing endpoint in zookeeper: {}", endpoint);
-        for (String name : interfaces) {
-            String path = ZookeeperEndpointRepository.getZooKeeperPath(name);
-            String fullPath = path + '/' + endpointKey;
-            LOG.info("Changing ZooKeeper node for service with path {}", 
fullPath);
-            createPath(path);
-            zk.setData(fullPath, getData(endpoint), -1);
-        }
+    public void modify(EndpointDescription endpoint) throws KeeperException, 
InterruptedException {
+        String path = getZooKeeperPath(endpoint.getId());
+        LOG.info("Changing endpoint in zookeeper. Endpoint: {}, Path: {}", 
endpoint, path);
+        createBasePath();
+        zk.setData(path, getData(endpoint), -1);
     }
     
-    public void remove(EndpointDescription endpoint) throws 
UnknownHostException, URISyntaxException {
-        Collection<String> interfaces = endpoint.getInterfaces();
-        String endpointKey = getKey(endpoint);
-        for (String name : interfaces) {
-            String path = ZookeeperEndpointRepository.getZooKeeperPath(name);
-            String fullPath = path + '/' + endpointKey;
-            LOG.debug("Removing ZooKeeper node: {}", fullPath);
-            try {
-                zk.delete(fullPath, -1);
-            } catch (Exception ex) {
-                LOG.debug("Error while removing endpoint: {}", ex); // e.g. 
session expired
-            }
-        }
+    public void remove(EndpointDescription endpoint) throws 
InterruptedException, KeeperException {
+        String path = getZooKeeperPath(endpoint.getId());
+        LOG.info("Removing endpoint in zookeeper. Endpoint: {}, Path: {}", 
endpoint, path);
+        zk.delete(path, -1);
     }
     
     public Collection<EndpointDescription> getAll() {
@@ -142,7 +113,8 @@ public class ZookeeperEndpointRepository implements 
Closeable, Watcher {
     }
 
     public static String getZooKeeperPath(String name) {
-        return name == null || name.isEmpty() ? PATH_PREFIX : PATH_PREFIX + 
'/' + name.replace('.', '/');
+        String escaped = name.replace('/', '#');
+        return name == null || name.isEmpty() ? PATH_PREFIX : PATH_PREFIX + 
'/' + escaped;
     }
 
     @Override
@@ -160,6 +132,11 @@ public class ZookeeperEndpointRepository implements 
Closeable, Watcher {
 
     }
 
+    private void createBasePath() throws KeeperException, InterruptedException 
{
+        String path = 
ZookeeperEndpointRepository.getZooKeeperPath(PATH_PREFIX);
+        createPath(path);
+    }
+
     private void registerWatcher() {
         try {
             watchRecursive(ZookeeperEndpointRepository.PATH_PREFIX);
@@ -228,12 +205,6 @@ public class ZookeeperEndpointRepository implements 
Closeable, Watcher {
         }
     }
 
-    private static String getKey(EndpointDescription endpoint) throws 
URISyntaxException {
-        URI uri = new URI(endpoint.getId());
-        return new 
StringBuilder().append(uri.getHost()).append("#").append(uri.getPort())
-            .append("#").append(uri.getPath().replace('/', '#')).toString();
-    }
-
     private void handleZNodeChanged(String path) {
         try {
             Stat stat = new Stat();
@@ -263,7 +234,8 @@ public class ZookeeperEndpointRepository implements 
Closeable, Watcher {
 
     private void handleChanged(String path, EndpointDescription endpoint) {
         EndpointDescription old = nodes.put(path, endpoint);
-        EndpointEvent event = new EndpointEvent(old == null ? 
EndpointEvent.ADDED : EndpointEvent.MODIFIED, endpoint);
+        int type = old == null ? EndpointEvent.ADDED : EndpointEvent.MODIFIED;
+        EndpointEvent event = new EndpointEvent(type, endpoint);
         if (listener != null) {
             listener.endpointChanged(event, null);
         }

http://git-wip-us.apache.org/repos/asf/aries-rsa/blob/4d8cdecb/discovery/zookeeper/src/test/java/org/apache/aries/rsa/discovery/zookeeper/publish/PublishingEndpointListenerTest.java
----------------------------------------------------------------------
diff --git 
a/discovery/zookeeper/src/test/java/org/apache/aries/rsa/discovery/zookeeper/publish/PublishingEndpointListenerTest.java
 
b/discovery/zookeeper/src/test/java/org/apache/aries/rsa/discovery/zookeeper/publish/PublishingEndpointListenerTest.java
index a61cf76..3fae691 100644
--- 
a/discovery/zookeeper/src/test/java/org/apache/aries/rsa/discovery/zookeeper/publish/PublishingEndpointListenerTest.java
+++ 
b/discovery/zookeeper/src/test/java/org/apache/aries/rsa/discovery/zookeeper/publish/PublishingEndpointListenerTest.java
@@ -39,7 +39,7 @@ import junit.framework.TestCase;
 
 public class PublishingEndpointListenerTest extends TestCase {
 
-    private static final String ENDPOINT_PATH = 
"/osgi/service_registry/myClass/google.de#80##test#sub";
+    private static final String ENDPOINT_PATH = 
"/osgi/service_registry/http:##google.de:80#test#sub";
 
     public void testEndpointRemovalAdding() throws KeeperException, 
InterruptedException {
         IMocksControl c = EasyMock.createNiceControl();

http://git-wip-us.apache.org/repos/asf/aries-rsa/blob/4d8cdecb/discovery/zookeeper/src/test/java/org/apache/aries/rsa/discovery/zookeeper/repository/ZookeeperEndpointRepositoryTest.java
----------------------------------------------------------------------
diff --git 
a/discovery/zookeeper/src/test/java/org/apache/aries/rsa/discovery/zookeeper/repository/ZookeeperEndpointRepositoryTest.java
 
b/discovery/zookeeper/src/test/java/org/apache/aries/rsa/discovery/zookeeper/repository/ZookeeperEndpointRepositoryTest.java
index d9f23e6..9bb6926 100644
--- 
a/discovery/zookeeper/src/test/java/org/apache/aries/rsa/discovery/zookeeper/repository/ZookeeperEndpointRepositoryTest.java
+++ 
b/discovery/zookeeper/src/test/java/org/apache/aries/rsa/discovery/zookeeper/repository/ZookeeperEndpointRepositoryTest.java
@@ -92,7 +92,7 @@ public class ZookeeperEndpointRepositoryTest {
         
         assertThat(sem.tryAcquire(1000, TimeUnit.SECONDS), equalTo(true));
 
-        String path = 
"/osgi/service_registry/java/lang/Runnable/test.de#-1##service1";
+        String path = "/osgi/service_registry/http:##test.de#service1";
         EndpointDescription ep2 = repository.read(path);
         assertNotNull(ep2);
 
@@ -107,11 +107,9 @@ public class ZookeeperEndpointRepositoryTest {
     
     @Test
     public void testGetZooKeeperPath() {
-        assertEquals(ZookeeperEndpointRepository.PATH_PREFIX + '/' + 
"org/example/Test",
-            ZookeeperEndpointRepository.getZooKeeperPath("org.example.Test"));
+        assertEquals(ZookeeperEndpointRepository.PATH_PREFIX + '/' + 
"http:##org.example.Test",
+            
ZookeeperEndpointRepository.getZooKeeperPath("http://org.example.Test";));
 
-        // used for the recursive discovery
-        assertEquals(ZookeeperEndpointRepository.PATH_PREFIX, 
ZookeeperEndpointRepository.getZooKeeperPath(null));
         assertEquals(ZookeeperEndpointRepository.PATH_PREFIX, 
ZookeeperEndpointRepository.getZooKeeperPath(""));
     }
 

http://git-wip-us.apache.org/repos/asf/aries-rsa/blob/4d8cdecb/itests/felix/src/test/java/org/apache/aries/rsa/itests/felix/tcp/TestDiscoveryExport.java
----------------------------------------------------------------------
diff --git 
a/itests/felix/src/test/java/org/apache/aries/rsa/itests/felix/tcp/TestDiscoveryExport.java
 
b/itests/felix/src/test/java/org/apache/aries/rsa/itests/felix/tcp/TestDiscoveryExport.java
index 218316e..0b6aadc 100644
--- 
a/itests/felix/src/test/java/org/apache/aries/rsa/itests/felix/tcp/TestDiscoveryExport.java
+++ 
b/itests/felix/src/test/java/org/apache/aries/rsa/itests/felix/tcp/TestDiscoveryExport.java
@@ -42,7 +42,7 @@ import 
org.osgi.service.remoteserviceadmin.EndpointDescription;
 
 @RunWith(PaxExam.class)
 public class TestDiscoveryExport extends RsaTestBase {
-    private static final String GREETER_ZOOKEEPER_NODE = 
"/osgi/service_registry/org/apache/aries/rsa/examples/echotcp/api/EchoService";
+    private static final String GREETER_ZOOKEEPER_NODE = 
"/osgi/service_registry";
 
     @Inject
     DistributionProvider tcpProvider;

Reply via email to