001: /*
002: * Copyright (c) 1998-2008 Caucho Technology -- all rights reserved
003: *
004: * This file is part of Resin(R) Open Source
005: *
006: * Each copy or derived work must preserve the copyright notice and this
007: * notice unmodified.
008: *
009: * Resin Open Source is free software; you can redistribute it and/or modify
010: * it under the terms of the GNU General Public License as published by
011: * the Free Software Foundation; either version 2 of the License, or
012: * (at your option) any later version.
013: *
014: * Resin Open Source is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
017: * of NON-INFRINGEMENT. See the GNU General Public License for more
018: * details.
019: *
020: * You should have received a copy of the GNU General Public License
021: * along with Resin Open Source; if not, write to the
022: * Free SoftwareFoundation, Inc.
023: * 59 Temple Place, Suite 330
024: * Boston, MA 02111-1307 USA
025: *
026: * @author Scott Ferguson
027: */
028:
029: package com.caucho.sql.spy;
030:
031: import com.caucho.log.Log;
032: import com.caucho.util.L10N;
033:
034: import java.sql.Connection;
035: import java.sql.Driver;
036: import java.sql.DriverPropertyInfo;
037: import java.sql.SQLException;
038: import java.util.Hashtable;
039: import java.util.Map;
040: import java.util.Properties;
041: import java.util.logging.Logger;
042:
043: /**
044: * Spying on a driver.
045: */
046: public class SpyDriver implements java.sql.Driver {
047: protected final static Logger log = Logger
048: .getLogger(SpyDriver.class.getName());
049: protected final static L10N L = new L10N(SpyDriver.class);
050:
051: private static int _staticId;
052:
053: private SpyDataSource _spyDataSource;
054:
055: private int _id;
056: private int _connCount;
057:
058: // The underlying driver
059: private Driver _driver;
060:
061: /**
062: * Creates a new SpyDriver.
063: */
064: public SpyDriver(Driver driver) {
065: _spyDataSource = new SpyDataSource();
066: _driver = driver;
067: _id = _staticId++;
068: }
069:
070: public boolean acceptsURL(String url) throws SQLException {
071: try {
072: boolean result = _driver.acceptsURL(url);
073:
074: log.fine(_id + ":acceptsURL(" + url + ") -> " + result);
075:
076: return result;
077: } catch (SQLException e) {
078: log.fine(_id + ":exn-acceptURL(" + e + ")");
079:
080: throw e;
081: }
082: }
083:
084: public Connection connect(String url, Properties fine)
085: throws SQLException {
086: try {
087: Connection conn = _driver.connect(url, fine);
088:
089: int connId = _connCount++;
090:
091: log.fine(_id + ":connect(" + url + ",fine=" + fine
092: + ") -> " + connId + ":" + conn);
093:
094: return new SpyConnection(conn, _spyDataSource);
095: } catch (SQLException e) {
096: log.fine(_id + ":exn-connect(" + e + ")");
097:
098: throw e;
099: }
100: }
101:
102: public int getMajorVersion() {
103: int result = _driver.getMajorVersion();
104:
105: log.fine(_id + ":getMajorVersion() -> " + result);
106:
107: return result;
108: }
109:
110: public int getMinorVersion() {
111: int result = _driver.getMinorVersion();
112:
113: log.fine(_id + ":getMinorVersion() -> " + result);
114:
115: return result;
116: }
117:
118: public DriverPropertyInfo[] getPropertyInfo(String url,
119: Properties fine) throws SQLException {
120: try {
121: DriverPropertyInfo[] result = _driver.getPropertyInfo(url,
122: fine);
123:
124: Hashtable<String, String> cleanFine = new Hashtable<String, String>();
125:
126: if (fine != null) {
127: for (Map.Entry<Object, Object> entry : fine.entrySet()) {
128: cleanFine.put((String) entry.getKey(),
129: (String) entry.getValue());
130: }
131: }
132:
133: if (cleanFine.get("password") != null)
134: cleanFine.put("password", "****");
135:
136: log
137: .fine(_id + ":getPropertyInfo(" + url + ") -> "
138: + result);
139:
140: return result;
141: } catch (SQLException e) {
142: log.fine(_id + ":exn-getPropertyInfo(" + e + ")");
143:
144: throw e;
145: }
146: }
147:
148: public boolean jdbcCompliant() {
149: boolean result = _driver.jdbcCompliant();
150:
151: log.fine(_id + ":jdbcCompliant() -> " + result);
152:
153: return result;
154: }
155:
156: public String toString() {
157: return "SpyDriver[id=" + _id + ",driver=" + _driver + "]";
158: }
159: }
|