FELIX-4681 - fix parsing of single-digit ports:

- when you specify a port of `0` (which essentially is the same as
  specifying `*`), this causes an exception;
- added unit tests to cover this additional case.



git-svn-id: https://svn.apache.org/repos/asf/felix/trunk@1634867 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/http/jetty/src/main/java/org/apache/felix/http/jetty/internal/JettyConfig.java b/http/jetty/src/main/java/org/apache/felix/http/jetty/internal/JettyConfig.java
index 568283b..106c828 100644
--- a/http/jetty/src/main/java/org/apache/felix/http/jetty/internal/JettyConfig.java
+++ b/http/jetty/src/main/java/org/apache/felix/http/jetty/internal/JettyConfig.java
@@ -457,22 +457,9 @@
 
         // asking for random port, so let ServerSocket handle it and return the answer
         portProp = portProp.trim();
-        if ("*".equals(portProp))
+        if ("*".equals(portProp) || "0".equals(portProp))
         {
-            ServerSocket ss = null;
-            try
-            {
-                ss = new ServerSocket(0);
-                return ss.getLocalPort();
-            }
-            catch (IOException e)
-            {
-                throw new RuntimeException(e.getMessage(), e);
-            }
-            finally
-            {
-                closeSilently(ss);
-            }
+            return getSocketPort(0);
         }
         else
         {
@@ -482,14 +469,15 @@
             // * start, end of interval defaults to 1, 65535, respectively, if missing.
             char startsWith = portProp.charAt(0);
             char endsWith = portProp.charAt(portProp.length() - 1);
-            String interval = portProp.substring(1, portProp.length() - 1);
 
             int minPort = 1;
             int maxPort = 65535;
 
-            int comma = interval.indexOf(',');
-            if (comma >= 0 && (startsWith == '[' || startsWith == '(') && (endsWith == ']' || endsWith == ')'))
+            if (portProp.contains(",") && (startsWith == '[' || startsWith == '(') && (endsWith == ']' || endsWith == ')'))
             {
+                String interval = portProp.substring(1, portProp.length() - 1);
+                int comma = interval.indexOf(',');
+
                 // check if the comma is first (start port in range is missing)
                 int start = (comma == 0) ? minPort : parseInt(interval.substring(0, comma), minPort);
                 // check if the comma is last (end port in range is missing)
@@ -507,20 +495,7 @@
                 int port = start - 1;
                 for (int i = start; port < start && i <= end; i++)
                 {
-                    ServerSocket ss = null;
-                    try
-                    {
-                        ss = new ServerSocket(i);
-                        port = ss.getLocalPort();
-                    }
-                    catch (IOException e)
-                    {
-                        SystemLogger.debug("Unable to bind to port: " + port + " | " + portProp);
-                    }
-                    finally
-                    {
-                        closeSilently(ss);
-                    }
+                    port = getSocketPort(i);
                 }
 
                 return (port < start) ? dflt : port;
@@ -532,6 +507,25 @@
             }
         }
     }
+    
+    private int getSocketPort(int i) {
+        int port = -1;
+        ServerSocket ss = null;
+        try
+        {
+            ss = new ServerSocket(i);
+            port = ss.getLocalPort();
+        }
+        catch (IOException e)
+        {
+            SystemLogger.debug("Unable to bind to port: " + i);
+        }
+        finally
+        {
+            closeSilently(ss);
+        }
+        return port;
+    }
 
     private Object getProperty(String name)
     {
diff --git a/http/jetty/src/test/java/org/apache/felix/http/jetty/internal/JettyConfigTest.java b/http/jetty/src/test/java/org/apache/felix/http/jetty/internal/JettyConfigTest.java
index 9d4e7cc..6b394b7 100644
--- a/http/jetty/src/test/java/org/apache/felix/http/jetty/internal/JettyConfigTest.java
+++ b/http/jetty/src/test/java/org/apache/felix/http/jetty/internal/JettyConfigTest.java
@@ -19,7 +19,6 @@
 import static org.easymock.EasyMock.createNiceMock;
 import static org.easymock.EasyMock.replay;
 
-import java.net.ServerSocket;
 import java.util.Hashtable;
 
 import junit.framework.TestCase;
@@ -76,6 +75,14 @@
         assertEquals(8443, this.config.getHttpsPort());
     }
 
+    public void testGetSpecificPortOne() throws Exception
+    {
+        Hashtable<String, Object> props = new Hashtable<String, Object>();
+        props.put("org.osgi.service.http.port", "1");
+        this.config.update(props);
+        assertTrue(this.config.getHttpPort() == 1);
+    }
+
     public void testGetRandomPort()
     {
         Hashtable<String, Object> props = new Hashtable<String, Object>();
@@ -86,11 +93,18 @@
         assertTrue(this.config.getHttpsPort() != 433);
     }
 
+    public void testGetRandomPortZero() throws Exception
+    {
+        Hashtable<String, Object> props = new Hashtable<String, Object>();
+        props.put("org.osgi.service.http.port", "0");
+        this.config.update(props);
+        assertTrue(this.config.getHttpPort() != 0);
+    }
+
     public void testGetSpecificPort() throws Exception
     {
-        ServerSocket ss = new ServerSocket(0);
-        int port = ss.getLocalPort();
-        ss.close();
+        int port = 80;
+
         Hashtable<String, Object> props = new Hashtable<String, Object>();
         props.put("org.osgi.service.http.port", port);
         props.put("org.osgi.service.http.port.secure", port);