tuscany-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From rf...@apache.org
Subject svn commit: r1095242 - in /tuscany/sca-java-2.x/trunk/modules: host-http/src/main/java/org/apache/tuscany/sca/host/http/extensibility/ host-http/src/main/java/org/apache/tuscany/sca/host/http/extensibility/impl/ host-http/src/main/resources/META-INF/se...
Date Wed, 20 Apr 2011 00:19:14 GMT
Author: rfeng
Date: Wed Apr 20 00:19:13 2011
New Revision: 1095242

URL: http://svn.apache.org/viewvc?rev=1095242&view=rev
Log:
Simplify the HttpPortAllocator and allows the default implementation to
allocate the port based on the system property/environment var (HTTP_PORT/
HTTPS_PORT) or a free port if the value is 0.

Removed:
    tuscany/sca-java-2.x/trunk/modules/host-http/src/main/java/org/apache/tuscany/sca/host/http/extensibility/DefaultHttpPortAllocatorExtensionPoint.java
    tuscany/sca-java-2.x/trunk/modules/host-http/src/main/java/org/apache/tuscany/sca/host/http/extensibility/ExtensibleHttpPortAllocator.java
    tuscany/sca-java-2.x/trunk/modules/host-http/src/main/java/org/apache/tuscany/sca/host/http/extensibility/HttpPortAllocatorExtensionPoint.java
    tuscany/sca-java-2.x/trunk/modules/host-http/src/main/resources/META-INF/services/org.apache.tuscany.sca.host.http.extensibility.HttpPortAllocatorExtensionPoint
Modified:
    tuscany/sca-java-2.x/trunk/modules/host-http/src/main/java/org/apache/tuscany/sca/host/http/extensibility/impl/DefaultHttpPortAllocatorImpl.java
    tuscany/sca-java-2.x/trunk/modules/host-http/src/main/resources/META-INF/services/org.apache.tuscany.sca.host.http.extensibility.HttpPortAllocator
    tuscany/sca-java-2.x/trunk/modules/host-jetty/src/main/java/org/apache/tuscany/sca/http/jetty/JettyServer.java
    tuscany/sca-java-2.x/trunk/modules/host-jetty/src/test/java/org/apache/tuscany/sca/http/jetty/JettyServerTestCase.java

Modified: tuscany/sca-java-2.x/trunk/modules/host-http/src/main/java/org/apache/tuscany/sca/host/http/extensibility/impl/DefaultHttpPortAllocatorImpl.java
URL: http://svn.apache.org/viewvc/tuscany/sca-java-2.x/trunk/modules/host-http/src/main/java/org/apache/tuscany/sca/host/http/extensibility/impl/DefaultHttpPortAllocatorImpl.java?rev=1095242&r1=1095241&r2=1095242&view=diff
==============================================================================
--- tuscany/sca-java-2.x/trunk/modules/host-http/src/main/java/org/apache/tuscany/sca/host/http/extensibility/impl/DefaultHttpPortAllocatorImpl.java
(original)
+++ tuscany/sca-java-2.x/trunk/modules/host-http/src/main/java/org/apache/tuscany/sca/host/http/extensibility/impl/DefaultHttpPortAllocatorImpl.java
Wed Apr 20 00:19:13 2011
@@ -19,6 +19,11 @@
 
 package org.apache.tuscany.sca.host.http.extensibility.impl;
 
+import java.io.IOException;
+import java.net.ServerSocket;
+import java.security.AccessController;
+import java.security.PrivilegedAction;
+
 import org.apache.tuscany.sca.host.http.HttpScheme;
 import org.apache.tuscany.sca.host.http.extensibility.HttpPortAllocator;
 
