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/03/20 04:10:59 UTC
svn commit: r520246 - in
/incubator/tuscany/java/sca/services/transports/http.tomcat: ./
src/main/java/org/apache/tuscany/service/tomcat/
src/test/java/org/apache/tuscany/service/tomcat/
Author: jsdelfino
Date: Mon Mar 19 20:10:58 2007
New Revision: 520246
URL: http://svn.apache.org/viewvc?view=rev&rev=520246
Log:
Implementation of the ServletHost interface using an Embedded Tomcat.
Added:
incubator/tuscany/java/sca/services/transports/http.tomcat/src/main/java/org/apache/tuscany/service/tomcat/ServletWrapper.java (with props)
incubator/tuscany/java/sca/services/transports/http.tomcat/src/main/java/org/apache/tuscany/service/tomcat/TomcatServiceImpl.java (with props)
incubator/tuscany/java/sca/services/transports/http.tomcat/src/test/java/org/apache/tuscany/service/tomcat/TomcatServiceImplTestCase.java (with props)
Modified:
incubator/tuscany/java/sca/services/transports/http.tomcat/pom.xml
Modified: incubator/tuscany/java/sca/services/transports/http.tomcat/pom.xml
URL: http://svn.apache.org/viewvc/incubator/tuscany/java/sca/services/transports/http.tomcat/pom.xml?view=diff&rev=520246&r1=520245&r2=520246
==============================================================================
--- incubator/tuscany/java/sca/services/transports/http.tomcat/pom.xml (original)
+++ incubator/tuscany/java/sca/services/transports/http.tomcat/pom.xml Mon Mar 19 20:10:58 2007
@@ -28,6 +28,21 @@
<name>Apache Tuscany Tomcat HTTP Service</name>
<description>Tuscany Tomcat HTTP Service</description>
+ <repositories>
+ <repository>
+ <id>tomcat-m2-repo</id>
+ <name>Tomcat M2 Repository</name>
+ <url>http://tomcat.apache.org/dev/dist/m2-repository/</url>
+ <layout>default</layout>
+ <snapshots>
+ <enabled>false</enabled>
+ </snapshots>
+ <releases>
+ <enabled>true</enabled>
+ </releases>
+ </repository>
+ </repositories>
+
<dependencies>
<dependency>
<groupId>org.apache.tuscany.sca.kernel</groupId>
@@ -39,6 +54,18 @@
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
+ </dependency>
+ <dependency>
+ <groupId>org.apache.tomcat</groupId>
+ <artifactId>catalina</artifactId>
+ <version>6.0.10</version>
+ <scope>provided</scope>
+ </dependency>
+ <dependency>
+ <groupId>org.apache.tomcat</groupId>
+ <artifactId>coyote</artifactId>
+ <version>6.0.10</version>
+ <scope>provided</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
Added: incubator/tuscany/java/sca/services/transports/http.tomcat/src/main/java/org/apache/tuscany/service/tomcat/ServletWrapper.java
URL: http://svn.apache.org/viewvc/incubator/tuscany/java/sca/services/transports/http.tomcat/src/main/java/org/apache/tuscany/service/tomcat/ServletWrapper.java?view=auto&rev=520246
==============================================================================
--- incubator/tuscany/java/sca/services/transports/http.tomcat/src/main/java/org/apache/tuscany/service/tomcat/ServletWrapper.java (added)
+++ incubator/tuscany/java/sca/services/transports/http.tomcat/src/main/java/org/apache/tuscany/service/tomcat/ServletWrapper.java Mon Mar 19 20:10:58 2007
@@ -0,0 +1,47 @@
+/*
+ * 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.service.tomcat;
+
+import javax.servlet.Servlet;
+
+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;
+ }
+
+}
Propchange: incubator/tuscany/java/sca/services/transports/http.tomcat/src/main/java/org/apache/tuscany/service/tomcat/ServletWrapper.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: incubator/tuscany/java/sca/services/transports/http.tomcat/src/main/java/org/apache/tuscany/service/tomcat/ServletWrapper.java
------------------------------------------------------------------------------
svn:keywords = Rev Date
Added: incubator/tuscany/java/sca/services/transports/http.tomcat/src/main/java/org/apache/tuscany/service/tomcat/TomcatServiceImpl.java
URL: http://svn.apache.org/viewvc/incubator/tuscany/java/sca/services/transports/http.tomcat/src/main/java/org/apache/tuscany/service/tomcat/TomcatServiceImpl.java?view=auto&rev=520246
==============================================================================
--- incubator/tuscany/java/sca/services/transports/http.tomcat/src/main/java/org/apache/tuscany/service/tomcat/TomcatServiceImpl.java (added)
+++ incubator/tuscany/java/sca/services/transports/http.tomcat/src/main/java/org/apache/tuscany/service/tomcat/TomcatServiceImpl.java Mon Mar 19 20:10:58 2007
@@ -0,0 +1,158 @@
+/*
+ * 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.service.tomcat;
+
+import java.net.InetAddress;
+
+import javax.servlet.Servlet;
+
+import org.apache.catalina.Context;
+import org.apache.catalina.Engine;
+import org.apache.catalina.Host;
+import org.apache.catalina.LifecycleException;
+import org.apache.catalina.Wrapper;
+import org.apache.catalina.connector.Connector;
+import org.apache.catalina.startup.Embedded;
+import org.apache.tomcat.util.buf.MessageBytes;
+import org.apache.tomcat.util.http.mapper.MappingData;
+import org.apache.tuscany.spi.host.ServletHost;
+import org.osoa.sca.annotations.Destroy;
+import org.osoa.sca.annotations.EagerInit;
+import org.osoa.sca.annotations.Init;
+import org.osoa.sca.annotations.Property;
+import org.osoa.sca.annotations.Scope;
+import org.osoa.sca.annotations.Service;
+
+/**
+ * A Tomcat based implementation of ServletHost.
+ *
+ * @version $Rev$ $Date$
+ */
+@Service(ServletHost.class)
+@Scope("COMPOSITE")
+@EagerInit
+public class TomcatServiceImpl implements ServletHost {
+
+ private int httpPort = 8080;
+ private Embedded tomcat;
+ private Engine engine;
+ private Host host;
+ private Connector connector;
+ private boolean started;
+
+ @Init
+ public void init() throws LifecycleException {
+ tomcat = new Embedded();
+
+ // Create an engine
+ engine = tomcat.createEngine();
+ engine.setDefaultHost("localhost");
+
+ // Create a host
+ host = tomcat.createHost("localhost", "");
+ engine.addChild(host);
+
+ // Create the ROOT context
+ Context context = tomcat.createContext("", "");
+ host.addChild(context);
+
+ // Install the engine
+ tomcat.addEngine(engine);
+
+ // Install a default HTTP connector
+ connector = tomcat.createConnector((InetAddress)null, httpPort, false);
+ tomcat.addConnector(connector);
+ }
+
+ @Destroy
+ public void destroy() throws LifecycleException {
+
+ // Stop the server
+ if (started) {
+ tomcat.stop();
+ started = false;
+ }
+ }
+
+ public boolean isMappingRegistered(String mapping) {
+ 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 false;
+ }
+ return md.wrapper instanceof ServletWrapper;
+ }
+
+ public void registerMapping(String mapping, Servlet servlet) {
+
+ // Register the servlet mapping
+ Context context = host.map(mapping);
+ Wrapper wrapper = new ServletWrapper(servlet);
+ wrapper.setName(mapping);
+ wrapper.addMapping(mapping);
+ context.addChild(wrapper);
+ context.addServletMapping(mapping, mapping);
+
+ // Start Tomcat
+ try {
+ if (!started) {
+ tomcat.start();
+ started = true;
+ }
+
+ } catch (LifecycleException e) {
+ //TODO use a better runtime exception
+ throw new RuntimeException(e);
+ }
+ }
+
+ public Servlet unregisterMapping(String mapping) {
+ 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);
+ return servletWrapper.getServlet();
+ } else {
+ return null;
+ }
+ }
+
+ @Property
+ public void setHttpPort(int httpPort) {
+ this.httpPort = httpPort;
+ }
+
+ public int getHttpPort() {
+ return httpPort;
+ }
+
+}
Propchange: incubator/tuscany/java/sca/services/transports/http.tomcat/src/main/java/org/apache/tuscany/service/tomcat/TomcatServiceImpl.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: incubator/tuscany/java/sca/services/transports/http.tomcat/src/main/java/org/apache/tuscany/service/tomcat/TomcatServiceImpl.java
------------------------------------------------------------------------------
svn:keywords = Rev Date
Added: incubator/tuscany/java/sca/services/transports/http.tomcat/src/test/java/org/apache/tuscany/service/tomcat/TomcatServiceImplTestCase.java
URL: http://svn.apache.org/viewvc/incubator/tuscany/java/sca/services/transports/http.tomcat/src/test/java/org/apache/tuscany/service/tomcat/TomcatServiceImplTestCase.java?view=auto&rev=520246
==============================================================================
--- incubator/tuscany/java/sca/services/transports/http.tomcat/src/test/java/org/apache/tuscany/service/tomcat/TomcatServiceImplTestCase.java (added)
+++ incubator/tuscany/java/sca/services/transports/http.tomcat/src/test/java/org/apache/tuscany/service/tomcat/TomcatServiceImplTestCase.java Mon Mar 19 20:10:58 2007
@@ -0,0 +1,168 @@
+/*
+ * 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.service.tomcat;
+
+import java.io.BufferedReader;
+import java.io.IOException;
+import java.io.InputStreamReader;
+import java.io.OutputStream;
+import java.net.ConnectException;
+import java.net.Socket;
+
+import javax.servlet.ServletException;
+import javax.servlet.http.HttpServlet;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+
+import junit.framework.TestCase;
+
+/**
+ * @version $Rev$ $Date$
+ */
+public class TomcatServiceImplTestCase 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 int HTTP_PORT = 8585;
+
+ /**
+ * Verifies requests are properly routed according to the servlet mapping
+ */
+ public void testRegisterServletMapping() throws Exception {
+ TomcatServiceImpl service = new TomcatServiceImpl();
+ service.setHttpPort(HTTP_PORT);
+ service.init();
+ TestServlet servlet = new TestServlet();
+ service.registerMapping("/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 testIsMappingRegistered() throws Exception {
+ TomcatServiceImpl service = new TomcatServiceImpl();
+ service.setHttpPort(HTTP_PORT);
+ service.init();
+ TestServlet servlet = new TestServlet();
+ service.registerMapping("/foo", servlet);
+ assertTrue(service.isMappingRegistered("/foo"));
+ assertFalse(service.isMappingRegistered("/bar"));
+ service.destroy();
+ }
+
+ public void testUnregisterMapping() throws Exception {
+ TomcatServiceImpl service = new TomcatServiceImpl();
+ service.setHttpPort(HTTP_PORT);
+ service.init();
+ TestServlet servlet = new TestServlet();
+ service.registerMapping("/foo", servlet);
+ assertTrue(service.isMappingRegistered("/foo"));
+ service.unregisterMapping("/foo");
+ assertFalse(service.isMappingRegistered("/foo"));
+ service.destroy();
+ }
+
+ public void testRequestSession() throws Exception {
+ TomcatServiceImpl service = new TomcatServiceImpl();
+ service.setHttpPort(HTTP_PORT);
+ service.init();
+ TestServlet servlet = new TestServlet();
+ service.registerMapping("/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 {
+ TomcatServiceImpl service = new TomcatServiceImpl();
+ service.setHttpPort(HTTP_PORT);
+ service.init();
+ service.destroy();
+ service.init();
+ service.destroy();
+ }
+
+ public void testNoMappings() throws Exception {
+ TomcatServiceImpl service = new TomcatServiceImpl();
+ service.setHttpPort(HTTP_PORT);
+ service.init();
+ Exception ex = null;
+ try {
+ new Socket("127.0.0.1", HTTP_PORT);
+ } catch (ConnectException e) {
+ ex = e;
+ }
+ assertNotNull(ex);
+ 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();
+ }
+ }
+
+
+ }
+}
Propchange: incubator/tuscany/java/sca/services/transports/http.tomcat/src/test/java/org/apache/tuscany/service/tomcat/TomcatServiceImplTestCase.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: incubator/tuscany/java/sca/services/transports/http.tomcat/src/test/java/org/apache/tuscany/service/tomcat/TomcatServiceImplTestCase.java
------------------------------------------------------------------------------
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