You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@tomcat.apache.org by vi...@apache.org on 2013/08/27 21:22:48 UTC

svn commit: r1517941 - in /tomcat/trunk: java/org/apache/catalina/startup/Tomcat.java test/org/apache/catalina/startup/TestTomcat.java

Author: violetagg
Date: Tue Aug 27 19:22:47 2013
New Revision: 1517941

URL: http://svn.apache.org/r1517941
Log:
Fix https://issues.apache.org/bugzilla/show_bug.cgi?id=51526
o.a.catalina.startup.Tomcat.addWebapp(...) will process web application's context.xml when it is presented in the provided baseDir.

Modified:
    tomcat/trunk/java/org/apache/catalina/startup/Tomcat.java
    tomcat/trunk/test/org/apache/catalina/startup/TestTomcat.java

Modified: tomcat/trunk/java/org/apache/catalina/startup/Tomcat.java
URL: http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/catalina/startup/Tomcat.java?rev=1517941&r1=1517940&r2=1517941&view=diff
==============================================================================
--- tomcat/trunk/java/org/apache/catalina/startup/Tomcat.java (original)
+++ tomcat/trunk/java/org/apache/catalina/startup/Tomcat.java Tue Aug 27 19:22:47 2013
@@ -18,12 +18,16 @@ package org.apache.catalina.startup;
 
 import java.io.File;
 import java.io.IOException;
+import java.net.MalformedURLException;
+import java.net.URL;
 import java.security.Principal;
 import java.util.ArrayList;
 import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
 import java.util.Stack;
+import java.util.jar.JarEntry;
+import java.util.jar.JarFile;
 import java.util.logging.Level;
 import java.util.logging.Logger;
 
