FELIX-4061 : Implement service endpoint registration property  - add loopback interface if no network interface is available

git-svn-id: https://svn.apache.org/repos/asf/felix/trunk@1485242 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/http/jetty/src/main/java/org/apache/felix/http/jetty/internal/JettyService.java b/http/jetty/src/main/java/org/apache/felix/http/jetty/internal/JettyService.java
index eaa5752..f78fe2d 100644
--- a/http/jetty/src/main/java/org/apache/felix/http/jetty/internal/JettyService.java
+++ b/http/jetty/src/main/java/org/apache/felix/http/jetty/internal/JettyService.java
@@ -350,10 +350,14 @@
         while (this.running) {
             startJetty();
 
-            synchronized (this) {
-                try {
+            synchronized (this)
+            {
+                try
+                {
                     wait();
-                } catch (InterruptedException e) {
+                }
+                catch (InterruptedException e)
+                {
                     // we will definitely be interrupted
                 }
             }
@@ -362,6 +366,7 @@
         }
     }
 
+
     private String getEndpoint(final Connector listener, final InetAddress ia)
     {
         if (ia.isLoopbackAddress())
@@ -409,6 +414,25 @@
         return sb.toString();
     }
 
+    private List<String> getEndpoints(final Connector connector, final List<NetworkInterface> interfaces)
+    {
+        final List<String> endpoints = new ArrayList<String>();
+        for (final NetworkInterface ni : interfaces)
+        {
+            final Enumeration<InetAddress> ias = ni.getInetAddresses();
+            while (ias.hasMoreElements())
+            {
+                final InetAddress ia = ias.nextElement();
+                final String endpoint = this.getEndpoint(connector, ia);
+                if (endpoint != null)
+                {
+                    endpoints.add(endpoint);
+                }
+            }
+        }
+        return endpoints;
+    }
+
     private void addEndpointProperties(final Hashtable<String, Object> props, Object container)
     {
         final List<String> endpoints = new ArrayList<String>();
@@ -424,26 +448,31 @@
                 {
                     try
                     {
+                        final List<NetworkInterface> interfaces = new ArrayList<NetworkInterface>();
+                        final List<NetworkInterface> loopBackInterfaces = new ArrayList<NetworkInterface>();
                         final Enumeration<NetworkInterface> nis = NetworkInterface.getNetworkInterfaces();
                         while ( nis.hasMoreElements() )
                         {
                             final NetworkInterface ni = nis.nextElement();
                             if ( ni.isLoopback() )
                             {
-                                continue;
+                                loopBackInterfaces.add(ni);
                             }
-
-                            final Enumeration<InetAddress> ias = ni.getInetAddresses();
-                            while (ias.hasMoreElements())
+                            else
                             {
-                                final InetAddress ia = ias.nextElement();
-                                final String endpoint = this.getEndpoint(connector, ia);
-                                if ( endpoint != null )
-                                {
-                                    endpoints.add(endpoint);
-                                }
+                                interfaces.add(ni);
                             }
                         }
+
+                        // only add loop back endpoints to the endpoint property if no other endpoint is available.
+                        if (!interfaces.isEmpty())
+                        {
+                            endpoints.addAll(getEndpoints(connector, interfaces));
+                        }
+                        else
+                        {
+                            endpoints.addAll(getEndpoints(connector, loopBackInterfaces));
+                        }
                     }
                     catch (final SocketException se)
                     {