📄 p6spydrivercore.java
字号:
classType = "driver";
i = driverNames.iterator();
while (i.hasNext()) {
P6SpyDriver spy = null;
// register P6 first if you are using it
if (hasModules) {
spy = new P6SpyDriver();
DriverManager.registerDriver(spy);
}
// this is bogus, but we need to make *sure*
// that p6 is registered before your real drivers. Otherwise
// the real driver will intercept the call before p6 gets it.
// so, deregister the driver if nec.
className = (String) i.next();
deregister(className);
Driver realDriver = (Driver)P6Util.forName(className).newInstance();
if (P6SpyOptions.getDeregisterDrivers()) {
// just in case you had to deregister
DriverManager.registerDriver(realDriver);
}
// now wrap your realDriver in the spy
if (hasModules) {
spy.setPassthru(realDriver);
realDrivers.add(realDriver);
}
P6LogQuery.logDebug("Registered driver: "+className+", realdriver: "+realDriver);
}
// instantiate the factories, if nec.
if (hasModules) {
factories = new ArrayList();
classType = "factory";
i = modules.iterator();
while (i.hasNext()) {
className = (String) i.next();
P6Factory factory = (P6Factory)P6Util.forName(className).newInstance();
factories.add(factory);
P6Options options = factory.getOptions();
if (options != null) {
OptionReloader.add(options, properties);
}
P6LogQuery.logDebug("Registered factory: "+className+" with options: "+options);
}
}
initialized = true;
for (Enumeration e = DriverManager.getDrivers() ; e.hasMoreElements() ;) {
P6LogQuery.logDebug("Driver manager reporting driver registered: "+e.nextElement());
}
} catch (Exception e) {
String err = "Error registering " + classType + " [" + className + "]\nCaused By: " + e.toString();
P6LogQuery.logError(err);
throw new P6DriverNotFoundError(err);
}
}
static void deregister(String className) throws SQLException {
ArrayList dereg = new ArrayList();
for (Enumeration e = DriverManager.getDrivers(); e.hasMoreElements();) {
Driver driver = (Driver) e.nextElement();
// once you reach a P6 driver, you can jump out
if (driver instanceof P6SpyDriver) {
break;
}
// now you have to be careful of concurrent update
// exceptions here, so save the drivers for later
// deregistration
if (driver.getClass().getName().equals(className)) {
dereg.add(driver);
}
}
// if you found any drivers let's dereg them now
int size = dereg.size();
if (size > 0) {
for (int i = 0; i < size; i++) {
Driver driver = (Driver) dereg.get(i);
if (P6SpyOptions.getDeregisterDrivers()) {
P6LogQuery.logInfo("deregistering driver " + driver.getClass().getName());
DriverManager.deregisterDriver(driver);
} else {
P6LogQuery.logError("driver " + driver.getClass().getName() + " is a real driver in spy.properties, but it has been loaded before p6spy. p6spy will not wrap these connections. Either prevent the driver from loading, or try setting 'deregisterdrivers' to true in spy.properties");
}
}
}
}
public P6SpyDriverCore(String _spydriver, P6Factory _p6factory) throws ClassNotFoundException, InstantiationException, IllegalAccessException, SQLException {
// if we couldn't find the spy.properties file, complain here
if (!foundSpyProperties) {
throw (new InstantiationException("spy.properties not found in classpath"));
}
// should really change the constructor here :)
}
// these methods are the secret sauce here
public static Connection wrapConnection(Connection realConnection) throws SQLException {
Connection con = realConnection;
if (factories != null) {
Iterator it = factories.iterator();
while (it.hasNext()) {
P6Factory factory = (P6Factory) it.next();
con = factory.getConnection(con);
}
}
return con;
}
public Driver getPassthru() {
return passthru;
}
public void setPassthru(Driver inVar) {
passthru = inVar;
}
private String getRealUrl(String url) {
if (P6SpyOptions.getUsePrefix()) {
return url.startsWith("p6spy:") ? url.substring("p6spy:".length()) : null;
} else {
return url;
}
}
// the remaining methods are for the Driver interface
public Connection connect(String p0, java.util.Properties p1) throws SQLException {
String realUrl = this.getRealUrl(p0);
// if there is no url, we have problems
if (realUrl==null) {
throw new SQLException("realURL is null, needs the p6spy prefix: "+p0);
}
// lets try to find the driver from the multiple divers in spy.properties
findPassthru(realUrl);
// if we can't find one, it may not be defined
if (passthru == null) {
throw new SQLException("Unable to find a driver that accepts " + realUrl);
}
P6LogQuery.logDebug("this is " + this + " and passthru is " + passthru);
if (passthru == null) {
findPassthru(realUrl);
}
Connection conn = passthru.connect(realUrl,p1);
if (conn != null) {
conn = wrapConnection(conn);
}
return conn;
}
protected void findPassthru(String url) {
Iterator i = realDrivers.iterator();
while (i.hasNext()) {
Driver driver = (Driver) i.next();
try {
if (driver.acceptsURL(url)) {
passthru = driver;
P6LogQuery.logDebug("found new driver " + driver);
break;
}
} catch (SQLException e) {}
}
}
/**
* for some reason the passthru is null, go create one
*/
public boolean acceptsURL(String p0) throws SQLException {
String realUrl = this.getRealUrl(p0);
boolean accepts = false;
// somehow we get initilized but no driver is created,
// lets try findPassthru
if (passthru == null && initialized){
// we should have some drivers
if (realDrivers.size() == 0){
throw new SQLException("P6 has no drivers registered");
} else {
findPassthru(realUrl);
// if we are still null, we have issues
if (passthru == null)
throw new SQLException("P6 can't find a driver to accept url ("+realUrl+") from the "+realDrivers.size()+" drivers P6 knows about. The current driver is null");
}
}
if (realUrl != null) {
accepts = passthru.acceptsURL(realUrl);
}
return accepts;
}
public DriverPropertyInfo [] getPropertyInfo(String p0, java.util.Properties p1) throws SQLException {
return(passthru.getPropertyInfo(p0,p1));
}
public int getMajorVersion(){
return(passthru.getMajorVersion());
}
public int getMinorVersion(){
return(passthru.getMinorVersion());
}
public boolean jdbcCompliant(){
return(passthru.jdbcCompliant());
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -