Author: violetagg
Date: Wed Aug 28 08:12:46 2013
New Revision: 1518097

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

Modified:
    tomcat/tc7.0.x/trunk/   (props changed)
    tomcat/tc7.0.x/trunk/java/org/apache/catalina/startup/Tomcat.java
    tomcat/tc7.0.x/trunk/test/org/apache/catalina/startup/TestTomcat.java
    tomcat/tc7.0.x/trunk/webapps/docs/changelog.xml

Propchange: tomcat/tc7.0.x/trunk/
------------------------------------------------------------------------------
  Merged /tomcat/trunk:r1517941

Modified: tomcat/tc7.0.x/trunk/java/org/apache/catalina/startup/Tomcat.java
URL: 
http://svn.apache.org/viewvc/tomcat/tc7.0.x/trunk/java/org/apache/catalina/startup/Tomcat.java?rev=1518097&r1=1518096&r2=1518097&view=diff
==============================================================================
--- tomcat/tc7.0.x/trunk/java/org/apache/catalina/startup/Tomcat.java (original)
+++ tomcat/tc7.0.x/trunk/java/org/apache/catalina/startup/Tomcat.java Wed Aug 
28 08:12:46 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;
 
@@ -187,13 +191,15 @@ public class Tomcat {
         hostname = s;
     }
 
-    /** 
-     * Add a webapp using normal WEB-INF/web.xml if found.
-     * 
-     * @param contextPath
-     * @param baseDir
-     * @return new Context
-     * @throws ServletException 
+    /**
+     * 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 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.
+     *
+     * @throws ServletException
      */
     public Context addWebapp(String contextPath, 
                                      String baseDir) throws ServletException {
@@ -529,7 +535,8 @@ public class Tomcat {
         ctx.setDocBase(path);
 
         ctx.addLifecycleListener(new DefaultWebXmlListener());
-        
+        ctx.setConfigFile(getWebappConfigFile(path, url));
+
         ContextConfig ctxCfg = new ContextConfig();
         ctx.addLifecycleListener(ctxCfg);
         
@@ -676,16 +683,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;
     }
     
     /**
@@ -1054,4 +1065,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/tc7.0.x/trunk/test/org/apache/catalina/startup/TestTomcat.java
URL: 
http://svn.apache.org/viewvc/tomcat/tc7.0.x/trunk/test/org/apache/catalina/startup/TestTomcat.java?rev=1518097&r1=1518096&r2=1518097&view=diff
==============================================================================
--- tomcat/tc7.0.x/trunk/test/org/apache/catalina/startup/TestTomcat.java 
(original)
+++ tomcat/tc7.0.x/trunk/test/org/apache/catalina/startup/TestTomcat.java Wed 
Aug 28 08:12:46 2013
@@ -40,6 +40,7 @@ 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;
 
@@ -49,6 +50,7 @@ import org.apache.catalina.deploy.Contex
 import org.apache.catalina.deploy.ContextResourceLink;
 import org.apache.catalina.realm.GenericPrincipal;
 import org.apache.catalina.realm.RealmBase;
+import org.apache.catalina.core.StandardContext;
 import org.apache.tomcat.util.buf.ByteChunk;
 
 public class TestTomcat extends TomcatBaseTest {
@@ -447,4 +449,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());
+    }
 }

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=1518097&r1=1518096&r2=1518097&view=diff
==============================================================================
--- tomcat/tc7.0.x/trunk/webapps/docs/changelog.xml (original)
+++ tomcat/tc7.0.x/trunk/webapps/docs/changelog.xml Wed Aug 28 08:12:46 2013
@@ -58,6 +58,11 @@
 <section name="Tomcat 7.0.43 (markt)">
   <subsection name="Catalina">
     <changelog>
+      <add>
+        <bug>51526</bug>: <code>o.a.catalina.startup.Tomcat#addWebapp</code>
+        methods now process the web application's 
<code>META-INF/context.xml</code>
+        when it is available in the provided path. (violetagg)
+      </add>
       <fix>
         <bug>55186</bug>: Ensure local name is recycled between requests so IP
         virtual hosting works correctly. (markt)



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

Reply via email to