cxf-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From cschnei...@apache.org
Subject [1/2] cxf-dosgi git commit: Removing local ip guessing as it is too unreliable
Date Wed, 06 Jul 2016 07:12:15 GMT
Repository: cxf-dosgi
Updated Branches:
  refs/heads/master f0dea5061 -> 1fef30511


Removing local ip guessing as it is too unreliable


Project: http://git-wip-us.apache.org/repos/asf/cxf-dosgi/repo
Commit: http://git-wip-us.apache.org/repos/asf/cxf-dosgi/commit/30817c5c
Tree: http://git-wip-us.apache.org/repos/asf/cxf-dosgi/tree/30817c5c
Diff: http://git-wip-us.apache.org/repos/asf/cxf-dosgi/diff/30817c5c

Branch: refs/heads/master
Commit: 30817c5cbb75dc17d46e6377fc57c1772b151709
Parents: f0dea50
Author: Christian Schneider <chris@die-schneider.net>
Authored: Wed Jul 6 09:10:38 2016 +0200
Committer: Christian Schneider <chris@die-schneider.net>
Committed: Wed Jul 6 09:10:38 2016 +0200

----------------------------------------------------------------------
 .../common/httpservice/HttpServiceManager.java  | 41 +++++----
 .../dosgi/common/httpservice/LocalHostUtil.java | 92 --------------------
 .../httpservice/HttpServiceManagerTest.java     |  5 +-
 3 files changed, 22 insertions(+), 116 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cxf-dosgi/blob/30817c5c/common/src/main/java/org/apache/cxf/dosgi/common/httpservice/HttpServiceManager.java
