diff options
Diffstat (limited to 'sca-java-2.x/trunk/modules/host-jetty')
2 files changed, 67 insertions, 33 deletions
diff --git a/sca-java-2.x/trunk/modules/host-jetty/src/main/java/org/apache/tuscany/sca/http/jetty/JettyServer.java b/sca-java-2.x/trunk/modules/host-jetty/src/main/java/org/apache/tuscany/sca/http/jetty/JettyServer.java index a7bfcfea2a..f62f24adc8 100644 --- a/sca-java-2.x/trunk/modules/host-jetty/src/main/java/org/apache/tuscany/sca/http/jetty/JettyServer.java +++ b/sca-java-2.x/trunk/modules/host-jetty/src/main/java/org/apache/tuscany/sca/http/jetty/JettyServer.java @@ -49,7 +49,6 @@ import org.apache.tuscany.sca.host.http.HttpScheme; 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 ServletHost, LifeCycleListener { 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"); diff --git a/sca-java-2.x/trunk/modules/host-jetty/src/test/java/org/apache/tuscany/sca/http/jetty/JettyServerTestCase.java b/sca-java-2.x/trunk/modules/host-jetty/src/test/java/org/apache/tuscany/sca/http/jetty/JettyServerTestCase.java index ee1de9b624..80666db8e3 100644 --- a/sca-java-2.x/trunk/modules/host-jetty/src/test/java/org/apache/tuscany/sca/http/jetty/JettyServerTestCase.java +++ b/sca-java-2.x/trunk/modules/host-jetty/src/test/java/org/apache/tuscany/sca/http/jetty/JettyServerTestCase.java @@ -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.HttpServletResponse; 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 TestCase { } }; - 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 TestCase { * 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 TestCase { 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 TestCase { 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 TestCase { * 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 TestCase { } 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 TestCase { } 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 TestCase { } 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 TestCase { } 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 TestCase { } 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 TestCase { 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); |