Author: markt
Date: Mon Dec  1 15:19:56 2014
New Revision: 1642702

URL: http://svn.apache.org/r1642702
Log:
Fix https://issues.apache.org/bugzilla/show_bug.cgi?id=57190
Fix ServletContext.getContext() when parallel deployment is in use.

Modified:
    tomcat/tc7.0.x/trunk/   (props changed)
    tomcat/tc7.0.x/trunk/java/org/apache/catalina/core/ApplicationContext.java
    
tomcat/tc7.0.x/trunk/test/org/apache/catalina/core/TestApplicationContext.java
    tomcat/tc7.0.x/trunk/webapps/docs/changelog.xml

Propchange: tomcat/tc7.0.x/trunk/
------------------------------------------------------------------------------
  Merged /tomcat/trunk:r1642679,1642697,1642699
  Merged /tomcat/tc8.0.x/trunk:r1642698,1642701

Modified: 
tomcat/tc7.0.x/trunk/java/org/apache/catalina/core/ApplicationContext.java
URL: 
http://svn.apache.org/viewvc/tomcat/tc7.0.x/trunk/java/org/apache/catalina/core/ApplicationContext.java?rev=1642702&r1=1642701&r2=1642702&view=diff
==============================================================================
--- tomcat/tc7.0.x/trunk/java/org/apache/catalina/core/ApplicationContext.java 
(original)
+++ tomcat/tc7.0.x/trunk/java/org/apache/catalina/core/ApplicationContext.java 
Mon Dec  1 15:19:56 2014
@@ -60,7 +60,6 @@ import org.apache.catalina.Container;
 import org.apache.catalina.Context;
 import org.apache.catalina.Engine;
 import org.apache.catalina.Globals;
-import org.apache.catalina.Host;
 import org.apache.catalina.LifecycleState;
 import org.apache.catalina.Service;
 import org.apache.catalina.Wrapper;
