提交 dfffbd5f 编写于 作者: L lancea

7034471: Wrap registeredDrivers in DriverManager

Reviewed-by: alanb, briangoetz
上级 aabc7a22
...@@ -80,7 +80,7 @@ public class DriverManager { ...@@ -80,7 +80,7 @@ public class DriverManager {
// List of registered JDBC drivers // List of registered JDBC drivers
private final static CopyOnWriteArrayList<Driver> registeredDrivers = new CopyOnWriteArrayList<Driver>(); private final static CopyOnWriteArrayList<DriverInfo> registeredDrivers = new CopyOnWriteArrayList<DriverInfo>();
private static volatile int loginTimeout = 0; private static volatile int loginTimeout = 0;
private static volatile java.io.PrintWriter logWriter = null; private static volatile java.io.PrintWriter logWriter = null;
private static volatile java.io.PrintStream logStream = null; private static volatile java.io.PrintStream logStream = null;
...@@ -265,22 +265,22 @@ public class DriverManager { ...@@ -265,22 +265,22 @@ public class DriverManager {
// Walk through the loaded registeredDrivers attempting to locate someone // Walk through the loaded registeredDrivers attempting to locate someone
// who understands the given URL. // who understands the given URL.
for (Driver aDriver : registeredDrivers) { for (DriverInfo aDriver : registeredDrivers) {
// If the caller does not have permission to load the driver then // If the caller does not have permission to load the driver then
// skip it. // skip it.
if(isDriverAllowed(aDriver, callerCL)) { if(isDriverAllowed(aDriver.driver, callerCL)) {
try { try {
if(aDriver.acceptsURL(url)) { if(aDriver.driver.acceptsURL(url)) {
// Success! // Success!
println("getDriver returning " + aDriver.getClass().getName()); println("getDriver returning " + aDriver.driver.getClass().getName());
return (aDriver); return (aDriver.driver);
} }
} catch(SQLException sqe) { } catch(SQLException sqe) {
// Drop through and try the next driver. // Drop through and try the next driver.
} }
} else { } else {
println(" skipping: " + aDriver.getClass().getName()); println(" skipping: " + aDriver.driver.getClass().getName());
} }
} }
...@@ -305,7 +305,7 @@ public class DriverManager { ...@@ -305,7 +305,7 @@ public class DriverManager {
/* Register the driver if it has not already been added to our list */ /* Register the driver if it has not already been added to our list */
if(driver != null) { if(driver != null) {
registeredDrivers.addIfAbsent(driver); registeredDrivers.addIfAbsent(new DriverInfo(driver));
} else { } else {
// This is for compatibility with the original DriverManager // This is for compatibility with the original DriverManager
throw new NullPointerException(); throw new NullPointerException();
...@@ -333,9 +333,10 @@ public class DriverManager { ...@@ -333,9 +333,10 @@ public class DriverManager {
ClassLoader callerCL = DriverManager.getCallerClassLoader(); ClassLoader callerCL = DriverManager.getCallerClassLoader();
println("DriverManager.deregisterDriver: " + driver); println("DriverManager.deregisterDriver: " + driver);
if(registeredDrivers.contains(driver)) { DriverInfo aDriver = new DriverInfo(driver);
if(registeredDrivers.contains(aDriver)) {
if (isDriverAllowed(driver, callerCL)) { if (isDriverAllowed(driver, callerCL)) {
registeredDrivers.remove(driver); registeredDrivers.remove(aDriver);
} else { } else {
// If the caller does not have permission to load the driver then // If the caller does not have permission to load the driver then
// throw a SecurityException. // throw a SecurityException.
...@@ -363,11 +364,11 @@ public class DriverManager { ...@@ -363,11 +364,11 @@ public class DriverManager {
ClassLoader callerCL = DriverManager.getCallerClassLoader(); ClassLoader callerCL = DriverManager.getCallerClassLoader();
// Walk through the loaded registeredDrivers. // Walk through the loaded registeredDrivers.
for(Driver aDriver : registeredDrivers) { for(DriverInfo aDriver : registeredDrivers) {
// If the caller does not have permission to load the driver then // If the caller does not have permission to load the driver then
// skip it. // skip it.
if(isDriverAllowed(aDriver, callerCL)) { if(isDriverAllowed(aDriver.driver, callerCL)) {
result.addElement(aDriver); result.addElement(aDriver.driver);
} else { } else {
println(" skipping: " + aDriver.getClass().getName()); println(" skipping: " + aDriver.getClass().getName());
} }
...@@ -569,16 +570,16 @@ public class DriverManager { ...@@ -569,16 +570,16 @@ public class DriverManager {
// Remember the first exception that gets raised so we can reraise it. // Remember the first exception that gets raised so we can reraise it.
SQLException reason = null; SQLException reason = null;
for(Driver aDriver : registeredDrivers) { for(DriverInfo aDriver : registeredDrivers) {
// If the caller does not have permission to load the driver then // If the caller does not have permission to load the driver then
// skip it. // skip it.
if(isDriverAllowed(aDriver, callerCL)) { if(isDriverAllowed(aDriver.driver, callerCL)) {
try { try {
println(" trying " + aDriver.getClass().getName()); println(" trying " + aDriver.driver.getClass().getName());
Connection con = aDriver.connect(url, info); Connection con = aDriver.driver.connect(url, info);
if (con != null) { if (con != null) {
// Success! // Success!
println("getConnection returning " + aDriver.getClass().getName()); println("getConnection returning " + aDriver.driver.getClass().getName());
return (con); return (con);
} }
} catch (SQLException ex) { } catch (SQLException ex) {
...@@ -607,3 +608,29 @@ public class DriverManager { ...@@ -607,3 +608,29 @@ public class DriverManager {
private static native ClassLoader getCallerClassLoader(); private static native ClassLoader getCallerClassLoader();
} }
/*
* Wrapper class for registered Drivers in order to not expose Driver.equals()
* to avoid the capture of the Driver it being compared to as it might not
* normally have access.
*/
class DriverInfo {
final Driver driver;
DriverInfo(Driver driver) {
this.driver = driver;
}
public boolean equals(Object other) {
return (other instanceof DriverInfo)
&& this.driver == ((DriverInfo) other).driver;
}
public int hashCode() {
return driver.hashCode();
}
public String toString() {
return ("driver[className=" + driver + "]");
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册