You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tuscany.apache.org by js...@apache.org on 2007/06/18 23:49:04 UTC

svn commit: r548498 - in /incubator/tuscany/java/sca/modules: host-http/src/main/java/org/apache/tuscany/sca/http/ http-jetty/src/main/java/org/apache/tuscany/sca/http/jetty/ http-jetty/src/test/java/org/apache/tuscany/sca/http/jetty/ http-jetty/src/te...

Author: jsdelfino
Date: Mon Jun 18 14:49:02 2007
New Revision: 548498

URL: http://svn.apache.org/viewvc?view=rev&rev=548498
Log:
Added support for serving web resources to the embedded Tomcat and Jetty servers.

Added:
    incubator/tuscany/java/sca/modules/host-http/src/main/java/org/apache/tuscany/sca/http/DefaultResourceServlet.java   (with props)
    incubator/tuscany/java/sca/modules/http-jetty/src/main/java/org/apache/tuscany/sca/http/jetty/JettyDefaultServlet.java   (with props)
    incubator/tuscany/java/sca/modules/http-jetty/src/test/resources/
    incubator/tuscany/java/sca/modules/http-jetty/src/test/resources/content/
    incubator/tuscany/java/sca/modules/http-jetty/src/test/resources/content/test.html   (with props)
    incubator/tuscany/java/sca/modules/http-tomcat/src/main/java/org/apache/tuscany/sca/http/tomcat/TomcatDefaultServlet.java   (with props)
    incubator/tuscany/java/sca/modules/http-tomcat/src/test/resources/
    incubator/tuscany/java/sca/modules/http-tomcat/src/test/resources/content/
    incubator/tuscany/java/sca/modules/http-tomcat/src/test/resources/content/test.html   (with props)
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/ServletWrapper.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/test/java/org/apache/tuscany/sca/http/tomcat/TomcatServerTestCase.java

Added: incubator/tuscany/java/sca/modules/host-http/src/main/java/org/apache/tuscany/sca/http/DefaultResourceServlet.java
URL: http://svn.apache.org/viewvc/incubator/tuscany/java/sca/modules/host-http/src/main/java/org/apache/tuscany/sca/http/DefaultResourceServlet.java?view=auto&rev=548498
==============================================================================
--- incubator/tuscany/java/sca/modules/host-http/src/main/java/org/apache/tuscany/sca/http/DefaultResourceServlet.java (added)
+++ incubator/tuscany/java/sca/modules/host-http/src/main/java/org/apache/tuscany/sca/http/DefaultResourceServlet.java Mon Jun 18 14:49:02 2007
@@ -0,0 +1,85 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ * 
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.    
+ */
+
+package org.apache.tuscany.sca.http;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.net.URL;
+
+import javax.servlet.Servlet;
+import javax.servlet.ServletException;
+import javax.servlet.http.HttpServlet;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+
+/**
+ * A minimal implementation of a servlet that serves documents in a document root
+ * directory.
+ * 
+ * A servlet host implementation is not required to use this implementation and can map
+ * the URI and document root to a more complete and more efficient implementation of  
+ * a resource servlet, for example the Tomcat or Jetty default servlets.
+ *
+ * @version $Rev$ $Date$
+ */
+public class DefaultResourceServlet extends HttpServlet implements Servlet {
+    private static final long serialVersionUID = 2865466417329430610L;
+    
+    private String documentRoot;
+    
+    /**
+     * Constructs a new ResourceServlet
+     * @param documentRoot the document root
+     */
+    public DefaultResourceServlet(String documentRoot) {
+        this.documentRoot = documentRoot;
+    }
+    
+    /**
+     * Returns the document root.
+     * @return the document root
+     */
+    public String getDocumentRoot() {
+        return documentRoot;
+    }
+
+    @Override
+    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
+        
+        // Determine the resource path
+        String requestPath = request.getPathInfo();
+        if (requestPath.startsWith("/")) {
+            requestPath = requestPath.substring(1);
+        }
+        URL url = new URL(documentRoot + '/' + requestPath);
+        
+        // Write the resource
+        InputStream is = url.openStream();
+        OutputStream os = response.getOutputStream(); 
+        byte[] buffer = new byte[2048];
+        for (;;) {
+            int n = is.read(buffer);
+            if (n <= 0)
+                break;
+            os.write(buffer, 0, n);
+        }
+    }
+}

