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