You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cxf.apache.org by da...@apache.org on 2009/08/10 15:40:30 UTC

svn commit: r802791 - in /cxf/dosgi/trunk/discovery/distributed/cxf-discovery/src: main/java/org/apache/cxf/dosgi/discovery/zookeeper/ test/java/org/apache/cxf/dosgi/discovery/zookeeper/

Author: davidb
Date: Mon Aug 10 13:40:29 2009
New Revision: 802791

URL: http://svn.apache.org/viewvc?rev=802791&view=rev
Log:
Fix for CXF-2385.
New unit tests included also.

Modified:
    cxf/dosgi/trunk/discovery/distributed/cxf-discovery/src/main/java/org/apache/cxf/dosgi/discovery/zookeeper/PublishToZooKeeperCustomizer.java
    cxf/dosgi/trunk/discovery/distributed/cxf-discovery/src/test/java/org/apache/cxf/dosgi/discovery/zookeeper/PublishToZooKeeperCustomizerTest.java

Modified: cxf/dosgi/trunk/discovery/distributed/cxf-discovery/src/main/java/org/apache/cxf/dosgi/discovery/zookeeper/PublishToZooKeeperCustomizer.java
URL: http://svn.apache.org/viewvc/cxf/dosgi/trunk/discovery/distributed/cxf-discovery/src/main/java/org/apache/cxf/dosgi/discovery/zookeeper/PublishToZooKeeperCustomizer.java?rev=802791&r1=802790&r2=802791&view=diff
==============================================================================
--- cxf/dosgi/trunk/discovery/distributed/cxf-discovery/src/main/java/org/apache/cxf/dosgi/discovery/zookeeper/PublishToZooKeeperCustomizer.java (original)
+++ cxf/dosgi/trunk/discovery/distributed/cxf-discovery/src/main/java/org/apache/cxf/dosgi/discovery/zookeeper/PublishToZooKeeperCustomizer.java Mon Aug 10 13:40:29 2009
@@ -29,6 +29,8 @@
 import java.util.Properties;
 import java.util.logging.Level;
 import java.util.logging.Logger;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
 
 import org.apache.zookeeper.CreateMode;
 import org.apache.zookeeper.KeeperException;
