diff options
author | lresende <lresende@13f79535-47bb-0310-9956-ffa450edef68> | 2009-10-18 06:19:52 +0000 |
---|---|---|
committer | lresende <lresende@13f79535-47bb-0310-9956-ffa450edef68> | 2009-10-18 06:19:52 +0000 |
commit | b33733526f64d4b035c09cb78808abe2671dae00 (patch) | |
tree | 9f329316af22c33097771761fa869c8b2b810fbe /java/sca | |
parent | 08f376c0fe3fd893882de787bf6c263ecb8599f8 (diff) |
Properly setting contextPath to avoid uri for service being available only with duplicated context root in uri (e.g localhost:8080/context root/context root/service)
git-svn-id: http://svn.us.apache.org/repos/asf/tuscany@826364 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to '')
-rw-r--r-- | java/sca/modules/host-webapp/src/main/java/org/apache/tuscany/sca/host/webapp/WebAppServletHost.java | 30 |
1 files changed, 30 insertions, 0 deletions
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 7617169b9b..a90674e8e7 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,11 +19,13 @@ package org.apache.tuscany.sca.host.webapp; +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; @@ -209,6 +211,8 @@ public class WebAppServletHost implements ServletHost { } ServletHostHelper.init(servletContext); + + initContextPath(config); // Initialize the registered Servlets for (Servlet servlet : servlets.values()) { @@ -217,6 +221,32 @@ public class WebAppServletHost implements ServletHost { } + /** + * 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 |