From 70d4e6049790789d4600b5ad65262543d05210da Mon Sep 17 00:00:00 2001 From: rfeng Date: Wed, 12 Nov 2008 23:45:10 +0000 Subject: Update the host-webapp to use Node API git-svn-id: http://svn.us.apache.org/repos/asf/tuscany@713566 13f79535-47bb-0310-9956-ffa450edef68 --- .../sca/host/webapp/HotUpdateContextListener.java | 57 ----------------- .../tuscany/sca/host/webapp/SCADomainHelper.java | 72 --------------------- .../sca/host/webapp/TuscanyContextListener.java | 73 ---------------------- .../tuscany/sca/host/webapp/TuscanyServlet.java | 67 -------------------- .../sca/host/webapp/TuscanyServletFilter.java | 25 +++++++- .../sca/host/webapp/WebAppModuleActivator.java | 24 ++----- .../tuscany/sca/host/webapp/WebAppServletHost.java | 30 +++++---- .../tuscany/sca/host/webapp/WebSCADomain.java | 53 ---------------- .../tuscany/sca/host/webapp/jsp/ReferenceTag.java | 9 +-- 9 files changed, 44 insertions(+), 366 deletions(-) delete mode 100644 branches/sca-equinox/modules/host-webapp/src/main/java/org/apache/tuscany/sca/host/webapp/HotUpdateContextListener.java delete mode 100644 branches/sca-equinox/modules/host-webapp/src/main/java/org/apache/tuscany/sca/host/webapp/SCADomainHelper.java delete mode 100644 branches/sca-equinox/modules/host-webapp/src/main/java/org/apache/tuscany/sca/host/webapp/TuscanyContextListener.java delete mode 100644 branches/sca-equinox/modules/host-webapp/src/main/java/org/apache/tuscany/sca/host/webapp/TuscanyServlet.java delete mode 100644 branches/sca-equinox/modules/host-webapp/src/main/java/org/apache/tuscany/sca/host/webapp/WebSCADomain.java (limited to 'branches/sca-equinox/modules/host-webapp/src') diff --git a/branches/sca-equinox/modules/host-webapp/src/main/java/org/apache/tuscany/sca/host/webapp/HotUpdateContextListener.java b/branches/sca-equinox/modules/host-webapp/src/main/java/org/apache/tuscany/sca/host/webapp/HotUpdateContextListener.java deleted file mode 100644 index d7ff73e31f..0000000000 --- a/branches/sca-equinox/modules/host-webapp/src/main/java/org/apache/tuscany/sca/host/webapp/HotUpdateContextListener.java +++ /dev/null @@ -1,57 +0,0 @@ -/* - * 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 SCADomain updated accordingly. - * - * @version $Rev$ $Date$ - */ -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/branches/sca-equinox/modules/host-webapp/src/main/java/org/apache/tuscany/sca/host/webapp/SCADomainHelper.java b/branches/sca-equinox/modules/host-webapp/src/main/java/org/apache/tuscany/sca/host/webapp/SCADomainHelper.java deleted file mode 100644 index 2dbd7e9aa1..0000000000 --- a/branches/sca-equinox/modules/host-webapp/src/main/java/org/apache/tuscany/sca/host/webapp/SCADomainHelper.java +++ /dev/null @@ -1,72 +0,0 @@ -/* - * 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 - * - * @version $Rev$ $Date$ - * @deprecated Not necessary anymore with TuscanyServletFilter - */ -@Deprecated -public class SCADomainHelper { - - 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. - */ - 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-equinox/modules/host-webapp/src/main/java/org/apache/tuscany/sca/host/webapp/TuscanyContextListener.java b/branches/sca-equinox/modules/host-webapp/src/main/java/org/apache/tuscany/sca/host/webapp/TuscanyContextListener.java deleted file mode 100644 index 593073b068..0000000000 --- a/branches/sca-equinox/modules/host-webapp/src/main/java/org/apache/tuscany/sca/host/webapp/TuscanyContextListener.java +++ /dev/null @@ -1,73 +0,0 @@ -/* - * 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.util.Enumeration; - -import javax.servlet.ServletConfig; -import javax.servlet.ServletContext; -import javax.servlet.ServletContextEvent; -import javax.servlet.ServletContextListener; -import javax.servlet.ServletException; - -import org.apache.tuscany.sca.host.embedded.SCADomain; - -/** - * A ServletContextListener to create and close the SCADomain - * when the webapp is initialized or destroyed. - * - * @version $Rev$ $Date$ - */ -public class TuscanyContextListener implements ServletContextListener { - - public void contextInitialized(ServletContextEvent event) { - final ServletContext servletContext = event.getServletContext(); - try { - WebAppServletHost.getInstance().init(new ServletConfig() { - public String getInitParameter(String name) { - return servletContext.getInitParameter(name); - } - - public Enumeration getInitParameterNames() { - return servletContext.getInitParameterNames(); - } - - public ServletContext getServletContext() { - return servletContext; - } - - public String getServletName() { - return null; - } - }); - } catch (ServletException e) { - throw new RuntimeException(e); - } - } - - public void contextDestroyed(ServletContextEvent event) { - ServletContext servletContext = event.getServletContext(); - SCADomain scaDomain = (SCADomain) servletContext.getAttribute(WebAppServletHost.SCA_DOMAIN_ATTRIBUTE); - if (scaDomain != null) { - scaDomain.close(); - } - } - -} diff --git a/branches/sca-equinox/modules/host-webapp/src/main/java/org/apache/tuscany/sca/host/webapp/TuscanyServlet.java b/branches/sca-equinox/modules/host-webapp/src/main/java/org/apache/tuscany/sca/host/webapp/TuscanyServlet.java deleted file mode 100644 index 559fef73cc..0000000000 --- a/branches/sca-equinox/modules/host-webapp/src/main/java/org/apache/tuscany/sca/host/webapp/TuscanyServlet.java +++ /dev/null @@ -1,67 +0,0 @@ -/* - * 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.RequestDispatcher; -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. - * - * @version $Rev$ $Date$ - * @deprecated Not needed anymore, TuscanyServletFilter is sufficient - */ -@Deprecated -public class TuscanyServlet extends HttpServlet { - private static final long serialVersionUID = 1L; - - private 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(); - RequestDispatcher dispatcher = servletHost.getRequestDispatcher(path); - if (dispatcher == null) { - throw new IllegalStateException("No servlet registered for path: " + path); - } - - dispatcher.forward(req, res); - } - -} diff --git a/branches/sca-equinox/modules/host-webapp/src/main/java/org/apache/tuscany/sca/host/webapp/TuscanyServletFilter.java b/branches/sca-equinox/modules/host-webapp/src/main/java/org/apache/tuscany/sca/host/webapp/TuscanyServletFilter.java index a5a3745d7f..cb4369fc01 100644 --- a/branches/sca-equinox/modules/host-webapp/src/main/java/org/apache/tuscany/sca/host/webapp/TuscanyServletFilter.java +++ b/branches/sca-equinox/modules/host-webapp/src/main/java/org/apache/tuscany/sca/host/webapp/TuscanyServletFilter.java @@ -20,7 +20,10 @@ package org.apache.tuscany.sca.host.webapp; import java.io.IOException; +import java.util.Collections; import java.util.Enumeration; +import java.util.Map; +import java.util.WeakHashMap; import javax.servlet.Filter; import javax.servlet.FilterConfig; @@ -32,6 +35,8 @@ import javax.servlet.ServletRequest; import javax.servlet.ServletResponse; import javax.servlet.http.HttpServletRequest; +import org.apache.tuscany.sca.host.http.ServletHost; + /** * A Servlet filter that forwards service requests to the Servlets registered with * the Tuscany ServletHost. @@ -42,11 +47,21 @@ public class TuscanyServletFilter implements Filter { private static final long serialVersionUID = 1L; //private static final Logger logger = Logger.getLogger(WebAppServletHost.class.getName()); - private WebAppServletHost servletHost; + private static Map servletHosts = + Collections.synchronizedMap(new WeakHashMap()); + + // [REVIEW] Assume the filter class is per webapp + private static WebAppServletHost servletHost; + + // Test if the servletHost == null to know if the filter is called (webapp) + static ServletHost getServletHost() { + return servletHosts.get(Thread.currentThread().getContextClassLoader()); + } public void init(final FilterConfig config) throws ServletException { // TODO: must be a better way to get this than using a static - servletHost = WebAppServletHost.getInstance(); + servletHost = new WebAppServletHost(); + servletHosts.put(Thread.currentThread().getContextClassLoader(), servletHost); // Initialize the Servlet host servletHost.init(new ServletConfig() { @@ -69,7 +84,11 @@ public class TuscanyServletFilter implements Filter { } public void destroy() { - WebAppServletHost.getInstance().destroy(); + if (servletHost != null) { + servletHost.destroy(); + servletHost = null; + servletHosts.remove(Thread.currentThread().getContextClassLoader()); + } } public void doFilter(ServletRequest request, ServletResponse response, javax.servlet.FilterChain chain) diff --git a/branches/sca-equinox/modules/host-webapp/src/main/java/org/apache/tuscany/sca/host/webapp/WebAppModuleActivator.java b/branches/sca-equinox/modules/host-webapp/src/main/java/org/apache/tuscany/sca/host/webapp/WebAppModuleActivator.java index dbe8a523f9..88caa33f2a 100644 --- a/branches/sca-equinox/modules/host-webapp/src/main/java/org/apache/tuscany/sca/host/webapp/WebAppModuleActivator.java +++ b/branches/sca-equinox/modules/host-webapp/src/main/java/org/apache/tuscany/sca/host/webapp/WebAppModuleActivator.java @@ -37,30 +37,16 @@ public class WebAppModuleActivator implements ModuleActivator { ServletHostExtensionPoint servletHosts = extensionPointRegistry.getExtensionPoint(ServletHostExtensionPoint.class); - + List hosts = servletHosts.getServletHosts(); - if (isRunningInWebapp()) { - if (hosts.size() > 0) { - hosts.removeAll(hosts); - } - servletHosts.addServletHost(WebAppServletHost.getInstance()); + ServletHost host = TuscanyServletFilter.getServletHost(); + if (host != null) { + hosts.clear(); + hosts.add(host); } } public void stop(ExtensionPointRegistry registry) { } - /** - * TODO: this seems a bit of a hacky way to find if its running in a webapp - * is there a better way? - */ - private boolean isRunningInWebapp() { - for (StackTraceElement ste : new Exception().getStackTrace()) { - if (ste.getClassName().equals(WebSCADomain.class.getName())) { - return true; - } - } - return false; - } - } diff --git a/branches/sca-equinox/modules/host-webapp/src/main/java/org/apache/tuscany/sca/host/webapp/WebAppServletHost.java b/branches/sca-equinox/modules/host-webapp/src/main/java/org/apache/tuscany/sca/host/webapp/WebAppServletHost.java index 599afc541c..729810b0fb 100644 --- a/branches/sca-equinox/modules/host-webapp/src/main/java/org/apache/tuscany/sca/host/webapp/WebAppServletHost.java +++ b/branches/sca-equinox/modules/host-webapp/src/main/java/org/apache/tuscany/sca/host/webapp/WebAppServletHost.java @@ -39,9 +39,11 @@ import javax.servlet.ServletConfig; import javax.servlet.ServletContext; import javax.servlet.ServletException; -import org.apache.tuscany.sca.host.embedded.SCADomain; import org.apache.tuscany.sca.host.http.ServletHost; import org.apache.tuscany.sca.host.http.ServletMappingException; +import org.apache.tuscany.sca.node.Contribution; +import org.apache.tuscany.sca.node.Node; +import org.apache.tuscany.sca.node.NodeFactory; /** * ServletHost implementation for use in a webapp environment. @@ -54,19 +56,19 @@ import org.apache.tuscany.sca.host.http.ServletMappingException; public class WebAppServletHost implements ServletHost { private static final Logger logger = Logger.getLogger(WebAppServletHost.class.getName()); - public static final String SCA_DOMAIN_ATTRIBUTE = "org.apache.tuscany.sca.SCADomain"; + public static final String SCA_NODE_ATTRIBUTE = Node.class.getName(); private static final WebAppServletHost instance = new WebAppServletHost(); private Map servlets; - private SCADomain scaDomain; + private Node node; private String contextPath = "/"; private int defaultPortNumber = 8080; private String contributionRoot; private ServletContext servletContext; - private WebAppServletHost() { + WebAppServletHost() { servlets = new HashMap(); } @@ -211,14 +213,13 @@ public class WebAppServletHost implements ServletHost { public void init(ServletConfig config) throws ServletException { servletContext = config.getServletContext(); - if (servletContext.getAttribute(SCA_DOMAIN_ATTRIBUTE) == null) { + if (servletContext.getAttribute(SCA_NODE_ATTRIBUTE) == null) { initContextPath(config); - String domainURI = "http://localhost/" + contextPath; contributionRoot = getContributionRoot(servletContext); - // logger.info("Contribution: " + contributionRoot); - System.setProperty(SCADomain.class.getName(), WebSCADomain.class.getName()); - this.scaDomain = SCADomain.newInstance(domainURI, contributionRoot); - servletContext.setAttribute(SCA_DOMAIN_ATTRIBUTE, scaDomain); + NodeFactory factory = NodeFactory.newInstance(); + node = factory.createNode(null, new Contribution(contributionRoot, contributionRoot)); + node.start(); + servletContext.setAttribute(SCA_NODE_ATTRIBUTE, node); } // Initialize the registered Servlets @@ -251,7 +252,7 @@ public class WebAppServletHost implements ServletHost { if (rootURL.getProtocol().equals("jndi")) { //this is Tomcat case, we should use getRealPath File warRootFile = new File(servletContext.getRealPath(root)); - contributionRoot = warRootFile.toURL().toString(); + contributionRoot = warRootFile.toURI().toString(); } else { //this is Jetty case contributionRoot = rootURL.toString(); @@ -299,11 +300,8 @@ public class WebAppServletHost implements ServletHost { } // Close the SCA domain - if (scaDomain != null) { - scaDomain.close(); - if (scaDomain instanceof WebSCADomain) { - ((WebSCADomain)scaDomain).destroy(); - } + if (node != null) { + node.stop(); } } diff --git a/branches/sca-equinox/modules/host-webapp/src/main/java/org/apache/tuscany/sca/host/webapp/WebSCADomain.java b/branches/sca-equinox/modules/host-webapp/src/main/java/org/apache/tuscany/sca/host/webapp/WebSCADomain.java deleted file mode 100644 index be35733220..0000000000 --- a/branches/sca-equinox/modules/host-webapp/src/main/java/org/apache/tuscany/sca/host/webapp/WebSCADomain.java +++ /dev/null @@ -1,53 +0,0 @@ -/* - * 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.host.embedded.impl.DefaultSCADomain; - -/** - * @version $Rev$ $Date$ - */ -public class WebSCADomain extends DefaultSCADomain { - - /** - * @param runtimeClassLoader - * @param applicationClassLoader - * @param domainURI - * @param contributionLocation - * @param composites - */ - public WebSCADomain(ClassLoader runtimeClassLoader, - ClassLoader applicationClassLoader, - String domainURI, - String contributionLocation, - String... composites) { - super(runtimeClassLoader, applicationClassLoader, domainURI, contributionLocation, composites); - } - - @Override - public void close() { - // Disable the close() as a hack to keep the WebSCADomain open - } - - public void destroy() { - super.close(); - } - -} diff --git a/branches/sca-equinox/modules/host-webapp/src/main/java/org/apache/tuscany/sca/host/webapp/jsp/ReferenceTag.java b/branches/sca-equinox/modules/host-webapp/src/main/java/org/apache/tuscany/sca/host/webapp/jsp/ReferenceTag.java index b7eb93222e..e76ecd45c6 100644 --- a/branches/sca-equinox/modules/host-webapp/src/main/java/org/apache/tuscany/sca/host/webapp/jsp/ReferenceTag.java +++ b/branches/sca-equinox/modules/host-webapp/src/main/java/org/apache/tuscany/sca/host/webapp/jsp/ReferenceTag.java @@ -21,12 +21,9 @@ package org.apache.tuscany.sca.host.webapp.jsp; import javax.servlet.ServletContext; import javax.servlet.ServletException; -import javax.servlet.jsp.JspException; -import javax.servlet.jsp.PageContext; -import javax.servlet.jsp.tagext.TagSupport; -import org.apache.tuscany.sca.host.embedded.SCADomain; import org.apache.tuscany.sca.host.webapp.WebAppServletHost; +import org.apache.tuscany.sca.node.Node; import org.osoa.sca.ComponentContext; /** @@ -58,9 +55,9 @@ public class ReferenceTag extends TagSupport { ServletContext servletContext = pageContext.getServletContext(); ComponentContext componentContext = (ComponentContext)servletContext.getAttribute("org.osoa.sca.ComponentContext"); - SCADomain scaDomain = null; + Node scaDomain = null; if (componentContext == null) { - scaDomain = (SCADomain)servletContext.getAttribute(WebAppServletHost.SCA_DOMAIN_ATTRIBUTE); + scaDomain = (Node)servletContext.getAttribute(WebAppServletHost.SCA_NODE_ATTRIBUTE); if (scaDomain == null) { throw new JspException("SCADomain is null. Check Tuscany configuration in web.xml"); } -- cgit v1.2.3