@@ -27,15 +32,68 @@ public class DefaultHttpPortAllocatorImp
     public int getDefaultPort(HttpScheme scheme) {
         int port = 0;
 
-        if(scheme == HttpScheme.HTTP) {
-            port = 8080;
-        } else if(scheme == HttpScheme.HTTPS) {
-           port = 8443;
-        } else {
-            throw new IllegalArgumentException("Scheme not support : " + scheme.toString());
+        if (scheme == null || scheme == HttpScheme.HTTP) {
+            try {
+                port = Integer.parseInt(getVariable("HTTP_PORT", "8080"));
+                if (port == 0) {
+                    port = findFreePort(8080, 9080);
+                }
+            } catch (NumberFormatException e) {
+                port = 8080;
+            }
+        } else if (scheme == HttpScheme.HTTPS) {
+            try {
+                port = Integer.parseInt(getVariable("HTTPS_PORT", "8443"));
+                if (port == 0) {
+                    port = findFreePort(8443, 9443);
+                }
+            } catch (NumberFormatException e) {
+                port = 8443;
+            }
         }
 
         return port;
+
+    }
+
+    private static String getVariable(final String variableName, final String defaultValue)
{
+        return AccessController.doPrivileged(new PrivilegedAction<String>() {
+            public String run() {
+                String value = System.getProperty(variableName);
+                if (value == null || value.length() == 0) {
+                    value = System.getenv(variableName);
+                    if (value == null || value.length() == 0) {
+                        value = defaultValue;
+                    }
+                }
+                return value;
+            }
+        });
+    }
+
+    private int findFreePort(final int start, final int end) {
+        return AccessController.doPrivileged(new PrivilegedAction<Integer>() {
+            public Integer run() {
+                for (int p = start; p <= end; p++) {
+                    ServerSocket socket = null;
+                    try {
+                        socket = new ServerSocket(p);
+                        return p;
+                    } catch (IOException e) {
+                        // Ignore
+                    } finally {
+                        if (socket != null) {
+                            try {
+                                socket.close();
+                            } catch (IOException e) {
+                                // Ignore
+                            }
+                        }
+                    }
+                }
+                return -1;
+            }
+        });
     }
 
 }

Modified: tuscany/sca-java-2.x/trunk/modules/host-http/src/main/resources/META-INF/services/org.apache.tuscany.sca.host.http.extensibility.HttpPortAllocator
URL: http://svn.apache.org/viewvc/tuscany/sca-java-2.x/trunk/modules/host-http/src/main/resources/META-INF/services/org.apache.tuscany.sca.host.http.extensibility.HttpPortAllocator?rev=1095242&r1=1095241&r2=1095242&view=diff
==============================================================================
--- tuscany/sca-java-2.x/trunk/modules/host-http/src/main/resources/META-INF/services/org.apache.tuscany.sca.host.http.extensibility.HttpPortAllocator
(original)
+++ tuscany/sca-java-2.x/trunk/modules/host-http/src/main/resources/META-INF/services/org.apache.tuscany.sca.host.http.extensibility.HttpPortAllocator
Wed Apr 20 00:19:13 2011
@@ -15,5 +15,5 @@
 # specific language governing permissions and limitations
 # under the License.
 
-org.apache.tuscany.sca.host.http.extensibility.impl.DefaultHttpPortAllocatorImpl,ranking=900
+org.apache.tuscany.sca.host.http.extensibility.impl.DefaultHttpPortAllocatorImpl
 

Modified: tuscany/sca-java-2.x/trunk/modules/host-jetty/src/main/java/org/apache/tuscany/sca/http/jetty/JettyServer.java
URL: http://svn.apache.org/viewvc/tuscany/sca-java-2.x/trunk/modules/host-jetty/src/main/java/org/apache/tuscany/sca/http/jetty/JettyServer.java?rev=1095242&r1=1095241&r2=1095242&view=diff
==============================================================================
--- tuscany/sca-java-2.x/trunk/modules/host-jetty/src/main/java/org/apache/tuscany/sca/http/jetty/JettyServer.java
(original)
+++ tuscany/sca-java-2.x/trunk/modules/host-jetty/src/main/java/org/apache/tuscany/sca/http/jetty/JettyServer.java
Wed Apr 20 00:19:13 2011
@@ -49,7 +49,6 @@ import org.apache.tuscany.sca.host.http.
 import org.apache.tuscany.sca.host.http.SecurityContext;
 import org.apache.tuscany.sca.host.http.ServletHost;
 import org.apache.tuscany.sca.host.http.ServletMappingException;