Propchange: incubator/tuscany/java/sca/modules/host-http/src/main/java/org/apache/tuscany/sca/http/DefaultResourceServlet.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: incubator/tuscany/java/sca/modules/host-http/src/main/java/org/apache/tuscany/sca/http/DefaultResourceServlet.java
------------------------------------------------------------------------------
    svn:keywords = Rev Date

Added: incubator/tuscany/java/sca/modules/http-jetty/src/main/java/org/apache/tuscany/sca/http/jetty/JettyDefaultServlet.java
URL: http://svn.apache.org/viewvc/incubator/tuscany/java/sca/modules/http-jetty/src/main/java/org/apache/tuscany/sca/http/jetty/JettyDefaultServlet.java?view=auto&rev=548498
==============================================================================
--- incubator/tuscany/java/sca/modules/http-jetty/src/main/java/org/apache/tuscany/sca/http/jetty/JettyDefaultServlet.java (added)
+++ incubator/tuscany/java/sca/modules/http-jetty/src/main/java/org/apache/tuscany/sca/http/jetty/JettyDefaultServlet.java Mon Jun 18 14:49:02 2007
@@ -0,0 +1,62 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ * 
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.    
+ */
+
+package org.apache.tuscany.sca.http.jetty;
+
+import org.mortbay.jetty.servlet.DefaultServlet;
+import org.mortbay.resource.Resource;
+
+/**
+ * Customizes the Jetty default servlet.
+ *
+ * @version $Rev$ $Date$
+ */
+public class JettyDefaultServlet extends DefaultServlet {
+    private static final long serialVersionUID = 7000218247190209353L;
+
+    private String documentRoot;
+    private String servletPath;
+    
+    public JettyDefaultServlet(String servletPath, String documentRoot) {
+        this.servletPath = servletPath + '/';
+        this.documentRoot = documentRoot;
+    }
+
+    @Override
+    public Resource getResource(String pathInContext) {
+        if (pathInContext.startsWith(servletPath)) {
+            if (pathInContext.length() > servletPath.length()) {
+                pathInContext = pathInContext.substring(servletPath.length());
+            } else {
+                pathInContext = "";
+            }
+        }
+        return super.getResource(pathInContext);
+    }
+    
+    @Override
+    public String getInitParameter(String name) {
+        if ("resourceBase".equals(name)) {
+            return documentRoot;
+        } else {
+            return super.getInitParameter(name);
+        }
+    }
+    
+}

Propchange: incubator/tuscany/java/sca/modules/http-jetty/src/main/java/org/apache/tuscany/sca/http/jetty/JettyDefaultServlet.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: incubator/tuscany/java/sca/modules/http-jetty/src/main/java/org/apache/tuscany/sca/http/jetty/JettyDefaultServlet.java
------------------------------------------------------------------------------
    svn:keywords = Rev Date

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=548498&r1=548497&r2=548498
==============================================================================
--- 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 Mon Jun 18 14:49:02 2007
@@ -26,6 +26,7 @@
 import javax.servlet.Servlet;
 import javax.servlet.ServletException;
 
+import org.apache.tuscany.sca.http.DefaultResourceServlet;
 import org.apache.tuscany.sca.http.ServletHost;
 import org.apache.tuscany.sca.http.ServletMappingException;
 import org.apache.tuscany.sca.work.WorkScheduler;
@@ -34,6 +35,7 @@
 import org.mortbay.jetty.handler.ContextHandler;
 import org.mortbay.jetty.nio.SelectChannelConnector;
 import org.mortbay.jetty.security.SslSocketConnector;
+import org.mortbay.jetty.servlet.DefaultServlet;
 import org.mortbay.jetty.servlet.ServletHandler;
 import org.mortbay.jetty.servlet.ServletHolder;
 import org.mortbay.jetty.servlet.ServletMapping;
@@ -188,7 +190,26 @@
             }
         }
 
