KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > tools > profiler > ConnectionWrapper


1 /*
2  * Copyright (c) 1998-2005 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 Sam
27  */

28
29
30 package com.caucho.tools.profiler;
31
32 import java.sql.*;
33 import java.util.Map JavaDoc;
34
35 public final class ConnectionWrapper
36   implements Connection
37 {
38   private final Connection _connection;
39   private final ProfilerPoint _profilerPoint;
40
41   public ConnectionWrapper(ProfilerPoint profilerPoint, Connection connection)
42   {
43     _profilerPoint = profilerPoint;
44     _connection = connection;
45   }
46
47   private ProfilerPoint createProfilerPoint(String JavaDoc sql)
48   {
49     // XXX: need to categorize under _profilerPoint even if it is
50
// not the parent in the call stack at time of execution
51
// switch _profilerPoint to DatabaseProfilerPoint, move createPP to DatabasePP
52
return _profilerPoint.createProfilerPoint(sql);
53   }
54
55   private StatementWrapper wrap(Statement statement)
56   {
57     return new StatementWrapper(_profilerPoint, statement);
58   }
59
60   private PreparedStatementWrapper wrap(String JavaDoc sql, PreparedStatement statement)
61   {
62     ProfilerPoint profilerPoint = createProfilerPoint(sql);
63
64     return new PreparedStatementWrapper(profilerPoint, statement);
65   }
66
67   private CallableStatementWrapper wrap(String JavaDoc sql, CallableStatement statement)
68   {
69     ProfilerPoint profilerPoint = createProfilerPoint(sql);
70
71     return new CallableStatementWrapper(profilerPoint, statement);
72   }
73
74   public Statement createStatement()
75     throws SQLException
76   {
77     return wrap(_connection.createStatement());
78   }
79
80   public Statement createStatement(int resultSetType, int resultSetConcurrency,
81                                    int resultSetHoldability)
82     throws SQLException
83   {
84     return wrap(_connection.createStatement(resultSetType,
85                                             resultSetConcurrency,
86                                             resultSetHoldability));
87   }
88
89   public Statement createStatement(int resultSetType, int resultSetConcurrency)
90     throws SQLException
91   {
92     return wrap(_connection.createStatement(resultSetType,
93                                             resultSetConcurrency));
94   }
95
96   public PreparedStatement prepareStatement(String JavaDoc sql)
97     throws SQLException
98   {
99     ProfilerPoint profilerPoint = _profilerPoint.createProfilerPoint(sql);
100
101     return wrap(sql, _connection.prepareStatement(sql));
102   }
103
104   public PreparedStatement prepareStatement(String JavaDoc sql,
105                                             int resultSetType,
106                                             int resultSetConcurrency,
107                                             int resultSetHoldability)
108     throws SQLException
109   {
110     ProfilerPoint profilerPoint = _profilerPoint.createProfilerPoint(sql);
111
112     return wrap(sql, _connection.prepareStatement(sql,
113                                                   resultSetType,
114                                                   resultSetConcurrency,
115                                                   resultSetHoldability));
116   }
117
118   public CallableStatement prepareCall(String JavaDoc sql)
119     throws SQLException
120   {
121     return wrap(sql, _connection.prepareCall(sql));
122   }
123
124   public CallableStatement prepareCall(String JavaDoc sql,
125                                        int resultSetType,
126                                        int resultSetConcurrency,
127                                        int resultSetHoldability)
128     throws SQLException
129   {
130     return wrap(sql, _connection.prepareCall(sql,
131                                         resultSetType,
132                                         resultSetConcurrency,
133                                         resultSetHoldability));
134   }
135
136   public PreparedStatement prepareStatement(String JavaDoc sql, int autoGeneratedKeys)
137     throws SQLException
138   {
139     return wrap(sql, _connection.prepareStatement(sql,
140                                              autoGeneratedKeys));
141   }
142
143   public PreparedStatement prepareStatement(String JavaDoc sql, int[] columnIndexes)
144     throws SQLException
145   {
146     return wrap(sql, _connection.prepareStatement(sql,
147                                              columnIndexes));
148   }
149
150   public PreparedStatement prepareStatement(String JavaDoc sql, String JavaDoc[] columnNames)
151     throws SQLException
152   {
153     return wrap(sql, _connection.prepareStatement(sql, columnNames));
154   }
155
156   public PreparedStatement prepareStatement(String JavaDoc sql,
157                                             int resultSetType,
158                                             int resultSetConcurrency)
159     throws SQLException
160   {
161     return wrap(sql, _connection.prepareStatement(sql,
162                                                   resultSetType,
163                                                   resultSetConcurrency));
164   }
165
166   public CallableStatement prepareCall(String JavaDoc sql,
167                                        int resultSetType,
168                                        int resultSetConcurrency)
169     throws SQLException
170   {
171     return wrap(sql, _connection.prepareCall(sql,
172                                              resultSetType,
173                                              resultSetConcurrency));
174   }
175
176   public String JavaDoc nativeSQL(String JavaDoc sql)
177     throws SQLException
178   {
179     Profiler profiler = _profilerPoint.start();
180
181     try {
182       return _connection.nativeSQL(sql);
183     }
184     finally {
185       profiler.finish();
186     }
187   }
188
189   public void setAutoCommit(boolean autoCommit)
190     throws SQLException
191   {
192     Profiler profiler = _profilerPoint.start();
193
194     try {
195       _connection.setAutoCommit(autoCommit);
196     }
197     finally {
198       profiler.finish();
199     }
200   }
201
202   public boolean getAutoCommit()
203     throws SQLException
204   {
205     Profiler profiler = _profilerPoint.start();
206
207     try {
208       return _connection.getAutoCommit();
209     }
210     finally {
211       profiler.finish();
212     }
213   }
214
215   public void commit()
216     throws SQLException
217   {
218     Profiler profiler = _profilerPoint.start();
219
220     try {
221       _connection.commit();
222     }
223     finally {
224       profiler.finish();
225     }
226   }
227
228   public void rollback()
229     throws SQLException
230   {
231     Profiler profiler = _profilerPoint.start();
232
233     try {
234       _connection.rollback();
235     }
236     finally {
237       profiler.finish();
238     }
239   }
240
241   public boolean isClosed()
242     throws SQLException
243   {
244     Profiler profiler = _profilerPoint.start();
245
246     try {
247       return _connection.isClosed();
248     }
249     finally {
250       profiler.finish();
251     }
252   }
253
254   public DatabaseMetaData getMetaData()
255     throws SQLException
256   {
257     Profiler profiler = _profilerPoint.start();
258
259     try {
260       return _connection.getMetaData();
261     }
262     finally {
263       profiler.finish();
264     }
265   }
266
267   public void setReadOnly(boolean readOnly)
268     throws SQLException
269   {
270     Profiler profiler = _profilerPoint.start();
271
272     try {
273       _connection.setReadOnly(readOnly);
274     }
275     finally {
276       profiler.finish();
277     }
278   }
279
280   public boolean isReadOnly()
281     throws SQLException
282   {
283     Profiler profiler = _profilerPoint.start();
284
285     try {
286       return _connection.isReadOnly();
287     }
288     finally {
289       profiler.finish();
290     }
291   }
292
293   public void setCatalog(String JavaDoc catalog)
294     throws SQLException
295   {
296     Profiler profiler = _profilerPoint.start();
297
298     try {
299       _connection.setCatalog(catalog);
300     }
301     finally {
302       profiler.finish();
303     }
304   }
305
306   public String JavaDoc getCatalog()
307     throws SQLException
308   {
309     Profiler profiler = _profilerPoint.start();
310
311     try {
312       return _connection.getCatalog();
313     }
314     finally {
315       profiler.finish();
316     }
317   }
318
319   public void setTransactionIsolation(int level)
320     throws SQLException
321   {
322     Profiler profiler = _profilerPoint.start();
323
324     try {
325       _connection.setTransactionIsolation(level);
326     }
327     finally {
328       profiler.finish();
329     }
330   }
331
332   public int getTransactionIsolation()
333     throws SQLException
334   {
335     Profiler profiler = _profilerPoint.start();
336
337     try {
338       return _connection.getTransactionIsolation();
339     }
340     finally {
341       profiler.finish();
342     }
343   }
344
345   public SQLWarning getWarnings()
346     throws SQLException
347   {
348     Profiler profiler = _profilerPoint.start();
349
350     try {
351       return _connection.getWarnings();
352     }
353     finally {
354       profiler.finish();
355     }
356   }
357
358   public void clearWarnings()
359     throws SQLException
360   {
361     Profiler profiler = _profilerPoint.start();
362
363     try {
364       _connection.clearWarnings();
365     }
366     finally {
367       profiler.finish();
368     }
369   }
370
371   public Map JavaDoc<String JavaDoc, Class JavaDoc<?>> getTypeMap()
372     throws SQLException
373   {
374     Profiler profiler = _profilerPoint.start();
375
376     try {
377       return _connection.getTypeMap();
378     }
379     finally {
380       profiler.finish();
381     }
382   }
383
384   public void setTypeMap(Map JavaDoc<String JavaDoc, Class JavaDoc<?>> map)
385     throws SQLException
386   {
387     Profiler profiler = _profilerPoint.start();
388
389     try {
390       _connection.setTypeMap(map);
391     }
392     finally {
393       profiler.finish();
394     }
395   }
396
397   public void setHoldability(int holdability)
398     throws SQLException
399   {
400     Profiler profiler = _profilerPoint.start();
401
402     try {
403       _connection.setHoldability(holdability);
404     }
405     finally {
406       profiler.finish();
407     }
408   }
409
410   public int getHoldability()
411     throws SQLException
412   {
413     Profiler profiler = _profilerPoint.start();
414
415     try {
416       return _connection.getHoldability();
417     }
418     finally {
419       profiler.finish();
420     }
421   }
422
423   public Savepoint setSavepoint()
424     throws SQLException
425   {
426     Profiler profiler = _profilerPoint.start();
427
428     try {
429       return _connection.setSavepoint();
430     }
431     finally {
432       profiler.finish();
433     }
434   }
435
436   public Savepoint setSavepoint(String JavaDoc name)
437     throws SQLException
438   {
439     Profiler profiler = _profilerPoint.start();
440
441     try {
442       return _connection.setSavepoint(name);
443     }
444     finally {
445       profiler.finish();
446     }
447   }
448
449   public void rollback(Savepoint savepoint)
450     throws SQLException
451   {
452     Profiler profiler = _profilerPoint.start();
453
454     try {
455       _connection.rollback(savepoint);
456     }
457     finally {
458       profiler.finish();
459     }
460   }
461
462   public void releaseSavepoint(Savepoint savepoint)
463     throws SQLException
464   {
465     Profiler profiler = _profilerPoint.start();
466
467     try {
468       _connection.releaseSavepoint(savepoint);
469     }
470     finally {
471       profiler.finish();
472     }
473   }
474
475   public void close()
476     throws SQLException
477   {
478     Profiler profiler = _profilerPoint.start();
479
480     try {
481       _connection.close();
482     }
483     finally {
484       profiler.finish();
485     }
486   }
487
488   public String JavaDoc toString()
489   {
490     return "ConnectionWrapper[" + _profilerPoint.getName() + "]";
491   }
492 }
493
Popular Tags