1 package org.apache.ojb.broker.util;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 import java.sql.Array;
19 import java.sql.Blob;
20 import java.sql.CallableStatement;
21 import java.sql.Clob;
22 import java.sql.Connection;
23 import java.sql.DatabaseMetaData;
24 import java.sql.NClob;
25 import java.sql.PreparedStatement;
26 import java.sql.SQLClientInfoException;
27 import java.sql.SQLException;
28 import java.sql.SQLWarning;
29 import java.sql.SQLXML;
30 import java.sql.Statement;
31 import java.sql.Struct;
32 import java.util.Map;
33 import java.util.Properties;
34
35 import org.apache.commons.lang.builder.ToStringBuilder;
36 import org.apache.commons.lang.builder.ToStringStyle;
37
38
39
40
41
42 public class WrappedConnection implements Connection
43 {
44 private Connection _conn = null;
45 private boolean _isClosed = false;
46
47 public WrappedConnection(Connection c)
48 {
49 _conn = c;
50 }
51
52
53
54
55
56 public Connection getDelegate()
57 {
58 return _conn;
59 }
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76 public Connection getInnermostDelegate()
77 {
78 Connection c = _conn;
79 while (c != null && c instanceof WrappedConnection)
80 {
81 c = ((WrappedConnection) c).getDelegate();
82 if (this == c)
83 {
84 return null;
85 }
86 }
87 return c;
88 }
89
90
91 public void setDelegate(Connection c)
92 {
93 _conn = c;
94 }
95
96 protected void checkOpen() throws SQLException
97 {
98 if (_isClosed)
99 {
100 throw new SQLException("Connection is closed. " + this);
101 }
102 }
103
104
105
106
107 public void activateConnection()
108 {
109 _isClosed = false;
110 if (_conn instanceof WrappedConnection)
111 {
112 ((WrappedConnection) _conn).activateConnection();
113 }
114 }
115
116
117
118
119 public void passivateConnection() throws SQLException
120 {
121 _isClosed = true;
122 if (_conn instanceof WrappedConnection)
123 {
124 ((WrappedConnection) _conn).passivateConnection();
125 }
126 }
127
128 public String toString()
129 {
130 return new ToStringBuilder(this, ToStringStyle.MULTI_LINE_STYLE)
131 .append("wrapped connection", (_conn != null ? _conn.toString() : null))
132 .toString();
133 }
134
135
136
137
138
139 public void close() throws SQLException
140 {
141 passivateConnection();
142 _conn.close();
143 }
144
145 public boolean isClosed() throws SQLException
146 {
147 if (_isClosed || _conn.isClosed())
148 {
149 return true;
150 }
151 return false;
152 }
153
154 public Statement createStatement() throws SQLException
155 {
156 checkOpen();
157 return _conn.createStatement();
158 }
159
160 public Statement createStatement(int resultSetType,
161 int resultSetConcurrency)
162 throws SQLException
163 {
164 checkOpen();
165 return _conn.createStatement(resultSetType, resultSetConcurrency);
166 }
167
168 public PreparedStatement prepareStatement(String sql) throws SQLException
169 {
170 checkOpen();
171 return _conn.prepareStatement(sql);
172 }
173
174 public PreparedStatement prepareStatement(String sql,
175 int resultSetType,
176 int resultSetConcurrency)
177 throws SQLException
178 {
179 checkOpen();
180 return _conn.prepareStatement(sql, resultSetType, resultSetConcurrency);
181 }
182
183 public CallableStatement prepareCall(String sql) throws SQLException
184 {
185 checkOpen();
186 return _conn.prepareCall(sql);
187 }
188
189 public CallableStatement prepareCall(String sql,
190 int resultSetType,
191 int resultSetConcurrency)
192 throws SQLException
193 {
194 checkOpen();
195 return _conn.prepareCall(sql, resultSetType, resultSetConcurrency);
196 }
197
198 public void clearWarnings() throws SQLException
199 {
200 checkOpen();
201 _conn.clearWarnings();
202 }
203
204 public void commit() throws SQLException
205 {
206 checkOpen();
207 _conn.commit();
208 }
209
210 public boolean getAutoCommit() throws SQLException
211 {
212 checkOpen();
213 return _conn.getAutoCommit();
214 }
215
216 public String getCatalog() throws SQLException
217 {
218 checkOpen();
219 return _conn.getCatalog();
220 }
221
222 public DatabaseMetaData getMetaData() throws SQLException
223 {
224 checkOpen();
225 return _conn.getMetaData();
226 }
227
228 public int getTransactionIsolation() throws SQLException
229 {
230 checkOpen();
231 return _conn.getTransactionIsolation();
232 }
233
234 public Map getTypeMap() throws SQLException
235 {
236 checkOpen();
237 return _conn.getTypeMap();
238 }
239
240 public SQLWarning getWarnings() throws SQLException
241 {
242 checkOpen();
243 return _conn.getWarnings();
244 }
245
246 public boolean isReadOnly() throws SQLException
247 {
248 checkOpen();
249 return _conn.isReadOnly();
250 }
251
252 public String nativeSQL(String sql) throws SQLException
253 {
254 checkOpen();
255 return _conn.nativeSQL(sql);
256 }
257
258 public void rollback() throws SQLException
259 {
260 checkOpen();
261 _conn.rollback();
262 }
263
264 public void setAutoCommit(boolean autoCommit) throws SQLException
265 {
266 checkOpen();
267 _conn.setAutoCommit(autoCommit);
268 }
269
270 public void setCatalog(String catalog) throws SQLException
271 {
272 checkOpen();
273 _conn.setCatalog(catalog);
274 }
275
276 public void setReadOnly(boolean readOnly) throws SQLException
277 {
278 checkOpen();
279 _conn.setReadOnly(readOnly);
280 }
281
282 public void setTransactionIsolation(int level) throws SQLException
283 {
284 checkOpen();
285 _conn.setTransactionIsolation(level);
286 }
287
288 @SuppressWarnings("unchecked")
289 public void setTypeMap(@SuppressWarnings("rawtypes") Map map) throws SQLException
290 {
291 checkOpen();
292 _conn.setTypeMap(map);
293 }
294
295 @Override
296 public boolean isWrapperFor(Class<?> arg0) throws SQLException {
297 checkOpen();
298 return _conn.isWrapperFor(arg0);
299 }
300
301 @Override
302 public <T> T unwrap(Class<T> arg0) throws SQLException {
303 checkOpen();
304 return _conn.unwrap(arg0);
305 }
306
307 @Override
308 public Array createArrayOf(String typeName, Object[] elements)
309 throws SQLException {
310 checkOpen();
311 return _conn.createArrayOf(typeName, elements);
312 }
313
314 @Override
315 public Blob createBlob() throws SQLException {
316 checkOpen();
317 return _conn.createBlob();
318 }
319
320 @Override
321 public Clob createClob() throws SQLException {
322 checkOpen();
323 return _conn.createClob();
324 }
325
326 @Override
327 public NClob createNClob() throws SQLException {
328 checkOpen();
329 return _conn.createNClob();
330 }
331
332 @Override
333 public SQLXML createSQLXML() throws SQLException {
334 checkOpen();
335 return _conn.createSQLXML();
336 }
337
338 @Override
339 public Struct createStruct(String typeName, Object[] attributes)
340 throws SQLException {
341 checkOpen();
342 return _conn.createStruct(typeName, attributes);
343 }
344
345 @Override
346 public Properties getClientInfo() throws SQLException {
347 checkOpen();
348 return _conn.getClientInfo();
349 }
350
351 @Override
352 public String getClientInfo(String name) throws SQLException {
353 checkOpen();
354 return _conn.getClientInfo(name);
355 }
356
357 @Override
358 public boolean isValid(int timeout) throws SQLException {
359 checkOpen();
360 return _conn.isValid(timeout);
361 }
362
363 @Override
364 public void setClientInfo(Properties properties)
365 throws SQLClientInfoException {
366 _conn.setClientInfo(properties);
367
368 }
369
370 @Override
371 public void setClientInfo(String name, String value)
372 throws SQLClientInfoException {
373 _conn.setClientInfo(name, value);
374
375 }
376
377
378
379
380
381
382 public int getHoldability() throws SQLException {
383 checkOpen();
384 return _conn.getHoldability();
385 }
386
387 public void setHoldability(int holdability) throws SQLException {
388 checkOpen();
389 _conn.setHoldability(holdability);
390 }
391
392 public java.sql.Savepoint setSavepoint() throws SQLException {
393 checkOpen();
394 return _conn.setSavepoint();
395 }
396
397 public java.sql.Savepoint setSavepoint(String name) throws SQLException {
398 checkOpen();
399 return _conn.setSavepoint(name);
400 }
401
402 public void rollback(java.sql.Savepoint savepoint) throws SQLException {
403 checkOpen();
404 _conn.rollback(savepoint);
405 }
406
407 public void releaseSavepoint(java.sql.Savepoint savepoint) throws SQLException {
408 checkOpen();
409 _conn.releaseSavepoint(savepoint);
410 }
411
412 public Statement createStatement(int resultSetType,
413 int resultSetConcurrency,
414 int resultSetHoldability)
415 throws SQLException {
416 checkOpen();
417 return _conn.createStatement(resultSetType, resultSetConcurrency,
418 resultSetHoldability);
419 }
420
421 public PreparedStatement prepareStatement(String sql, int resultSetType,
422 int resultSetConcurrency,
423 int resultSetHoldability)
424 throws SQLException {
425 checkOpen();
426 return _conn.prepareStatement(sql, resultSetType,
427 resultSetConcurrency,
428 resultSetHoldability);
429 }
430
431 public CallableStatement prepareCall(String sql, int resultSetType,
432 int resultSetConcurrency,
433 int resultSetHoldability)
434 throws SQLException {
435 checkOpen();
436 return _conn.prepareCall(sql, resultSetType,
437 resultSetConcurrency,
438 resultSetHoldability);
439 }
440
441 public PreparedStatement prepareStatement(String sql, int autoGeneratedKeys)
442 throws SQLException {
443 checkOpen();
444 return _conn.prepareStatement(sql, autoGeneratedKeys);
445 }
446
447 public PreparedStatement prepareStatement(String sql, int columnIndexes[])
448 throws SQLException {
449 checkOpen();
450 return _conn.prepareStatement(sql, columnIndexes);
451 }
452
453 public PreparedStatement prepareStatement(String sql, String columnNames[])
454 throws SQLException {
455 checkOpen();
456 return _conn.prepareStatement(sql, columnNames);
457 }
458
459
460 }