Author: markt
Date: Tue May 29 13:11:51 2012
New Revision: 1343709

URL: http://svn.apache.org/viewvc?rev=1343709&view=rev
Log:
Fix https://issues.apache.org/bugzilla/show_bug.cgi?id=53081
Do not always cache resources loaded by the web application class loader

Modified:
    tomcat/tc7.0.x/trunk/   (props changed)
    tomcat/tc7.0.x/trunk/java/org/apache/catalina/loader/WebappClassLoader.java
    tomcat/tc7.0.x/trunk/webapps/docs/changelog.xml

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

Modified: 
tomcat/tc7.0.x/trunk/java/org/apache/catalina/loader/WebappClassLoader.java
URL: 
http://svn.apache.org/viewvc/tomcat/tc7.0.x/trunk/java/org/apache/catalina/loader/WebappClassLoader.java?rev=1343709&r1=1343708&r2=1343709&view=diff
==============================================================================
--- tomcat/tc7.0.x/trunk/java/org/apache/catalina/loader/WebappClassLoader.java 
(original)
+++ tomcat/tc7.0.x/trunk/java/org/apache/catalina/loader/WebappClassLoader.java 
Tue May 29 13:11:51 2012
@@ -2945,6 +2945,7 @@ public class WebappClassLoader
 
         int contentLength = -1;
         InputStream binaryStream = null;
+        boolean isClassResource = path.endsWith(".class");
 
         int jarFilesLength = jarFiles.length;
         int repositoriesLength = repositories.length;
@@ -3146,7 +3147,20 @@ public class WebappClassLoader
                     return null;
                 }
 
-                if (binaryStream != null) {
+                /* Only cache the binary content if there is some content
+                 * available and either:
+                 * a) It is a class file since the binary content is only 
cached
+                 *    until the class has been loaded
+                 *    or
+                 * b) The file needs conversion to address encoding issues (see
+                 *    below)
+                 *
+                 * In all other cases do not cache the content to prevent
+                 * excessive memory usage if large resources are present (see
+                 * https://issues.apache.org/bugzilla/show_bug.cgi?id=53081).
+                 */
+                if (binaryStream != null &&
+                        (isClassResource || fileNeedConvert)) {
 
                     byte[] binaryContent = new byte[contentLength];
 
@@ -3248,8 +3262,15 @@ public class WebappClassLoader
         if (entry != null) {
             if (entry.binaryContent != null)
                 return new ByteArrayInputStream(entry.binaryContent);
+            else {
+                try {
+                    return entry.source.openStream();
+                } catch (IOException ioe) {
+                    // Ignore
+                }
+            }
         }
-        return (null);
+        return null;
 
     }
 

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=1343709&r1=1343708&r2=1343709&view=diff
==============================================================================
--- tomcat/tc7.0.x/trunk/webapps/docs/changelog.xml (original)
+++ tomcat/tc7.0.x/trunk/webapps/docs/changelog.xml Tue May 29 13:11:51 2012
@@ -120,6 +120,13 @@
         Enable host's xmlBase attribute in ContextConfig. (kfujino)
       </fix>
       <fix>
+        <bug>53081</bug>: Do not always cache resources loaded by the web
+        application class loader since they may be very large which in turn
+        could trigger a memory leak. Calls to the web application class
+        loader&apos;s <code>getResourceAsStream()</code> method will now access
+        the resource directly rather than via the cache in most cases. (markt)
+      </fix>
+      <fix>
         <bug>53090</bug>: Include superclasses when considering injection
         targets. Patch provided by Borislav Kapukaranov. (markt)
       </fix>



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

Reply via email to