tuscany-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jsdelf...@apache.org
Subject svn commit: r561391 - in /incubator/tuscany/java/sca/modules: http-jetty/src/main/java/org/apache/tuscany/sca/http/jetty/ http-jetty/src/test/java/org/apache/tuscany/sca/http/jetty/ http-tomcat/src/main/java/org/apache/tuscany/sca/http/tomcat/ http-tom...
Date Tue, 31 Jul 2007 16:50:11 GMT
Author: jsdelfino
Date: Tue Jul 31 09:50:09 2007
New Revision: 561391

URL: http://svn.apache.org/viewvc?view=rev&rev=561391
Log:
Fixed Jetty and Tomcat embedded servers to use the specified port number when registering
servlets.

Modified:
    incubator/tuscany/java/sca/modules/http-jetty/src/main/java/org/apache/tuscany/sca/http/jetty/JettyServer.java
    incubator/tuscany/java/sca/modules/http-jetty/src/test/java/org/apache/tuscany/sca/http/jetty/JettyServerTestCase.java
    incubator/tuscany/java/sca/modules/http-tomcat/src/main/java/org/apache/tuscany/sca/http/tomcat/TomcatServer.java
    incubator/tuscany/java/sca/modules/http-tomcat/src/main/java/org/apache/tuscany/sca/http/tomcat/module/TomcatRuntimeModuleActivator.java
    incubator/tuscany/java/sca/modules/http-tomcat/src/test/java/org/apache/tuscany/sca/http/tomcat/TomcatServerTestCase.java

