You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@tomcat.apache.org by ma...@apache.org on 2013/11/26 00:55:31 UTC

svn commit: r1545462 - in /tomcat/tc7.0.x/trunk: ./ java/org/apache/catalina/core/ java/org/apache/catalina/startup/ test/deployment/ test/org/apache/catalina/startup/

Author: markt
Date: Mon Nov 25 23:55:31 2013
New Revision: 1545462

URL: http://svn.apache.org/r1545462
Log:
Backport automatic deployment changes part 19
Add support for a per Context copyXML attribute

Added:
    tomcat/tc7.0.x/trunk/test/deployment/contextCopyXMLFalse.war
      - copied unchanged from r1489812, tomcat/trunk/test/deployment/contextCopyXMLFalse.war
    tomcat/tc7.0.x/trunk/test/deployment/contextCopyXMLTrue.war
      - copied unchanged from r1489812, tomcat/trunk/test/deployment/contextCopyXMLTrue.war
Modified:
    tomcat/tc7.0.x/trunk/   (props changed)
    tomcat/tc7.0.x/trunk/java/org/apache/catalina/core/StandardContext.java
    tomcat/tc7.0.x/trunk/java/org/apache/catalina/startup/HostConfig.java
    tomcat/tc7.0.x/trunk/test/org/apache/catalina/startup/TestHostConfigAutomaticDeployment.java

Propchange: tomcat/tc7.0.x/trunk/
------------------------------------------------------------------------------
  Merged /tomcat/trunk:r1489633,1489648,1489812,1505929

Modified: tomcat/tc7.0.x/trunk/java/org/apache/catalina/core/StandardContext.java
URL: http://svn.apache.org/viewvc/tomcat/tc7.0.x/trunk/java/org/apache/catalina/core/StandardContext.java?rev=1545462&r1=1545461&r2=1545462&view=diff
==============================================================================
--- tomcat/tc7.0.x/trunk/java/org/apache/catalina/core/StandardContext.java (original)
+++ tomcat/tc7.0.x/trunk/java/org/apache/catalina/core/StandardContext.java Mon Nov 25 23:55:31 2013
@@ -524,6 +524,12 @@ public class StandardContext extends Con
 
 
     /**
+     * Context level override for default {@link StandardHost#isCopyXML()}.
+     */
+    private boolean copyXML = false;
+
+
+    /**
      * The default context override flag for this web application.
      */
     private boolean override = false;
@@ -2467,6 +2473,17 @@ public class StandardContext extends Con
 
     }
 
