KickJava   Java API By Example, From Geeks To Geeks.

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


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 java.sql.Connection JavaDoc;
35 import java.sql.Driver JavaDoc;
36 import java.sql.DriverPropertyInfo JavaDoc;
37 import java.sql.SQLException JavaDoc;
38 import java.util.Hashtable JavaDoc;
39 import java.util.Map JavaDoc;
40 import java.util.Properties JavaDoc;
41 import java.util.logging.Logger JavaDoc;
42
43 /**
44  * Spying on a driver.
45  */

46 public class SpyDriver implements java.sql.Driver JavaDoc {
47   protected final static Logger JavaDoc log = Log.open(SpyDriver.class);
48   protected final static L10N L = new L10N(SpyDriver.class);
49
50   private static int _staticId;
51   private int _id;
52   private int _connCount;
53
54   // The underlying driver
55
private Driver JavaDoc _driver;
56   
57   /**
58    * Creates a new SpyDriver.
59    */

60   public SpyDriver(Driver JavaDoc driver)
61   {
62     _driver = driver;
63     _id = _staticId++;
64   }
65   
66   public boolean acceptsURL(String JavaDoc url)
67     throws SQLException JavaDoc
68   {
69     try {
70       boolean result = _driver.acceptsURL(url);
71
72       log.info(_id + ":acceptsURL(" + url + ") -> " + result);
73       
74       return result;
75     } catch (SQLException JavaDoc e) {
76       log.info(_id + ":exn-acceptURL(" + e + ")");
77       
78       throw e;
79     }
80   }
81   
82   public Connection JavaDoc connect(String JavaDoc url, Properties JavaDoc info)
83     throws SQLException JavaDoc
84   {
85     try {
86       Connection JavaDoc conn = _driver.connect(url, info);
87
88       int connId = _connCount++;
89
90       log.info(_id + ":connect(" + url + ",info=" + info + ") -> " + connId + ":" + conn);
91
92       return new SpyConnection(conn, connId);
93     } catch (SQLException JavaDoc e) {
94       log.info(_id + ":exn-connect(" + e + ")");
95       
96       throw e;
97     }
98   }
99   
100   public int getMajorVersion()
101   {
102       int result = _driver.getMajorVersion();
103
104       log.info(_id + ":getMajorVersion() -> " + result);
105
106       return result;
107   }
108   
109   public int getMinorVersion()
110   {
111       int result = _driver.getMinorVersion();
112
113       log.info(_id + ":getMinorVersion() -> " + result);
114
115       return result;
116   }
117   
118   public DriverPropertyInfo JavaDoc []getPropertyInfo(String JavaDoc url, Properties JavaDoc info)
119     throws SQLException JavaDoc
120   {
121     try {
122       DriverPropertyInfo JavaDoc []result = _driver.getPropertyInfo(url, info);
123
124       Hashtable JavaDoc<String JavaDoc,String JavaDoc> cleanInfo
125         = new Hashtable JavaDoc<String JavaDoc,String JavaDoc>();
126       
127       if (info != null) {
128         for (Map.Entry JavaDoc<Object JavaDoc,Object JavaDoc> entry : info.entrySet()) {
129           cleanInfo.put((String JavaDoc) entry.getKey(), (String JavaDoc) entry.getValue());
130         }
131       }
132         
133       if (cleanInfo.get("password") != null)
134         cleanInfo.put("password", "****");
135       
136       log.info(_id + ":getPropertyInfo(" + url + ") -> " + result);
137
138       return result;
139     } catch (SQLException JavaDoc e) {
140       log.info(_id + ":exn-getPropertyInfo(" + e + ")");
141       
142       throw e;
143     }
144   }
145   
146   public boolean jdbcCompliant()
147   {
148     boolean result = _driver.jdbcCompliant();
149
150     log.info(_id + ":jdbcCompliant() -> " + result);
151
152     return result;
153   }
154
155   public String JavaDoc toString()
156   {
157     return "SpyDriver[id=" + _id + ",driver=" + _driver + "]";
158   }
159 }
160
Popular Tags