diff options
Diffstat (limited to 'branches/sca-java-0.90/modules/host-webapp/src/main')
6 files changed, 334 insertions, 0 deletions
diff --git a/branches/sca-java-0.90/modules/host-webapp/src/main/java/org/apache/tuscany/sca/webapp/SCADomainHelper.java b/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/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/branches/sca-java-0.90/modules/host-webapp/src/main/java/org/apache/tuscany/sca/webapp/TuscanyContextListener.java b/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/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/branches/sca-java-0.90/modules/host-webapp/src/main/java/org/apache/tuscany/sca/webapp/TuscanyServlet.java b/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/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/branches/sca-java-0.90/modules/host-webapp/src/main/java/org/apache/tuscany/sca/webapp/WebAppModuleActivator.java b/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/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/branches/sca-java-0.90/modules/host-webapp/src/main/java/org/apache/tuscany/sca/webapp/WebAppServletHost.java b/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/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<String, Servlet> servlets; + + private WebAppServletHost() { + servlets = new HashMap<String, Servlet>(); + } + + 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); + } + } + +} diff --git a/branches/sca-java-0.90/modules/host-webapp/src/main/resources/META-INF/services/org.apache.tuscany.sca.core.ModuleActivator b/branches/sca-java-0.90/modules/host-webapp/src/main/resources/META-INF/services/org.apache.tuscany.sca.core.ModuleActivator new file mode 100644 index 0000000000..ad19f0464d --- /dev/null +++ b/branches/sca-java-0.90/modules/host-webapp/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.webapp.WebAppModuleActivator
|