+
+    public boolean getCopyXML() {
+        return copyXML;
+    }
+
+
+    public void setCopyXML(boolean copyXML) {
+        this.copyXML = copyXML;
+    }
+
+
     /**
      * Return the Java class name of the Wrapper implementation used
      * for servlets registered in this Context.

Modified: tomcat/tc7.0.x/trunk/java/org/apache/catalina/startup/HostConfig.java
URL: http://svn.apache.org/viewvc/tomcat/tc7.0.x/trunk/java/org/apache/catalina/startup/HostConfig.java?rev=1545462&r1=1545461&r2=1545462&view=diff
==============================================================================
--- tomcat/tc7.0.x/trunk/java/org/apache/catalina/startup/HostConfig.java (original)
+++ tomcat/tc7.0.x/trunk/java/org/apache/catalina/startup/HostConfig.java Mon Nov 25 23:55:31 2013
@@ -56,6 +56,7 @@ import org.apache.catalina.Lifecycle;
 import org.apache.catalina.LifecycleEvent;
 import org.apache.catalina.LifecycleListener;
 import org.apache.catalina.Manager;
+import org.apache.catalina.core.StandardContext;
 import org.apache.catalina.core.StandardHost;
 import org.apache.catalina.util.ContextName;
 import org.apache.catalina.util.IOTools;
@@ -864,68 +865,25 @@ public class HostConfig
 
         // Checking for a nested /META-INF/context.xml
         JarFile jar = null;
-        JarEntry entry = null;
         InputStream istream = null;
         FileOutputStream fos = null;
         BufferedOutputStream ostream = null;
-        File xml;
-        if (copyXML) {
-            xml = new File(configBase(), cn.getBaseName() + ".xml");
-        } else {
-            xml = new File(appBase(),
-                    cn.getBaseName() + "/META-INF/context.xml");
-        }
-        boolean xmlInWar = false;
 
-        if (deployXML && !xml.exists()) {
+        File xml = new File(appBase(),
+                cn.getBaseName() + "/META-INF/context.xml");
+
+        boolean xmlInWar = false;
+        if (deployXML) {
+            JarEntry entry = null;
             try {
                 jar = new JarFile(war);
                 entry = jar.getJarEntry(Constants.ApplicationContextXml);
                 if (entry != null) {
                     xmlInWar = true;
                 }
-                if ((copyXML || unpackWARs) && xmlInWar) {
-                    istream = jar.getInputStream(entry);
-
-                    fos = new FileOutputStream(xml);
-                    ostream = new BufferedOutputStream(fos, 1024);
-                    byte buffer[] = new byte[1024];
-                    while (true) {
-                        int n = istream.read(buffer);
-                        if (n < 0) {
-                            break;
-                        }
-                        ostream.write(buffer, 0, n);
-                    }
-                    ostream.flush();
-                }
             } catch (IOException e) {
                 /* Ignore */
             } finally {
-                if (ostream != null) {
-                    try {
-                        ostream.close();
-                    } catch (IOException ioe) {
-                        // Ignore
-                    }
-                    ostream = null;
-                }
-                if (fos != null) {
-                    try {
-                        fos.close();
-                    } catch (IOException ioe) {
-                        // Ignore
-                    }
-                    fos = null;
-                }
-                if (istream != null) {
-                    try {
-                        istream.close();
-                    } catch (IOException ioe) {
-                        // Ignore
-                    }
-                    istream = null;
-                }
                 entry = null;
                 if (jar != null) {
                     try {
@@ -938,17 +896,9 @@ public class HostConfig
             }
         }
 
-        DeployedApplication deployedApp = new DeployedApplication(cn.getName(),
-                xml.exists() && deployXML && copyXML);
-
-        // Deploy the application in this WAR file
-        if(log.isInfoEnabled())
-            log.info(sm.getString("hostConfig.deployWar",
-                    war.getAbsolutePath()));
-
         Context context = null;
         try {
-            if (deployXML && xml.exists()) {
+            if (deployXML && xml.exists() && !copyXML) {
                 synchronized (digesterLock) {
                     try {
                         context = (Context) digester.parse(xml);
@@ -964,6 +914,7 @@ public class HostConfig
                 context.setConfigFile(xml.toURI().toURL());
             } else if (deployXML && xmlInWar) {
                 synchronized (digesterLock) {
+                    JarEntry entry = null;
                     try {
                         jar = new JarFile(war);
                         entry =
@@ -1004,7 +955,96 @@ public class HostConfig
             } else {
                 context = (Context) Class.forName(contextClass).newInstance();
             }
+        } catch (Throwable t) {
+            ExceptionUtils.handleThrowable(t);
+            log.error(sm.getString("hostConfig.deployWar.error",
+                    war.getAbsolutePath()), t);
+        } finally {
+            if (context == null) {
+                context = new FailedContext();
+            }
+        }
+
+        boolean copyThisXml = false;
+        if (deployXML) {
+            if (host instanceof StandardHost) {
+                copyThisXml = ((StandardHost) host).isCopyXML();
+            }
+
+            // If Host is using default value Context can override it.
+            if (!copyThisXml && context instanceof StandardContext) {
+                copyThisXml = ((StandardContext) context).getCopyXML();
+            }
 
+            if (xmlInWar && copyThisXml) {
+                // Change location of XML file to config base
+                xml = new File(configBase(), cn.getBaseName() + ".xml");
+                JarEntry entry = null;
+                try {
+                    jar = new JarFile(war);
+                    entry =
+                        jar.getJarEntry(Constants.ApplicationContextXml);
+                    istream = jar.getInputStream(entry);
+
+                    fos = new FileOutputStream(xml);
+                    ostream = new BufferedOutputStream(fos, 1024);
+                    byte buffer[] = new byte[1024];
+                    while (true) {
+                        int n = istream.read(buffer);
+                        if (n < 0) {
+                            break;
+                        }
+                        ostream.write(buffer, 0, n);
+                    }
+                    ostream.flush();
+                } catch (IOException e) {
+                    /* Ignore */
+                } finally {
+                    if (ostream != null) {
+                        try {
+                            ostream.close();
+                        } catch (IOException ioe) {
+                            // Ignore
+                        }
+                        ostream = null;
+                    }
+                    if (fos != null) {
+                        try {
+                            fos.close();
+                        } catch (IOException ioe) {
+                            // Ignore
+                        }
+                        fos = null;
+                    }
+                    if (istream != null) {
+                        try {
+                            istream.close();
+                        } catch (IOException ioe) {
+                            // Ignore
+                        }
+                        istream = null;
+                    }
+                    if (jar != null) {
+                        try {
+                            jar.close();
+                        } catch (IOException ioe) {
+                            // Ignore;
+                        }
+                        jar = null;
+                    }
+                }
+            }
+        }
+
+        DeployedApplication deployedApp = new DeployedApplication(cn.getName(),
+                xml.exists() && deployXML && copyXML);
+
+        // Deploy the application in this WAR file
+        if(log.isInfoEnabled())
+            log.info(sm.getString("hostConfig.deployWar",
+                    war.getAbsolutePath()));
+
+        try {
             // Populate redeploy resources with the WAR file
             deployedApp.redeployResources.put
                 (war.getAbsolutePath(), Long.valueOf(war.lastModified()));
@@ -1114,8 +1154,7 @@ public class HostConfig
         File xml = new File(dir, Constants.ApplicationContextXml);
         File xmlCopy = new File(configBase(), cn.getBaseName() + ".xml");
 
-        DeployedApplication deployedApp = new DeployedApplication(cn.getName(),
-                xml.exists() && deployXML && copyXML);
+        DeployedApplication deployedApp;
 
         try {
             if (deployXML && xml.exists()) {
@@ -1131,7 +1170,14 @@ public class HostConfig
                         digester.reset();
                     }
                 }
-                if (copyXML) {
+
+                boolean copyThisXml = copyXML;
+                if (copyThisXml == false && context instanceof StandardContext) {
+                    // Host is using default value. Context may override it.
+                    copyThisXml = ((StandardContext) context).getCopyXML();
+                }
+
+                if (copyThisXml) {
                     InputStream is = null;
                     OutputStream os = null;
                     try {
@@ -1174,6 +1220,9 @@ public class HostConfig
             log.error(sm.getString("hostConfig.deployDir.error",
                     dir.getAbsolutePath()), t);
         } finally {
+            deployedApp = new DeployedApplication(cn.getName(),
+                    xml.exists() && deployXML && copyXML);
+
             // Fake re-deploy resource to detect if a WAR is added at a later
             // point
             deployedApp.redeployResources.put(dir.getAbsolutePath() + ".war",

Modified: tomcat/tc7.0.x/trunk/test/org/apache/catalina/startup/TestHostConfigAutomaticDeployment.java
URL: http://svn.apache.org/viewvc/tomcat/tc7.0.x/trunk/test/org/apache/catalina/startup/TestHostConfigAutomaticDeployment.java?rev=1545462&r1=1545461&r2=1545462&view=diff
==============================================================================
--- tomcat/tc7.0.x/trunk/test/org/apache/catalina/startup/TestHostConfigAutomaticDeployment.java (original)
+++ tomcat/tc7.0.x/trunk/test/org/apache/catalina/startup/TestHostConfigAutomaticDeployment.java Mon Nov 25 23:55:31 2013
@@ -50,6 +50,10 @@ public class TestHostConfigAutomaticDepl
             new File("test/deployment/context.xml");
     private static final File WAR_XML_SOURCE =
             new File("test/deployment/context.war");
+    private static final File WAR_XML_COPYXML_FALSE_SOURCE =
+            new File("test/deployment/contextCopyXMLFalse.war");
+    private static final File WAR_XML_COPYXML_TRUE_SOURCE =
+            new File("test/deployment/contextCopyXMLTrue.war");
     private static final File WAR_XML_UNPACKWAR_FALSE_SOURCE =
             new File("test/deployment/contextUnpackWARFalse.war");
     private static final File WAR_XML_UNPACKWAR_TRUE_SOURCE =
@@ -1751,6 +1755,89 @@ public class TestHostConfigAutomaticDepl
         }
     }
 
+
+    /*
+     * Test context copyXML setting.
+     * If context.copyXML != Host.copyXML the Host wins.
+     * For external WARs, a context.xml must always already exist
+     */
+    @Test
+    public void testCopyXMLFFF() throws Exception  {
+        doTestCopyXML(false, false, false, false);
+    }
+
+    @Test
+    public void testCopyXMLFFT() throws Exception  {
+        doTestCopyXML(false, false, true, true);
+    }
+
+    @Test
+    public void testCopyXMLFTF() throws Exception  {
+        doTestCopyXML(false, true, false, true);
+    }
+
+    @Test
+    public void testCopyXMLFTT() throws Exception  {
+        doTestCopyXML(false, true, true, true);
+    }
+
+    @Test
+    public void testCopyXMLTFF() throws Exception  {
+        doTestCopyXML(true, false, false, true);
+    }
+
+    @Test
+    public void testCopyXMLTFT() throws Exception  {
+        doTestCopyXML(true, false, true, true);
+    }
+
+    @Test
+    public void testCopyXMLTTF() throws Exception  {
+        doTestCopyXML(true, true, false, true);
+    }
+
+    @Test
+    public void testCopyXMLTTT() throws Exception  {
+        doTestCopyXML(true, true, true, true);
+    }
+
+    private void doTestCopyXML(boolean copyXmlHost, boolean copyXmlWar,
+            boolean external, boolean resultXml) throws Exception {
+
+        Tomcat tomcat = getTomcatInstance();
+        StandardHost host = (StandardHost) tomcat.getHost();
+
+        host.setCopyXML(copyXmlHost);
+
+        tomcat.start();
+
+        File war;
+        if (copyXmlWar) {
+            war = createWar(WAR_XML_COPYXML_TRUE_SOURCE, !external);
+        } else {
+            war = createWar(WAR_XML_COPYXML_FALSE_SOURCE, !external);
+        }
+        if (external) {
+            createXmlInConfigBaseForExternal(war);
+        }
+
+        host.backgroundProcess();
+
+        File xml = new File(getConfigBaseFile(host),
+                APP_NAME.getBaseName() + ".xml");
+        Assert.assertEquals(
+                Boolean.valueOf(resultXml), Boolean.valueOf(xml.isFile()));
+
+        Context context = (Context) host.findChild(APP_NAME.getName());
+        if (external) {
+            Assert.assertEquals(XML_COOKIE_NAME,
+                    context.getSessionCookieName());
+        } else {
+            Assert.assertEquals(WAR_COOKIE_NAME,
+                    context.getSessionCookieName());
+        }
+    }
+
     
     // Static methods to compensate for methods that are present in 8.0.x but
     // not in 7.0.x



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


Re: svn commit: r1545462 - in /tomcat/tc7.0.x/trunk: ./ java/org/apache/catalina/core/ java/org/apache/catalina/startup/ test/deployment/ test/org/apache/catalina/startup/

Posted by Mark Thomas <ma...@apache.org>.
On 26/11/2013 00:05, Konstantin Kolinko wrote:
> 2013/11/26  <ma...@apache.org>:
>> Author: markt
>> Date: Mon Nov 25 23:55:31 2013
>> New Revision: 1545462
>>
>> URL: http://svn.apache.org/r1545462
>> Log:
>> Backport automatic deployment changes part 19
>> Add support for a per Context copyXML attribute
> 
> There is no documentation for this new attribute. It is not mentioned here:
> http://tomcat.apache.org/tomcat-8.0-doc/config/context.html

Fixed.

Mark


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


Re: svn commit: r1545462 - in /tomcat/tc7.0.x/trunk: ./ java/org/apache/catalina/core/ java/org/apache/catalina/startup/ test/deployment/ test/org/apache/catalina/startup/

Posted by Konstantin Kolinko <kn...@gmail.com>.
2013/11/26  <ma...@apache.org>:
> Author: markt
> Date: Mon Nov 25 23:55:31 2013
> New Revision: 1545462
>
> URL: http://svn.apache.org/r1545462
> Log:
> Backport automatic deployment changes part 19
> Add support for a per Context copyXML attribute

There is no documentation for this new attribute. It is not mentioned here:
http://tomcat.apache.org/tomcat-8.0-doc/config/context.html


>
> Added:
>     tomcat/tc7.0.x/trunk/test/deployment/contextCopyXMLFalse.war
>       - copied unchanged from r1489812, tomcat/trunk/test/deployment/contextCopyXMLFalse.war
>     tomcat/tc7.0.x/trunk/test/deployment/contextCopyXMLTrue.war
>       - copied unchanged from r1489812, tomcat/trunk/test/deployment/contextCopyXMLTrue.war
> Modified:
>     tomcat/tc7.0.x/trunk/   (props changed)
>     tomcat/tc7.0.x/trunk/java/org/apache/catalina/core/StandardContext.java
>     tomcat/tc7.0.x/trunk/java/org/apache/catalina/startup/HostConfig.java
>     tomcat/tc7.0.x/trunk/test/org/apache/catalina/startup/TestHostConfigAutomaticDeployment.java
>
> Propchange: tomcat/tc7.0.x/trunk/
> ------------------------------------------------------------------------------
>   Merged /tomcat/trunk:r1489633,1489648,1489812,1505929
>

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