-import org.apache.tuscany.sca.host.http.extensibility.ExtensibleHttpPortAllocator;
 import org.apache.tuscany.sca.host.http.extensibility.HttpPortAllocator;
 import org.apache.tuscany.sca.work.WorkScheduler;
 import org.mortbay.jetty.Connector;
@@ -120,19 +119,25 @@ public class JettyServer implements Serv
     private org.mortbay.log.Logger jettyLogger;
 
     public JettyServer(ExtensionPointRegistry registry) {
-        this(registry.getExtensionPoint(UtilityExtensionPoint.class).getUtility(WorkScheduler.class),
ExtensibleHttpPortAllocator.getInstance(registry));
+        UtilityExtensionPoint utilityExtensionPoint = registry.getExtensionPoint(UtilityExtensionPoint.class);
+        this.workScheduler = utilityExtensionPoint.getUtility(WorkScheduler.class);
+        this.httpPortAllocator = utilityExtensionPoint.getUtility(HttpPortAllocator.class);
+        init();
     }
 
     protected JettyServer(WorkScheduler workScheduler, HttpPortAllocator httpPortAllocator)
{
         this.httpPortAllocator = httpPortAllocator;
         this.workScheduler = workScheduler;
+        init();
+    }
 
-        this.defaultPort = httpPortAllocator.getDefaultPort(HttpScheme.HTTP);
+    private void init() {
+        this.defaultPort = this.httpPortAllocator.getDefaultPort(HttpScheme.HTTP);
         //handle backdoor to set specific default port in tests
         if(portDefault > 0) {
             this.defaultPort = portDefault;
         }
-        this.defaultSSLPort = httpPortAllocator.getDefaultPort(HttpScheme.HTTPS);
+        this.defaultSSLPort = this.httpPortAllocator.getDefaultPort(HttpScheme.HTTPS);
         AccessController.doPrivileged(new PrivilegedAction<Object>() {
             public Object run() {
                 trustStore = System.getProperty("javax.net.ssl.trustStore");

Modified: tuscany/sca-java-2.x/trunk/modules/host-jetty/src/test/java/org/apache/tuscany/sca/http/jetty/JettyServerTestCase.java
URL: http://svn.apache.org/viewvc/tuscany/sca-java-2.x/trunk/modules/host-jetty/src/test/java/org/apache/tuscany/sca/http/jetty/JettyServerTestCase.java?rev=1095242&r1=1095241&r2=1095242&view=diff
==============================================================================
--- tuscany/sca-java-2.x/trunk/modules/host-jetty/src/test/java/org/apache/tuscany/sca/http/jetty/JettyServerTestCase.java
(original)
+++ tuscany/sca-java-2.x/trunk/modules/host-jetty/src/test/java/org/apache/tuscany/sca/http/jetty/JettyServerTestCase.java
Wed Apr 20 00:19:13 2011
@@ -25,6 +25,7 @@ import java.io.InputStreamReader;
 import java.io.OutputStream;
 import java.net.ConnectException;
 import java.net.InetAddress;
+import java.net.ServerSocket;
 import java.net.Socket;
 import java.net.URL;
 import java.util.concurrent.ExecutorService;
@@ -42,8 +43,8 @@ import javax.servlet.http.HttpServletRes
 import junit.framework.TestCase;
 
 import org.apache.tuscany.sca.core.DefaultExtensionPointRegistry;
+import org.apache.tuscany.sca.core.UtilityExtensionPoint;
 import org.apache.tuscany.sca.host.http.DefaultResourceServlet;
-import org.apache.tuscany.sca.host.http.extensibility.ExtensibleHttpPortAllocator;
 import org.apache.tuscany.sca.host.http.extensibility.HttpPortAllocator;
 import org.apache.tuscany.sca.work.NotificationListener;
 import org.apache.tuscany.sca.work.WorkScheduler;
@@ -54,23 +55,23 @@ import org.junit.Assert;
  */
 public class JettyServerTestCase extends TestCase {
 
-    private static final String REQUEST1_HEADER =
-        "GET / HTTP/1.0\n" + "Host: localhost\n"
-            + "Content-Type: text/xml\n"
-            + "Connection: close\n"
-            + "Content-Length: ";
+    private static final String REQUEST1_HEADER = "GET / HTTP/1.0\n" + "Host: localhost\n"
+        + "Content-Type: text/xml\n"
+        + "Connection: close\n"
+        + "Content-Length: ";
     private static final String REQUEST1_CONTENT = "";
-    private static final String REQUEST1 =
-        REQUEST1_HEADER + REQUEST1_CONTENT.getBytes().length + "\n\n" + REQUEST1_CONTENT;
-
-    private static final String REQUEST2_HEADER =
-        "GET /webcontent/test.html HTTP/1.0\n" + "Host: localhost\n"
-            + "Content-Type: text/xml\n"
-            + "Connection: close\n"
-            + "Content-Length: ";
+    private static final String REQUEST1 = REQUEST1_HEADER + REQUEST1_CONTENT.getBytes().length
+        + "\n\n"
+        + REQUEST1_CONTENT;
+
+    private static final String REQUEST2_HEADER = "GET /webcontent/test.html HTTP/1.0\n"
+ "Host: localhost\n"
+        + "Content-Type: text/xml\n"
+        + "Connection: close\n"
+        + "Content-Length: ";
     private static final String REQUEST2_CONTENT = "";
-    private static final String REQUEST2 =
-        REQUEST2_HEADER + REQUEST2_CONTENT.getBytes().length + "\n\n" + REQUEST2_CONTENT;
+    private static final String REQUEST2 = REQUEST2_HEADER + REQUEST2_CONTENT.getBytes().length
+        + "\n\n"
+        + REQUEST2_CONTENT;
 
     private static final int HTTP_PORT = 8085;
 
@@ -90,7 +91,8 @@ public class JettyServerTestCase extends
         }
     };
 
-    private HttpPortAllocator httpPortAllocator = new ExtensibleHttpPortAllocator(new DefaultExtensionPointRegistry());
+    private HttpPortAllocator httpPortAllocator = new DefaultExtensionPointRegistry()
+        .getExtensionPoint(UtilityExtensionPoint.class).getUtility(HttpPortAllocator.class);
 
     /**
      * Verifies requests are properly routed according to the Servlet mapping
@@ -113,7 +115,7 @@ public class JettyServerTestCase extends
      * Verifies requests are properly routed according to the Servlet mapping
      */
     public void testDeployedURI() throws Exception {
-        JettyServer service = new JettyServer(workScheduler,httpPortAllocator);
+        JettyServer service = new JettyServer(workScheduler, httpPortAllocator);
         service.setDefaultPort(8085);
         service.start();
         TestServlet servlet = new TestServlet();
@@ -139,7 +141,7 @@ public class JettyServerTestCase extends
         System.setProperty("javax.net.ssl.keyStore", "target/test-classes/tuscany.keyStore");
         System.setProperty("javax.net.ssl.keyStorePassword", "apache");
         System.setProperty("jetty.ssl.password", "apache");
-        JettyServer service = new JettyServer(workScheduler,httpPortAllocator);
+        JettyServer service = new JettyServer(workScheduler, httpPortAllocator);
         service.start();
         TestServlet servlet = new TestServlet();
         try {
@@ -152,12 +154,12 @@ public class JettyServerTestCase extends
         System.setProperty("javax.net.ssl.trustStore", "target/test-classes/tuscany.keyStore");
         System.setProperty("javax.net.ssl.trustStorePassword", "apache");
         URL url = new URL("https://127.0.0.1:8085/foo");
-        HttpsURLConnection conn = (HttpsURLConnection) url.openConnection();
+        HttpsURLConnection conn = (HttpsURLConnection)url.openConnection();
         conn.setHostnameVerifier(new HostnameVerifier() {
             public boolean verify(String hostname, SSLSession session) {
                 return true;
-            }}
-        );
+            }
+        });
 
         conn.connect();
         read(conn.getInputStream());
@@ -171,7 +173,7 @@ public class JettyServerTestCase extends
      * Verifies that Servlets can be registered with multiple ports
      */
     public void testRegisterMultiplePorts() throws Exception {
-        JettyServer service = new JettyServer(workScheduler,httpPortAllocator);
+        JettyServer service = new JettyServer(workScheduler, httpPortAllocator);
         service.start();
         TestServlet servlet = new TestServlet();
         service.addServletMapping("http://127.0.0.1:" + HTTP_PORT + "/", servlet);
@@ -198,7 +200,7 @@ public class JettyServerTestCase extends
     }
 
     public void testUnregisterMapping() throws Exception {
-        JettyServer service = new JettyServer(workScheduler,httpPortAllocator);
+        JettyServer service = new JettyServer(workScheduler, httpPortAllocator);
         service.start();
         TestServlet servlet = new TestServlet();
         String uri = "http://127.0.0.1:" + HTTP_PORT + "/foo";
@@ -218,7 +220,7 @@ public class JettyServerTestCase extends
     }
 
     public void testRequestSession() throws Exception {
-        JettyServer service = new JettyServer(workScheduler,httpPortAllocator);
+        JettyServer service = new JettyServer(workScheduler, httpPortAllocator);
         service.start();
         TestServlet servlet = new TestServlet();
         service.addServletMapping("http://127.0.0.1:" + HTTP_PORT + "/", servlet);
@@ -233,14 +235,14 @@ public class JettyServerTestCase extends
     }
 
     public void testRestart() throws Exception {
-        JettyServer service = new JettyServer(workScheduler,httpPortAllocator);
+        JettyServer service = new JettyServer(workScheduler, httpPortAllocator);
         service.start();
         service.stop();
         service.stop();
     }
 
     public void testNoMappings() throws Exception {
-        JettyServer service = new JettyServer(workScheduler,httpPortAllocator);
+        JettyServer service = new JettyServer(workScheduler, httpPortAllocator);
         service.start();
         Exception ex = null;
         try {
@@ -253,7 +255,7 @@ public class JettyServerTestCase extends
     }
 
     public void testResourceServlet() throws Exception {
-        JettyServer service = new JettyServer(workScheduler,httpPortAllocator);
+        JettyServer service = new JettyServer(workScheduler, httpPortAllocator);
         service.start();
 
         String documentRoot = getClass().getClassLoader().getResource("content/test.html").toString();
@@ -274,7 +276,7 @@ public class JettyServerTestCase extends
     }
 
     public void testDefaultServlet() throws Exception {
-        JettyServer service = new JettyServer(workScheduler,httpPortAllocator);
+        JettyServer service = new JettyServer(workScheduler, httpPortAllocator);
         service.start();
 
         String documentRoot = getClass().getClassLoader().getResource("content/test.html").toString();
@@ -293,6 +295,33 @@ public class JettyServerTestCase extends
         service.stop();
     }
 
+    public void testDefaultPort() throws IOException {
+        try {
+            // Open 9085
+            System.setProperty("HTTP_PORT", "9085");
+            JettyServer service = new JettyServer(workScheduler, httpPortAllocator);
+            assertEquals(9085, service.getDefaultPort());
+
+            // Try to find a free port
+            System.setProperty("HTTP_PORT", "0");
+            service = new JettyServer(workScheduler, httpPortAllocator);
+            int port = service.getDefaultPort();
+            assertNotSame(0, port);
+
+            // Try to find the next free port
+            ServerSocket socket = null;
+            try {
+                socket = new ServerSocket(port);
+                service = new JettyServer(workScheduler, httpPortAllocator);
+                assertNotSame(port, service.getDefaultPort());
+            } finally {
+                socket.close();
+            }
+        } finally {
+            System.clearProperty("HTTP_PORT");
+        }
+    }
+
     private static String read(Socket socket) throws IOException {
         InputStream is = socket.getInputStream();
         return read(is);



Mime
View raw message