----------------------------------------------------------------------
diff --git a/common/src/main/java/org/apache/cxf/dosgi/common/httpservice/HttpServiceManager.java
b/common/src/main/java/org/apache/cxf/dosgi/common/httpservice/HttpServiceManager.java
index e5015c9..e03b605 100644
--- a/common/src/main/java/org/apache/cxf/dosgi/common/httpservice/HttpServiceManager.java
+++ b/common/src/main/java/org/apache/cxf/dosgi/common/httpservice/HttpServiceManager.java
@@ -44,22 +44,21 @@ import org.osgi.service.http.HttpService;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
-@Component(
-           configurationPid = "cxf-dsw",
-           service = HttpServiceManager.class
-           )
+@Component //
+(//
+    name = "org.apache.cxf.dosgi.http", //
+    service = HttpServiceManager.class //
+)
 public class HttpServiceManager {
     /**
-     * Prefix to create an absolute URL from a relative URL.
-     * See HttpServiceManager.getAbsoluteAddress
-     *
-     * Defaults to: http://<host name>:8181
+     * Prefix to create an absolute URL from a relative URL. See HttpServiceManager.getAbsoluteAddress
+     * Defaults to: http://localhost:8181
      */
     public static final String KEY_HTTP_BASE = "httpBase";
     public static final String KEY_CXF_SERVLET_ALIAS = "cxfServletAlias";
     public static final String DEFAULT_CXF_SERVLET_ALIAS = "/cxf";
     private static final Logger LOG = LoggerFactory.getLogger(HttpServiceManager.class);
-    
+
     private Map<Long, String> exportedAliases = Collections.synchronizedMap(new HashMap<Long,
String>());
     private String httpBase;
     private String cxfServletAlias;
@@ -77,7 +76,7 @@ public class HttpServiceManager {
         if (config == null) {
             config = new Hashtable<String, Object>();
         }
-        this.httpBase = getWithDefault(config.get(KEY_HTTP_BASE), "http://" + LocalHostUtil.getLocalIp()
+ ":8181");
+        this.httpBase = getWithDefault(config.get(KEY_HTTP_BASE), "http://localhost:8181");
         this.cxfServletAlias = getWithDefault(config.get(KEY_CXF_SERVLET_ALIAS), "/cxf");
     }
 
@@ -92,8 +91,7 @@ public class HttpServiceManager {
         try {
             HttpContext httpContext1 = httpService.createDefaultHttpContext();
             HttpContext httpContext = new SecurityDelegatingHttpContext(callingContext, httpContext1);
-            httpService.registerServlet(contextRoot, cxf, new Hashtable<String, String>(),
-                                       httpContext);
+            httpService.registerServlet(contextRoot, cxf, new Hashtable<String, String>(),
httpContext);
 
             registerUnexportHook(sid, contextRoot);
 
@@ -105,8 +103,7 @@ public class HttpServiceManager {
     }
 
     /**
-     * This listens for service removal events and "un-exports" the service
-     * from the HttpService.
+     * This listens for service removal events and "un-exports" the service from the HttpService.
      *
      * @param sref the service reference to track
      * @param alias the HTTP servlet context alias
@@ -127,7 +124,8 @@ public class HttpServiceManager {
                 LOG.warn("Service listener could not be started. The service will not be
automatically unexported.");
             }
         } catch (InvalidSyntaxException e) {
-            LOG.warn("Service listener could not be started. The service will not be automatically
unexported.", e);
+            LOG.warn("Service listener could not be started. The service will not be automatically
unexported.",
+                     e);
         }
     }
 
@@ -150,27 +148,28 @@ public class HttpServiceManager {
                 return;
             }
             final ServiceReference<?> sref = event.getServiceReference();
-            final Long sid = (Long) sref.getProperty(org.osgi.framework.Constants.SERVICE_ID);
+            final Long sid = (Long)sref.getProperty(org.osgi.framework.Constants.SERVICE_ID);
             final String alias = exportedAliases.remove(sid);
             if (alias == null) {
                 LOG.error("Unable to unexport HTTP servlet for service class '{}',"
-                        + " service-id {}: no servlet alias found",
-                        sref.getProperty(org.osgi.framework.Constants.OBJECTCLASS), sid);
+                          + " service-id {}: no servlet alias found",
+                          sref.getProperty(org.osgi.framework.Constants.OBJECTCLASS), sid);
                 return;
             }
             LOG.debug("Unexporting HTTP servlet for alias '{}'", alias);
             try {
                 httpService.unregister(alias);
             } catch (Exception e) {
-                LOG.warn("An exception occurred while unregistering service for HTTP servlet
alias '{}'", alias, e);
+                LOG.warn("An exception occurred while unregistering service for HTTP servlet
alias '{}'",
+                         alias, e);
             }
         }
     }
-    
+
     public void setContext(BundleContext context) {
         this.context = context;
     }
-    
+
     @Reference
     public void setHttpService(HttpService httpService) {
         this.httpService = httpService;

http://git-wip-us.apache.org/repos/asf/cxf-dosgi/blob/30817c5c/common/src/main/java/org/apache/cxf/dosgi/common/httpservice/LocalHostUtil.java
----------------------------------------------------------------------
diff --git a/common/src/main/java/org/apache/cxf/dosgi/common/httpservice/LocalHostUtil.java
b/common/src/main/java/org/apache/cxf/dosgi/common/httpservice/LocalHostUtil.java
deleted file mode 100644
index 0ac245d..0000000
--- a/common/src/main/java/org/apache/cxf/dosgi/common/httpservice/LocalHostUtil.java
+++ /dev/null
@@ -1,92 +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.cxf.dosgi.common.httpservice;
-
-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.List;
-
-/**
- * Utility methods to get the local address even on a linux host.
- */
-final class LocalHostUtil {
-
-    private LocalHostUtil() {
-        // Util Class
-    }
-
-    /**
-     * 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 determining the address
-     */
-    public static InetAddress getLocalHost() throws UnknownHostException {
-        InetAddress localHost = InetAddress.getLocalHost();
-        if (!localHost.isLoopbackAddress()) {
-            return localHost;
-        }
-        InetAddress[] addrs = getAllLocalUsingNetworkInterface();
-        for (InetAddress addr : addrs) {
-            if (!addr.isLoopbackAddress() && !addr.getHostAddress().contains(":"))
{
-                return addr;
-            }
-        }
-        return localHost;
-    }
-
-    /**
-     * Utility method that delegates to the methods of NetworkInterface to
-     * determine addresses for this machine.
-     *
-     * @return all addresses found from the NetworkInterfaces
-     * @throws UnknownHostException if there is a problem determining addresses
-     */
-    private static InetAddress[] getAllLocalUsingNetworkInterface() throws UnknownHostException
{
-        try {
-            List<InetAddress> addresses = new ArrayList<InetAddress>();
-            Enumeration<NetworkInterface> e = NetworkInterface.getNetworkInterfaces();
-            while (e.hasMoreElements()) {
-                NetworkInterface ni = e.nextElement();
-                for (Enumeration<InetAddress> e2 = ni.getInetAddresses(); e2.hasMoreElements();)
{
-                    addresses.add(e2.nextElement());
-                }
-            }
-            return addresses.toArray(new InetAddress[] {});
-        } catch (SocketException ex) {
-            throw new UnknownHostException("127.0.0.1");
-        }
-    }
-
-    public static String getLocalIp() {
-        String localIP;
-        try {
-            localIP = getLocalHost().getHostAddress();
-        } catch (Exception e) {
-            localIP = "localhost";
-        }
-        return localIP;
-    }
-}

http://git-wip-us.apache.org/repos/asf/cxf-dosgi/blob/30817c5c/common/src/test/java/org/apache/cxf/dosgi/common/httpservice/HttpServiceManagerTest.java
----------------------------------------------------------------------
diff --git a/common/src/test/java/org/apache/cxf/dosgi/common/httpservice/HttpServiceManagerTest.java
b/common/src/test/java/org/apache/cxf/dosgi/common/httpservice/HttpServiceManagerTest.java
index c943f79..1f49f09 100644
--- a/common/src/test/java/org/apache/cxf/dosgi/common/httpservice/HttpServiceManagerTest.java
+++ b/common/src/test/java/org/apache/cxf/dosgi/common/httpservice/HttpServiceManagerTest.java
@@ -50,13 +50,12 @@ public class HttpServiceManagerTest extends TestCase {
     public void testGetAbsoluteAddress() {
         HttpServiceManager manager = new HttpServiceManager();
         manager.initFromConfig(null);
-        String localIp = LocalHostUtil.getLocalIp();
 
         String address1 = manager.getAbsoluteAddress(null, "/myservice");
-        assertEquals("http://" + localIp + ":8181/cxf/myservice", address1);
+        assertEquals("http://localhost:8181/cxf/myservice", address1);
 
         String address2 = manager.getAbsoluteAddress("/mycontext", "/myservice");
-        assertEquals("http://" + localIp + ":8181/mycontext/myservice", address2);
+        assertEquals("http://localhost:8181/mycontext/myservice", address2);
     }
 
     public void testRegisterAndUnregisterServlet() throws Exception {


Mime
View raw message