From a40e527938d76ba71f211da7e327adb50384ba69 Mon Sep 17 00:00:00 2001 From: lresende Date: Wed, 11 Nov 2009 23:26:33 +0000 Subject: Moving 1.x tags git-svn-id: http://svn.us.apache.org/repos/asf/tuscany@835157 13f79535-47bb-0310-9956-ffa450edef68 --- .../sca/host/webapp/HotUpdateContextListener.java | 55 ++++++++++ .../tuscany/sca/host/webapp/SCADomainHelper.java | 68 ++++++++++++ .../sca/host/webapp/TuscanyContextListener.java | 51 +++++++++ .../tuscany/sca/host/webapp/TuscanyServlet.java | 68 ++++++++++++ .../sca/host/webapp/WebAppModuleActivator.java | 42 ++++++++ .../tuscany/sca/host/webapp/WebAppServletHost.java | 116 +++++++++++++++++++++ 6 files changed, 400 insertions(+) create mode 100644 sca-java-1.x/tags/0.99-incubating/modules/host-webapp/src/main/java/org/apache/tuscany/sca/host/webapp/HotUpdateContextListener.java create mode 100644 sca-java-1.x/tags/0.99-incubating/modules/host-webapp/src/main/java/org/apache/tuscany/sca/host/webapp/SCADomainHelper.java create mode 100644 sca-java-1.x/tags/0.99-incubating/modules/host-webapp/src/main/java/org/apache/tuscany/sca/host/webapp/TuscanyContextListener.java create mode 100644 sca-java-1.x/tags/0.99-incubating/modules/host-webapp/src/main/java/org/apache/tuscany/sca/host/webapp/TuscanyServlet.java create mode 100644 sca-java-1.x/tags/0.99-incubating/modules/host-webapp/src/main/java/org/apache/tuscany/sca/host/webapp/WebAppModuleActivator.java create mode 100644 sca-java-1.x/tags/0.99-incubating/modules/host-webapp/src/main/java/org/apache/tuscany/sca/host/webapp/WebAppServletHost.java (limited to 'sca-java-1.x/tags/0.99-incubating/modules/host-webapp/src/main/java/org/apache') diff --git a/sca-java-1.x/tags/0.99-incubating/modules/host-webapp/src/main/java/org/apache/tuscany/sca/host/webapp/HotUpdateContextListener.java b/sca-java-1.x/tags/0.99-incubating/modules/host-webapp/src/main/java/org/apache/tuscany/sca/host/webapp/HotUpdateContextListener.java new file mode 100644 index 0000000000..e6300c81b9 --- /dev/null +++ b/sca-java-1.x/tags/0.99-incubating/modules/host-webapp/src/main/java/org/apache/tuscany/sca/host/webapp/HotUpdateContextListener.java @@ -0,0 +1,55 @@ +/* + * 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.host.webapp; + +import java.io.File; + +import javax.servlet.ServletContext; +import javax.servlet.ServletContextEvent; + +import org.apache.tuscany.sca.host.embedded.impl.HotUpdatableSCADomain; + +/** + * A WebApp ContextListener that starts a Tuscany runtime supporting multiple + * SCA contribution jars. All contribution jars found in the repository + * directory named "sca-contributions" will be contributed to the SCA + * domain. Any changes to the contributions in the repository will be + * automatically detected and the sca domain updated accordingly. + */ +public class HotUpdateContextListener extends TuscanyContextListener { + + public static final String REPOSITORY_FOLDER_NAME = "sca-contributions"; + + @Override + public void contextInitialized(ServletContextEvent event) { + ServletContext servletContext = event.getServletContext(); + + // TODO better domaiURI, maybe based on webapp name? + String domainURI = "http://localhost/" + servletContext.getServletContextName().replace(' ', '.'); + + File repository = new File(servletContext.getRealPath(REPOSITORY_FOLDER_NAME)); + HotUpdatableSCADomain scaDomain = new HotUpdatableSCADomain(domainURI, repository, 2000); + + servletContext.setAttribute(SCADomainHelper.SCA_DOMAIN_ATTRIBUTE, scaDomain); + + super.contextInitialized(event); + } + +} diff --git a/sca-java-1.x/tags/0.99-incubating/modules/host-webapp/src/main/java/org/apache/tuscany/sca/host/webapp/SCADomainHelper.java b/sca-java-1.x/tags/0.99-incubating/modules/host-webapp/src/main/java/org/apache/tuscany/sca/host/webapp/SCADomainHelper.java new file mode 100644 index 0000000000..dff63ed01e --- /dev/null +++ b/sca-java-1.x/tags/0.99-incubating/modules/host-webapp/src/main/java/org/apache/tuscany/sca/host/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.host.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/tags/0.99-incubating/modules/host-webapp/src/main/java/org/apache/tuscany/sca/host/webapp/TuscanyContextListener.java b/sca-java-1.x/tags/0.99-incubating/modules/host-webapp/src/main/java/org/apache/tuscany/sca/host/webapp/TuscanyContextListener.java new file mode 100644 index 0000000000..5bc83df77f --- /dev/null +++ b/sca-java-1.x/tags/0.99-incubating/modules/host-webapp/src/main/java/org/apache/tuscany/sca/host/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.host.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/tags/0.99-incubating/modules/host-webapp/src/main/java/org/apache/tuscany/sca/host/webapp/TuscanyServlet.java b/sca-java-1.x/tags/0.99-incubating/modules/host-webapp/src/main/java/org/apache/tuscany/sca/host/webapp/TuscanyServlet.java new file mode 100644 index 0000000000..0e64ebc9b8 --- /dev/null +++ b/sca-java-1.x/tags/0.99-incubating/modules/host-webapp/src/main/java/org/apache/tuscany/sca/host/webapp/TuscanyServlet.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.host.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) { + path = ((HttpServletRequest)req).getRequestURI(); + 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/tags/0.99-incubating/modules/host-webapp/src/main/java/org/apache/tuscany/sca/host/webapp/WebAppModuleActivator.java b/sca-java-1.x/tags/0.99-incubating/modules/host-webapp/src/main/java/org/apache/tuscany/sca/host/webapp/WebAppModuleActivator.java new file mode 100644 index 0000000000..09b11145d6 --- /dev/null +++ b/sca-java-1.x/tags/0.99-incubating/modules/host-webapp/src/main/java/org/apache/tuscany/sca/host/webapp/WebAppModuleActivator.java @@ -0,0 +1,42 @@ +/* + * 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.host.webapp; + +import org.apache.tuscany.sca.core.ExtensionPointRegistry; +import org.apache.tuscany.sca.core.ModuleActivator; +import org.apache.tuscany.sca.host.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) { + } + +} diff --git a/sca-java-1.x/tags/0.99-incubating/modules/host-webapp/src/main/java/org/apache/tuscany/sca/host/webapp/WebAppServletHost.java b/sca-java-1.x/tags/0.99-incubating/modules/host-webapp/src/main/java/org/apache/tuscany/sca/host/webapp/WebAppServletHost.java new file mode 100644 index 0000000000..4e0d3d88c5 --- /dev/null +++ b/sca-java-1.x/tags/0.99-incubating/modules/host-webapp/src/main/java/org/apache/tuscany/sca/host/webapp/WebAppServletHost.java @@ -0,0 +1,116 @@ +/* + * 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.host.webapp; + +import java.net.URI; +import java.util.HashMap; +import java.util.Map; +import java.util.logging.Logger; + +import javax.servlet.Servlet; +import javax.servlet.ServletConfig; +import javax.servlet.ServletException; + +import org.apache.tuscany.sca.host.http.DefaultResourceServlet; +import org.apache.tuscany.sca.host.http.ServletHost; +import org.apache.tuscany.sca.host.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 final Logger logger = Logger.getLogger(WebAppServletHost.class.getName()); + 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); + + // Ignore registrations of our default resource servlet, as resources + // are already served by the web container + if (servlet instanceof DefaultResourceServlet) { + //TODO maybe ignore registration of the servlet only if it's + // mapped to "/" and still honor other registrations, to do this + // we will need a way to determine what's the default servlet + // in the web container that we are running in. + return; + } + + // For webapps just use the path and ignore the host and port + path = pathURI.getPath(); + if (!path.startsWith("/")) { + path = '/' + path; + } + servlets.put(path, servlet); + logger.info("addServletMapping: " + path); + } + + public Servlet removeServletMapping(String path) throws ServletMappingException { + URI pathURI = URI.create(path); + path = pathURI.getPath(); + if (!path.startsWith("/")) { + path = '/' + path; + } + // for webapps just use the path and ignore the host and port + return servlets.remove(path); + } + + public Servlet getServlet(String path) { + if (path == null) { + return null; + } + if (!path.startsWith("/")) { + path = '/' + 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