From 9666be814d8e65f45d0710b5dd93821a8836cee5 Mon Sep 17 00:00:00 2001 From: markt Date: Tue, 8 Sep 2009 10:24:29 +0000 Subject: [PATCH] Make the JDBC leak prevention play nicely with a security manager 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 --- .../apache/catalina/loader/JdbcLeakPrevention.java | 29 ++++++---------------- .../apache/catalina/loader/WebappClassLoader.java | 17 ++++++++----- 2 files changed, 18 insertions(+), 28 deletions(-) diff --git a/java/org/apache/catalina/loader/JdbcLeakPrevention.java b/java/org/apache/catalina/loader/JdbcLeakPrevention.java index 19c45c672..1fd7ea262 100644 --- a/java/org/apache/catalina/loader/JdbcLeakPrevention.java +++ b/java/org/apache/catalina/loader/JdbcLeakPrevention.java @@ -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 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); } } diff --git a/java/org/apache/catalina/loader/WebappClassLoader.java b/java/org/apache/catalina/loader/WebappClassLoader.java index f48c860d1..442eb0b3a 100644 --- a/java/org/apache/catalina/loader/WebappClassLoader.java +++ b/java/org/apache/catalina/loader/WebappClassLoader.java @@ -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", -- 2.11.0