You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tuscany.apache.org by lr...@apache.org on 2009/10/18 08:19:52 UTC

svn commit: r826364 - /tuscany/java/sca/modules/host-webapp/src/main/java/org/apache/tuscany/sca/host/webapp/WebAppServletHost.java

Author: lresende
Date: Sun Oct 18 06:19:52 2009
New Revision: 826364

URL: http://svn.apache.org/viewvc?rev=826364&view=rev
Log:
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)

Modified:
    tuscany/java/sca/modules/host-webapp/src/main/java/org/apache/tuscany/sca/host/webapp/WebAppServletHost.java

Modified: tuscany/java/sca/modules/host-webapp/src/main/java/org/apache/tuscany/sca/host/webapp/WebAppServletHost.java
URL: http://svn.apache.org/viewvc/tuscany/java/sca/modules/host-webapp/src/main/java/org/apache/tuscany/sca/host/webapp/WebAppServletHost.java?rev=826364&r1=826363&r2=826364&view=diff
==============================================================================
--- tuscany/java/sca/modules/host-webapp/src/main/java/org/apache/tuscany/sca/host/webapp/WebAppServletHost.java (original)
+++ tuscany/java/sca/modules/host-webapp/src/main/java/org/apache/tuscany/sca/host/webapp/WebAppServletHost.java Sun Oct 18 06:19:52 2009
@@ -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 @@
         }
         
         ServletHostHelper.init(servletContext);
+        
+        initContextPath(config);
 
         // Initialize the registered Servlets
         for (Servlet servlet : servlets.values()) {
@@ -217,6 +221,32 @@
         
     }
     
+    /**
+     * 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