diff options
Diffstat (limited to 'sca-java-2.x/trunk/modules/host-webapp/src/main/java/org/apache/tuscany/sca/host/webapp/WebAppHelper.java')
-rw-r--r-- | sca-java-2.x/trunk/modules/host-webapp/src/main/java/org/apache/tuscany/sca/host/webapp/WebAppHelper.java | 398 |
1 files changed, 291 insertions, 107 deletions
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; } } |