net.sf.log4jdbc
Class DriverSpy

java.lang.Object
  extended by net.sf.log4jdbc.DriverSpy
All Implemented Interfaces:
java.sql.Driver

public class DriverSpy
extends java.lang.Object
implements java.sql.Driver

A JDBC driver which is a facade that delegates to one or more real underlying JDBC drivers. The driver will spy on any other JDBC driver that is loaded, simply by prepending jdbc:log4 to the normal jdbc driver URL used by any other JDBC driver. The driver, by default, also loads several well known drivers at class load time, so that this driver can be "dropped in" to any Java program that uses these drivers without making any code changes.

The well known driver classes that are loaded are:

Additional drivers can be set via a property: log4jdbc.drivers This can be either a single driver class name or a list of comma separated driver class names.

The autoloading behavior can be disabled by setting a property: log4jdbc.auto.load.popular.drivers to false. If that is done, then the only drivers that log4jdbc will attempt to load are the ones specified in log4jdbc.drivers.

If any of the above driver classes cannot be loaded, the driver continues on without failing.

Note that the getMajorVersion, getMinorVersion and jdbcCompliant method calls attempt to delegate to the last underlying driver requested through any other call that accepts a JDBC URL.

This can cause unexpected behavior in certain circumstances. For example, if one of these 3 methods is called before any underlying driver has been established, then they will return default values that might not be correct in all situations. Similarly, if this spy driver is used to spy on more than one underlying driver concurrently, the values returned by these 3 method calls may change depending on what the last underlying driver used was at the time. This will not usually be a problem, since the driver is retrieved by it's URL from the DriverManager in the first place (thus establishing an underlying real driver), and in most applications their is only one database.

Author:
Arthur Blake

Constructor Summary
DriverSpy()
          Default constructor.
 
Method Summary
 boolean acceptsURL(java.lang.String url)
          Returns true if this is a jdbc:log4 URL and if the URL is for an underlying driver that this DriverSpy can spy on.
 java.sql.Connection connect(java.lang.String url, java.util.Properties info)
          Get a Connection to the database from the underlying driver that this DriverSpy is spying on.
 int getMajorVersion()
          Get the major version of the driver.
 int getMinorVersion()
          Get the minor version of the driver.
 java.sql.DriverPropertyInfo[] getPropertyInfo(java.lang.String url, java.util.Properties info)
          Gets information about the possible properties for the underlying driver.
 boolean jdbcCompliant()
          Report whether the underlying driver is JDBC compliant.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

DriverSpy

public DriverSpy()
Default constructor.

Method Detail

getMajorVersion

public int getMajorVersion()
Get the major version of the driver. This call will be delegated to the underlying driver that is being spied upon (if there is no underlying driver found, then 1 will be returned.)

Specified by:
getMajorVersion in interface java.sql.Driver
Returns:
the major version of the JDBC driver.

getMinorVersion

public int getMinorVersion()
Get the minor version of the driver. This call will be delegated to the underlying driver that is being spied upon (if there is no underlying driver found, then 0 will be returned.)

Specified by:
getMinorVersion in interface java.sql.Driver
Returns:
the minor version of the JDBC driver.

jdbcCompliant

public boolean jdbcCompliant()
Report whether the underlying driver is JDBC compliant. If there is no underlying driver, false will be returned, because the driver cannot actually do any work without an underlying driver.

Specified by:
jdbcCompliant in interface java.sql.Driver
Returns:
true if the underlying driver is JDBC Compliant; false otherwise.

acceptsURL

public boolean acceptsURL(java.lang.String url)
                   throws java.sql.SQLException
Returns true if this is a jdbc:log4 URL and if the URL is for an underlying driver that this DriverSpy can spy on.

Specified by:
acceptsURL in interface java.sql.Driver
Parameters:
url - JDBC URL.
Returns:
true if this Driver can handle the URL.
Throws:
java.sql.SQLException - if a database access error occurs

connect

public java.sql.Connection connect(java.lang.String url,
                                   java.util.Properties info)
                            throws java.sql.SQLException
Get a Connection to the database from the underlying driver that this DriverSpy is spying on. If logging is not enabled, an actual Connection to the database returned. If logging is enabled, a ConnectionSpy object which wraps the real Connection is returned.

Specified by:
connect in interface java.sql.Driver
Parameters:
url - JDBC connection URL .
info - a list of arbitrary string tag/value pairs as connection arguments. Normally at least a "user" and "password" property should be included.
Returns:
a Connection object that represents a connection to the URL.
Throws:
java.sql.SQLException - if a database access error occurs

getPropertyInfo

public java.sql.DriverPropertyInfo[] getPropertyInfo(java.lang.String url,
                                                     java.util.Properties info)
                                              throws java.sql.SQLException
Gets information about the possible properties for the underlying driver.

Specified by:
getPropertyInfo in interface java.sql.Driver
Parameters:
url - the URL of the database to which to connect
info - a proposed list of tag/value pairs that will be sent on connect open
Returns:
an array of DriverPropertyInfo objects describing possible properties. This array may be an empty array if no properties are required.
Throws:
java.sql.SQLException - if a database access error occurs