diff options
Diffstat (limited to 'java')
5 files changed, 238 insertions, 159 deletions
diff --git a/java/sca/modules/host-webapp/src/main/java/org/apache/tuscany/sca/host/webapp/ServletHostHelper.java b/java/sca/modules/host-webapp/src/main/java/org/apache/tuscany/sca/host/webapp/ServletHostHelper.java new file mode 100644 index 0000000000..9cbd9de57f --- /dev/null +++ b/java/sca/modules/host-webapp/src/main/java/org/apache/tuscany/sca/host/webapp/ServletHostHelper.java @@ -0,0 +1,187 @@ +/*
+ * 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.BufferedReader;
+import java.io.File;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.InputStreamReader;
+import java.lang.reflect.Method;
+import java.net.MalformedURLException;
+import java.net.URL;
+import java.util.Collections;
+import java.util.Enumeration;
+import java.util.logging.Logger;
+
+import javax.naming.InitialContext;
+import javax.naming.NamingException;
+import javax.servlet.ServletConfig;
+import javax.servlet.ServletContext;
+import javax.servlet.ServletException;
+
+import org.apache.tuscany.sca.host.http.ServletHost;
+import org.apache.tuscany.sca.node.Contribution;
+import org.apache.tuscany.sca.node.Node;
+import org.apache.tuscany.sca.node.NodeFactory;
+import org.oasisopen.sca.ServiceRuntimeException;
+
+public class ServletHostHelper {
+ private static final Logger logger = Logger.getLogger(ServletHostHelper.class.getName());
+
+ public static final String SCA_NODE_ATTRIBUTE = Node.class.getName();
+
+ public static ServletHost getServletHost() {
+ return WebAppServletHost.getInstance();
+ }
+
+ public static void init(ServletConfig servletConfig) {
+ init(servletConfig.getServletContext());
+ }
+
+ public static void init(final ServletContext servletContext) {
+ if (servletContext.getAttribute(SCA_NODE_ATTRIBUTE) == null) {
+ try {
+ servletContext.setAttribute(SCA_NODE_ATTRIBUTE, createNode(servletContext));
+ 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 servletContext.getServletContextName();
+ }});
+ } catch (ServletException e) {
+ throw new RuntimeException(e);
+ }
+ }
+ }
+
+ private static Node createNode(final ServletContext servletContext) throws ServletException {
+ String contextPath = initContextPath(servletContext);
+ String contributionRoot = getContributionRoot(servletContext);
+ NodeFactory factory = NodeFactory.newInstance();
+ String webComposite = getWebComposite(servletContext);
+ Node node = factory.createNode(contextPath, webComposite, new Contribution(contributionRoot, contributionRoot));
+ node.start();
+ return node;
+ }
+
+ private static String getWebComposite(ServletContext servletContext) {
+ InputStream stream = servletContext.getResourceAsStream("/WEB-INF/web.composite");
+ BufferedReader reader = new BufferedReader(new InputStreamReader(stream));
+
+ StringBuilder sb = new StringBuilder();
+ String s = null;
+ try {
+ while ((s = reader.readLine()) != null) {
+ sb.append(s + "\n");
+ }
+ } catch (IOException e) {
+ throw new ServiceRuntimeException(e);
+ } finally {
+ try {
+ stream.close();
+ } catch (IOException e) {
+ throw new ServiceRuntimeException(e);
+ }
+ }
+
+ return sb.toString();
+ }
+
+ private static String getContributionRoot(ServletContext servletContext) {
+ String contributionRoot = null;
+ try {
+
+ InitialContext ic = new InitialContext();
+ URL repoURL = (URL)ic.lookup("java:comp/env/url/contributions");
+
+ contributionRoot = repoURL.toString();
+
+ } catch (NamingException e) {
+
+ // ignore exception and use default location
+
+ try {
+
+ String root = servletContext.getInitParameter("contributionRoot");
+ if (root == null || root.length() < 1) {
+ root = "/";
+ }
+ URL rootURL = servletContext.getResource(root);
+ if (rootURL.getProtocol().equals("jndi")) {
+ //this is Tomcat case, we should use getRealPath
+ File warRootFile = new File(servletContext.getRealPath(root));
+ contributionRoot = warRootFile.toURI().toString();
+ } else {
+ //this is Jetty case
+ contributionRoot = rootURL.toString();
+ }
+
+ } catch (MalformedURLException mf) {
+ //ignore, pass null
+ }
+ }
+
+ logger.info("contributionRoot: " + contributionRoot);
+ return contributionRoot;
+ }
+
+ /**
+ * Initializes the contextPath
+ * The 2.5 Servlet API has a getter for this, for pre 2.5 Servlet
+ * containers use an init parameter.
+ */
+ @SuppressWarnings("unchecked")
+ private static String initContextPath(ServletContext context) {
+ String contextPath;
+ if (Collections.list(context.getInitParameterNames()).contains("contextPath")) {
+ contextPath = context.getInitParameter("contextPath");
+ } else {
+ try {
+ // Try to get the method anyway since some ServletContext impl has this method even before 2.5
+ Method m = context.getClass().getMethod("getContextPath", new Class[] {});
+ contextPath = (String)m.invoke(context, new Object[] {});
+ } catch (Exception e) {
+ logger.warning("Servlet level is: " + context.getMajorVersion() + "." + context.getMinorVersion());
+ throw new IllegalStateException("'contextPath' init parameter must be set for pre-2.5 servlet container");
+ }
+ }
+ logger.info("ContextPath: " + contextPath);
+ return contextPath;
+ }
+
+ public static void stop(ServletContext servletContext) {
+ Node node = (Node) servletContext.getAttribute(ServletHostHelper.SCA_NODE_ATTRIBUTE);
+ if (node != null) {
+ node.stop();
+ servletContext.setAttribute(ServletHostHelper.SCA_NODE_ATTRIBUTE, null);
+ }
+ }
+}
diff --git a/java/sca/modules/host-webapp/src/main/java/org/apache/tuscany/sca/host/webapp/TuscanyContextListener.java b/java/sca/modules/host-webapp/src/main/java/org/apache/tuscany/sca/host/webapp/TuscanyContextListener.java new file mode 100644 index 0000000000..0f36094311 --- /dev/null +++ b/java/sca/modules/host-webapp/src/main/java/org/apache/tuscany/sca/host/webapp/TuscanyContextListener.java @@ -0,0 +1,40 @@ +/* + * 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.ServletContextEvent; +import javax.servlet.ServletContextListener; + + +/** + * 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) { + ServletHostHelper.init(event.getServletContext()); + } + + public void contextDestroyed(ServletContextEvent event) { + ServletHostHelper.stop(event.getServletContext()); + } + +} diff --git a/java/sca/modules/host-webapp/src/main/java/org/apache/tuscany/sca/host/webapp/TuscanyServletFilter.java b/java/sca/modules/host-webapp/src/main/java/org/apache/tuscany/sca/host/webapp/TuscanyServletFilter.java index cb4369fc01..599d852c0e 100644 --- a/java/sca/modules/host-webapp/src/main/java/org/apache/tuscany/sca/host/webapp/TuscanyServletFilter.java +++ b/java/sca/modules/host-webapp/src/main/java/org/apache/tuscany/sca/host/webapp/TuscanyServletFilter.java @@ -20,22 +20,16 @@ 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; 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.HttpServletRequest; -import org.apache.tuscany.sca.host.http.ServletHost; /** * A Servlet filter that forwards service requests to the Servlets registered with @@ -45,50 +39,16 @@ import org.apache.tuscany.sca.host.http.ServletHost; */ public class TuscanyServletFilter implements Filter { private static final long serialVersionUID = 1L; - //private static final Logger logger = Logger.getLogger(WebAppServletHost.class.getName()); - - private static Map<ClassLoader, ServletHost> servletHosts = - Collections.synchronizedMap(new WeakHashMap<ClassLoader, ServletHost>()); - - // [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()); - } + + private transient ServletContext context; public void init(final FilterConfig config) throws ServletException { - // TODO: must be a better way to get this than using a static - servletHost = new WebAppServletHost(); - servletHosts.put(Thread.currentThread().getContextClassLoader(), servletHost); - - // Initialize the Servlet host - servletHost.init(new ServletConfig() { - public String getInitParameter(String name) { - return config.getInitParameter(name); - } - - public Enumeration getInitParameterNames() { - return config.getInitParameterNames(); - } - - public ServletContext getServletContext() { - return config.getServletContext(); - } - - public String getServletName() { - return config.getFilterName(); - } - }); + context = config.getServletContext(); + ServletHostHelper.init(context); } public void destroy() { - if (servletHost != null) { - servletHost.destroy(); - servletHost = null; - servletHosts.remove(Thread.currentThread().getContextClassLoader()); - } + ServletHostHelper.stop(context); } public void doFilter(ServletRequest request, ServletResponse response, javax.servlet.FilterChain chain) @@ -105,7 +65,7 @@ public class TuscanyServletFilter implements Filter { } // Get a request dispatcher for the Servlet mapped to that path - RequestDispatcher dispatcher = servletHost.getRequestDispatcher(path); + RequestDispatcher dispatcher = ServletHostHelper.getServletHost().getRequestDispatcher(path); if (dispatcher != null) { // Let the dispatcher forward the request to the Servlet diff --git a/java/sca/modules/host-webapp/src/main/java/org/apache/tuscany/sca/host/webapp/WebAppModuleActivator.java b/java/sca/modules/host-webapp/src/main/java/org/apache/tuscany/sca/host/webapp/WebAppModuleActivator.java index 1b39b32d75..7e8933e6aa 100644 --- a/java/sca/modules/host-webapp/src/main/java/org/apache/tuscany/sca/host/webapp/WebAppModuleActivator.java +++ b/java/sca/modules/host-webapp/src/main/java/org/apache/tuscany/sca/host/webapp/WebAppModuleActivator.java @@ -41,7 +41,7 @@ public class WebAppModuleActivator implements ModuleActivator { List<ServletHost> hosts = servletHosts.getServletHosts(); ServletHost host = null; try { - host = TuscanyServletFilter.getServletHost(); + host = ServletHostHelper.getServletHost(); } catch (NoClassDefFoundError e) { // ignore } diff --git a/java/sca/modules/host-webapp/src/main/java/org/apache/tuscany/sca/host/webapp/WebAppServletHost.java b/java/sca/modules/host-webapp/src/main/java/org/apache/tuscany/sca/host/webapp/WebAppServletHost.java index 5808b21b37..feb35c2bce 100644 --- a/java/sca/modules/host-webapp/src/main/java/org/apache/tuscany/sca/host/webapp/WebAppServletHost.java +++ b/java/sca/modules/host-webapp/src/main/java/org/apache/tuscany/sca/host/webapp/WebAppServletHost.java @@ -19,24 +19,15 @@ package org.apache.tuscany.sca.host.webapp; -import java.io.BufferedReader; -import java.io.File; -import java.io.IOException; -import java.io.InputStream; -import java.io.InputStreamReader; -import java.lang.reflect.Method; import java.net.InetAddress; import java.net.MalformedURLException; import java.net.URI; import java.net.URL; import java.net.UnknownHostException; -import java.util.Collections; import java.util.HashMap; import java.util.Map; import java.util.logging.Logger; -import javax.naming.InitialContext; -import javax.naming.NamingException; import javax.servlet.RequestDispatcher; import javax.servlet.Servlet; import javax.servlet.ServletConfig; @@ -45,14 +36,12 @@ import javax.servlet.ServletException; 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. * - * FIXME: using a static singleton seems a big hack but how should it be shared? + * FIXME: TUSCANY-2881: using a static singleton seems a big hack but how should it be shared? * Need some way for TuscanyServlet to pull it out. * * @version $Rev$ $Date$ @@ -65,7 +54,6 @@ public class WebAppServletHost implements ServletHost { private static final WebAppServletHost instance = new WebAppServletHost(); private Map<String, Servlet> servlets; - private Node node; private String contextPath = "/"; private int defaultPortNumber = 8080; private String contributionRoot; @@ -222,15 +210,8 @@ public class WebAppServletHost implements ServletHost { for (String name : tempAttributes.keySet()) { servletContext.setAttribute(name, tempAttributes.get(name)); } - - if (servletContext.getAttribute(SCA_NODE_ATTRIBUTE) == null) { - initContextPath(config); - contributionRoot = getContributionRoot(servletContext); - NodeFactory factory = NodeFactory.newInstance(); - node = factory.createNode(contextPath, getWebComposite(servletContext), new Contribution(contributionRoot, contributionRoot)); - node.start(); - servletContext.setAttribute(SCA_NODE_ATTRIBUTE, node); - } + + ServletHostHelper.init(servletContext); // Initialize the registered Servlets for (Servlet servlet : servlets.values()) { @@ -239,93 +220,6 @@ public class WebAppServletHost implements ServletHost { } - protected String getWebComposite(ServletContext servletContext) throws ServletException { - InputStream stream = servletContext.getResourceAsStream("/WEB-INF/web.composite"); - BufferedReader reader = new BufferedReader(new InputStreamReader(stream)); - - StringBuilder sb = new StringBuilder(); - String s = null; - try { - while ((s = reader.readLine()) != null) { - sb.append(s + "\n"); - } - } catch (IOException e) { - throw new ServletException(e); - } finally { - try { - stream.close(); - } catch (IOException e) { - throw new ServletException(e); - } - } - - return sb.toString(); - } - - protected String getContributionRoot(ServletContext servletContext) { - String contributionRoot = null; - try { - - InitialContext ic = new InitialContext(); - URL repoURL = (URL)ic.lookup("java:comp/env/url/contributions"); - - contributionRoot = repoURL.toString(); - - } catch (NamingException e) { - - // ignore exception and use default location - - try { - - String root = servletContext.getInitParameter("contributionRoot"); - if (root == null || root.length() < 1) { - root = "/"; - } - URL rootURL = servletContext.getResource(root); - if (rootURL.getProtocol().equals("jndi")) { - //this is Tomcat case, we should use getRealPath - File warRootFile = new File(servletContext.getRealPath(root)); - contributionRoot = warRootFile.toURI().toString(); - } else { - //this is Jetty case - contributionRoot = rootURL.toString(); - } - - } catch (MalformedURLException mf) { - //ignore, pass null - } - } - - logger.info("contributionRoot: " + contributionRoot); - return contributionRoot; - } - - /** - * Initializes the contextPath - * The 2.5 Servlet API has a getter for this, for pre 2.5 Servlet - * containers use an init parameter. - */ - @SuppressWarnings("unchecked") - public void initContextPath(ServletConfig config) { - - if (Collections.list(config.getInitParameterNames()).contains("contextPath")) { - contextPath = config.getInitParameter("contextPath"); - } else { - // The getContextPath() is introduced since Servlet 2.5 - ServletContext context = config.getServletContext(); - try { - // Try to get the method anyway since some ServletContext impl has this method even before 2.5 - Method m = context.getClass().getMethod("getContextPath", new Class[] {}); - contextPath = (String)m.invoke(context, new Object[] {}); - } catch (Exception e) { - logger.warning("Servlet level is: " + context.getMajorVersion() + "." + context.getMinorVersion()); - throw new IllegalStateException("'contextPath' init parameter must be set for pre-2.5 servlet container"); - } - } - - logger.info("ContextPath: " + contextPath); - } - void destroy() { // Destroy the registered Servlets @@ -334,9 +228,7 @@ public class WebAppServletHost implements ServletHost { } // Close the SCA domain - if (node != null) { - node.stop(); - } + ServletHostHelper.stop(servletContext); } public String getContextPath() { |