@@ -181,8 +185,8 @@ public class Tomcat {
     /**
      * This is equivalent to adding a web application to Tomcat's webapps
      * directory. The equivalent of the default web.xml will be applied  to the
-     * web application and any WEB-INF/web.xml packaged with the application
-     * will be processed normally. Normal web fragment and
+     * web application and any WEB-INF/web.xml and META-INF/context.xml packaged
+     * with the application will be processed normally. Normal web fragment and
      * {@link javax.servlet.ServletContainerInitializer} processing will be
      * applied.
      *
@@ -525,6 +529,7 @@ public class Tomcat {
         ctx.setPath(url);
         ctx.setDocBase(path);
         ctx.addLifecycleListener(new DefaultWebXmlListener());
+        ctx.setConfigFile(getWebappConfigFile(path, url));
 
         ContextConfig ctxCfg = new ContextConfig();
         ctx.addLifecycleListener(ctxCfg);
@@ -668,16 +673,20 @@ public class Tomcat {
     }
 
     private void silence(Host host, String ctx) {
-        String base = "org.apache.catalina.core.ContainerBase.[default].[";
+        Logger.getLogger(getLoggerName(host, ctx)).setLevel(Level.WARNING);
+    }
+
+    private String getLoggerName(Host host, String ctx) {
+        String loggerName = "org.apache.catalina.core.ContainerBase.[default].[";
         if (host == null) {
-            base += getHost().getName();
+            loggerName += getHost().getName();
         } else {
-            base += host.getName();
+            loggerName += host.getName();
         }
-        base += "].[";
-        base += ctx;
-        base += "]";
-        Logger.getLogger(base).setLevel(Level.WARNING);
+        loggerName += "].[";
+        loggerName += ctx;
+        loggerName += "]";
+        return loggerName;
     }
 
     /**
@@ -1059,4 +1068,52 @@ public class Tomcat {
         "z", "application/x-compress",
         "zip", "application/zip"
     };
+
+    protected URL getWebappConfigFile(String path, String url) {
+        File docBase = new File(path);
+        if (docBase.isDirectory()) {
+            return getWebappConfigFileFromDirectory(docBase, url);
+        } else {
+            return getWebappConfigFileFromJar(docBase, url);
+        }
+    }
+
+    private URL getWebappConfigFileFromDirectory(File docBase, String url) {
+        URL result = null;
+        File webAppContextXml = new File(docBase, Constants.ApplicationContextXml);
+        if (webAppContextXml.exists()) {
+            try {
+                result = webAppContextXml.toURI().toURL();
+            } catch (MalformedURLException e) {
+                Logger.getLogger(getLoggerName(getHost(), url)).log(Level.WARNING,
+                        "Unable to determine web application context.xml " + docBase, e);
+            }
+        }
+        return result;
+    }
+
+    private URL getWebappConfigFileFromJar(File docBase, String url) {
+        URL result = null;
+        JarFile jar = null;
+        try {
+            jar = new JarFile(docBase);
+            JarEntry entry = jar.getJarEntry(Constants.ApplicationContextXml);
+            if (entry != null) {
+                result = new URL("jar:" + docBase.toURI().toString() + "!/"
+                        + Constants.ApplicationContextXml);
+            }
+        } catch (IOException e) {
+            Logger.getLogger(getLoggerName(getHost(), url)).log(Level.WARNING,
+                    "Unable to determine web application context.xml " + docBase, e);
+        } finally {
+            if (jar != null) {
+                try {
+                    jar.close();
+                } catch (IOException e) {
+                    // ignore
+                }
+            }
+        }
+        return result;
+    }
 }

Modified: tomcat/trunk/test/org/apache/catalina/startup/TestTomcat.java
URL: http://svn.apache.org/viewvc/tomcat/trunk/test/org/apache/catalina/startup/TestTomcat.java?rev=1517941&r1=1517940&r2=1517941&view=diff
==============================================================================
--- tomcat/trunk/test/org/apache/catalina/startup/TestTomcat.java (original)
+++ tomcat/trunk/test/org/apache/catalina/startup/TestTomcat.java Tue Aug 27 19:22:47 2013
@@ -35,11 +35,13 @@ import javax.servlet.http.HttpServletRes
 import javax.servlet.http.HttpSession;
 
 import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
 import static org.junit.Assert.assertNull;
 import static org.junit.Assert.assertTrue;
 
 import org.junit.Test;
 
+import org.apache.catalina.core.StandardContext;
 import org.apache.tomcat.util.buf.ByteChunk;
 import org.apache.tomcat.util.descriptor.web.ApplicationListener;
 import org.apache.tomcat.util.descriptor.web.ContextEnvironment;
@@ -409,4 +411,41 @@ public class TestTomcat extends TomcatBa
 
         assertEquals(1, initCount.getCallCount());
     }
+
+    @Test
+    public void testGetWebappConfigFileFromDirectory() {
+        Tomcat tomcat = new Tomcat();
+        assertNotNull(tomcat.getWebappConfigFile("test/deployment/dirContext", ""));
+    }
+
+    @Test
+    public void testGetWebappConfigFileFromDirectoryNegative() {
+        Tomcat tomcat = new Tomcat();
+        assertNull(tomcat.getWebappConfigFile("test/deployment/dirNoContext", ""));
+    }
+
+    @Test
+    public void testGetWebappConfigFileFromJar() {
+        Tomcat tomcat = new Tomcat();
+        assertNotNull(tomcat.getWebappConfigFile("test/deployment/context.war", ""));
+    }
+
+    @Test
+    public void testGetWebappConfigFileFromJarNegative() {
+        Tomcat tomcat = new Tomcat();
+        assertNull(tomcat.getWebappConfigFile("test/deployment/noContext.war", ""));
+    }
+
+    @Test
+    public void testBug51526() throws Exception {
+        Tomcat tomcat = getTomcatInstance();
+
+        File appFile = new File("test/deployment/context.war");
+        StandardContext context = (StandardContext) tomcat.addWebapp(null, "/test",
+                appFile.getAbsolutePath());
+
+        tomcat.start();
+
+        assertEquals("WAR_CONTEXT", context.getSessionCookieName());
+    }
 }



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