felix-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From j...@apache.org
Subject svn commit: r1634867 - in /felix/trunk/http/jetty/src: main/java/org/apache/felix/http/jetty/internal/JettyConfig.java test/java/org/apache/felix/http/jetty/internal/JettyConfigTest.java
Date Tue, 28 Oct 2014 13:03:39 GMT
Author: jawi
Date: Tue Oct 28 13:03:39 2014
New Revision: 1634867

URL: http://svn.apache.org/r1634867
Log:
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.


Modified:
    felix/trunk/http/jetty/src/main/java/org/apache/felix/http/jetty/internal/JettyConfig.java
    felix/trunk/http/jetty/src/test/java/org/apache/felix/http/jetty/internal/JettyConfigTest.java

Modified: felix/trunk/http/jetty/src/main/java/org/apache/felix/http/jetty/internal/JettyConfig.java
URL: http://svn.apache.org/viewvc/felix/trunk/http/jetty/src/main/java/org/apache/felix/http/jetty/internal/JettyConfig.java?rev=1634867&r1=1634866&r2=1634867&view=diff
==============================================================================
--- felix/trunk/http/jetty/src/main/java/org/apache/felix/http/jetty/internal/JettyConfig.java
(original)
+++ felix/trunk/http/jetty/src/main/java/org/apache/felix/http/jetty/internal/JettyConfig.java
Tue Oct 28 13:03:39 2014
@@ -457,22 +457,9 @@ public final class JettyConfig
 
         // 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 @@ public final class JettyConfig
             // * 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 @@ public final class JettyConfig
                 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 @@ public final class JettyConfig
             }
         }
     }
+    
+    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)
     {

Modified: felix/trunk/http/jetty/src/test/java/org/apache/felix/http/jetty/internal/JettyConfigTest.java
URL: http://svn.apache.org/viewvc/felix/trunk/http/jetty/src/test/java/org/apache/felix/http/jetty/internal/JettyConfigTest.java?rev=1634867&r1=1634866&r2=1634867&view=diff
==============================================================================
--- felix/trunk/http/jetty/src/test/java/org/apache/felix/http/jetty/internal/JettyConfigTest.java
(original)
+++ felix/trunk/http/jetty/src/test/java/org/apache/felix/http/jetty/internal/JettyConfigTest.java
Tue Oct 28 13:03:39 2014
@@ -19,7 +19,6 @@ package org.apache.felix.http.jetty.inte
 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 @@ public class JettyConfigTest extends Tes
         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 @@ public class JettyConfigTest extends Tes
         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);



Mime
View raw message