KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > sql > spy > SpyConnectionPoolDataSource


1 /*
2  * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  * Free SoftwareFoundation, Inc.
23  * 59 Temple Place, Suite 330
24  * Boston, MA 02111-1307 USA
25  *
26  * @author Scott Ferguson
27  */

28
29 package com.caucho.sql.spy;
30
31 import com.caucho.log.Log;
32 import com.caucho.util.L10N;
33
34 import javax.sql.ConnectionPoolDataSource JavaDoc;
35 import javax.sql.PooledConnection JavaDoc;
36 import java.io.PrintWriter JavaDoc;
37 import java.sql.SQLException JavaDoc;
38 import java.util.logging.Logger JavaDoc;
39
40 /**
41  * Spying on a driver.
42  */

43 public class SpyConnectionPoolDataSource implements ConnectionPoolDataSource JavaDoc {
44   protected final static Logger JavaDoc log = Log.open(SpyConnectionPoolDataSource.class);
45   protected final static L10N L = new L10N(SpyConnectionPoolDataSource.class);
46
47   private static int _staticId;
48   private int _id;
49   private int _connCount;
50
51   // The underlying data source
52
private ConnectionPoolDataSource JavaDoc _dataSource;
53
54   /**
55    * Creates a new SpyDriver.
56    */

57   public SpyConnectionPoolDataSource(ConnectionPoolDataSource JavaDoc dataSource)
58   {
59     _dataSource = dataSource;
60     _id = _staticId++;
61   }
62
63   /**
64    * Returns the pooled connection.
65    */

66   public PooledConnection JavaDoc getPooledConnection()
67     throws SQLException JavaDoc
68   {
69     try {
70       PooledConnection JavaDoc conn = _dataSource.getPooledConnection();
71
72       int connId = _connCount++;
73
74       log.info(_id + ":getConnectionPool() -> " + connId + ":" + conn);
75
76       return new SpyPooledConnection(conn, connId);
77     } catch (SQLException JavaDoc e) {
78       log.info(_id + ":exn-connect(" + e + ")");
79       
80       throw e;
81     }
82   }
83
84   /**
85    * Returns the XAConnection.
86    */

87   public PooledConnection JavaDoc getPooledConnection(String JavaDoc user, String JavaDoc password)
88     throws SQLException JavaDoc
89   {
90     try {
91       PooledConnection JavaDoc conn = _dataSource.getPooledConnection(user, password);
92
93       int connId = _connCount++;
94
95       log.info(_id + ":getPooledConnection(" + user + ") -> " + connId + ":" + conn);
96
97       return new SpyPooledConnection(conn, connId);
98     } catch (SQLException JavaDoc e) {
99       log.info(_id + ":exn-connect(" + e + ")");
100       
101       throw e;
102     }
103   }
104
105   /**
106    * Returns the login timeout.
107    */

108   public int getLoginTimeout()
109     throws SQLException JavaDoc
110   {
111     return _dataSource.getLoginTimeout();
112   }
113
114   /**
115    * Sets the login timeout.
116    */

117   public void setLoginTimeout(int timeout)
118     throws SQLException JavaDoc
119   {
120     _dataSource.setLoginTimeout(timeout);
121   }
122
123   /**
124    * Returns the log writer
125    */

126   public PrintWriter JavaDoc getLogWriter()
127     throws SQLException JavaDoc
128   {
129     return _dataSource.getLogWriter();
130   }
131
132   /**
133    * Sets the log writer.
134    */

135   public void setLogWriter(PrintWriter JavaDoc log)
136     throws SQLException JavaDoc
137   {
138     _dataSource.setLogWriter(log);
139   }
140
141   public String JavaDoc toString()
142   {
143     return "SpyConnectionPoolDataSource[id=" + _id + ",data-source=" + _dataSource + "]";
144   }
145 }
146
Popular Tags