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 2010/06/25 16:32:51 UTC

svn commit: r957989 - in /cxf/dosgi/trunk/dsw/cxf-dsw/src: main/java/org/apache/cxf/dosgi/dsw/handlers/ test/java/org/apache/cxf/dosgi/dsw/handlers/

Author: davidb
Date: Fri Jun 25 14:32:51 2010
New Revision: 957989

URL: http://svn.apache.org/viewvc?rev=957989&view=rev
Log:
Fix for DOSGI-62. Patch applied on behalf of Julien Vey.

Modified:
    cxf/dosgi/trunk/dsw/cxf-dsw/src/main/java/org/apache/cxf/dosgi/dsw/handlers/AbstractConfigurationHandler.java
    cxf/dosgi/trunk/dsw/cxf-dsw/src/main/java/org/apache/cxf/dosgi/dsw/handlers/HttpServiceConfigurationTypeHandler.java
    cxf/dosgi/trunk/dsw/cxf-dsw/src/test/java/org/apache/cxf/dosgi/dsw/handlers/HttpServiceConfigurationTypeHandlerTest.java
    cxf/dosgi/trunk/dsw/cxf-dsw/src/test/java/org/apache/cxf/dosgi/dsw/handlers/PojoConfigurationTypeHandlerTest.java

Modified: cxf/dosgi/trunk/dsw/cxf-dsw/src/main/java/org/apache/cxf/dosgi/dsw/handlers/AbstractConfigurationHandler.java
URL: http://svn.apache.org/viewvc/cxf/dosgi/trunk/dsw/cxf-dsw/src/main/java/org/apache/cxf/dosgi/dsw/handlers/AbstractConfigurationHandler.java?rev=957989&r1=957988&r2=957989&view=diff
==============================================================================
--- cxf/dosgi/trunk/dsw/cxf-dsw/src/main/java/org/apache/cxf/dosgi/dsw/handlers/AbstractConfigurationHandler.java (original)
+++ cxf/dosgi/trunk/dsw/cxf-dsw/src/main/java/org/apache/cxf/dosgi/dsw/handlers/AbstractConfigurationHandler.java Fri Jun 25 14:32:51 2010
@@ -19,6 +19,12 @@
 package org.apache.cxf.dosgi.dsw.handlers;
 
 import java.lang.reflect.Proxy;
+import java.net.InetAddress;
+import java.net.NetworkInterface;
+import java.net.SocketException;
+import java.net.UnknownHostException;
+import java.util.ArrayList;
+import java.util.Enumeration;
 import java.util.HashMap;
 import java.util.Map;
 import java.util.Set;
@@ -48,8 +54,16 @@ public abstract class AbstractConfigurat
     
     protected String getDefaultAddress(Class<?> type, String port) {
         Object h = handlerProps.get(Constants.DEFAULT_HOST_CONFIG);
-        if (h == null) {
-            h = "localhost";
+        if (h == null || h.toString().equals("localhost")) {
+            try {
+                h = getLocalHost().getHostAddress();
+            } catch (Exception e) {
+                h = "localhost";
+            }
+            if (h == null) {
+                h = "localhost";
+            }
+
         }
         String host = h.toString();
 
@@ -153,4 +167,73 @@ public abstract class AbstractConfigurat
             props.put(RemoteConstants.ENDPOINT_ID, address);
         }
     }
