001: /*
002: * Copyright (c) 2005, Your Corporation. All Rights Reserved.
003: */
004:
005: package com.technoetic.xplanner.db.jdbc;
006:
007: import java.io.PrintWriter;
008: import java.sql.Connection;
009: import java.sql.Driver;
010: import java.sql.DriverManager;
011: import java.sql.DriverPropertyInfo;
012: import java.sql.SQLException;
013: import java.util.ArrayList;
014: import java.util.Iterator;
015: import java.util.List;
016: import java.util.Properties;
017:
018: import org.apache.commons.collections.EnumerationUtils;
019: import org.apache.log4j.Logger;
020: import com.thoughtworks.proxy.toys.echo.Echoing;
021:
022: import com.technoetic.xplanner.XPlannerProperties;
023: import com.technoetic.xplanner.util.LogUtil;
024:
025: public class DriverWrapper implements Driver {
026: Driver driver;
027:
028: static {
029: try {
030: initWrapperDriver();
031: } catch (SQLException e) {
032: e.printStackTrace();
033: }
034:
035: }
036:
037: private static void initWrapperDriver() throws SQLException {
038: DriverManager.setLogWriter(new PrintWriter(System.err));
039: // List registeredDrivers = new ArrayList(EnumerationUtils.toList(DriverManager.getDrivers()));
040: // for (Iterator it = registeredDrivers.iterator(); it.hasNext();) {
041: // Driver driver = (Driver) it.next();
042: // DriverManager.deregisterDriver(driver);
043: // }
044: DriverManager.registerDriver(new DriverWrapper());
045: // for (Iterator it = registeredDrivers.iterator(); it.hasNext();) {
046: // Driver driver = (Driver) it.next();
047: // DriverManager.registerDriver(driver);
048: // }
049: }
050:
051: private static Driver findDriver(String driverClassName)
052: throws SQLException {
053: Class driverClass = null;
054: try {
055: driverClass = Class.forName(driverClassName);
056: } catch (ClassNotFoundException e) {
057: throwDriverNotFoundException(driverClassName);
058: }
059: List registeredDrivers = new ArrayList(EnumerationUtils
060: .toList(DriverManager.getDrivers()));
061: for (Iterator it = registeredDrivers.iterator(); it.hasNext();) {
062: Driver driver = (Driver) it.next();
063: if (driver.getClass().equals(driverClass))
064: return driver;
065: }
066: throwDriverNotFoundException(driverClassName);
067: return null; //never reached
068: }
069:
070: private static void throwDriverNotFoundException(
071: String driverClassName) throws SQLException {
072: throw new SQLException("Could not find driver '"
073: + driverClassName + "'");
074: }
075:
076: public DriverWrapper() throws SQLException {
077: String driverClassName = new XPlannerProperties()
078: .getProperty("xplanner.wrapped.driver");
079: driver = findDriver(driverClassName);
080: DriverManager.deregisterDriver(driver);
081: }
082:
083: public int getMajorVersion() {
084: return driver.getMajorVersion();
085: }
086:
087: public int getMinorVersion() {
088: return driver.getMinorVersion();
089: }
090:
091: public boolean jdbcCompliant() {
092: return driver.jdbcCompliant();
093: }
094:
095: public boolean acceptsURL(String url) throws SQLException {
096: return driver.acceptsURL(url);
097: }
098:
099: public Connection connect(String url, Properties info)
100: throws SQLException {
101: return (Connection) Echoing.object(Connection.class, driver
102: .connect(url, info));
103: }
104:
105: public DriverPropertyInfo[] getPropertyInfo(String url,
106: Properties info) throws SQLException {
107: return driver.getPropertyInfo(url, info);
108: }
109: }
|