Author: lresende
Date: Tue Mar 29 18:25:52 2011
New Revision: 1086672

URL: http://svn.apache.org/viewvc?rev=1086672&view=rev
Log:
TUSCANY-3856 - Integrating HttpPortAllocator with embedded jetty server and 
fixing up test cases which need to provide a new parameter

Modified:
    tuscany/sca-java-2.x/trunk/modules/host-jetty/META-INF/MANIFEST.MF
    
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-jetty/META-INF/MANIFEST.MF
URL: 
http://svn.apache.org/viewvc/tuscany/sca-java-2.x/trunk/modules/host-jetty/META-INF/MANIFEST.MF?rev=1086672&r1=1086671&r2=1086672&view=diff
==============================================================================
--- tuscany/sca-java-2.x/trunk/modules/host-jetty/META-INF/MANIFEST.MF 
(original)
+++ tuscany/sca-java-2.x/trunk/modules/host-jetty/META-INF/MANIFEST.MF Tue Mar 
29 18:25:52 2011
@@ -10,6 +10,7 @@ Bundle-Description: Apache Tuscany SCA J
 Import-Package: javax.net.ssl;resolution:=optional,
  javax.servlet,
  javax.servlet.http;resolution:=optional,
+ org.apache.tuscany.sca.host.http.extensibility;version="2.0.0",
  org.apache.tuscany.sca.core;version="2.0.0",
  org.apache.tuscany.sca.host.http;version="2.0.0",
  org.apache.tuscany.sca.work;version="2.0.0",

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=1086672&r1=1086671&r2=1086672&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
 Tue Mar 29 18:25:52 2011
@@ -45,9 +45,12 @@ import org.apache.tuscany.sca.core.Exten
 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 Serv
     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 Serv
     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 Serv
     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 Serv
         if (trustStore != null) {
             connector.setNeedClientAuth(true);
         }
-
     }
 
     public String addServletMapping(String suri, Servlet servlet) throws 
ServletMappingException {
@@ -254,7 +258,7 @@ public class JettyServer implements Serv
                     //                    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 Serv
                 } 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 Serv
                 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 Serv
             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 Serv
         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 =

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=1086672&r1=1086671&r2=1086672&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
 Tue Mar 29 18:25:52 2011
@@ -41,7 +41,10 @@ import javax.servlet.http.HttpServletRes
 
 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
 
     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
         }
     };
 
+    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
         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
         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
 
         conn.connect();
         read(conn.getInputStream());
-        
+
         service.stop();
         assertTrue(servlet.invoked);
 
@@ -166,7 +171,7 @@ public class JettyServerTestCase extends
      * 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
     }
 
     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
     }
 
     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
     }
 
     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
     }
 
     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
     }
 
     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();


Reply via email to