-        ServletHolder holder = new ServletHolder(servlet);
+        // Register the servlet mapping
+        ServletHolder holder;
+        if (servlet instanceof DefaultResourceServlet) {
+            
+            // Optimize the handling of resource requests, use the Jetty default servlet
+            // instead of our default resource servlet
+            String servletPath = uri.getPath();
+            if (servletPath.endsWith("*")) {
+                servletPath = servletPath.substring(0, servletPath.length()-1);
+            }
+            if (servletPath.endsWith("/")) {
+                servletPath = servletPath.substring(0, servletPath.length()-1);
+            }
+            DefaultResourceServlet resourceServlet = (DefaultResourceServlet)servlet;
+            DefaultServlet defaultServlet = new JettyDefaultServlet(servletPath, resourceServlet.getDocumentRoot());
+            holder = new ServletHolder(defaultServlet);
+            
+        } else {
+            holder = new ServletHolder(servlet);
+        }
         servletHandler.addServlet(holder);
         ServletMapping mapping = new ServletMapping();
         mapping.setServletName(holder.getName());

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=548498&r1=548497&r2=548498
==============================================================================
--- 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 Mon Jun 18 14:49:02 2007
@@ -25,6 +25,7 @@
 import java.net.ConnectException;
 import java.net.Socket;
 
+import javax.servlet.ServletConfig;
 import javax.servlet.ServletException;
 import javax.servlet.http.HttpServlet;
 import javax.servlet.http.HttpServletRequest;
@@ -32,6 +33,7 @@
 
 import junit.framework.TestCase;
 
+import org.apache.tuscany.sca.http.DefaultResourceServlet;
 import org.apache.tuscany.sca.work.NotificationListener;
 import org.apache.tuscany.sca.work.WorkScheduler;
 
@@ -49,6 +51,15 @@
     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 int HTTP_PORT = 8085;
 
     private WorkScheduler workScheduler = new WorkScheduler() {
@@ -132,6 +143,47 @@
         assertNotNull(ex);
         service.destroy();
     }
+    
+    public void testResourceServlet() throws Exception {
+        JettyServer service = new JettyServer(workScheduler);
+        service.init();
+        
+        String documentRoot = getClass().getClassLoader().getResource("content/test.html").toString();
+        documentRoot = documentRoot.substring(0, documentRoot.lastIndexOf('/'));
+        DefaultResourceServlet resourceServlet = new DefaultResourceServlet(documentRoot);
+        TestResourceServlet servlet = new TestResourceServlet(resourceServlet);
+        service.addServletMapping("http://127.0.0.1:" + HTTP_PORT + "/webcontent/*", servlet);
+        
+        Socket client = new Socket("127.0.0.1", HTTP_PORT);
+        OutputStream os = client.getOutputStream();
+        os.write(REQUEST2.getBytes());
+        os.flush();
+        
+        String document = read(client);
+        assertTrue(document.indexOf("<html><body><p>hello</body></html>") != -1);
+        
+        service.destroy();
+    }
+
+    public void testDefaultServlet() throws Exception {
+        JettyServer service = new JettyServer(workScheduler);
+        service.init();
+        
+        String documentRoot = getClass().getClassLoader().getResource("content/test.html").toString();
+        documentRoot = documentRoot.substring(0, documentRoot.lastIndexOf('/'));
+        DefaultResourceServlet resourceServlet = new DefaultResourceServlet(documentRoot);
+        service.addServletMapping("http://127.0.0.1:" + HTTP_PORT + "/webcontent/*", resourceServlet);
+        
+        Socket client = new Socket("127.0.0.1", HTTP_PORT);
+        OutputStream os = client.getOutputStream();
+        os.write(REQUEST2.getBytes());
+        os.flush();
+        
+        String document = read(client);
+        assertTrue(document.indexOf("<html><body><p>hello</body></html>") != -1);
+        
+        service.destroy();
+    }
 
     private static String read(Socket socket) throws IOException {
         BufferedReader reader = null;
@@ -166,5 +218,37 @@
             }
         }
 
+    }
+
+    private class TestResourceServlet extends HttpServlet {
+        private static final long serialVersionUID = 1L;
+        private HttpServlet delegate;
+        
+        public TestResourceServlet(HttpServlet delegate) {
+            this.delegate = delegate;
+        }
+        
+        @Override
+        public void init() throws ServletException {
+            super.init();
+            delegate.init();
+        }
+
+        @Override
+        public void init(ServletConfig config) throws ServletException {
+            super.init();
+            delegate.init(config);
+        }
+        
+        @Override
+        protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
+            delegate.service(req, resp);
+        }
+        
+        @Override
+        public void destroy() {
+            super.destroy();
+            delegate.destroy();
+        }
     }
 }

