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 javax.sql.ConnectionPoolDataSource;
035: import javax.sql.PooledConnection;
036: import java.io.PrintWriter;
037: import java.sql.SQLException;
038: import java.util.logging.Logger;
039:
040: /**
041: * Spying on a driver.
042: */
043: public class SpyConnectionPoolDataSource implements
044: ConnectionPoolDataSource {
045: protected final static Logger log = Log
046: .open(SpyConnectionPoolDataSource.class);
047: protected final static L10N L = new L10N(
048: SpyConnectionPoolDataSource.class);
049:
050: private static int _staticId;
051: private int _id;
052: private int _connCount;
053:
054: // The underlying data source
055: private ConnectionPoolDataSource _dataSource;
056:
057: /**
058: * Creates a new SpyDriver.
059: */
060: public SpyConnectionPoolDataSource(
061: ConnectionPoolDataSource dataSource) {
062: _dataSource = dataSource;
063: _id = _staticId++;
064: }
065:
066: /**
067: * Returns the pooled connection.
068: */
069: public PooledConnection getPooledConnection() throws SQLException {
070: try {
071: PooledConnection conn = _dataSource.getPooledConnection();
072:
073: String connId = _id + "." + _connCount++;
074:
075: log.fine(_id + ":getConnectionPool() -> " + connId + ":"
076: + conn);
077:
078: return new SpyPooledConnection(conn, connId);
079: } catch (SQLException e) {
080: log.fine(_id + ":exn-connect(" + e + ")");
081:
082: throw e;
083: }
084: }
085:
086: /**
087: * Returns the XAConnection.
088: */
089: public PooledConnection getPooledConnection(String user,
090: String password) throws SQLException {
091: try {
092: PooledConnection conn = _dataSource.getPooledConnection(
093: user, password);
094:
095: String connId = _id + "." + _connCount++;
096:
097: log.fine(_id + ":getPooledConnection(" + user + ") -> "
098: + connId + ":" + conn);
099:
100: return new SpyPooledConnection(conn, connId);
101: } catch (SQLException e) {
102: log.fine(_id + ":exn-connect(" + e + ")");
103:
104: throw e;
105: }
106: }
107:
108: /**
109: * Returns the login timeout.
110: */
111: public int getLoginTimeout() throws SQLException {
112: return _dataSource.getLoginTimeout();
113: }
114:
115: /**
116: * Sets the login timeout.
117: */
118: public void setLoginTimeout(int timeout) throws SQLException {
119: _dataSource.setLoginTimeout(timeout);
120: }
121:
122: /**
123: * Returns the log writer
124: */
125: public PrintWriter getLogWriter() throws SQLException {
126: return _dataSource.getLogWriter();
127: }
128:
129: /**
130: * Sets the log writer.
131: */
132: public void setLogWriter(PrintWriter log) throws SQLException {
133: _dataSource.setLogWriter(log);
134: }
135:
136: public String toString() {
137: return "SpyConnectionPoolDataSource[id=" + _id
138: + ",data-source=" + _dataSource + "]";
139: }
140: }
|