diff options
Diffstat (limited to 'sca-java-2.x/trunk/modules/host-webapp')
5 files changed, 368 insertions, 123 deletions
diff --git a/sca-java-2.x/trunk/modules/host-webapp/src/main/java/org/apache/tuscany/sca/host/webapp/TuscanyContextListener.java b/sca-java-2.x/trunk/modules/host-webapp/src/main/java/org/apache/tuscany/sca/host/webapp/TuscanyContextListener.java index 45c1bbf3da..d71abdacb2 100644 --- a/sca-java-2.x/trunk/modules/host-webapp/src/main/java/org/apache/tuscany/sca/host/webapp/TuscanyContextListener.java +++ b/sca-java-2.x/trunk/modules/host-webapp/src/main/java/org/apache/tuscany/sca/host/webapp/TuscanyContextListener.java @@ -25,6 +25,8 @@ import java.util.logging.Logger; import javax.servlet.ServletContextEvent; import javax.servlet.ServletContextListener; +import org.apache.tuscany.sca.host.webapp.WebAppHelper.Configurator; + /** * A ServletContextListener to create and close the SCADomain * when the webapp is initialized or destroyed. @@ -36,7 +38,8 @@ public class TuscanyContextListener implements ServletContextListener { public void contextInitialized(ServletContextEvent event) { logger.info(event.getServletContext().getServletContextName() + " is starting."); try { - WebAppHelper.init(event.getServletContext()); + Configurator configurator = WebAppHelper.getConfigurator(event.getServletContext()); + WebAppHelper.init(configurator); } catch (Throwable e) { logger.log(Level.SEVERE, e.getMessage(), e); } @@ -49,7 +52,8 @@ public class TuscanyContextListener implements ServletContextListener { return; } try { - WebAppHelper.stop(event.getServletContext()); + Configurator configurator = WebAppHelper.getConfigurator(event.getServletContext()); + WebAppHelper.stop(configurator); } catch (Throwable e) { logger.log(Level.SEVERE, e.getMessage(), e); } diff --git a/sca-java-2.x/trunk/modules/host-webapp/src/main/java/org/apache/tuscany/sca/host/webapp/TuscanyServlet.java b/sca-java-2.x/trunk/modules/host-webapp/src/main/java/org/apache/tuscany/sca/host/webapp/TuscanyServlet.java new file mode 100644 index 0000000000..6f5c35d3fb --- /dev/null +++ b/sca-java-2.x/trunk/modules/host-webapp/src/main/java/org/apache/tuscany/sca/host/webapp/TuscanyServlet.java @@ -0,0 +1,62 @@ +/* + * 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.logging.Level; +import java.util.logging.Logger; + +import javax.servlet.ServletConfig; +import javax.servlet.ServletException; +import javax.servlet.http.HttpServlet; + +import org.apache.tuscany.sca.host.webapp.WebAppHelper.Configurator; + +/** + * A Servlet that provides a hook to control the lifecycle of Tuscany node + * + * @version $Rev$ $Date$ + */ +public class TuscanyServlet extends HttpServlet { + private static final long serialVersionUID = 1L; + private Logger logger = Logger.getLogger(TuscanyServlet.class.getName()); + + private transient Configurator configurator; + + public TuscanyServlet() { + super(); + } + + @Override + public void init(ServletConfig config) throws ServletException { + try { + super.init(config); + WebAppHelper.init(WebAppHelper.getConfigurator(this)); + } catch (Throwable e) { + logger.log(Level.SEVERE, e.getMessage(), e); + configurator.getServletContext().log(e.getMessage(), e); + throw new ServletException(e); + } + } + + public void destroy() { + WebAppHelper.stop(configurator); + } + +} diff --git a/sca-java-2.x/trunk/modules/host-webapp/src/main/java/org/apache/tuscany/sca/host/webapp/TuscanyServletFilter.java b/sca-java-2.x/trunk/modules/host-webapp/src/main/java/org/apache/tuscany/sca/host/webapp/TuscanyServletFilter.java index a12466a9fd..a3800f221b 100644 --- a/sca-java-2.x/trunk/modules/host-webapp/src/main/java/org/apache/tuscany/sca/host/webapp/TuscanyServletFilter.java +++ b/sca-java-2.x/trunk/modules/host-webapp/src/main/java/org/apache/tuscany/sca/host/webapp/TuscanyServletFilter.java @@ -20,20 +20,19 @@ package org.apache.tuscany.sca.host.webapp; import java.io.IOException; -import java.util.Enumeration; import java.util.logging.Level; import java.util.logging.Logger; import javax.servlet.Filter; import javax.servlet.FilterConfig; import javax.servlet.RequestDispatcher; -import javax.servlet.ServletContext; import javax.servlet.ServletException; import javax.servlet.ServletRequest; import javax.servlet.ServletResponse; import javax.servlet.http.HttpServletRequest; import org.apache.tuscany.sca.host.http.ServletHost; +import org.apache.tuscany.sca.host.webapp.WebAppHelper.Configurator; /** * A Servlet filter that forwards service requests to the Servlets registered with @@ -45,7 +44,7 @@ public class TuscanyServletFilter implements Filter { private static final long serialVersionUID = 1L; private Logger logger = Logger.getLogger(TuscanyServletFilter.class.getName()); - private transient ServletContext context; + private transient Configurator configurator; private transient ServletHost servletHost; public TuscanyServletFilter() { @@ -54,22 +53,18 @@ public class TuscanyServletFilter implements Filter { public void init(final FilterConfig config) throws ServletException { try { - context = config.getServletContext(); - for (Enumeration<String> e = config.getInitParameterNames(); e.hasMoreElements();) { - String name = e.nextElement(); - String value = config.getInitParameter(name); - context.setAttribute(name, value); - } - servletHost = WebAppHelper.init(context); + configurator = WebAppHelper.getConfigurator(config); + WebAppHelper.init(configurator); + servletHost = WebAppHelper.getServletHost(); } catch (Throwable e) { logger.log(Level.SEVERE, e.getMessage(), e); - context.log(e.getMessage(), e); + configurator.getServletContext().log(e.getMessage(), e); throw new ServletException(e); } } public void destroy() { - WebAppHelper.stop(context); + WebAppHelper.stop(configurator); servletHost = null; } @@ -101,7 +96,7 @@ public class TuscanyServletFilter implements Filter { } } catch (Throwable e) { logger.log(Level.SEVERE, e.getMessage(), e); - context.log(e.getMessage(), e); + configurator.getServletContext().log(e.getMessage(), e); throw new ServletException(e); } } diff --git a/sca-java-2.x/trunk/modules/host-webapp/src/main/java/org/apache/tuscany/sca/host/webapp/WebAppHelper.java b/sca-java-2.x/trunk/modules/host-webapp/src/main/java/org/apache/tuscany/sca/host/webapp/WebAppHelper.java index 0d905eb6eb..5c4cfe8bfe 100644 --- a/sca-java-2.x/trunk/modules/host-webapp/src/main/java/org/apache/tuscany/sca/host/webapp/WebAppHelper.java +++ b/sca-java-2.x/trunk/modules/host-webapp/src/main/java/org/apache/tuscany/sca/host/webapp/WebAppHelper.java @@ -26,17 +26,19 @@ import java.net.URISyntaxException; import java.net.URL; import java.util.Enumeration; +import javax.servlet.FilterConfig; +import javax.servlet.Servlet; import javax.servlet.ServletConfig; import javax.servlet.ServletContext; import javax.servlet.ServletException; import org.apache.tuscany.sca.core.ExtensionPointRegistry; import org.apache.tuscany.sca.core.UtilityExtensionPoint; -import org.apache.tuscany.sca.host.http.ServletHost; import org.apache.tuscany.sca.host.http.ServletHostExtensionPoint; import org.apache.tuscany.sca.node.Node; import org.apache.tuscany.sca.node.NodeFactory; import org.apache.tuscany.sca.node.configuration.NodeConfiguration; +import org.oasisopen.sca.ServiceRuntimeException; public class WebAppHelper { private static final String ROOT = "/"; @@ -46,9 +48,9 @@ public class WebAppHelper { // The prefix for the parameters in web.xml which configure the individual SCA contributions private static final String CONTRIBUTION = "contribution"; private static final String NODE_CONFIGURATION = "node.configuration"; - private static final String WEB_COMPOSITE = "/WEB-INF/web.composite"; private static final String DOMAIN_URI = "domain.uri"; private static final String NODE_URI = "node.uri"; + private static final String COMPOSITE_URI = "composite.uri"; public static final String DOMAIN_NAME_ATTR = "org.apache.tuscany.sca.domain.name"; public static final String SCA_NODE_ATTRIBUTE = Node.class.getName(); private static NodeFactory factory; @@ -74,7 +76,7 @@ public class WebAppHelper { } } } - + private static String[] parse(String listOfValues) { if (listOfValues == null) { return null; @@ -82,26 +84,165 @@ public class WebAppHelper { return listOfValues.split("(\\s|,)+"); } - @SuppressWarnings("unchecked") - private static NodeConfiguration getNodeConfiguration(ServletContext servletContext) throws IOException, + // TODO: Temp for now to get the old samples working till i clean up all the domain uri/name after the ML discussion. + private static String getDomainName(String configURI) { + String domainName; + if (configURI.startsWith("tuscany:vm:")) { + domainName = configURI.substring("tuscany:vm:".length()); + } else if (configURI.startsWith("tuscany:")) { + int i = configURI.indexOf('?'); + if (i == -1) { + domainName = configURI.substring("tuscany:".length()); + } else { + domainName = configURI.substring("tuscany:".length(), i); + } + } else { + domainName = configURI; + } + return domainName; + } + + public static WebAppServletHost getServletHost() { + return host; + } + + public static Node init(final Configurator configurator) { + synchronized (configurator) { + + bootstrapRuntime(configurator); + Node node = (Node)configurator.getAttribute(SCA_NODE_ATTRIBUTE); + if (node == null) { + try { + node = createAndStartNode(configurator); + } catch (ServletException e) { + throw new ServiceRuntimeException(e); + } + configurator.setAttribute(SCA_NODE_ATTRIBUTE, node); + } + return node; + } + } + + /** + * Bootstrap the Tuscany runtime for the given scope + * @param configurator + */ + private synchronized static void bootstrapRuntime(final Configurator configurator) { + if (host == null) { + try { + + String configValue = configurator.getInitParameter("org.apache.tuscany.sca.config"); + if (configValue != null) { + factory = NodeFactory.newInstance(configValue); + } else { + factory = NodeFactory.newInstance(); + } + + // Add ServletContext as a utility + ExtensionPointRegistry registry = factory.getExtensionPointRegistry(); + UtilityExtensionPoint utilityExtensionPoint = registry.getExtensionPoint(UtilityExtensionPoint.class); + utilityExtensionPoint.addUtility(ServletContext.class, configurator.getServletContext()); + + ServletHostExtensionPoint servletHosts = registry.getExtensionPoint(ServletHostExtensionPoint.class); + servletHosts.setWebApp(true); + + host = getServletHost(configurator); + + } catch (ServletException e) { + throw new ServiceRuntimeException(e); + } + } + } + + private static WebAppServletHost getServletHost(final Configurator configurator) throws ServletException { + ExtensionPointRegistry registry = factory.getExtensionPointRegistry(); + WebAppServletHost host = + (WebAppServletHost)org.apache.tuscany.sca.host.http.ServletHostHelper.getServletHost(registry); + + host.init(new ServletConfig() { + public String getInitParameter(String name) { + return configurator.getInitParameter(name); + } + + public Enumeration<?> getInitParameterNames() { + return configurator.getInitParameterNames(); + } + + public ServletContext getServletContext() { + return configurator.getServletContext(); + } + + public String getServletName() { + return configurator.getServletContext().getServletContextName(); + } + }); + return host; + } + + private static Node createAndStartNode(final Configurator configurator) throws ServletException { + NodeConfiguration configuration = null; + try { + configuration = getNodeConfiguration(configurator); + } catch (IOException e) { + throw new ServletException(e); + } catch (URISyntaxException e) { + throw new ServletException(e); + } + Node node = null; + if (configuration != null) { + factory.createNode(configuration).start(); + } + return node; + } + + public static void stop(Configurator configurator) { + Node node = (Node)configurator.getAttribute(SCA_NODE_ATTRIBUTE); + if (node != null) { + node.stop(); + configurator.setAttribute(SCA_NODE_ATTRIBUTE, null); + } + } + + public static void destroy() { + if (factory != null) { + factory.destroy(); + } + factory = null; + host = null; + } + + public static NodeFactory getNodeFactory() { + return factory; + } + + private static String getDefaultComposite(Configurator configurator) { + String name = configurator.getName(); + if ("".equals(name)) { + return "/WEB-INF/web.composite"; + } else { + return "/WEB-INF/" + name + "/servlet.composite"; + } + } + + private static NodeConfiguration getNodeConfiguration(Configurator configurator) throws IOException, URISyntaxException { NodeConfiguration configuration = null; - String nodeConfigURI = (String)servletContext.getAttribute(NODE_CONFIGURATION); + String nodeConfigURI = configurator.getInitParameter(NODE_CONFIGURATION); + ServletContext servletContext = configurator.getServletContext(); if (nodeConfigURI != null) { URL url = getResource(servletContext, nodeConfigURI); configuration = factory.loadConfiguration(url.openStream(), url); } else { configuration = factory.createNodeConfiguration(); - - + boolean explicitContributions = false; - Enumeration<String> names = servletContext.getAttributeNames(); + Enumeration<String> names = configurator.getInitParameterNames(); while (names.hasMoreElements()) { String name = names.nextElement(); if (name.equals(CONTRIBUTION) || name.startsWith(CONTRIBUTION + ".")) { explicitContributions = true; // We need to have a way to select one or more folders within the webapp as the contributions - String listOfValues = (String)servletContext.getAttribute(name); + String listOfValues = configurator.getInitParameter(name); if (listOfValues != null) { for (String path : parse(listOfValues)) { if ("".equals(path)) { @@ -113,7 +254,7 @@ public class WebAppHelper { } } else if (name.equals(CONTRIBUTIONS) || name.startsWith(CONTRIBUTIONS + ".")) { explicitContributions = true; - String listOfValues = (String)servletContext.getAttribute(name); + String listOfValues = (String)configurator.getInitParameter(name); if (listOfValues != null) { for (String path : parse(listOfValues)) { if ("".equals(path)) { @@ -132,10 +273,19 @@ public class WebAppHelper { } } - URL composite = getResource(servletContext, WEB_COMPOSITE); + String compositeURI = configurator.getInitParameter(COMPOSITE_URI); + if (compositeURI == null) { + compositeURI = getDefaultComposite(configurator); + } + URL composite = getResource(servletContext, compositeURI); if (configuration.getContributions().isEmpty() || (!explicitContributions && composite != null)) { - // TODO: Which path should be the default root - configuration.addContribution(getResource(servletContext, ROOT)); + if ("".equals(configurator.getName())) { + // Add the root of the web application + configuration.addContribution(getResource(servletContext, ROOT)); + } else { + // Add a dummy contribution + configuration.addContribution(URI.create("sca:contributions/" + configurator.getName()), null); + } } if (composite != null) { configuration.getContributions().get(0).addDeploymentComposite(composite); @@ -151,16 +301,16 @@ public class WebAppHelper { } } } - String nodeURI = (String)servletContext.getAttribute(NODE_URI); + String nodeURI = configurator.getInitParameter(NODE_URI); if (nodeURI == null) { - nodeURI = new File(servletContext.getRealPath(ROOT)).getName(); + nodeURI = getResource(servletContext, ROOT).getPath() + configurator.getName(); } configuration.setURI(nodeURI); - String domainURI = (String)servletContext.getAttribute(DOMAIN_URI); + String domainURI = configurator.getInitParameter(DOMAIN_URI); if (domainURI != null) { configuration.setDomainURI(domainURI); } else { - domainURI = servletContext.getInitParameter("org.apache.tuscany.sca.defaultDomainURI"); + domainURI = configurator.getInitParameter("org.apache.tuscany.sca.defaultDomainURI"); if (domainURI != null) { configuration.setDomainURI(getDomainName(domainURI)); configuration.setDomainRegistryURI(domainURI); @@ -170,118 +320,152 @@ public class WebAppHelper { return configuration; } - // TODO: Temp for now to get the old samples working till i clean up all the domain uri/name after the ML discussion. - private static String getDomainName(String configURI) { - String domainName; - if (configURI.startsWith("tuscany:vm:")) { - domainName = configURI.substring("tuscany:vm:".length()); - } else if (configURI.startsWith("tuscany:")) { - int i = configURI.indexOf('?'); - if (i == -1) { - domainName = configURI.substring("tuscany:".length()); + static Configurator getConfigurator(FilterConfig config) { + return new FilterConfigurator(config); + } + + static Configurator getConfigurator(ServletContext context) { + return new ServletContextConfigurator(context); + } + + static Configurator getConfigurator(Servlet context) { + return new ServletConfigurator(context); + } + + /** + * The interface that represents a given scope (Webapp vs Servlet) that provides the configuration of the Tuscany node + */ + public static interface Configurator { + String getInitParameter(String name); + + Enumeration<String> getInitParameterNames(); + + ServletContext getServletContext(); + + void setAttribute(String name, Object value); + + <T> T getAttribute(String name); + + String getName(); + } + + public static class FilterConfigurator implements Configurator { + private FilterConfig config; + + public FilterConfigurator(FilterConfig config) { + super(); + this.config = config; + } + + public String getInitParameter(String name) { + String value = config.getInitParameter(name); + if (value == null) { + return config.getServletContext().getInitParameter(name); } else { - domainName = configURI.substring("tuscany:".length(), i); + return value; } - } else { - domainName = configURI; } - return domainName; + + public Enumeration<String> getInitParameterNames() { + Enumeration<String> names = config.getInitParameterNames(); + if (!names.hasMoreElements()) { + return getServletContext().getInitParameterNames(); + } else { + return names; + } + } + + public ServletContext getServletContext() { + return config.getServletContext(); + } + + public void setAttribute(String name, Object value) { + String prefix = "filter:" + config.getFilterName() + ":"; + getServletContext().setAttribute(prefix + name, value); + } + + public <T> T getAttribute(String name) { + String prefix = "filter:" + config.getFilterName() + ":"; + return (T)getServletContext().getAttribute(prefix + name); + } + + public String getName() { + return ""; + } + } - public synchronized static ServletHost init(final ServletContext servletContext) { - if (host == null) { - try { + public static class ServletContextConfigurator implements Configurator { + private ServletContext context; - String configValue = servletContext.getInitParameter("org.apache.tuscany.sca.config"); - if (configValue != null) { - factory = NodeFactory.newInstance(configValue); - } else { - factory = NodeFactory.newInstance(); - } - - // Add ServletContext as a utility - ExtensionPointRegistry registry = factory.getExtensionPointRegistry(); - UtilityExtensionPoint utilityExtensionPoint = registry.getExtensionPoint(UtilityExtensionPoint.class); - utilityExtensionPoint.addUtility(ServletContext.class, servletContext); - - ServletHostExtensionPoint servletHosts = registry.getExtensionPoint(ServletHostExtensionPoint.class); - servletHosts.setWebApp(true); + public ServletContextConfigurator(ServletContext context) { + super(); + this.context = context; + } - // TODO: why are the init parameters copied to the attributes? - for (Enumeration<?> e = servletContext.getInitParameterNames(); e.hasMoreElements();) { - String name = (String)e.nextElement(); - String value = servletContext.getInitParameter(name); - servletContext.setAttribute(name, value); - } + public String getInitParameter(String name) { + return context.getInitParameter(name); + } - host = getServletHost(servletContext); + public Enumeration<String> getInitParameterNames() { + return context.getInitParameterNames(); + } - } catch (ServletException e) { - throw new RuntimeException(e); - } + public ServletContext getServletContext() { + return context; } - Node node = (Node)servletContext.getAttribute(SCA_NODE_ATTRIBUTE); - if (node == null) { - try { - node = createAndStartNode(servletContext); - } catch (ServletException e) { - throw new RuntimeException(e); - } - servletContext.setAttribute(SCA_NODE_ATTRIBUTE, node); + + public void setAttribute(String name, Object value) { + context.setAttribute(name, value); } - return host; + public <T> T getAttribute(String name) { + return (T)context.getAttribute(name); + } + + public String getName() { + return ""; + } } - private static WebAppServletHost getServletHost(final ServletContext servletContext) throws ServletException { - WebAppServletHost host = getServletHost(factory); - host.init(new ServletConfig() { - public String getInitParameter(String name) { - return servletContext.getInitParameter(name); - } + public static class ServletConfigurator implements Configurator { + private ServletConfig config; - public Enumeration<?> getInitParameterNames() { - return servletContext.getInitParameterNames(); - } + public ServletConfigurator(Servlet servlet) { + super(); + this.config = servlet.getServletConfig(); + } - public ServletContext getServletContext() { - return servletContext; + public String getInitParameter(String name) { + String value = config.getInitParameter(name); + if (value == null) { + return config.getServletContext().getInitParameter(name); + } else { + return value; } + } - public String getServletName() { - return servletContext.getServletContextName(); - } - }); - return host; - } + public Enumeration<String> getInitParameterNames() { + return config.getInitParameterNames(); + } - private static WebAppServletHost getServletHost(NodeFactory factory) { - ExtensionPointRegistry registry = factory.getExtensionPointRegistry(); - return (WebAppServletHost)org.apache.tuscany.sca.host.http.ServletHostHelper.getServletHost(registry); - } + public ServletContext getServletContext() { + return config.getServletContext(); + } - private static Node createAndStartNode(final ServletContext servletContext) throws ServletException { - NodeConfiguration configuration; - try { - configuration = getNodeConfiguration(servletContext); - } catch (IOException e) { - throw new ServletException(e); - } catch (URISyntaxException e) { - throw new ServletException(e); + public void setAttribute(String name, Object value) { + String prefix = "servlet:" + config.getServletName() + ":"; + getServletContext().setAttribute(prefix + name, value); } - Node node = factory.createNode(configuration).start(); - return node; - } - public static void stop(ServletContext servletContext) { - Node node = (Node)servletContext.getAttribute(SCA_NODE_ATTRIBUTE); - if (node != null) { - node.stop(); - servletContext.setAttribute(SCA_NODE_ATTRIBUTE, null); + public <T> T getAttribute(String name) { + String prefix = "servlet:" + config.getServletName() + ":"; + return (T)getServletContext().getAttribute(prefix + name); + } + + public String getName() { + return config.getServletName(); } - } - public static NodeFactory getNodeFactory() { - return factory; } } diff --git a/sca-java-2.x/trunk/modules/host-webapp/src/main/java/org/apache/tuscany/sca/host/webapp/WebAppServletHost.java b/sca-java-2.x/trunk/modules/host-webapp/src/main/java/org/apache/tuscany/sca/host/webapp/WebAppServletHost.java index e6f63681a9..fed2f67893 100644 --- a/sca-java-2.x/trunk/modules/host-webapp/src/main/java/org/apache/tuscany/sca/host/webapp/WebAppServletHost.java +++ b/sca-java-2.x/trunk/modules/host-webapp/src/main/java/org/apache/tuscany/sca/host/webapp/WebAppServletHost.java @@ -306,7 +306,7 @@ public class WebAppServletHost implements ServletHost { } // Close the SCA domain - WebAppHelper.stop(servletContext); + WebAppHelper.destroy(); } public String getContextPath() { |