Added: incubator/tuscany/java/sca/modules/http-jetty/src/test/resources/content/test.html
URL: http://svn.apache.org/viewvc/incubator/tuscany/java/sca/modules/http-jetty/src/test/resources/content/test.html?view=auto&rev=548498
==============================================================================
--- incubator/tuscany/java/sca/modules/http-jetty/src/test/resources/content/test.html (added)
+++ incubator/tuscany/java/sca/modules/http-jetty/src/test/resources/content/test.html Mon Jun 18 14:49:02 2007
@@ -0,0 +1 @@
+<html><body><p>hello</body></html>
\ No newline at end of file

Propchange: incubator/tuscany/java/sca/modules/http-jetty/src/test/resources/content/test.html
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: incubator/tuscany/java/sca/modules/http-jetty/src/test/resources/content/test.html
------------------------------------------------------------------------------
    svn:keywords = Rev Date

Modified: incubator/tuscany/java/sca/modules/http-tomcat/src/main/java/org/apache/tuscany/sca/http/tomcat/ServletWrapper.java
URL: http://svn.apache.org/viewvc/incubator/tuscany/java/sca/modules/http-tomcat/src/main/java/org/apache/tuscany/sca/http/tomcat/ServletWrapper.java?view=diff&rev=548498&r1=548497&r2=548498
==============================================================================
--- incubator/tuscany/java/sca/modules/http-tomcat/src/main/java/org/apache/tuscany/sca/http/tomcat/ServletWrapper.java (original)
+++ incubator/tuscany/java/sca/modules/http-tomcat/src/main/java/org/apache/tuscany/sca/http/tomcat/ServletWrapper.java Mon Jun 18 14:49:02 2007
@@ -19,6 +19,7 @@
 package org.apache.tuscany.sca.http.tomcat;
 
 import javax.servlet.Servlet;
+import javax.servlet.ServletException;
 
 import org.apache.catalina.core.StandardWrapper;
 
@@ -42,6 +43,14 @@
 
     public Servlet getServlet() {
         return servlet;
+    }
+    
+    public void initServlet() throws ServletException {
+        servlet.init(facade);
+    }
+    
+    public void destroyServlet() {
+        servlet.destroy();
     }
 
 }

Added: incubator/tuscany/java/sca/modules/http-tomcat/src/main/java/org/apache/tuscany/sca/http/tomcat/TomcatDefaultServlet.java
URL: http://svn.apache.org/viewvc/incubator/tuscany/java/sca/modules/http-tomcat/src/main/java/org/apache/tuscany/sca/http/tomcat/TomcatDefaultServlet.java?view=auto&rev=548498
==============================================================================
--- incubator/tuscany/java/sca/modules/http-tomcat/src/main/java/org/apache/tuscany/sca/http/tomcat/TomcatDefaultServlet.java (added)
+++ incubator/tuscany/java/sca/modules/http-tomcat/src/main/java/org/apache/tuscany/sca/http/tomcat/TomcatDefaultServlet.java Mon Jun 18 14:49:02 2007
@@ -0,0 +1,70 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ * 
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.    
+ */
+
+package org.apache.tuscany.sca.http.tomcat;
+
+import java.net.URI;
+import java.util.Hashtable;
+
+import javax.servlet.ServletConfig;
+import javax.servlet.ServletException;
+import javax.servlet.http.HttpServletRequest;
+
+import org.apache.catalina.servlets.DefaultServlet;
+import org.apache.naming.resources.FileDirContext;
+import org.apache.naming.resources.ProxyDirContext;
+
+public class TomcatDefaultServlet extends DefaultServlet {
+    private static final long serialVersionUID = -7503581551326796573L;
+    
+    private String documentRoot;
+    private ProxyDirContext proxyDirContext;
+    
+    public TomcatDefaultServlet(String servletPath, String documentRoot) {
+        this.documentRoot = documentRoot;
+        
+        FileDirContext dirContext = new FileDirContext();
+        URI uri = URI.create(this.documentRoot);
+        dirContext.setDocBase(uri.getPath());
+        
+        proxyDirContext = new ProxyDirContext(new Hashtable(), dirContext);
+        resources = proxyDirContext;
+    }
+    
+    @Override
+    public void init() throws ServletException {
+        super.init();
+        resources = proxyDirContext;
+    }
+    
+    @Override
+    public void init(ServletConfig servletConfig) throws ServletException {
+        super.init(servletConfig);
+        resources = proxyDirContext;
+    }
+
+    @Override
+    protected String getRelativePath(HttpServletRequest request) {
+        String path = request.getPathInfo();
+        if (path == null || path.length() == 0) {
+            path = "/";
+        }
+        return path;
+    }
+}