Modified: incubator/tuscany/java/sca/modules/http-jetty/src/main/java/org/apache/tuscany/sca/http/jetty/JettyServer.java
URL: http://svn.apache.org/viewvc/incubator/tuscany/java/sca/modules/http-jetty/src/main/java/org/apache/tuscany/sca/http/jetty/JettyServer.java?view=diff&rev=561391&r1=561390&r2=561391
==============================================================================
--- incubator/tuscany/java/sca/modules/http-jetty/src/main/java/org/apache/tuscany/sca/http/jetty/JettyServer.java
(original)
+++ incubator/tuscany/java/sca/modules/http-jetty/src/main/java/org/apache/tuscany/sca/http/jetty/JettyServer.java
Tue Jul 31 09:50:09 2007
@@ -146,7 +146,7 @@
         Port port = ports.get(portNumber);
         if (port == null) {
 
-            // Create an start a new server
+            // Create and start a new server
             try {
                 Server server = new Server();
                 server.setThreadPool(new WorkSchedulerThreadPool());

Modified: incubator/tuscany/java/sca/modules/http-jetty/src/test/java/org/apache/tuscany/sca/http/jetty/JettyServerTestCase.java
URL: http://svn.apache.org/viewvc/incubator/tuscany/java/sca/modules/http-jetty/src/test/java/org/apache/tuscany/sca/http/jetty/JettyServerTestCase.java?view=diff&rev=561391&r1=561390&r2=561391
==============================================================================
--- incubator/tuscany/java/sca/modules/http-jetty/src/test/java/org/apache/tuscany/sca/http/jetty/JettyServerTestCase.java
(original)
+++ incubator/tuscany/java/sca/modules/http-jetty/src/test/java/org/apache/tuscany/sca/http/jetty/JettyServerTestCase.java
Tue Jul 31 09:50:09 2007
@@ -90,6 +90,35 @@
         assertTrue(servlet.invoked);
     }
 
+    /**
+     * Verifies that servlets can be registered with multiple ports
+     */
+    public void testRegisterMultiplePorts() throws Exception {
+        JettyServer service = new JettyServer(workScheduler);
+        TestServlet servlet = new TestServlet();
+        service.addServletMapping("http://127.0.0.1:" + HTTP_PORT + "/", servlet);
+        TestServlet servlet2 = new TestServlet();
+        service.addServletMapping("http://127.0.0.1:" + (HTTP_PORT + 1) + "/", servlet2);
+        {
+            Socket client = new Socket("127.0.0.1", HTTP_PORT);
+            OutputStream os = client.getOutputStream();
+            os.write(REQUEST1.getBytes());
+            os.flush();
+            read(client);
+        }
+        {
+            Socket client = new Socket("127.0.0.1", HTTP_PORT + 1);
+            OutputStream os = client.getOutputStream();
+            os.write(REQUEST1.getBytes());
+            os.flush();
+            read(client);
+        }
+        
+        service.stop();
+        assertTrue(servlet.invoked);
+        assertTrue(servlet2.invoked);
+    }
+
     public void testUnregisterMapping() throws Exception {
         JettyServer service = new JettyServer(workScheduler);
         TestServlet servlet = new TestServlet();

Modified: incubator/tuscany/java/sca/modules/http-tomcat/src/main/java/org/apache/tuscany/sca/http/tomcat/TomcatServer.java
URL: http://svn.apache.org/viewvc/incubator/tuscany/java/sca/modules/http-tomcat/src/main/java/org/apache/tuscany/sca/http/tomcat/TomcatServer.java?view=diff&rev=561391&r1=561390&r2=561391
==============================================================================
--- incubator/tuscany/java/sca/modules/http-tomcat/src/main/java/org/apache/tuscany/sca/http/tomcat/TomcatServer.java
(original)
+++ incubator/tuscany/java/sca/modules/http-tomcat/src/main/java/org/apache/tuscany/sca/http/tomcat/TomcatServer.java
Tue Jul 31 09:50:09 2007
@@ -19,6 +19,11 @@
 package org.apache.tuscany.sca.http.tomcat;
 
 import java.net.URI;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.Map;
+import java.util.Set;
+import java.util.Map.Entry;
 import java.util.concurrent.Executor;
 
 import javax.servlet.Servlet;
@@ -49,78 +54,37 @@
 public class TomcatServer implements ServletHost {
 
     private static final int DEFAULT_PORT = 8080;
-    private StandardEngine engine;
-    private StandardHost host;
-    private Connector connector;
-    private WorkScheduler workScheduler;
-
+    
     /**
-     * A custom connector that uses our WorkScheduler to schedule
-     * worker threads.
+     * Represents a port and the server that serves it.
      */
-    private class CustomConnector extends Connector {
-
-        private class CustomHttpProtocolHandler extends Http11Protocol {
-
-            /**
-             * An Executor wrappering our WorkScheduler
-             */
-            private class WorkSchedulerExecutor implements Executor {
-                public void execute(Runnable command) {
-                    workScheduler.scheduleWork(command);
-                }
-            }
-
-            /**
-             * A custom Endpoint that waits for its acceptor thread to
-             * terminate before stopping.
-             */
-            private class CustomEndpoint extends JIoEndpoint {
-                private Thread acceptorThread;
-
-                private class CustomAcceptor extends Acceptor {
-                    CustomAcceptor() {
-                        super();
-                    }
-                }
-
-                public void start() throws Exception {
-                    if (!initialized)
-                        init();
-                    if (!running) {
-                        running = true;
-                        paused = false;
-                        acceptorThread = new Thread(new CustomAcceptor(), getName() + "-Acceptor-"
+ 0);
-                        acceptorThread.setPriority(threadPriority);
-                        acceptorThread.setDaemon(daemon);
-                        acceptorThread.start();
-                    }
-                }
-
-                public void stop() {
-                    super.stop();
-                    try {
-                        acceptorThread.join();
-                    } catch (InterruptedException e) {
-                        throw new RuntimeException(e);
-                    }
-                }
-
-                public int getCurrentThreadsBusy() {
-                    return 0;
-                }
-            }
-
-            CustomHttpProtocolHandler() {
-                endpoint = new CustomEndpoint();
-                endpoint.setExecutor(new WorkSchedulerExecutor());
-            }
+    private class Port {
+        private StandardEngine engine;
+        private StandardHost host;
+        private Connector connector;
+        
+        private Port(StandardEngine engine, StandardHost host, Connector connector) {
+            this.engine = engine;
+            this.host = host;
+            this.connector = connector;
         }
 
-        CustomConnector() throws Exception {
-            protocolHandler = new CustomHttpProtocolHandler();
+        public StandardEngine getEngine() {
+            return engine;
+        }
+        
+        public StandardHost getHost() {
+            return host;
+        }
+        
+        public Connector getConnector() {
+            return connector;
         }
     }
+    
+    private Map<Integer, Port> ports = new HashMap<Integer, Port>();
+
+    private WorkScheduler workScheduler;
 
     /**
      * Constructs a new embedded Tomcat server.
@@ -131,61 +95,73 @@
         this.workScheduler = workScheduler;
     }
 
-    public void init() throws ServletMappingException {
-
-        // Create an engine
-        engine = new StandardEngine();
-        engine.setBaseDir("");
-        engine.setDefaultHost("localhost");
-
-        // Create a host
-        host = new StandardHost();
-        host.setAppBase("");
-        host.setName("localhost");
-        engine.addChild(host);
-
-        // Create the root context
-        StandardContext context = new StandardContext();
-        context.setDocBase("");
-        context.setPath("");
-        ContextConfig config = new ContextConfig();
-        ((Lifecycle)context).addLifecycleListener(config);
-        host.addChild(context);
-    }
-
-    public void destroy() throws ServletMappingException {
-
-        // Stop the server
-        try {
-            if (connector !=null) {
-                connector.stop();
-                engine.stop();
+    /**
+     * Stop all the started servers.
+     */
+    public void stop() throws ServletMappingException {
+        if (!ports.isEmpty()) {
+            try {
+                Set<Entry<Integer, Port>> entries = new HashSet<Entry<Integer,
Port>>(ports.entrySet());
+                for (Entry<Integer, Port> entry: entries) {
+                    entry.getValue().getConnector().stop();
+                    entry.getValue().getEngine().stop();
+                    ports.remove(entry.getKey());
+                }
+            } catch (Exception e) {
+                throw new ServletMappingException(e);
             }
-        } catch (Exception e) {
-            throw new ServletMappingException(e);
         }
     }
 
     public void addServletMapping(String strURI, Servlet servlet) {
         URI uri = URI.create(strURI);
+        
+        // Get the URI port
+        int portNumber = uri.getPort();
+        if (portNumber == -1) {
+            portNumber = DEFAULT_PORT;
+        }
 
-        int port = uri.getPort();
-        if (port == -1) {
-            port = DEFAULT_PORT;
-        }
-        // Install a default HTTP connector
-        if (connector == null) {
-            //TODO support multiple connectors on different ports
+        // Get the port object associated with the given port number
+        Port port = ports.get(portNumber);
+        if (port == null) {
+
+            // Create an engine
+            StandardEngine engine = new StandardEngine();
+            engine.setBaseDir("");
+            engine.setDefaultHost("localhost");
+            engine.setName("engine/" + portNumber);
+
+            // Create a host
+            StandardHost host = new StandardHost();
+            host.setAppBase("");
+            host.setName("localhost");
+            engine.addChild(host);
+
+            // Create the root context
+            StandardContext context = new StandardContext();
+            context.setDocBase("");
+            context.setPath("");
+            ContextConfig config = new ContextConfig();
+            ((Lifecycle)context).addLifecycleListener(config);
+            host.addChild(context);
+            
+            // Install an HTTP connector
+            Connector connector;
             try {
                 engine.start();
                 connector = new CustomConnector();
-                connector.setPort(port);
+                connector.setPort(portNumber);
                 connector.setContainer(engine);
                 connector.initialize();
                 connector.start();
             } catch (Exception e) {
-                throw new RuntimeException(e);
+                throw new ServletMappingException(e);
             }
+            
+            // Keep track of the running server
+            port = new Port(engine, host, connector);
+            ports.put(portNumber, port);
         }
 
         // Register the servlet mapping
@@ -209,14 +185,12 @@
             wrapper = new ServletWrapper(servlet);
         }
         String mapping = uri.getPath();
-        Context context = host.map(mapping);
+        Context context = port.getHost().map(mapping);
         wrapper.setName(mapping);
         wrapper.addMapping(mapping);
         context.addChild(wrapper);
         context.addServletMapping(mapping, mapping);
-        connector.getMapper().addWrapper("localhost", "", mapping, wrapper);
-
-        System.out.println("addServletMapping port: " + port + " path: " + mapping);
+        port.getConnector().getMapper().addWrapper("localhost", "", mapping, wrapper);
 
         // Initialize the servlet
         try {
@@ -224,11 +198,27 @@
         } catch (ServletException e) {
             throw new ServletMappingException(e);
         }
+
+        System.out.println("addServletMapping port: " + portNumber + " path: " + mapping);
     }
 
-    public Servlet removeServletMapping(String uri) {
-        String mapping = URI.create(uri).getPath();
-        Context context = host.map(mapping);
+    public Servlet removeServletMapping(String uriStr) {
+        URI uri = URI.create(uriStr);
+        
+        // Get the URI port
+        int portNumber = uri.getPort();
+        if (portNumber == -1) {
+            portNumber = DEFAULT_PORT;
+        }
+
+        // Get the port object associated with the given port number
+        Port port = ports.get(portNumber);
+        if (port == null) {
+            throw new IllegalStateException("No servlet registered at this URI: " + uriStr);
+        }
+        
+        String mapping = uri.getPath();
+        Context context = port.getHost().map(mapping);
         MappingData md = new MappingData();
         MessageBytes mb = MessageBytes.newInstance();
         mb.setString(mapping);
@@ -245,6 +235,74 @@
             return servletWrapper.getServlet();
         } else {
             return null;
+        }
+    }
+
+    /**
+     * A custom connector that uses our WorkScheduler to schedule
+     * worker threads.
+     */
+    private class CustomConnector extends Connector {
+
+        private class CustomHttpProtocolHandler extends Http11Protocol {
+
+            /**
+             * An Executor wrappering our WorkScheduler
+             */
+            private class WorkSchedulerExecutor implements Executor {
+                public void execute(Runnable command) {
+                    workScheduler.scheduleWork(command);
+                }
+            }
+
+            /**
+             * A custom Endpoint that waits for its acceptor thread to
+             * terminate before stopping.
+             */
+            private class CustomEndpoint extends JIoEndpoint {
+                private Thread acceptorThread;
+
+                private class CustomAcceptor extends Acceptor {
+                    CustomAcceptor() {
+                        super();
+                    }
+                }
+
+                public void start() throws Exception {
+                    if (!initialized)
+                        init();
+                    if (!running) {
+                        running = true;
+                        paused = false;
+                        acceptorThread = new Thread(new CustomAcceptor(), getName() + "-Acceptor-"
+ 0);
+                        acceptorThread.setPriority(threadPriority);
+                        acceptorThread.setDaemon(daemon);
+                        acceptorThread.start();
+                    }
+                }
+
+                public void stop() {
+                    super.stop();
+                    try {
+                        acceptorThread.join();
+                    } catch (InterruptedException e) {
+                        throw new RuntimeException(e);
+                    }
+                }
+
+                public int getCurrentThreadsBusy() {
+                    return 0;
+                }
+            }
+
+            CustomHttpProtocolHandler() {
+                endpoint = new CustomEndpoint();
+                endpoint.setExecutor(new WorkSchedulerExecutor());
+            }
+        }
+
+        CustomConnector() throws Exception {
+            protocolHandler = new CustomHttpProtocolHandler();
         }
     }
 

Modified: incubator/tuscany/java/sca/modules/http-tomcat/src/main/java/org/apache/tuscany/sca/http/tomcat/module/TomcatRuntimeModuleActivator.java
URL: http://svn.apache.org/viewvc/incubator/tuscany/java/sca/modules/http-tomcat/src/main/java/org/apache/tuscany/sca/http/tomcat/module/TomcatRuntimeModuleActivator.java?view=diff&rev=561391&r1=561390&r2=561391
==============================================================================
--- incubator/tuscany/java/sca/modules/http-tomcat/src/main/java/org/apache/tuscany/sca/http/tomcat/module/TomcatRuntimeModuleActivator.java
(original)
+++ incubator/tuscany/java/sca/modules/http-tomcat/src/main/java/org/apache/tuscany/sca/http/tomcat/module/TomcatRuntimeModuleActivator.java
Tue Jul 31 09:50:09 2007
@@ -43,12 +43,11 @@
             extensionPointRegistry.getExtensionPoint(ServletHostExtensionPoint.class);
         WorkScheduler workScheduler = extensionPointRegistry.getExtensionPoint(WorkScheduler.class);
         server = new TomcatServer(workScheduler);
-        server.init();
         servletHosts.addServletHost(server);
     }
 
     public void stop(ExtensionPointRegistry registry) {
-        server.destroy();
+        server.stop();
     }
 
 }

Modified: incubator/tuscany/java/sca/modules/http-tomcat/src/test/java/org/apache/tuscany/sca/http/tomcat/TomcatServerTestCase.java
URL: http://svn.apache.org/viewvc/incubator/tuscany/java/sca/modules/http-tomcat/src/test/java/org/apache/tuscany/sca/http/tomcat/TomcatServerTestCase.java?view=diff&rev=561391&r1=561390&r2=561391
==============================================================================
--- incubator/tuscany/java/sca/modules/http-tomcat/src/test/java/org/apache/tuscany/sca/http/tomcat/TomcatServerTestCase.java
(original)
+++ incubator/tuscany/java/sca/modules/http-tomcat/src/test/java/org/apache/tuscany/sca/http/tomcat/TomcatServerTestCase.java
Tue Jul 31 09:50:09 2007
@@ -78,7 +78,6 @@
      */
     public void testRegisterServletMapping() throws Exception {
         TomcatServer service = new TomcatServer(workScheduler);
-        service.init();
         TestServlet servlet = new TestServlet();
         service.addServletMapping("http://127.0.0.1:" + HTTP_PORT + "/foo", servlet);
         Socket client = new Socket("127.0.0.1", HTTP_PORT);
@@ -86,13 +85,41 @@
         os.write(REQUEST1.getBytes());
         os.flush();
         read(client);
-        service.destroy();
+        service.stop();
         assertTrue(servlet.invoked);
     }
 
+    /**
+     * Verifies that servlets can be registered with multiple ports
+     */
+    public void testRegisterMultiplePorts() throws Exception {
+        TomcatServer service = new TomcatServer(workScheduler);
+        TestServlet servlet = new TestServlet();
+        service.addServletMapping("http://127.0.0.1:" + HTTP_PORT + "/", servlet);
+        TestServlet servlet2 = new TestServlet();
+        service.addServletMapping("http://127.0.0.1:" + (HTTP_PORT + 1) + "/", servlet2);
+        {
+            Socket client = new Socket("127.0.0.1", HTTP_PORT);
+            OutputStream os = client.getOutputStream();
+            os.write(REQUEST1.getBytes());
+            os.flush();
+            read(client);
+        }
+        {
+            Socket client = new Socket("127.0.0.1", HTTP_PORT + 1);
+            OutputStream os = client.getOutputStream();
+            os.write(REQUEST1.getBytes());
+            os.flush();
+            read(client);
+        }
+        
+        service.stop();
+        assertTrue(servlet.invoked);
+        assertTrue(servlet2.invoked);
+    }
+
     public void testUnregisterMapping() throws Exception {
         TomcatServer service = new TomcatServer(workScheduler);
-        service.init();
         TestServlet servlet = new TestServlet();
         service.addServletMapping("http://127.0.0.1:" + HTTP_PORT + "/foo", servlet);
         service.removeServletMapping("http://127.0.0.1:" + HTTP_PORT + "/foo");
@@ -101,13 +128,12 @@
         os.write(REQUEST1.getBytes());
         os.flush();
         read(client);
-        service.destroy();
+        service.stop();
         assertFalse(servlet.invoked);
     }
 
     public void testRequestSession() throws Exception {
         TomcatServer service = new TomcatServer(workScheduler);
-        service.init();
         TestServlet servlet = new TestServlet();
         service.addServletMapping("http://127.0.0.1:" + HTTP_PORT + "/foo", servlet);
         Socket client = new Socket("127.0.0.1", HTTP_PORT);
@@ -115,22 +141,19 @@
         os.write(REQUEST1.getBytes());
         os.flush();
         read(client);
-        service.destroy();
+        service.stop();
         assertTrue(servlet.invoked);
         assertNotNull(servlet.sessionId);
     }
 
     public void testRestart() throws Exception {
         TomcatServer service = new TomcatServer(workScheduler);
-        service.init();
-        service.destroy();
-        service.init();
-        service.destroy();
+        service.stop();
+        service.stop();
     }
 
     public void testNoMappings() throws Exception {
         TomcatServer service = new TomcatServer(workScheduler);
-        service.init();
         Exception ex = null;
         try {
             Socket client = new Socket("127.0.0.1", HTTP_PORT);
@@ -141,12 +164,11 @@
             ex = e;
         }
         assertNotNull(ex);
-        service.destroy();
+        service.stop();
     }
 
     public void testResourceServlet() throws Exception {
         TomcatServer service = new TomcatServer(workScheduler);
-        service.init();
         
         String documentRoot = getClass().getClassLoader().getResource("content/test.html").toString();
         documentRoot = documentRoot.substring(0, documentRoot.lastIndexOf('/'));
@@ -162,12 +184,11 @@
         String document = read(client);
         assertTrue(document.indexOf("<body><p>hello</body>") != -1);
         
-        service.destroy();
+        service.stop();
     }
 
     public void testDefaultServlet() throws Exception {
         TomcatServer service = new TomcatServer(workScheduler);
-        service.init();
         
         String documentRoot = getClass().getClassLoader().getResource("content/test.html").toString();
         documentRoot = documentRoot.substring(0, documentRoot.lastIndexOf('/'));
@@ -182,7 +203,7 @@
         String document = read(client);
         assertTrue(document.indexOf("<body><p>hello</body>") != -1);
         
-        service.destroy();
+        service.stop();
     }
 
     private static String read(Socket socket) throws IOException {



---------------------------------------------------------------------
To unsubscribe, e-mail: tuscany-commits-unsubscribe@ws.apache.org
For additional commands, e-mail: tuscany-commits-help@ws.apache.org


Mime
View raw message