@@ -270,16 +269,31 @@ public class ApplicationContext
 
         Context child = null;
         try {
-            Host host = (Host) context.getParent();
-            String mapuri = uri;
-            while (true) {
-                child = (Context) host.findChild(mapuri);
-                if (child != null)
-                    break;
-                int slash = mapuri.lastIndexOf('/');
-                if (slash < 0)
-                    break;
-                mapuri = mapuri.substring(0, slash);
+            // Look for an exact match
+            Container host = context.getParent();
+            child = (Context) host.findChild(uri);
+
+            // Remove any version information and use the mapper
+            if (child == null) {
+                int i = uri.indexOf("##");
+                if (i > -1) {
+                    uri = uri.substring(0, i);
+                }
+                // Note: This could be more efficient with a dedicated Mapper
+                //       method but such an implementation would require some
+                //       refactoring of the Mapper to avoid copy/paste of
+                //       existing code.
+                MessageBytes hostMB = MessageBytes.newInstance();
+                hostMB.setString(host.getName());
+
+                MessageBytes pathMB = MessageBytes.newInstance();
+                pathMB.setString(uri);
+
+                MappingData mappingData = new MappingData();
+                ((Engine) 
host.getParent()).getService().findConnectors()[0].getMapper().map(
+                        hostMB, pathMB, null, mappingData);
+
+                child = (Context) mappingData.context;
             }
         } catch (Throwable t) {
             ExceptionUtils.handleThrowable(t);

Modified: 
tomcat/tc7.0.x/trunk/test/org/apache/catalina/core/TestApplicationContext.java
URL: 
http://svn.apache.org/viewvc/tomcat/tc7.0.x/trunk/test/org/apache/catalina/core/TestApplicationContext.java?rev=1642702&r1=1642701&r2=1642702&view=diff
==============================================================================
--- 
tomcat/tc7.0.x/trunk/test/org/apache/catalina/core/TestApplicationContext.java 
(original)
+++ 
tomcat/tc7.0.x/trunk/test/org/apache/catalina/core/TestApplicationContext.java 
Mon Dec  1 15:19:56 2014
@@ -17,16 +17,26 @@
 package org.apache.catalina.core;
 
 import java.io.File;
+import java.io.IOException;
+import java.io.PrintWriter;
 
 import javax.servlet.Filter;
 import javax.servlet.Servlet;
 import javax.servlet.ServletContext;
+import javax.servlet.ServletException;
+import javax.servlet.http.HttpServlet;
+import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;
 
 import org.junit.Assert;
 import org.junit.Test;
 
+import org.apache.catalina.Context;
+import org.apache.catalina.Lifecycle;
+import org.apache.catalina.LifecycleEvent;
+import org.apache.catalina.LifecycleListener;
 import org.apache.catalina.startup.Tomcat;
+import org.apache.catalina.startup.Tomcat.FixContextListener;
 import org.apache.catalina.startup.TomcatBaseTest;
 import org.apache.tomcat.util.buf.ByteChunk;
 
@@ -134,4 +144,87 @@ public class TestApplicationContext exte
         getTomcatInstance().start();
         getServletContext().setInitParameter("name", "value");
     }
+
+
+    /*
+     * Cross-context requests with parallel deployment
+     */
+    @Test
+    public void testBug57190() throws Exception {
+        Tomcat tomcat = getTomcatInstance();
+
+        Context foo1 = new StandardContext();
+        foo1.setName("/foo##1");
+        foo1.setPath("/foo");
+        foo1.setWebappVersion("1");
+        foo1.setDocBase(System.getProperty("java.io.tmpdir"));
+        foo1.addLifecycleListener(new FixContextListener());
+        foo1.addLifecycleListener(new SetIdListener("foo1"));
+        tomcat.getHost().addChild(foo1);
+
+        Context foo2 = new StandardContext();
+        foo2.setName("/foo##2");
+        foo2.setPath("/foo");
+        foo2.setWebappVersion("2");
+        foo2.setDocBase(System.getProperty("java.io.tmpdir"));
+        foo2.addLifecycleListener(new FixContextListener());
+        foo2.addLifecycleListener(new SetIdListener("foo2"));
+        tomcat.getHost().addChild(foo2);
+
+        Context bar = tomcat.addContext("/bar", 
System.getProperty("java.io.tmpdir"));
+        bar.addLifecycleListener(new SetIdListener("bar"));
+
+        Context ctx = tomcat.addContext("", 
System.getProperty("java.io.tmpdir"));
+        ctx.setCrossContext(true);
+
+        Tomcat.addServlet(ctx, "Bug57190Servlet", new Bug57190Servlet());
+        ctx.addServletMapping("/", "Bug57190Servlet");
+
+        tomcat.start();
+
+        ByteChunk res = getUrl("http://localhost:"; + getPort() + "/");
+        String body = res.toString();
+
+        Assert.assertTrue(body, body.contains("01-bar"));
+        Assert.assertTrue(body, body.contains("02-foo2"));
+        Assert.assertTrue(body, body.contains("03-foo1"));
+        Assert.assertTrue(body, body.contains("04-foo2"));
+        Assert.assertTrue(body, body.contains("05-foo2"));
+    }
+
+
+    private static class Bug57190Servlet extends HttpServlet {
+
+        private static final long serialVersionUID = 1L;
+
+        @Override
+        protected void doGet(HttpServletRequest req, HttpServletResponse resp)
+                throws ServletException, IOException {
+            resp.setContentType("text/plain");
+            PrintWriter pw = resp.getWriter();
+            ServletContext sc = req.getServletContext();
+            pw.println("01-" + sc.getContext("/bar").getInitParameter("id"));
+            pw.println("02-" + sc.getContext("/foo").getInitParameter("id"));
+            pw.println("03-" + 
sc.getContext("/foo##1").getInitParameter("id"));
+            pw.println("04-" + 
sc.getContext("/foo##2").getInitParameter("id"));
+            pw.println("05-" + 
sc.getContext("/foo##3").getInitParameter("id"));
+        }
+    }
+
+
+    private static class SetIdListener implements LifecycleListener {
+
+        private final String id;
+
+        public SetIdListener(String id) {
+            this.id = id;
+        }
+
+        @Override
+        public void lifecycleEvent(LifecycleEvent event) {
+            if (Lifecycle.CONFIGURE_START_EVENT.equals(event.getType())) {
+                ((Context) 
event.getSource()).getServletContext().setInitParameter("id", id);
+            }
+        }
+    }
 }

Modified: tomcat/tc7.0.x/trunk/webapps/docs/changelog.xml
URL: 
http://svn.apache.org/viewvc/tomcat/tc7.0.x/trunk/webapps/docs/changelog.xml?rev=1642702&r1=1642701&r2=1642702&view=diff
==============================================================================
--- tomcat/tc7.0.x/trunk/webapps/docs/changelog.xml (original)
+++ tomcat/tc7.0.x/trunk/webapps/docs/changelog.xml Mon Dec  1 15:19:56 2014
@@ -67,6 +67,11 @@
         requests that use an HTTP method defined in RFC 7231. (markt)
       </fix>
       <fix>
+        <bug>57190</bug>: Fix <code>ServletContext.getContext(String)</code>
+        when parallel deployment is used so that the correct ServletContext is
+        returned. (markt) 
+      </fix>
+      <fix>
         <bug>57208</bug>: Prevent NPE in JNDI Realm when no results are found
         in a directory context for a user with specified user name. Based on
         a patch provided by Jason McIntosh. (violetagg)



---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscr...@tomcat.apache.org
For additional commands, e-mail: dev-h...@tomcat.apache.org

Reply via email to