summaryrefslogtreecommitdiffstats
path: root/tags/java/sca/0.91-rc3-incubating/modules/http-tomcat/src
diff options
context:
space:
mode:
Diffstat (limited to 'tags/java/sca/0.91-rc3-incubating/modules/http-tomcat/src')
-rw-r--r--tags/java/sca/0.91-rc3-incubating/modules/http-tomcat/src/main/java/org/apache/tuscany/sca/http/tomcat/ServletWrapper.java56
-rw-r--r--tags/java/sca/0.91-rc3-incubating/modules/http-tomcat/src/main/java/org/apache/tuscany/sca/http/tomcat/TomcatDefaultServlet.java70
-rw-r--r--tags/java/sca/0.91-rc3-incubating/modules/http-tomcat/src/main/java/org/apache/tuscany/sca/http/tomcat/TomcatServer.java249
-rw-r--r--tags/java/sca/0.91-rc3-incubating/modules/http-tomcat/src/main/java/org/apache/tuscany/sca/http/tomcat/module/TomcatRuntimeModuleActivator.java54
-rw-r--r--tags/java/sca/0.91-rc3-incubating/modules/http-tomcat/src/main/resources/META-INF/services/org.apache.tuscany.sca.core.ModuleActivator18
-rw-r--r--tags/java/sca/0.91-rc3-incubating/modules/http-tomcat/src/test/java/org/apache/tuscany/sca/http/tomcat/TomcatServerTestCase.java254
-rw-r--r--tags/java/sca/0.91-rc3-incubating/modules/http-tomcat/src/test/resources/content/test.html21
7 files changed, 722 insertions, 0 deletions
diff --git a/tags/java/sca/0.91-rc3-incubating/modules/http-tomcat/src/main/java/org/apache/tuscany/sca/http/tomcat/ServletWrapper.java b/tags/java/sca/0.91-rc3-incubating/modules/http-tomcat/src/main/java/org/apache/tuscany/sca/http/tomcat/ServletWrapper.java
new file mode 100644
index 0000000000..e5dbbc5694
--- /dev/null
+++ b/tags/java/sca/0.91-rc3-incubating/modules/http-tomcat/src/main/java/org/apache/tuscany/sca/http/tomcat/ServletWrapper.java
@@ -0,0 +1,56 @@
+/*
+ * 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 javax.servlet.Servlet;
+import javax.servlet.ServletException;
+
+import org.apache.catalina.core.StandardWrapper;
+
+/**
+ * A servlet wrapper.
+ *
+ * @version $Rev$ $Date$
+ */
+public class ServletWrapper extends StandardWrapper {
+ private static final long serialVersionUID = 1L;
+
+ private final Servlet servlet;
+
+ public ServletWrapper(Servlet servlet) {
+ this.servlet = servlet;
+ }
+
+ public synchronized Servlet loadServlet() {
+ return servlet;
+ }
+
+ public Servlet getServlet() {
+ return servlet;
+ }
+
+ public void initServlet() throws ServletException {
+ servlet.init(facade);
+ }
+
+ public void destroyServlet() {
+ servlet.destroy();
+ }
+
+}
diff --git a/tags/java/sca/0.91-rc3-incubating/modules/http-tomcat/src/main/java/org/apache/tuscany/sca/http/tomcat/TomcatDefaultServlet.java b/tags/java/sca/0.91-rc3-incubating/modules/http-tomcat/src/main/java/org/apache/tuscany/sca/http/tomcat/TomcatDefaultServlet.java
new file mode 100644
index 0000000000..e38f9a0dc9
--- /dev/null
+++ b/tags/java/sca/0.91-rc3-incubating/modules/http-tomcat/src/main/java/org/apache/tuscany/sca/http/tomcat/TomcatDefaultServlet.java
@@ -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;
+ }
+}
diff --git a/tags/java/sca/0.91-rc3-incubating/modules/http-tomcat/src/main/java/org/apache/tuscany/sca/http/tomcat/TomcatServer.java b/tags/java/sca/0.91-rc3-incubating/modules/http-tomcat/src/main/java/org/apache/tuscany/sca/http/tomcat/TomcatServer.java
new file mode 100644
index 0000000000..bf794486f2
--- /dev/null
+++ b/tags/java/sca/0.91-rc3-incubating/modules/http-tomcat/src/main/java/org/apache/tuscany/sca/http/tomcat/TomcatServer.java
@@ -0,0 +1,249 @@
+/*
+ * 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.concurrent.Executor;
+
+import javax.servlet.Servlet;
+import javax.servlet.ServletException;
+
+import org.apache.catalina.Context;
+import org.apache.catalina.Lifecycle;
+import org.apache.catalina.connector.Connector;
+import org.apache.catalina.core.StandardContext;
+import org.apache.catalina.core.StandardEngine;
+import org.apache.catalina.core.StandardHost;
+import org.apache.catalina.startup.ContextConfig;
+import org.apache.coyote.http11.Http11Protocol;
+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;
+
+/**
+ * A Tomcat based implementation of ServletHost.
+ *
+ * @version $Rev$ $Date$
+ */
+@SuppressWarnings("deprecation")
+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.
+ */
+ 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();
+ }
+ }
+
+ /**
+ * Constructs a new embedded Tomcat server.
+ *
+ * @param workScheduler the WorkScheduler to use to process requests.
+ */
+ public TomcatServer(WorkScheduler workScheduler) {
+ 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();
+ }
+ } catch (Exception e) {
+ throw new ServletMappingException(e);
+ }
+ }
+
+ public void addServletMapping(String strURI, Servlet servlet) {
+ URI uri = URI.create(strURI);
+
+ 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
+ try {
+ engine.start();
+ connector = new CustomConnector();
+ connector.setPort(port);
+ connector.setContainer(engine);
+ connector.initialize();
+ connector.start();
+ } catch (Exception e) {
+ throw new RuntimeException(e);
+ }
+ }
+
+ // Register the servlet mapping
+ 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.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) {
+ String mapping = URI.create(uri).getPath();
+ Context context = host.map(mapping);
+ MappingData md = new MappingData();
+ MessageBytes mb = MessageBytes.newInstance();
+ mb.setString(mapping);
+ try {
+ context.getMapper().map(mb, md);
+ } catch (Exception e) {
+ return null;
+ }
+ if (md.wrapper instanceof ServletWrapper) {
+ ServletWrapper servletWrapper = (ServletWrapper)md.wrapper;
+ context.removeServletMapping(mapping);
+ context.removeChild(servletWrapper);
+ servletWrapper.destroyServlet();
+ return servletWrapper.getServlet();
+ } else {
+ return null;
+ }
+ }
+
+}
diff --git a/tags/java/sca/0.91-rc3-incubating/modules/http-tomcat/src/main/java/org/apache/tuscany/sca/http/tomcat/module/TomcatRuntimeModuleActivator.java b/tags/java/sca/0.91-rc3-incubating/modules/http-tomcat/src/main/java/org/apache/tuscany/sca/http/tomcat/module/TomcatRuntimeModuleActivator.java
new file mode 100644
index 0000000000..3d59813608
--- /dev/null
+++ b/tags/java/sca/0.91-rc3-incubating/modules/http-tomcat/src/main/java/org/apache/tuscany/sca/http/tomcat/module/TomcatRuntimeModuleActivator.java
@@ -0,0 +1,54 @@
+/*
+ * 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.module;
+
+import org.apache.tuscany.sca.core.ExtensionPointRegistry;
+import org.apache.tuscany.sca.core.ModuleActivator;
+import org.apache.tuscany.sca.http.ServletHostExtensionPoint;
+import org.apache.tuscany.sca.http.tomcat.TomcatServer;
+import org.apache.tuscany.sca.work.WorkScheduler;
+
+/**
+ * @version $Rev$ $Date$
+ */
+public class TomcatRuntimeModuleActivator implements ModuleActivator {
+
+ private TomcatServer server;
+
+ public Object[] getExtensionPoints() {
+ return null;
+ }
+
+ public void start(ExtensionPointRegistry extensionPointRegistry) {
+
+ // Register a Tomcat servlet host
+ ServletHostExtensionPoint servletHosts =
+ 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();
+ }
+
+}
diff --git a/tags/java/sca/0.91-rc3-incubating/modules/http-tomcat/src/main/resources/META-INF/services/org.apache.tuscany.sca.core.ModuleActivator b/tags/java/sca/0.91-rc3-incubating/modules/http-tomcat/src/main/resources/META-INF/services/org.apache.tuscany.sca.core.ModuleActivator
new file mode 100644
index 0000000000..f0f4e17507
--- /dev/null
+++ b/tags/java/sca/0.91-rc3-incubating/modules/http-tomcat/src/main/resources/META-INF/services/org.apache.tuscany.sca.core.ModuleActivator
@@ -0,0 +1,18 @@
+# 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.
+# Implementation class for the ModuleActivator
+org.apache.tuscany.sca.http.tomcat.module.TomcatRuntimeModuleActivator
diff --git a/tags/java/sca/0.91-rc3-incubating/modules/http-tomcat/src/test/java/org/apache/tuscany/sca/http/tomcat/TomcatServerTestCase.java b/tags/java/sca/0.91-rc3-incubating/modules/http-tomcat/src/test/java/org/apache/tuscany/sca/http/tomcat/TomcatServerTestCase.java
new file mode 100644
index 0000000000..0431ea320b
--- /dev/null
+++ b/tags/java/sca/0.91-rc3-incubating/modules/http-tomcat/src/test/java/org/apache/tuscany/sca/http/tomcat/TomcatServerTestCase.java
@@ -0,0 +1,254 @@
+/*
+ * 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.io.BufferedReader;
+import java.io.IOException;
+import java.io.InputStreamReader;
+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;
+import javax.servlet.http.HttpServletResponse;
+
+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;
+
+/**
+ * @version $Rev$ $Date$
+ */
+public class TomcatServerTestCase extends TestCase {
+
+ private static final String REQUEST1_HEADER =
+ "GET /foo 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 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() {
+
+ public <T extends Runnable> void scheduleWork(T work) {
+ Thread thread = new Thread(work);
+ thread.start();
+ }
+
+ public <T extends Runnable> void scheduleWork(T work, NotificationListener<T> listener) {
+ scheduleWork(work);
+ }
+ };
+
+ /**
+ * Verifies requests are properly routed according to the servlet mapping
+ */
+ 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);
+ OutputStream os = client.getOutputStream();
+ os.write(REQUEST1.getBytes());
+ os.flush();
+ read(client);
+ service.destroy();
+ assertTrue(servlet.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");
+ Socket client = new Socket("127.0.0.1", HTTP_PORT);
+ OutputStream os = client.getOutputStream();
+ os.write(REQUEST1.getBytes());
+ os.flush();
+ read(client);
+ service.destroy();
+ 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);
+ OutputStream os = client.getOutputStream();
+ os.write(REQUEST1.getBytes());
+ os.flush();
+ read(client);
+ service.destroy();
+ assertTrue(servlet.invoked);
+ assertNotNull(servlet.sessionId);
+ }
+
+ public void testRestart() throws Exception {
+ TomcatServer service = new TomcatServer(workScheduler);
+ service.init();
+ service.destroy();
+ service.init();
+ service.destroy();
+ }
+
+ 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);
+ OutputStream os = client.getOutputStream();
+ os.write(REQUEST1.getBytes());
+ os.flush();
+ } catch (Exception e) {
+ ex = e;
+ }
+ assertNotNull(ex);
+ 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("<body><p>hello</body>") != -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("<body><p>hello</body>") != -1);
+
+ service.destroy();
+ }
+
+ private static String read(Socket socket) throws IOException {
+ BufferedReader reader = null;
+ try {
+ reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
+ StringBuffer sb = new StringBuffer();
+ String str;
+ while ((str = reader.readLine()) != null) {
+ sb.append(str);
+ }
+ return sb.toString();
+ } finally {
+ if (reader != null) {
+ reader.close();
+ }
+ }
+ }
+
+ private class TestServlet extends HttpServlet {
+ private static final long serialVersionUID = 1L;
+ boolean invoked;
+ String sessionId;
+
+ protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
+ invoked = true;
+ sessionId = req.getSession().getId();
+ OutputStream writer = resp.getOutputStream();
+ try {
+ writer.write("result".getBytes());
+ } finally {
+ writer.close();
+ }
+ }
+
+ }
+
+ 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();
+ }
+ }
+}
diff --git a/tags/java/sca/0.91-rc3-incubating/modules/http-tomcat/src/test/resources/content/test.html b/tags/java/sca/0.91-rc3-incubating/modules/http-tomcat/src/test/resources/content/test.html
new file mode 100644
index 0000000000..f4b79d7f01
--- /dev/null
+++ b/tags/java/sca/0.91-rc3-incubating/modules/http-tomcat/src/test/resources/content/test.html
@@ -0,0 +1,21 @@
+<html>
+<!--
+ * 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.
+-->
+<body><p>hello</body>
+</html> \ No newline at end of file