Fix https://issues.apache.org/bugzilla/show_bug.cgi?id=51862
authorslaurent <slaurent@13f79535-47bb-0310-9956-ffa450edef68>
Thu, 22 Sep 2011 20:01:52 +0000 (20:01 +0000)
committerslaurent <slaurent@13f79535-47bb-0310-9956-ffa450edef68>
Thu, 22 Sep 2011 20:01:52 +0000 (20:01 +0000)
bug 51862: JreMemoryLeakPreventionListener enhancement to load configurable classes
https://issues.apache.org/bugzilla/show_bug.cgi?id=51862

git-svn-id: https://svn.apache.org/repos/asf/tomcat/trunk@1174353 13f79535-47bb-0310-9956-ffa450edef68

java/org/apache/catalina/core/JreMemoryLeakPreventionListener.java
java/org/apache/catalina/core/LocalStrings.properties
webapps/docs/config/listeners.xml

index 3223154..22b0a48 100644 (file)
@@ -24,6 +24,7 @@ import java.net.MalformedURLException;
 import java.net.URL;
 import java.net.URLConnection;
 import java.sql.DriverManager;
+import java.util.StringTokenizer;
 
 import javax.imageio.ImageIO;
 import javax.xml.parsers.DocumentBuilderFactory;
@@ -202,7 +203,21 @@ public class JreMemoryLeakPreventionListener implements LifecycleListener {
     public void setDriverManagerProtection(boolean driverManagerProtection) {
         this.driverManagerProtection = driverManagerProtection;
     }
-
+    
+    /**
+     * List of comma-separated fully qualified class names to load and initialize during
+     * the startup of this Listener. This allows to pre-load classes that are known to 
+     * provoke classloader leaks if they are loaded during a request processing.
+     */
+    private String classesToInitialize = null;
+    public String getClassesToInitialize() {
+        return classesToInitialize;
+    }
+    public void setClassesToInitialize(String classesToInitialize) {
+        this.classesToInitialize = classesToInitialize;
+    }
+    
+    
     @Override
     public void lifecycleEvent(LifecycleEvent event) {
         // Initialise these classes when Tomcat starts
@@ -419,6 +434,22 @@ public class JreMemoryLeakPreventionListener implements LifecycleListener {
                         }
                     }
                 }
+                
+                if (classesToInitialize != null) {
+                    StringTokenizer strTok =
+                        new StringTokenizer(classesToInitialize, ", \r\n\t");
+                    while (strTok.hasMoreTokens()) {
+                        String classNameToLoad = strTok.nextToken();
+                        try {
+                            Class.forName(classNameToLoad);
+                        } catch (ClassNotFoundException e) {
+                            log.error(
+                                sm.getString("jreLeakListener.classToInitializeFail",
+                                    classNameToLoad), e);
+                            // continue with next class to load
+                        }
+                    }
+                }
 
             } finally {
                 Thread.currentThread().setContextClassLoader(loader);
index 5581523..839005c 100644 (file)
@@ -83,6 +83,7 @@ jreLeakListener.jarUrlConnCacheFail=Failed to disable Jar URL connection caching
 jreLeakListener.xmlParseFail=Error whilst attempting to prevent memory leaks during XML parsing
 jreLeakListener.authPolicyFail=Error whilst attempting to prevent memory leak in javax.security.auth.Policy class
 jreLeakListener.ldapPoolManagerFail=Failed to trigger creation of the com.sun.jndi.ldap.LdapPoolManager class during Tomcat start to prevent possible memory leaks. This is expected on non-Sun JVMs.
+jreLeakListener.classToInitializeFail=Failed to load class {0} during Tomcat start to prevent possible memory leaks.
 naming.wsdlFailed=Failed to find wsdl file: {0}
 naming.bindFailed=Failed to bind object: {0}
 naming.jmxRegistrationFailed=Failed to register in JMX: {0}
index 7044eff..ef29c3c 100644 (file)
         Defaults to <code>false</code> because an AWT thread is launched.</p>
       </attribute>
 
+      <attribute name="classesToInitialize" required="false">
+        <p>List of comma-separated fully qualified class names to load and initialize 
+        during the startup of this Listener. This allows to pre-load classes that are 
+        known to provoke classloader leaks if they are loaded during a request 
+        processing. Non-JRE classes may be referenced, like 
+        <code>oracle.jdbc.driver.OracleTimeoutThreadPerVM</code>.
+        The default value is empty, but specific JRE classes are loaded by other leak 
+        protection features managed by other attributes of this Listener.</p>
+      </attribute>
+
       <attribute name="driverManagerProtection" required="false">
         <p>The first use of <code>java.sql.DriverManager</code> will trigger the
         loading of JDBNC Driver in the the current class loader. The web