+    
+    // Utility methods to get the local address even on a linux host
+
+    /**
+     * Returns an InetAddress representing the address of the localhost. Every attempt is made to find an address for
+     * this host that is not the loopback address. If no other address can be found, the loopback will be returned.
+     * 
+     * @return InetAddress - the address of localhost
+     * @throws UnknownHostException
+     *             - if there is a problem determing the address
+     */
+    public static InetAddress getLocalHost() throws UnknownHostException {
+        InetAddress localHost = InetAddress.getLocalHost();
+        if (!localHost.isLoopbackAddress())
+            return localHost;
+        InetAddress[] addrs = getAllLocalUsingNetworkInterface();
+        for (int i = 0; i < addrs.length; i++) {
+            if (!addrs[i].isLoopbackAddress() && !addrs[i].getHostAddress().contains(":"))
+                return addrs[i];
+        }
+        return localHost;
+    }
+
+    /**
+     * This method attempts to find all InetAddresses for this machine in a conventional way (via InetAddress). If only
+     * one address is found and it is the loopback, an attempt is made to determine the addresses for this machine using
+     * NetworkInterface.
+     * 
+     * @return InetAddress[] - all addresses assigned to the local machine
+     * @throws UnknownHostException
+     *             - if there is a problem determining addresses
+     */
+    public static InetAddress[] getAllLocal() throws UnknownHostException {
+        InetAddress[] iAddresses = InetAddress.getAllByName("127.0.0.1");
+        if (iAddresses.length != 1)
+            return iAddresses;
+        if (!iAddresses[0].isLoopbackAddress())
+            return iAddresses;
+        return getAllLocalUsingNetworkInterface();
+
+    }
+
+    /**
+     * Utility method that delegates to the methods of NetworkInterface to determine addresses for this machine.
+     * 
+     * @return InetAddress[] - all addresses found from the NetworkInterfaces
+     * @throws UnknownHostException
+     *             - if there is a problem determining addresses
+     */
+    private static InetAddress[] getAllLocalUsingNetworkInterface() throws UnknownHostException {
+        ArrayList addresses = new ArrayList();
+        Enumeration e = null;
+        try {
+            e = NetworkInterface.getNetworkInterfaces();
+        } catch (SocketException ex) {
+            throw new UnknownHostException("127.0.0.1");
+        }
+        while (e.hasMoreElements()) {
+            NetworkInterface ni = (NetworkInterface) e.nextElement();
+            for (Enumeration e2 = ni.getInetAddresses(); e2.hasMoreElements();) {
+                addresses.add(e2.nextElement());
+            }
+        }
+        InetAddress[] iAddresses = new InetAddress[addresses.size()];
+        for (int i = 0; i < iAddresses.length; i++) {
+            iAddresses[i] = (InetAddress) addresses.get(i);
+        }
+        return iAddresses;
+    }
 }

Modified: cxf/dosgi/trunk/dsw/cxf-dsw/src/main/java/org/apache/cxf/dosgi/dsw/handlers/HttpServiceConfigurationTypeHandler.java
URL: http://svn.apache.org/viewvc/cxf/dosgi/trunk/dsw/cxf-dsw/src/main/java/org/apache/cxf/dosgi/dsw/handlers/HttpServiceConfigurationTypeHandler.java?rev=957989&r1=957988&r2=957989&view=diff
==============================================================================
--- cxf/dosgi/trunk/dsw/cxf-dsw/src/main/java/org/apache/cxf/dosgi/dsw/handlers/HttpServiceConfigurationTypeHandler.java (original)
+++ cxf/dosgi/trunk/dsw/cxf-dsw/src/main/java/org/apache/cxf/dosgi/dsw/handlers/HttpServiceConfigurationTypeHandler.java Fri Jun 25 14:32:51 2010
@@ -169,7 +169,7 @@ public class HttpServiceConfigurationTyp
 
         String hostName = null;
         try {
-            hostName = InetAddress.getLocalHost().getHostName();
+            hostName = AbstractConfigurationHandler.getLocalHost().getHostAddress();
         } catch (UnknownHostException e) {
             hostName = "localhost";
         }

Modified: cxf/dosgi/trunk/dsw/cxf-dsw/src/test/java/org/apache/cxf/dosgi/dsw/handlers/HttpServiceConfigurationTypeHandlerTest.java
URL: http://svn.apache.org/viewvc/cxf/dosgi/trunk/dsw/cxf-dsw/src/test/java/org/apache/cxf/dosgi/dsw/handlers/HttpServiceConfigurationTypeHandlerTest.java?rev=957989&r1=957988&r2=957989&view=diff
==============================================================================
--- cxf/dosgi/trunk/dsw/cxf-dsw/src/test/java/org/apache/cxf/dosgi/dsw/handlers/HttpServiceConfigurationTypeHandlerTest.java (original)
+++ cxf/dosgi/trunk/dsw/cxf-dsw/src/test/java/org/apache/cxf/dosgi/dsw/handlers/HttpServiceConfigurationTypeHandlerTest.java Fri Jun 25 14:32:51 2010
@@ -106,7 +106,12 @@ public class HttpServiceConfigurationTyp
         //assertEquals(1, dp.getExposedServices().size());
         //assertSame(sr, dp.getExposedServices().iterator().next());
         
