Author: markt
Date: Thu Oct 29 12:03:48 2009
New Revision: 830908

URL: http://svn.apache.org/viewvc?rev=830908&view=rev
Log:
Make leak prevention listener more configurable

Modified:
    
tomcat/trunk/java/org/apache/catalina/core/JreMemoryLeakPreventionListener.java
    tomcat/trunk/webapps/docs/config/listeners.xml

Modified: 
tomcat/trunk/java/org/apache/catalina/core/JreMemoryLeakPreventionListener.java
URL: 
http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/catalina/core/JreMemoryLeakPreventionListener.java?rev=830908&r1=830907&r2=830908&view=diff
==============================================================================
--- 
tomcat/trunk/java/org/apache/catalina/core/JreMemoryLeakPreventionListener.java 
(original)
+++ 
tomcat/trunk/java/org/apache/catalina/core/JreMemoryLeakPreventionListener.java 
Thu Oct 29 12:03:48 2009
@@ -51,7 +51,30 @@
         LogFactory.getLog(JreMemoryLeakPreventionListener.class);
     protected static final StringManager sm =
         StringManager.getManager(Constants.Package);
-    
+
+    /**
+     * Protect against the memory leak caused when the first call to
+     * <code>sun.awt.AppContext.getAppContext()</code> is triggered by a web
+     * application. Defaults to <code>true</code>.
+     */
+    protected boolean appContextProtection = true;
+    public boolean isAppContextProtection() { return appContextProtection; }
+    public void setAppContextProtection(boolean appContextProtection) {
+        this.appContextProtection = appContextProtection;
+    }
+
+    /**
+     * Protect against resources being read for JAR files and, as a 
side-effect,
+     * the JAR file becoming locked. Note this disables caching for all
+     * {...@link URLConnection}s, regardless of type. Defaults to
+     * <code>true</code>.
+     */
+    protected boolean urlCacheProtection = true;
+    public boolean isUrlCacheProtection() { return urlCacheProtection; }
+    public void setUrlCacheProtection(boolean urlCacheProtection) {
+        this.urlCacheProtection = urlCacheProtection;
+    }
+
     @Override
     public void lifecycleEvent(LifecycleEvent event) {
         // Initialise these classes when Tomcat starts
@@ -71,7 +94,9 @@
             // Trigger a call to sun.awt.AppContext.getAppContext(). This will
             // pin the common class loader in memory but that shouldn't be an
             // issue.
-            ImageIO.getCacheDirectory();
+            if (appContextProtection) {
+                ImageIO.getCacheDirectory();
+            }
             
             /*
              * Several components end up opening JarURLConnections without 
first
@@ -84,19 +109,21 @@
              * - javax.xml.bind.JAXBContext.newInstance()
              */
             
-            // Set the default JAR URL caching policy to not to cache
-            try {
-                // Doesn't matter that this JAR doesn't exist - just as long as
-                // the URL is well-formed
-                URL url = new URL("jar:file://dummy.jar!/");
-                URLConnection uConn = url.openConnection();
-                uConn.setDefaultUseCaches(false);
-            } catch (MalformedURLException e) {
-                log.error(sm.getString(
-                        "jreLeakListener.jarUrlConnCacheFail"), e);
-            } catch (IOException e) {
-                log.error(sm.getString(
-                "jreLeakListener.jarUrlConnCacheFail"), e);
+            // Set the default URL caching policy to not to cache
+            if (urlCacheProtection) {
+                try {
+                    // Doesn't matter that this JAR doesn't exist - just as 
long as
+                    // the URL is well-formed
+                    URL url = new URL("jar:file://dummy.jar!/");
+                    URLConnection uConn = url.openConnection();
+                    uConn.setDefaultUseCaches(false);
+                } catch (MalformedURLException e) {
+                    log.error(sm.getString(
+                            "jreLeakListener.jarUrlConnCacheFail"), e);
+                } catch (IOException e) {
+                    log.error(sm.getString(
+                    "jreLeakListener.jarUrlConnCacheFail"), e);
+                }
             }
         }
     }

Modified: tomcat/trunk/webapps/docs/config/listeners.xml
URL: 
http://svn.apache.org/viewvc/tomcat/trunk/webapps/docs/config/listeners.xml?rev=830908&r1=830907&r2=830908&view=diff
==============================================================================
--- tomcat/trunk/webapps/docs/config/listeners.xml (original)
+++ tomcat/trunk/webapps/docs/config/listeners.xml Thu Oct 29 12:03:48 2009
@@ -227,19 +227,36 @@
     leak if a web application class loader happens to be the context class
     loader at the time. The work-around is to initialise these singletons when
     this listener starts as Tomcat's common class loader is the context class
-    loader at that time.</p>
+    loader at that time. It also provides work-arounds for known issues that
+    can result in locked JAR files.</p>
     
-    <p>Currently the <strong>JRE Memory Leak Prevention Listener</strong>
-    provides work-arounds for the following:</p>
-    <ul>
-      <li><code>sun.awt.AppContext.getAppContext()</code></li>
-    </ul>
-
     <p>This listener must only be nested within <a 
href="server.html">Server</a>
     elements.</p>
 
-    <p>No additional attributes are supported by the <strong>JRE Memory Leak
-    Prevention Listener</strong>.</p>
+    <p>The following additional attributes are supported by the <strong>JRE
+    Memory Leak Prevention Listener</strong>:</p>
+
+    <attributes>
+
+      <attribute name="appContextProtection" required="false">
+        <p>Enables protection so that calls to
+        <code>sun.awt.AppContext.getAppContext()</code> triggered by a web
+        application do not result in a memory leak. Note that a call to this
+        method will be triggered as part of the web application stop process so
+        it is strongly recommended that this protection is enabled. The default
+        is <code>true</code></p>
+      </attribute>
+
+      <attribute name="urlCacheProtection" required="false">
+        <p>Enables protection so that reading resources from JAR files using
+        <code>java.net.URLConnection</code>s does not result in the JAR file
+        being locked. Note that enabling this protection disables caching by
+        default for all resources obtained via
+        <code>java.net.URLConnection</code>s. Caching may be re-enabled on a
+        case by case basis is required. Defaults to <code>true</code>.</p>
+      </attribute>
+
+    </attributes>
 
   </subsection>
 



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

Reply via email to