Propchange: incubator/tuscany/java/sca/modules/http-tomcat/src/main/java/org/apache/tuscany/sca/http/tomcat/TomcatDefaultServlet.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: incubator/tuscany/java/sca/modules/http-tomcat/src/main/java/org/apache/tuscany/sca/http/tomcat/TomcatDefaultServlet.java
------------------------------------------------------------------------------
    svn:keywords = Rev Date

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=548498&r1=548497&r2=548498
==============================================================================
--- 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 Mon Jun 18 14:49:02 2007
@@ -22,10 +22,10 @@
 import java.util.concurrent.Executor;
 
 import javax.servlet.Servlet;
+import javax.servlet.ServletException;
 
 import org.apache.catalina.Context;
 import org.apache.catalina.Lifecycle;
-import org.apache.catalina.Wrapper;
 import org.apache.catalina.connector.Connector;
 import org.apache.catalina.core.StandardContext;
 import org.apache.catalina.core.StandardEngine;
@@ -35,6 +35,7 @@
 import org.apache.tomcat.util.buf.MessageBytes;
 import org.apache.tomcat.util.http.mapper.MappingData;
 import org.apache.tomcat.util.net.JIoEndpoint;
+import org.apache.tuscany.sca.http.DefaultResourceServlet;
 import org.apache.tuscany.sca.http.ServletHost;
 import org.apache.tuscany.sca.http.ServletMappingException;
 import org.apache.tuscany.sca.work.WorkScheduler;
@@ -165,9 +166,10 @@
         }
     }
 
