antelder
Thu, 29 Nov 2007 09:23:33 -0800
Author: antelder Date: Thu Nov 29 09:22:50 2007 New Revision: 599524 URL: http://svn.apache.org/viewvc?rev=599524&view=rev Log: Start of a ServletHost impl for tomcat deep integration Added: incubator/tuscany/java/sca/modules/runtime-tomcat/src/main/java/org/apache/tuscany/sca/runtime/tomcat/TomcatModuleActivator.java (with props) incubator/tuscany/java/sca/modules/runtime-tomcat/src/main/java/org/apache/tuscany/sca/runtime/tomcat/TomcatServletHost.java (with props) incubator/tuscany/java/sca/modules/runtime-tomcat/src/main/java/org/apache/tuscany/sca/runtime/tomcat/TuscanyContext.java (with props) incubator/tuscany/java/sca/modules/runtime-tomcat/src/main/java/org/apache/tuscany/sca/runtime/tomcat/TuscanyHost.java - copied, changed from r599368, incubator/tuscany/java/sca/modules/runtime-tomcat/src/main/java/org/apache/tuscany/sca/runtime/tomcat/TomcatHost.java incubator/tuscany/java/sca/modules/runtime-tomcat/src/main/java/org/apache/tuscany/sca/runtime/tomcat/TuscanyWrapper.java (with props) incubator/tuscany/java/sca/modules/runtime-tomcat/src/main/resources/META-INF/services/org.apache.tuscany.sca.core.ModuleActivator Removed: incubator/tuscany/java/sca/modules/runtime-tomcat/src/main/java/org/apache/tuscany/sca/runtime/tomcat/TomcatHost.java Modified: incubator/tuscany/java/sca/modules/runtime-tomcat/pom.xml incubator/tuscany/java/sca/modules/runtime-tomcat/src/main/java/org/apache/tuscany/sca/runtime/tomcat/TuscanyContextListener.java Modified: incubator/tuscany/java/sca/modules/runtime-tomcat/pom.xml URL: http://svn.apache.org/viewvc/incubator/tuscany/java/sca/modules/runtime-tomcat/pom.xml?rev=599524&r1=599523&r2=599524&view=diff ============================================================================== --- incubator/tuscany/java/sca/modules/runtime-tomcat/pom.xml (original) +++ incubator/tuscany/java/sca/modules/runtime-tomcat/pom.xml Thu Nov 29 09:22:50 2007 @@ -55,8 +55,16 @@ <artifactId>servlet-api</artifactId> </exclusion> </exclusions> + <scope>provided</scope> </dependency> - + + <dependency> + <groupId>org.apache.tomcat</groupId> + <artifactId>coyote</artifactId> + <version>6.0.10</version> + <scope>provided</scope> + </dependency> + <dependency> <groupId>javax.servlet</groupId> <artifactId>servlet-api</artifactId> Added: incubator/tuscany/java/sca/modules/runtime-tomcat/src/main/java/org/apache/tuscany/sca/runtime/tomcat/TomcatModuleActivator.java URL: http://svn.apache.org/viewvc/incubator/tuscany/java/sca/modules/runtime-tomcat/src/main/java/org/apache/tuscany/sca/runtime/tomcat/TomcatModuleActivator.java?rev=599524&view=auto ============================================================================== --- incubator/tuscany/java/sca/modules/runtime-tomcat/src/main/java/org/apache/tuscany/sca/runtime/tomcat/TomcatModuleActivator.java (added) +++ incubator/tuscany/java/sca/modules/runtime-tomcat/src/main/java/org/apache/tuscany/sca/runtime/tomcat/TomcatModuleActivator.java Thu Nov 29 09:22:50 2007 @@ -0,0 +1,39 @@ +/* + * 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.runtime.tomcat; + +import org.apache.tuscany.sca.core.ExtensionPointRegistry; +import org.apache.tuscany.sca.core.ModuleActivator; +import org.apache.tuscany.sca.host.http.ServletHostExtensionPoint; + +/** + * Activates the Tomcat ServletHost by registering the Tomcat ServletHost impl + */ +public class TomcatModuleActivator implements ModuleActivator { + + public void start(ExtensionPointRegistry epr) { + ServletHostExtensionPoint servletHosts = epr.getExtensionPoint(ServletHostExtensionPoint.class); + servletHosts.addServletHost(TomcatServletHost.getInstance()); + } + + public void stop(ExtensionPointRegistry registry) { + } + +} Propchange: incubator/tuscany/java/sca/modules/runtime-tomcat/src/main/java/org/apache/tuscany/sca/runtime/tomcat/TomcatModuleActivator.java ------------------------------------------------------------------------------ svn:eol-style = native Propchange: incubator/tuscany/java/sca/modules/runtime-tomcat/src/main/java/org/apache/tuscany/sca/runtime/tomcat/TomcatModuleActivator.java ------------------------------------------------------------------------------ svn:keywords = Rev Date Added: incubator/tuscany/java/sca/modules/runtime-tomcat/src/main/java/org/apache/tuscany/sca/runtime/tomcat/TomcatServletHost.java URL: http://svn.apache.org/viewvc/incubator/tuscany/java/sca/modules/runtime-tomcat/src/main/java/org/apache/tuscany/sca/runtime/tomcat/TomcatServletHost.java?rev=599524&view=auto ============================================================================== --- incubator/tuscany/java/sca/modules/runtime-tomcat/src/main/java/org/apache/tuscany/sca/runtime/tomcat/TomcatServletHost.java (added) +++ incubator/tuscany/java/sca/modules/runtime-tomcat/src/main/java/org/apache/tuscany/sca/runtime/tomcat/TomcatServletHost.java Thu Nov 29 09:22:50 2007 @@ -0,0 +1,195 @@ +/* + * 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.runtime.tomcat; + +import java.net.InetAddress; +import java.net.MalformedURLException; +import java.net.URI; +import java.net.URL; +import java.net.UnknownHostException; +import java.util.logging.Logger; + +import javax.servlet.RequestDispatcher; +import javax.servlet.Servlet; + +import org.apache.tuscany.sca.host.http.ServletHost; +import org.apache.tuscany.sca.host.http.ServletMappingException; + +/** + */ +public class TomcatServletHost implements ServletHost { + private static final Logger logger = Logger.getLogger(TomcatServletHost.class.getName()); + + private static final TomcatServletHost instance = new TomcatServletHost(); + + private String contextPath = "/tuscany"; + + private int defaultPortNumber = 8080; + + protected TuscanyHost tuscanyHost; + + private TomcatServletHost() { + } + + public void setDefaultPort(int port) { + defaultPortNumber = port; + } + + public int getDefaultPort() { + return defaultPortNumber; + } + + public void addServletMapping(String suri, Servlet servlet) throws ServletMappingException { + suri = patchURI(suri); + tuscanyHost.registerMapping(suri, servlet); + logger.info("Added Servlet mapping: " + suri); + } + + public Servlet removeServletMapping(String suri) throws ServletMappingException { + suri = patchURI(suri); + Servlet servlet = tuscanyHost.unregisterMapping(suri); + logger.info("removed Servlet mapping: " + suri); + return servlet; + } + + private String patchURI(String suri) { + URI pathURI = URI.create(suri); + + // Make sure that the path starts with a / + suri = pathURI.getPath(); + if (!suri.startsWith("/")) { + suri = '/' + suri; + } + + if (!suri.startsWith("/tuscany")) { + suri = "/tuscany" + suri; + } + return suri; + } + + public Servlet getServletMapping(String suri) throws ServletMappingException { +// if (!suri.startsWith("/")) { +// suri = '/' + suri; +// } +// +// // Get the servlet mapped to the given path +// Servlet servlet = servlets.get(suri); +// return servlet; + return null; + } + + public URL getURLMapping(String suri) throws ServletMappingException { + URI uri = URI.create(suri); + + // Get the URI scheme and port + String scheme = uri.getScheme(); + if (scheme == null) { + scheme = "http"; + } + int portNumber = uri.getPort(); + if (portNumber == -1) { + portNumber = defaultPortNumber; + } + + // Get the host + String host; + try { + host = InetAddress.getLocalHost().getHostName(); + } catch (UnknownHostException e) { + host = "localhost"; + } + + // Construct the URL + String path = uri.getPath(); + if (!path.startsWith("/")) { + path = '/' + path; + } + URL url; + try { + url = new URL(scheme, host, portNumber, path); + } catch (MalformedURLException e) { + throw new ServletMappingException(e); + } + return url; + } + + public RequestDispatcher getRequestDispatcher(String suri) throws ServletMappingException { + +// // Make sure that the path starts with a / +// if (!suri.startsWith("/")) { +// suri = '/' + suri; +// } +// +// // Get the servlet mapped to the given path +// Servlet servlet = servlets.get(suri); +// if (servlet != null) { +// return new WebAppRequestDispatcher(suri, servlet); +// } +// for (Map.Entry<String, Servlet> entry : servlets.entrySet()) { +// String servletPath = entry.getKey(); +// if (servletPath.endsWith("*")) { +// servletPath = servletPath.substring(0, servletPath.length() -1); +// if (suri.startsWith(servletPath)) { +// return new WebAppRequestDispatcher(entry.getKey(), entry.getValue()); +// } else { +// if ((suri + "/").startsWith(servletPath)) { +// return new WebAppRequestDispatcher(entry.getKey(), entry.getValue()); +// } +// } +// } +// } + + // No servlet found + return null; + } + + static TomcatServletHost getInstance() { + return instance; + } + + +// void destroy() { +// +// // Destroy the registered servlets +// for (Servlet servlet : servlets.values()) { +// servlet.destroy(); +// } +// +//// // Close the SCA domain +//// if (scaDomain != null) { +//// scaDomain.close(); +//// } +// } + + public String getContextPath() { + return contextPath; + } + + public void setContextPath(String path) { +// if (!contextPath.equals(path)) { +// throw new IllegalArgumentException("invalid context path for webapp, existing context path: " + contextPath + " new contextPath: " + path); +// } + } + + public void setTuscanyHost(TuscanyHost tuscanyHost) { + this.tuscanyHost = tuscanyHost; + } + +} Propchange: incubator/tuscany/java/sca/modules/runtime-tomcat/src/main/java/org/apache/tuscany/sca/runtime/tomcat/TomcatServletHost.java ------------------------------------------------------------------------------ svn:eol-style = native Propchange: incubator/tuscany/java/sca/modules/runtime-tomcat/src/main/java/org/apache/tuscany/sca/runtime/tomcat/TomcatServletHost.java ------------------------------------------------------------------------------ svn:keywords = Rev Date Added: incubator/tuscany/java/sca/modules/runtime-tomcat/src/main/java/org/apache/tuscany/sca/runtime/tomcat/TuscanyContext.java URL: http://svn.apache.org/viewvc/incubator/tuscany/java/sca/modules/runtime-tomcat/src/main/java/org/apache/tuscany/sca/runtime/tomcat/TuscanyContext.java?rev=599524&view=auto ============================================================================== --- incubator/tuscany/java/sca/modules/runtime-tomcat/src/main/java/org/apache/tuscany/sca/runtime/tomcat/TuscanyContext.java (added) +++ incubator/tuscany/java/sca/modules/runtime-tomcat/src/main/java/org/apache/tuscany/sca/runtime/tomcat/TuscanyContext.java Thu Nov 29 09:22:50 2007 @@ -0,0 +1,31 @@ +/* + * 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.runtime.tomcat; + +import org.apache.catalina.core.StandardContext; + +public class TuscanyContext extends StandardContext { + private static final long serialVersionUID = 1L; + + @Override + public boolean getConfigured() { + return true; + } +} Propchange: incubator/tuscany/java/sca/modules/runtime-tomcat/src/main/java/org/apache/tuscany/sca/runtime/tomcat/TuscanyContext.java ------------------------------------------------------------------------------ svn:eol-style = native Propchange: incubator/tuscany/java/sca/modules/runtime-tomcat/src/main/java/org/apache/tuscany/sca/runtime/tomcat/TuscanyContext.java ------------------------------------------------------------------------------ svn:keywords = Rev Date Modified: incubator/tuscany/java/sca/modules/runtime-tomcat/src/main/java/org/apache/tuscany/sca/runtime/tomcat/TuscanyContextListener.java URL: http://svn.apache.org/viewvc/incubator/tuscany/java/sca/modules/runtime-tomcat/src/main/java/org/apache/tuscany/sca/runtime/tomcat/TuscanyContextListener.java?rev=599524&r1=599523&r2=599524&view=diff ============================================================================== --- incubator/tuscany/java/sca/modules/runtime-tomcat/src/main/java/org/apache/tuscany/sca/runtime/tomcat/TuscanyContextListener.java (original) +++ incubator/tuscany/java/sca/modules/runtime-tomcat/src/main/java/org/apache/tuscany/sca/runtime/tomcat/TuscanyContextListener.java Thu Nov 29 09:22:50 2007 @@ -38,11 +38,9 @@ } protected void startContext(Context context) { - System.out.println("xxxxxxxx TuscanyContextListener.startContext" + context); } protected void stopContext(Context context) { - System.out.println("xxxxxxxx TuscanyContextListener.stopContext" + context); } } Copied: incubator/tuscany/java/sca/modules/runtime-tomcat/src/main/java/org/apache/tuscany/sca/runtime/tomcat/TuscanyHost.java (from r599368, incubator/tuscany/java/sca/modules/runtime-tomcat/src/main/java/org/apache/tuscany/sca/runtime/tomcat/TomcatHost.java) URL: http://svn.apache.org/viewvc/incubator/tuscany/java/sca/modules/runtime-tomcat/src/main/java/org/apache/tuscany/sca/runtime/tomcat/TuscanyHost.java?p2=incubator/tuscany/java/sca/modules/runtime-tomcat/src/main/java/org/apache/tuscany/sca/runtime/tomcat/TuscanyHost.java&p1=incubator/tuscany/java/sca/modules/runtime-tomcat/src/main/java/org/apache/tuscany/sca/runtime/tomcat/TomcatHost.java&r1=599368&r2=599524&rev=599524&view=diff ============================================================================== --- incubator/tuscany/java/sca/modules/runtime-tomcat/src/main/java/org/apache/tuscany/sca/runtime/tomcat/TomcatHost.java (original) +++ incubator/tuscany/java/sca/modules/runtime-tomcat/src/main/java/org/apache/tuscany/sca/runtime/tomcat/TuscanyHost.java Thu Nov 29 09:22:50 2007 @@ -20,9 +20,19 @@ package org.apache.tuscany.sca.runtime.tomcat; import java.io.File; +import java.io.IOException; +import java.io.PrintWriter; + +import javax.servlet.Servlet; +import javax.servlet.ServletException; +import javax.servlet.http.HttpServlet; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; import org.apache.catalina.Container; +import org.apache.catalina.Context; import org.apache.catalina.LifecycleException; +import org.apache.catalina.Wrapper; import org.apache.catalina.core.StandardContext; import org.apache.catalina.core.StandardHost; import org.apache.tuscany.sca.runtime.Launcher; @@ -39,10 +49,10 @@ * xmlValidation="false" xmlNamespaceAware="false"> * */ -public class TomcatHost extends StandardHost { +public class TuscanyHost extends StandardHost { private static final long serialVersionUID = 1L; - private static final String REPO = "/sca-contributions"; + private static final String REPO = "../sca-contributions"; protected Launcher launcher; @@ -58,14 +68,57 @@ private void startRuntime() { System.out.println("XXXXXXXX TomcatHost.startRuntime"); + + TomcatServletHost.getInstance().setTuscanyHost(this); + + addTuscany(); + launcher = new Launcher(new File(REPO)); try { launcher.start(); } catch (Exception e) { e.printStackTrace(); } + + } + + private void addTuscany() { + StandardContext tc = new TuscanyContext(); + tc.setPath("/tuscany"); + tc.setDocBase("tuscany"); + super.addChild(tc); + } + + public void registerMapping(String mapping, Servlet servlet) { + Context ctx = map(mapping); + if (ctx == null) { + throw new UnsupportedOperationException("Cannot find context for mapping " + mapping); + } + String contextPath = ctx.getPath(); + + mapping = mapping.substring(contextPath.length()); + Wrapper wrapper = new TuscanyWrapper(servlet); + wrapper.setName(mapping); + ctx.addChild(wrapper); + wrapper.addMapping(mapping); + ctx.getMapper().addWrapper(mapping, wrapper, false); } + public Servlet unregisterMapping(String mapping) { + Context ctx = map(mapping); + if (ctx == null) { + throw new UnsupportedOperationException("Cannot find context for mapping " + mapping); + } + String contextPath = ctx.getPath(); + + mapping = mapping.substring(contextPath.length()); + + TuscanyWrapper wrapper = (TuscanyWrapper) ctx.findChild(mapping); + ctx.getMapper().removeWrapper(mapping); + ctx.removeChild(wrapper); + + return wrapper.getServlet(); + } private void stopRuntime() { System.out.println("XXXXXXXX TomcatHost.stopRuntime"); if (launcher != null) { @@ -82,4 +135,15 @@ ctx.addLifecycleListener(new TuscanyContextListener()); super.addChild(child); } + +} + +class TestServlet extends HttpServlet { + public void doGet(HttpServletRequest request, + HttpServletResponse response) + throws ServletException, IOException { + PrintWriter out = response.getWriter(); + out.println("hi!"); + } + } Added: incubator/tuscany/java/sca/modules/runtime-tomcat/src/main/java/org/apache/tuscany/sca/runtime/tomcat/TuscanyWrapper.java URL: http://svn.apache.org/viewvc/incubator/tuscany/java/sca/modules/runtime-tomcat/src/main/java/org/apache/tuscany/sca/runtime/tomcat/TuscanyWrapper.java?rev=599524&view=auto ============================================================================== --- incubator/tuscany/java/sca/modules/runtime-tomcat/src/main/java/org/apache/tuscany/sca/runtime/tomcat/TuscanyWrapper.java (added) +++ incubator/tuscany/java/sca/modules/runtime-tomcat/src/main/java/org/apache/tuscany/sca/runtime/tomcat/TuscanyWrapper.java Thu Nov 29 09:22:50 2007 @@ -0,0 +1,45 @@ +/* + * 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.runtime.tomcat; + +import javax.servlet.Servlet; + +import org.apache.catalina.core.StandardWrapper; + +/** + */ +public class TuscanyWrapper extends StandardWrapper { + private static final long serialVersionUID = 1L; + + private final Servlet servlet; + + public TuscanyWrapper(Servlet servlet) { + super(); + this.servlet = servlet; + } + + public synchronized Servlet loadServlet() { + return servlet; + } + + public Servlet getServlet() { + return servlet; + } +} \ No newline at end of file Propchange: incubator/tuscany/java/sca/modules/runtime-tomcat/src/main/java/org/apache/tuscany/sca/runtime/tomcat/TuscanyWrapper.java ------------------------------------------------------------------------------ svn:eol-style = native Propchange: incubator/tuscany/java/sca/modules/runtime-tomcat/src/main/java/org/apache/tuscany/sca/runtime/tomcat/TuscanyWrapper.java ------------------------------------------------------------------------------ svn:keywords = Rev Date Added: incubator/tuscany/java/sca/modules/runtime-tomcat/src/main/resources/META-INF/services/org.apache.tuscany.sca.core.ModuleActivator URL: http://svn.apache.org/viewvc/incubator/tuscany/java/sca/modules/runtime-tomcat/src/main/resources/META-INF/services/org.apache.tuscany.sca.core.ModuleActivator?rev=599524&view=auto ============================================================================== --- incubator/tuscany/java/sca/modules/runtime-tomcat/src/main/resources/META-INF/services/org.apache.tuscany.sca.core.ModuleActivator (added) +++ incubator/tuscany/java/sca/modules/runtime-tomcat/src/main/resources/META-INF/services/org.apache.tuscany.sca.core.ModuleActivator Thu Nov 29 09:22:50 2007 @@ -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.runtime.tomcat.TomcatModuleActivator --------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]