Author: markt Date: Tue Sep 8 10:24:29 2009 New Revision: 812432 URL: http://svn.apache.org/viewvc?rev=812432&view=rev Log: Make the JDBC leak prevention play nicely with a security manager Don't use a fixed size buffer to load the class.
Modified: tomcat/trunk/java/org/apache/catalina/loader/JdbcLeakPrevention.java tomcat/trunk/java/org/apache/catalina/loader/WebappClassLoader.java Modified: tomcat/trunk/java/org/apache/catalina/loader/JdbcLeakPrevention.java URL: http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/catalina/loader/JdbcLeakPrevention.java?rev=812432&r1=812431&r2=812432&view=diff ============================================================================== --- tomcat/trunk/java/org/apache/catalina/loader/JdbcLeakPrevention.java (original) +++ tomcat/trunk/java/org/apache/catalina/loader/JdbcLeakPrevention.java Tue Sep 8 10:24:29 2009 @@ -23,41 +23,26 @@ 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); } } Modified: tomcat/trunk/java/org/apache/catalina/loader/WebappClassLoader.java URL: http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/catalina/loader/WebappClassLoader.java?rev=812432&r1=812431&r2=812432&view=diff ============================================================================== --- tomcat/trunk/java/org/apache/catalina/loader/WebappClassLoader.java (original) +++ tomcat/trunk/java/org/apache/catalina/loader/WebappClassLoader.java Tue Sep 8 10:24:29 2009 @@ -1619,16 +1619,21 @@ */ 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", --------------------------------------------------------------------- To unsubscribe, e-mail: dev-unsubscr...@tomcat.apache.org For additional commands, e-mail: dev-h...@tomcat.apache.org