-    public void addServletMapping(String uri, Servlet servlet) {
+    public void addServletMapping(String strURI, Servlet servlet) {
+        URI uri = URI.create(strURI);
 
-        int port = URI.create(uri).getPort();
+        int port = uri.getPort();
         if (port == -1) {
             port = DEFAULT_PORT;
         }
@@ -187,14 +189,39 @@
         }
 
         // Register the servlet mapping
-        String mapping = URI.create(uri).getPath();
+        ServletWrapper wrapper;
+        if (servlet instanceof DefaultResourceServlet) {
+            
+            // Optimize the handling of resource requests, use the Tomcat default servlet
+            // instead of our default resource servlet
+            String servletPath = uri.getPath();
+            if (servletPath.endsWith("*")) {
+                servletPath = servletPath.substring(0, servletPath.length()-1);
+            }
+            if (servletPath.endsWith("/")) {
+                servletPath = servletPath.substring(0, servletPath.length()-1);
+            }
+            DefaultResourceServlet resourceServlet = (DefaultResourceServlet)servlet;
+            TomcatDefaultServlet defaultServlet = new TomcatDefaultServlet(servletPath, resourceServlet.getDocumentRoot());
+            wrapper = new ServletWrapper(defaultServlet);
+            
+        } else {
+            wrapper = new ServletWrapper(servlet);
+        }
+        String mapping = uri.getPath();
         Context context = host.map(mapping);
-        Wrapper wrapper = new ServletWrapper(servlet);
         wrapper.setName(mapping);
         wrapper.addMapping(mapping);
         context.addChild(wrapper);
         context.addServletMapping(mapping, mapping);
         connector.getMapper().addWrapper("localhost", "", mapping, wrapper);
+
+        // Initialize the servlet
+        try {
+            wrapper.initServlet();
+        } catch (ServletException e) {
+            throw new ServletMappingException(e);
+        }
     }
 
     public Servlet removeServletMapping(String uri) {
@@ -212,6 +239,7 @@
             ServletWrapper servletWrapper = (ServletWrapper)md.wrapper;
             context.removeServletMapping(mapping);
             context.removeChild(servletWrapper);
+            servletWrapper.destroyServlet();
             return servletWrapper.getServlet();
         } else {
             return null;

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=548498&r1=548497&r2=548498
==============================================================================
--- 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 Mon Jun 18 14:49:02 2007
@@ -24,6 +24,7 @@
 import java.io.OutputStream;
 import java.net.Socket;
 
+import javax.servlet.ServletConfig;
 import javax.servlet.ServletException;
 import javax.servlet.http.HttpServlet;
 import javax.servlet.http.HttpServletRequest;
@@ -31,6 +32,7 @@
 
 import junit.framework.TestCase;
 
+import org.apache.tuscany.sca.http.DefaultResourceServlet;
 import org.apache.tuscany.sca.work.NotificationListener;
 import org.apache.tuscany.sca.work.WorkScheduler;
 
@@ -48,6 +50,15 @@
     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 int HTTP_PORT = 8085;
 
     private WorkScheduler workScheduler = new WorkScheduler() {
@@ -133,6 +144,47 @@
         service.destroy();
     }
 
+    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('/'));
+        DefaultResourceServlet resourceServlet = new DefaultResourceServlet(documentRoot);
+        TestResourceServlet servlet = new TestResourceServlet(resourceServlet);
+        service.addServletMapping("http://127.0.0.1:" + HTTP_PORT + "/webcontent/*", servlet);
+        
+        Socket client = new Socket("127.0.0.1", HTTP_PORT);
+        OutputStream os = client.getOutputStream();
+        os.write(REQUEST2.getBytes());
+        os.flush();
+        
+        String document = read(client);
+        assertTrue(document.indexOf("<html><body><p>hello</body></html>") != -1);
+        
+        service.destroy();
+    }
+
+    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('/'));
+        DefaultResourceServlet resourceServlet = new DefaultResourceServlet(documentRoot);
+        service.addServletMapping("http://127.0.0.1:" + HTTP_PORT + "/webcontent/*", resourceServlet);
+        
+        Socket client = new Socket("127.0.0.1", HTTP_PORT);
+        OutputStream os = client.getOutputStream();
+        os.write(REQUEST2.getBytes());
+        os.flush();
+        
+        String document = read(client);
+        assertTrue(document.indexOf("<html><body><p>hello</body></html>") != -1);
+        
+        service.destroy();
+    }
+
     private static String read(Socket socket) throws IOException {
         BufferedReader reader = null;
         try {
@@ -166,5 +218,37 @@
             }
         }
 
+    }
+
+    private class TestResourceServlet extends HttpServlet {
+        private static final long serialVersionUID = 1L;
+        private HttpServlet delegate;
+        
+        public TestResourceServlet(HttpServlet delegate) {
+            this.delegate = delegate;
+        }
+        
+        @Override
+        public void init() throws ServletException {
+            super.init();
+            delegate.init();
+        }
+
+        @Override
+        public void init(ServletConfig config) throws ServletException {
+            super.init();
+            delegate.init(config);
+        }
+        
+        @Override
+        protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
+            delegate.service(req, resp);
+        }
+        
+        @Override
+        public void destroy() {
+            super.destroy();
+            delegate.destroy();
+        }
     }
 }

Added: incubator/tuscany/java/sca/modules/http-tomcat/src/test/resources/content/test.html
URL: http://svn.apache.org/viewvc/incubator/tuscany/java/sca/modules/http-tomcat/src/test/resources/content/test.html?view=auto&rev=548498
==============================================================================
--- incubator/tuscany/java/sca/modules/http-tomcat/src/test/resources/content/test.html (added)
+++ incubator/tuscany/java/sca/modules/http-tomcat/src/test/resources/content/test.html Mon Jun 18 14:49:02 2007
@@ -0,0 +1 @@
+<html><body><p>hello</body></html>
\ No newline at end of file

Propchange: incubator/tuscany/java/sca/modules/http-tomcat/src/test/resources/content/test.html
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: incubator/tuscany/java/sca/modules/http-tomcat/src/test/resources/content/test.html
------------------------------------------------------------------------------
    svn:keywords = Rev Date



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