Make the JDBC leak prevention play nicely with a security manager
authormarkt <markt@13f79535-47bb-0310-9956-ffa450edef68>
Tue, 8 Sep 2009 10:24:29 +0000 (10:24 +0000)
committermarkt <markt@13f79535-47bb-0310-9956-ffa450edef68>
Tue, 8 Sep 2009 10:24:29 +0000 (10:24 +0000)
Don't use a fixed size buffer to load the class.

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

java/org/apache/catalina/loader/JdbcLeakPrevention.java
java/org/apache/catalina/loader/WebappClassLoader.java

index 19c45c6..1fd7ea2 100644 (file)
@@ -23,41 +23,26 @@ import java.sql.DriverManager;
 import java.sql.SQLException;
 import java.util.Enumeration;
 
-import org.apache.tomcat.util.res.StringManager;
-
 /**
- * This class is loaded by the  {@link WebappClassLoader} to enable it to
+ * This class is loaded by the {@link WebappClassLoader} to enable it to
  * deregister JDBC drivers forgotten by the web application. There are some
  * classloading hacks involved - see {@link WebappClassLoader#clearReferences()}
  * for details - but the short version is do not just create a new instance of
  * this class with the new keyword.
+ * 
+ * Since this class is loaded by {@link WebappClassLoader}, it can not refer to
+ * any internal Tomcat classes as that will cause the security manager to
+ * complain.
  */
 public class JdbcLeakPrevention {
 
-    /**
-     * The logger for this class.
-     */
-    protected static org.apache.juli.logging.Log log=
-        org.apache.juli.logging.LogFactory.getLog( JdbcLeakPrevention.class );
-
-    /**
-     * The string manager for this package.
-     */
-    protected static final StringManager sm =
-        StringManager.getManager(Constants.Package);
-
-    public void clearJdbcDriverRegistrations() {
+    public void clearJdbcDriverRegistrations() throws SQLException {
         // Unregister any JDBC drivers loaded by the class loader that loaded
         // this class - ie the webapp class loader
         Enumeration<Driver> drivers = DriverManager.getDrivers();
         while (drivers.hasMoreElements()) {
             Driver driver = drivers.nextElement();
-            try {
-                DriverManager.deregisterDriver(driver);
-            } catch (SQLException sqle) {
-                log.warn(sm.getString("jdbcLeakPrevention.jdbcRemoveFailed",
-                        driver.toString()), sqle);
-            }
+            DriverManager.deregisterDriver(driver);
         }
         
     }
index f48c860..442eb0b 100644 (file)
@@ -1619,16 +1619,21 @@ public class WebappClassLoader
          */
         InputStream is = getResourceAsStream(
                 "org/apache/catalina/loader/JdbcLeakPrevention.class");
-        // Cheat - we know roughly how big the class will be (~1K) but allow
-        // plenty room to grow
-        // TODO Let buffer grow as required
-        byte[] classBytes = new byte[4096];
+        // We know roughly how big the class will be (~ 1K) so allow 2k as a
+        // starting point
+        byte[] classBytes = new byte[2048];
         int offset = 0;
         try {
-            int read = is.read(classBytes, offset, 4096-offset);
+            int read = is.read(classBytes, offset, classBytes.length-offset);
             while (read > -1) {
                 offset += read;
-                read = is.read(classBytes, offset, 4096-offset);
+                if (offset == classBytes.length) {
+                    // Buffer full - double size
+                    byte[] tmp = new byte[classBytes.length * 2];
+                    System.arraycopy(classBytes, 0, tmp, 0, classBytes.length);
+                    classBytes = tmp;
+                }
+                read = is.read(classBytes, offset, classBytes.length-offset);
             }
             Class<?> lpClass =
                 defineClass("org.apache.catalina.loader.JdbcLeakPrevention",