-        String hostName = InetAddress.getLocalHost().getHostName();
+        String hostName;
+        try {
+            hostName = AbstractConfigurationHandler.getLocalHost().getHostAddress();
+        } catch (Exception e) {
+            hostName = "localhost";
+        }
         
         Map edProps = exReg.getEndpointDescription().getProperties();
         
@@ -178,7 +183,13 @@ public class HttpServiceConfigurationTyp
         //assertEquals(1, dp.getExposedServices().size());
         //assertSame(sr, dp.getExposedServices().iterator().next());
         
-        String hostname = InetAddress.getLocalHost().getHostName();
+        String hostname;
+        try {
+            hostname = AbstractConfigurationHandler.getLocalHost().getHostAddress();
+        } catch (Exception e) {
+            hostname = "localhost";
+        }
+        
         Map edProps = exReg.getEndpointDescription().getProperties();
         
         assertNotNull(edProps.get(RemoteConstants.SERVICE_IMPORTED_CONFIGS));
@@ -247,7 +258,12 @@ public class HttpServiceConfigurationTyp
         //assertEquals(1, dp.getExposedServices().size());
         //assertSame(sr, dp.getExposedServices().iterator().next());
         
-        String hostName = InetAddress.getLocalHost().getHostName();
+        String hostName;
+        try {
+            hostName = AbstractConfigurationHandler.getLocalHost().getHostAddress();
+        } catch (Exception e) {
+            hostName = "localhost";
+        }
         Map edProps = exReg.getEndpointDescription().getProperties();
         
         assertNotNull(edProps.get(RemoteConstants.SERVICE_IMPORTED_CONFIGS));

Modified: cxf/dosgi/trunk/dsw/cxf-dsw/src/test/java/org/apache/cxf/dosgi/dsw/handlers/PojoConfigurationTypeHandlerTest.java
URL: http://svn.apache.org/viewvc/cxf/dosgi/trunk/dsw/cxf-dsw/src/test/java/org/apache/cxf/dosgi/dsw/handlers/PojoConfigurationTypeHandlerTest.java?rev=957989&r1=957988&r2=957989&view=diff
==============================================================================
--- cxf/dosgi/trunk/dsw/cxf-dsw/src/test/java/org/apache/cxf/dosgi/dsw/handlers/PojoConfigurationTypeHandlerTest.java (original)
+++ cxf/dosgi/trunk/dsw/cxf-dsw/src/test/java/org/apache/cxf/dosgi/dsw/handlers/PojoConfigurationTypeHandlerTest.java Fri Jun 25 14:32:51 2010
@@ -18,6 +18,7 @@
   */
 package org.apache.cxf.dosgi.dsw.handlers;
 
+import java.net.UnknownHostException;
 import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.HashMap;
@@ -83,7 +84,13 @@ public class PojoConfigurationTypeHandle
         Map<String, Object> hp = new HashMap<String, Object>();
         PojoConfigurationTypeHandler handler = new PojoConfigurationTypeHandler(null, hp);
         Map<String, Object> sd = new HashMap<String, Object>();
-        String url = "http://localhost:1234/java/lang/String";
+        String localIP;
+        try {
+            localIP = AbstractConfigurationHandler.getLocalHost().getHostAddress();
+        } catch (Exception e) {
+            localIP = "localhost";
+        }
+        String url = "http://"+localIP+":1234/java/lang/String";
         sd.put("org.apache.cxf.ws.port", "1234");
         assertEquals(url, handler.getPojoAddress(sd, String.class));        
     }
@@ -92,7 +99,13 @@ public class PojoConfigurationTypeHandle
         Map<String, Object> hp = new HashMap<String, Object>();
         PojoConfigurationTypeHandler handler = new PojoConfigurationTypeHandler(null, hp);
         Map<String, Object> sd = new HashMap<String, Object>(); 
-        assertEquals("http://localhost:9000/java/lang/String", handler.getPojoAddress(sd, String.class));
+        String localIP;
+        try {
+            localIP = AbstractConfigurationHandler.getLocalHost().getHostAddress();
+        } catch (Exception e) {
+            localIP = "localhost";
+        }
+        assertEquals("http://"+localIP+":9000/java/lang/String", handler.getPojoAddress(sd, String.class));
     }
 
     private Map<String, Object> handlerProps;