From f860c2f35b2f94e379d2ff7d5c13f54cd4a3132a Mon Sep 17 00:00:00 2001 From: lresende Date: Wed, 11 Nov 2009 23:06:42 +0000 Subject: Moving 1.x branches git-svn-id: http://svn.us.apache.org/repos/asf/tuscany@835119 13f79535-47bb-0310-9956-ffa450edef68 --- .../apache/tuscany/sca/webapp/SCADomainHelper.java | 68 +++++++++++++++++ .../tuscany/sca/webapp/TuscanyContextListener.java | 51 +++++++++++++ .../apache/tuscany/sca/webapp/TuscanyServlet.java | 63 ++++++++++++++++ .../tuscany/sca/webapp/WebAppModuleActivator.java | 46 +++++++++++ .../tuscany/sca/webapp/WebAppServletHost.java | 88 ++++++++++++++++++++++ 5 files changed, 316 insertions(+) create mode 100644 sca-java-1.x/branches/sca-java-0.90/modules/host-webapp/src/main/java/org/apache/tuscany/sca/webapp/SCADomainHelper.java create mode 100644 sca-java-1.x/branches/sca-java-0.90/modules/host-webapp/src/main/java/org/apache/tuscany/sca/webapp/TuscanyContextListener.java create mode 100644 sca-java-1.x/branches/sca-java-0.90/modules/host-webapp/src/main/java/org/apache/tuscany/sca/webapp/TuscanyServlet.java create mode 100644 sca-java-1.x/branches/sca-java-0.90/modules/host-webapp/src/main/java/org/apache/tuscany/sca/webapp/WebAppModuleActivator.java create mode 100644 sca-java-1.x/branches/sca-java-0.90/modules/host-webapp/src/main/java/org/apache/tuscany/sca/webapp/WebAppServletHost.java (limited to 'sca-java-1.x/branches/sca-java-0.90/modules/host-webapp/src/main/java/org/apache/tuscany') diff --git a/sca-java-1.x/branches/sca-java-0.90/modules/host-webapp/src/main/java/org/apache/tuscany/sca/webapp/SCADomainHelper.java b/sca-java-1.x/branches/sca-java-0.90/modules/host-webapp/src/main/java/org/apache/tuscany/sca/webapp/SCADomainHelper.java new file mode 100644 index 0000000000..61a7b54a0c --- /dev/null +++ b/sca-java-1.x/branches/sca-java-0.90/modules/host-webapp/src/main/java/org/apache/tuscany/sca/webapp/SCADomainHelper.java @@ -0,0 +1,68 @@ +/* + * 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.webapp; + +import java.io.File; +import java.net.MalformedURLException; +import java.net.URL; + +import javax.servlet.ServletContext; + +import org.apache.tuscany.sca.host.embedded.SCADomain; + +/** + * Utility class to initialize the SCADomian associated with a webapp + */ +public class SCADomainHelper { + + public static final String SCA_DOMAIN_ATTRIBUTE = "org.apache.tuscany.sca.SCADomain"; + + /** + * Initializes the SCADomian associated with a webapp context. If a SCADomain + * doesn't exist already then one is create based on the webapp config. + */ + public static SCADomain initSCADomain(ServletContext servletContext) { + SCADomain scaDomain = (SCADomain)servletContext.getAttribute(SCA_DOMAIN_ATTRIBUTE); + + String domainURI = "http://localhost/" + servletContext.getServletContextName().replace(' ', '.'); + String contributionRoot = null; + + try { + URL rootURL = servletContext.getResource("/"); + if (rootURL.getProtocol().equals("jndi")) { + //this is tomcat case, we should use getRealPath + File warRootFile = new File(servletContext.getRealPath("/")); + contributionRoot = warRootFile.toURL().toString(); + } else { + //this is jetty case + contributionRoot = rootURL.toString(); + } + } catch(MalformedURLException mf) { + //ignore, pass null + } + + + if (scaDomain == null) { + scaDomain = SCADomain.newInstance(domainURI, contributionRoot); + servletContext.setAttribute(SCA_DOMAIN_ATTRIBUTE, scaDomain); + } + return scaDomain; + } +} diff --git a/sca-java-1.x/branches/sca-java-0.90/modules/host-webapp/src/main/java/org/apache/tuscany/sca/webapp/TuscanyContextListener.java b/sca-java-1.x/branches/sca-java-0.90/modules/host-webapp/src/main/java/org/apache/tuscany/sca/webapp/TuscanyContextListener.java new file mode 100644 index 0000000000..e88ace8d32 --- /dev/null +++ b/sca-java-1.x/branches/sca-java-0.90/modules/host-webapp/src/main/java/org/apache/tuscany/sca/webapp/TuscanyContextListener.java @@ -0,0 +1,51 @@ +/* + * 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.webapp; + +import javax.servlet.ServletContext; +import javax.servlet.ServletContextEvent; +import javax.servlet.ServletContextListener; + +import org.apache.tuscany.sca.host.embedded.SCADomain; + +/** + * A ServletContextListener to create and close the SCADomain + * when the webapp is initialized or destroyed. + */ +public class TuscanyContextListener implements ServletContextListener { + + public void contextInitialized(ServletContextEvent event) { + ServletContext servletContext = event.getServletContext(); + try { + SCADomainHelper.initSCADomain(servletContext); + } catch (Throwable e) { + servletContext.log("exception initializing SCADomain", e); + } + } + + public void contextDestroyed(ServletContextEvent event) { + ServletContext servletContext = event.getServletContext(); + SCADomain scaDomain = (SCADomain) servletContext.getAttribute(SCADomainHelper.SCA_DOMAIN_ATTRIBUTE); + if (scaDomain != null) { + scaDomain.close(); + } + } + +} diff --git a/sca-java-1.x/branches/sca-java-0.90/modules/host-webapp/src/main/java/org/apache/tuscany/sca/webapp/TuscanyServlet.java b/sca-java-1.x/branches/sca-java-0.90/modules/host-webapp/src/main/java/org/apache/tuscany/sca/webapp/TuscanyServlet.java new file mode 100644 index 0000000000..ae2cb59e35 --- /dev/null +++ b/sca-java-1.x/branches/sca-java-0.90/modules/host-webapp/src/main/java/org/apache/tuscany/sca/webapp/TuscanyServlet.java @@ -0,0 +1,63 @@ +/* + * 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.webapp; + +import java.io.IOException; + +import javax.servlet.Servlet; +import javax.servlet.ServletConfig; +import javax.servlet.ServletContext; +import javax.servlet.ServletException; +import javax.servlet.ServletRequest; +import javax.servlet.ServletResponse; +import javax.servlet.http.HttpServlet; +import javax.servlet.http.HttpServletRequest; + +/** + * A servlet that forwards requests to the servlets registered with the Tuscany + * ServletHost. + */ +public class TuscanyServlet extends HttpServlet { + private static final long serialVersionUID = 1L; + + protected transient WebAppServletHost servletHost; + + @Override + public void init(ServletConfig config) throws ServletException { + ServletContext servletContext = config.getServletContext(); + SCADomainHelper.initSCADomain(servletContext); + + // TODO: must be a better way to get this than using a static + servletHost = WebAppServletHost.getInstance(); + servletHost.init(config); + } + + @Override + public void service(ServletRequest req, ServletResponse res) throws ServletException, IOException { + String path = ((HttpServletRequest)req).getPathInfo(); + Servlet servlet = servletHost.getServlet(path); + if (servlet == null) { + throw new IllegalStateException("No servlet registered for path: " + path); + } + + servlet.service(req, res); + } + +} diff --git a/sca-java-1.x/branches/sca-java-0.90/modules/host-webapp/src/main/java/org/apache/tuscany/sca/webapp/WebAppModuleActivator.java b/sca-java-1.x/branches/sca-java-0.90/modules/host-webapp/src/main/java/org/apache/tuscany/sca/webapp/WebAppModuleActivator.java new file mode 100644 index 0000000000..2dee6f3ae2 --- /dev/null +++ b/sca-java-1.x/branches/sca-java-0.90/modules/host-webapp/src/main/java/org/apache/tuscany/sca/webapp/WebAppModuleActivator.java @@ -0,0 +1,46 @@ +/* + * 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.webapp; + +import org.apache.tuscany.sca.core.ExtensionPointRegistry; +import org.apache.tuscany.sca.core.ModuleActivator; +import org.apache.tuscany.sca.http.ServletHostExtensionPoint; + +/** + * Activates the webapp host by registering the webapp ServletHost impl + */ +public class WebAppModuleActivator implements ModuleActivator { + + public void start(ExtensionPointRegistry extensionPointRegistry) { + + ServletHostExtensionPoint servletHosts = + extensionPointRegistry.getExtensionPoint(ServletHostExtensionPoint.class); + + servletHosts.addServletHost(WebAppServletHost.getInstance()); + } + + public void stop(ExtensionPointRegistry registry) { + } + + public Object[] getExtensionPoints() { + return null; + } + +} diff --git a/sca-java-1.x/branches/sca-java-0.90/modules/host-webapp/src/main/java/org/apache/tuscany/sca/webapp/WebAppServletHost.java b/sca-java-1.x/branches/sca-java-0.90/modules/host-webapp/src/main/java/org/apache/tuscany/sca/webapp/WebAppServletHost.java new file mode 100644 index 0000000000..29c54b3153 --- /dev/null +++ b/sca-java-1.x/branches/sca-java-0.90/modules/host-webapp/src/main/java/org/apache/tuscany/sca/webapp/WebAppServletHost.java @@ -0,0 +1,88 @@ +/* + * 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.webapp; + +import java.net.URI; +import java.util.HashMap; +import java.util.Map; + +import javax.servlet.Servlet; +import javax.servlet.ServletConfig; +import javax.servlet.ServletException; + +import org.apache.tuscany.sca.http.ServletHost; +import org.apache.tuscany.sca.http.ServletMappingException; + +/** + * ServletHost impl singleton thats shared between the SCADomain + * instance and the TuscanyServlet instance. + * TODO: using a static singleton seems a big hack but how + * should it be shared? Need some way for TuscanyServlet + * to pull it out of the SCADomain instance. + * + */ +public class WebAppServletHost implements ServletHost { + + private static WebAppServletHost instance = new WebAppServletHost(); + + private Map servlets; + + private WebAppServletHost() { + servlets = new HashMap(); + } + + public void addServletMapping(String path, Servlet servlet) throws ServletMappingException { + URI pathURI = URI.create(path); + // for webapps just use the path and ignore the host and port + servlets.put(pathURI.getPath(), servlet); + } + + public Servlet removeServletMapping(String path) throws ServletMappingException { + URI pathURI = URI.create(path); + // for webapps just use the path and ignore the host and port + return servlets.remove(pathURI.getPath()); + } + + public Servlet getServlet(String path) { + Servlet servlet = servlets.get(path); + if (servlet != null) { + return servlet; + } + for (String servletPath : servlets.keySet()) { + if (servletPath.endsWith("*")) { + if (path.startsWith(servletPath.substring(0, servletPath.length()-1))) { + return servlets.get(servletPath); + } + } + } + return null; + } + + public static WebAppServletHost getInstance() { + return instance; + } + + public void init(ServletConfig config) throws ServletException { + for (Servlet servlet : servlets.values()) { + servlet.init(config); + } + } + +} -- cgit v1.2.3