diff options
author | lresende <lresende@13f79535-47bb-0310-9956-ffa450edef68> | 2011-03-29 18:25:52 +0000 |
---|---|---|
committer | lresende <lresende@13f79535-47bb-0310-9956-ffa450edef68> | 2011-03-29 18:25:52 +0000 |
commit | 594e4a70db075f5516358d43eb88b6c97ee24a2a (patch) | |
tree | f75d0a9b477730a981f57794515e2bee7f947dbf /sca-java-2.x/trunk/modules/host-jetty/src | |
parent | faad66a153eb6e04068f821d89f0f671758568c3 (diff) |
TUSCANY-3856 - Integrating HttpPortAllocator with embedded jetty server and fixing up test cases which need to provide a new parameter
git-svn-id: http://svn.us.apache.org/repos/asf/tuscany@1086672 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'sca-java-2.x/trunk/modules/host-jetty/src')
2 files changed, 40 insertions, 31 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 aa53815b7f..c4108cf8df 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 @@ -45,9 +45,12 @@ import org.apache.tuscany.sca.core.ExtensionPointRegistry; import org.apache.tuscany.sca.core.LifeCycleListener; import org.apache.tuscany.sca.core.UtilityExtensionPoint; import org.apache.tuscany.sca.host.http.DefaultResourceServlet; +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; import org.mortbay.jetty.Server; @@ -82,11 +85,13 @@ public class JettyServer implements ServletHost, LifeCycleListener { private boolean sendServerVersion; private WorkScheduler workScheduler; + private HttpPortAllocator httpPortAllocator; + // TODO - this static seems to be set by the JSORPC binding unit test // doesn't look to be a great way of doing things - public static int portDefault = 8080; - private int defaultPort = portDefault; - private int defaultSSLPort = 8443; + //public static int portDefault = 8080; + private int defaultPort; + private int defaultSSLPort; /** * Represents a port and the server that serves it. @@ -115,12 +120,15 @@ public class JettyServer implements ServletHost, LifeCycleListener { private org.mortbay.log.Logger jettyLogger; public JettyServer(ExtensionPointRegistry registry) { - this(registry.getExtensionPoint(UtilityExtensionPoint.class).getUtility(WorkScheduler.class)); + this(registry.getExtensionPoint(UtilityExtensionPoint.class).getUtility(WorkScheduler.class), ExtensibleHttpPortAllocator.getInstance(registry)); } - protected JettyServer(WorkScheduler workScheduler) { - this.defaultPort = portDefault; + protected JettyServer(WorkScheduler workScheduler, HttpPortAllocator httpPortAllocator) { + this.httpPortAllocator = httpPortAllocator; this.workScheduler = workScheduler; + + this.defaultPort = httpPortAllocator.getDefaultPort(HttpScheme.HTTP); + this.defaultSSLPort = httpPortAllocator.getDefaultPort(HttpScheme.HTTPS); AccessController.doPrivileged(new PrivilegedAction<Object>() { public Object run() { trustStore = System.getProperty("javax.net.ssl.trustStore"); @@ -182,14 +190,11 @@ public class JettyServer implements ServletHost, LifeCycleListener { private void configureSSL(SslSocketConnector connector, SecurityContext securityContext) { connector.setProtocol("TLS"); if (securityContext != null) { - keyStoreType = - securityContext.getSSLProperties().getProperty("javax.net.ssl.keyStoreType", KeyStore.getDefaultType()); + keyStoreType = securityContext.getSSLProperties().getProperty("javax.net.ssl.keyStoreType", KeyStore.getDefaultType()); keyStore = securityContext.getSSLProperties().getProperty("javax.net.ssl.keyStore"); keyStorePassword = securityContext.getSSLProperties().getProperty("javax.net.ssl.keyStorePassword"); - trustStoreType = - securityContext.getSSLProperties().getProperty("javax.net.ssl.trustStoreType", - KeyStore.getDefaultType()); + trustStoreType = securityContext.getSSLProperties().getProperty("javax.net.ssl.trustStoreType", KeyStore.getDefaultType()); trustStore = securityContext.getSSLProperties().getProperty("javax.net.ssl.trustStore"); trustStorePassword = securityContext.getSSLProperties().getProperty("javax.net.ssl.trustStorePassword"); } @@ -205,7 +210,6 @@ public class JettyServer implements ServletHost, LifeCycleListener { if (trustStore != null) { connector.setNeedClientAuth(true); } - } public String addServletMapping(String suri, Servlet servlet) throws ServletMappingException { @@ -254,7 +258,7 @@ public class JettyServer implements ServletHost, LifeCycleListener { // httpConnector.setPort(portNumber); SslSocketConnector sslConnector = new SslSocketConnector(); sslConnector.setPort(portNumber); - // FIXME: [rfeng] We should set the host to be bound but binding-ws-axis2 is passing + // FIXME: [rfeng] We should set the host to be bound but binding-ws-axis2 is passing // in an absolute URI with host set to one of the ip addresses sslConnector.setHost(host); configureSSL(sslConnector, securityContext); @@ -262,7 +266,7 @@ public class JettyServer implements ServletHost, LifeCycleListener { } else { SelectChannelConnector selectConnector = new SelectChannelConnector(); selectConnector.setPort(portNumber); - // FIXME: [rfeng] We should set the host to be bound but binding-ws-axis2 is passing + // FIXME: [rfeng] We should set the host to be bound but binding-ws-axis2 is passing // in an absolute URI with host set to one of the ip addresses selectConnector.setHost(host); server.setConnectors(new Connector[] {selectConnector}); @@ -283,7 +287,7 @@ public class JettyServer implements ServletHost, LifeCycleListener { server.setSendServerVersion(sendServerVersion); server.start(); - // Keep track of the new server and Servlet handler + // Keep track of the new server and Servlet handler port = new Port(server, servletHandler); ports.put(portNumber, port); @@ -432,7 +436,7 @@ public class JettyServer implements ServletHost, LifeCycleListener { return null; } - // Remove the Servlet mapping for the given Servlet + // Remove the Servlet mapping for the given Servlet ServletHandler servletHandler = port.getServletHandler(); Servlet servlet = null; List<ServletMapping> mappings = @@ -474,13 +478,13 @@ public class JettyServer implements ServletHost, LifeCycleListener { if (port == null) { // TODO - EPR - SL commented out exception temporarily as the runtime is shared // between multiple nodes in a VM and shutting down one node blows - // up any other nodes when they shut down. + // up any other nodes when they shut down. //throw new IllegalStateException("No servlet registered at this URI: " + suri); logger.warning("No servlet registered at this URI: " + suri); return null; } - // Remove the Servlet mapping for the given Servlet + // Remove the Servlet mapping for the given Servlet ServletHandler servletHandler = port.getServletHandler(); Servlet removedServlet = null; List<ServletMapping> mappings = 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 e8c6bba1d4..ee1de9b624 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 @@ -41,7 +41,10 @@ import javax.servlet.http.HttpServletResponse; import junit.framework.TestCase; +import org.apache.tuscany.sca.core.DefaultExtensionPointRegistry; 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; import org.junit.Assert; @@ -73,7 +76,7 @@ public class JettyServerTestCase extends TestCase { private WorkScheduler workScheduler = new WorkScheduler() { private ExecutorService executorService = Executors.newCachedThreadPool(); - + public <T extends Runnable> void scheduleWork(T work) { executorService.submit(work); } @@ -87,11 +90,13 @@ public class JettyServerTestCase extends TestCase { } }; + private HttpPortAllocator httpPortAllocator = new ExtensibleHttpPortAllocator(new DefaultExtensionPointRegistry()); + /** * Verifies requests are properly routed according to the Servlet mapping */ public void testRegisterServletMapping() throws Exception { - JettyServer service = new JettyServer(workScheduler); + JettyServer service = new JettyServer(workScheduler, httpPortAllocator); service.start(); TestServlet servlet = new TestServlet(); service.addServletMapping("http://127.0.0.1:" + HTTP_PORT + "/", servlet); @@ -103,12 +108,12 @@ public class JettyServerTestCase extends TestCase { service.stop(); assertTrue(servlet.invoked); } - + /** * Verifies requests are properly routed according to the Servlet mapping */ public void testDeployedURI() throws Exception { - JettyServer service = new JettyServer(workScheduler); + JettyServer service = new JettyServer(workScheduler,httpPortAllocator); service.setDefaultPort(8085); service.start(); TestServlet servlet = new TestServlet(); @@ -134,7 +139,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); + JettyServer service = new JettyServer(workScheduler,httpPortAllocator); service.start(); TestServlet servlet = new TestServlet(); try { @@ -156,7 +161,7 @@ public class JettyServerTestCase extends TestCase { conn.connect(); read(conn.getInputStream()); - + service.stop(); assertTrue(servlet.invoked); @@ -166,7 +171,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); + JettyServer service = new JettyServer(workScheduler,httpPortAllocator); service.start(); TestServlet servlet = new TestServlet(); service.addServletMapping("http://127.0.0.1:" + HTTP_PORT + "/", servlet); @@ -193,7 +198,7 @@ public class JettyServerTestCase extends TestCase { } public void testUnregisterMapping() throws Exception { - JettyServer service = new JettyServer(workScheduler); + JettyServer service = new JettyServer(workScheduler,httpPortAllocator); service.start(); TestServlet servlet = new TestServlet(); String uri = "http://127.0.0.1:" + HTTP_PORT + "/foo"; @@ -213,7 +218,7 @@ public class JettyServerTestCase extends TestCase { } public void testRequestSession() throws Exception { - JettyServer service = new JettyServer(workScheduler); + JettyServer service = new JettyServer(workScheduler,httpPortAllocator); service.start(); TestServlet servlet = new TestServlet(); service.addServletMapping("http://127.0.0.1:" + HTTP_PORT + "/", servlet); @@ -228,14 +233,14 @@ public class JettyServerTestCase extends TestCase { } public void testRestart() throws Exception { - JettyServer service = new JettyServer(workScheduler); + JettyServer service = new JettyServer(workScheduler,httpPortAllocator); service.start(); service.stop(); service.stop(); } public void testNoMappings() throws Exception { - JettyServer service = new JettyServer(workScheduler); + JettyServer service = new JettyServer(workScheduler,httpPortAllocator); service.start(); Exception ex = null; try { @@ -248,7 +253,7 @@ public class JettyServerTestCase extends TestCase { } public void testResourceServlet() throws Exception { - JettyServer service = new JettyServer(workScheduler); + JettyServer service = new JettyServer(workScheduler,httpPortAllocator); service.start(); String documentRoot = getClass().getClassLoader().getResource("content/test.html").toString(); @@ -269,7 +274,7 @@ public class JettyServerTestCase extends TestCase { } public void testDefaultServlet() throws Exception { - JettyServer service = new JettyServer(workScheduler); + JettyServer service = new JettyServer(workScheduler,httpPortAllocator); service.start(); String documentRoot = getClass().getClassLoader().getResource("content/test.html").toString(); |