@@ -41,6 +43,7 @@
 
 public class PublishToZooKeeperCustomizer implements ServiceTrackerCustomizer {
     private static final Logger LOG = Logger.getLogger(PublishToZooKeeperCustomizer.class.getName());
+    private static final Pattern LOCALHOST_MATCH = Pattern.compile("(.*://)(localhost|127.0.0.1)(:.*)");
 
     private final BundleContext bundleContext;
     private final ZooKeeper zookeeper;
@@ -115,26 +118,33 @@
     @SuppressWarnings("unchecked")
     static byte[] getData(ServiceReference sr) throws IOException {
         Properties p = new Properties();
+        String host = InetAddress.getLocalHost().getHostAddress();
         
         Map<String, Object> serviceProps = (Map<String, Object>) sr.getProperty(ServicePublication.SERVICE_PROPERTIES);
         if (serviceProps != null) {
             for (Map.Entry<String, Object> prop : serviceProps.entrySet()) {
-                p.setProperty(prop.getKey(), prop.getValue().toString());
+                p.setProperty(prop.getKey(), 
+                    filterLocalHost(prop.getValue().toString(), host));
             }
         }
         
-        copyProperty(ServicePublication.ENDPOINT_ID, sr, p);
-        copyProperty(ServicePublication.ENDPOINT_LOCATION, sr, p);
+        copyProperty(ServicePublication.ENDPOINT_ID, sr, p, host);
+        copyProperty(ServicePublication.ENDPOINT_LOCATION, sr, p, host);
         
         ByteArrayOutputStream baos = new ByteArrayOutputStream();
         p.store(baos, "");
         return baos.toByteArray();
     }
 
-    private static void copyProperty(String key, ServiceReference sr, Properties p) {
+    static String filterLocalHost(String value, String replacement) {
+        Matcher m = LOCALHOST_MATCH.matcher(value);
+        return m.replaceAll("$1" + replacement + "$3");
+    }
+
+    private static void copyProperty(String key, ServiceReference sr, Properties p, String localhost) {
         Object eID = sr.getProperty(key);
         if (eID != null) {
-            p.setProperty(key, eID.toString());
+            p.setProperty(key, filterLocalHost(eID.toString(), localhost));
         }
     }
 

Modified: cxf/dosgi/trunk/discovery/distributed/cxf-discovery/src/test/java/org/apache/cxf/dosgi/discovery/zookeeper/PublishToZooKeeperCustomizerTest.java
URL: http://svn.apache.org/viewvc/cxf/dosgi/trunk/discovery/distributed/cxf-discovery/src/test/java/org/apache/cxf/dosgi/discovery/zookeeper/PublishToZooKeeperCustomizerTest.java?rev=802791&r1=802790&r2=802791&view=diff
==============================================================================
--- cxf/dosgi/trunk/discovery/distributed/cxf-discovery/src/test/java/org/apache/cxf/dosgi/discovery/zookeeper/PublishToZooKeeperCustomizerTest.java (original)
+++ cxf/dosgi/trunk/discovery/distributed/cxf-discovery/src/test/java/org/apache/cxf/dosgi/discovery/zookeeper/PublishToZooKeeperCustomizerTest.java Mon Aug 10 13:40:29 2009
@@ -196,13 +196,18 @@
         initial.put("osgi.remote.configuration.pojo.address", "http://localhost:9090/ps");
         
         String eid = UUID.randomUUID().toString();
+        String epLoc = "http://localhost:9090/ps";
         HashMap<String, Object> expected = new HashMap<String, Object>(initial);
         expected.put(ServicePublication.ENDPOINT_ID, eid);
+        expected.put(ServicePublication.ENDPOINT_LOCATION, 
+            "http://" + InetAddress.getLocalHost().getHostAddress() + ":9090/ps");
+        expected.put("osgi.remote.configuration.pojo.address", "http://" + 
+            InetAddress.getLocalHost().getHostAddress() + ":9090/ps");
                 
         ServiceReference sr = EasyMock.createMock(ServiceReference.class);
         EasyMock.expect(sr.getProperty(ServicePublication.SERVICE_PROPERTIES)).andReturn(initial);
         EasyMock.expect(sr.getProperty(ServicePublication.ENDPOINT_ID)).andReturn(eid);
-        EasyMock.expect(sr.getProperty(ServicePublication.ENDPOINT_LOCATION)).andReturn(null);
+        EasyMock.expect(sr.getProperty(ServicePublication.ENDPOINT_LOCATION)).andReturn(epLoc);
         EasyMock.replay(sr);
         
         byte[] data = PublishToZooKeeperCustomizer.getData(sr);
@@ -224,4 +229,17 @@
         assertEquals(hostAddr + "#8000##ps",
             PublishToZooKeeperCustomizer.getKey("http://localhost:8000/ps"));
     }
+    
+    public void testLocalHostTranslation() {
+        assertEquals("http://somehost:9090", 
+            PublishToZooKeeperCustomizer.filterLocalHost("http://localhost:9090", "somehost"));
+        assertEquals("http://somehost:9090/myPath", 
+            PublishToZooKeeperCustomizer.filterLocalHost("http://127.0.0.1:9090/myPath", "somehost"));
+
+        // a few negative tests too
+        assertEquals("http://localhostt:9090", 
+            PublishToZooKeeperCustomizer.filterLocalHost("http://localhostt:9090", "somehost"));
+        assertEquals("There is a localhost on the planet.", 
+            PublishToZooKeeperCustomizer.filterLocalHost("There is a localhost on the planet.", "somehost"));
+    }
 }