KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > ojb > broker > util > WrappedConnection


1 package org.apache.ojb.broker.util;
2
3 /* Copyright 2002-2005 The Apache Software Foundation
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17
18 import java.sql.CallableStatement JavaDoc;
19 import java.sql.Connection JavaDoc;
20 import java.sql.DatabaseMetaData JavaDoc;
21 import java.sql.PreparedStatement JavaDoc;
22 import java.sql.SQLException JavaDoc;
23 import java.sql.SQLWarning JavaDoc;
24 import java.sql.Statement JavaDoc;
25 import java.util.Map JavaDoc;
26
27 import org.apache.commons.lang.builder.ToStringBuilder;
28 import org.apache.commons.lang.builder.ToStringStyle;
29
30 /**
31  * Wrapper class for connections.
32  * Simplified version of {@link org.apache.commons.dbcp.DelegatingConnection}
33  */

34 public class WrappedConnection implements Connection JavaDoc
35 {
36     private Connection JavaDoc _conn = null;
37     private boolean _isClosed = false;
38
39     public WrappedConnection(Connection JavaDoc c)
40     {
41         _conn = c;
42     }
43
44     /**
45      * Returns my underlying {@link Connection}.
46      * @return my underlying {@link Connection}.
47      */

48     public Connection JavaDoc getDelegate()
49     {
50         return _conn;
51     }
52
53     /**
54      * If my underlying <tt>Connection</tt> is not a
55      * <tt>WrappedConnection</tt>, returns it,
56      * otherwise recursively invokes this method on
57      * my delegate.
58      * <p>
59      * Hence this method will return the first
60      * delegate that is not a <tt>WrappedConnection</tt>,
61      * or <tt>null</tt> when no non-<tt>WrappedConnection</tt>
62      * delegate can be found by transversing this chain.
63      * <p>
64      * This method is useful when you may have nested
65      * <tt>WrappedConnection</tt>s, and you want to make
66      * sure to obtain a "genuine" {@link java.sql.Connection}.
67      */

68     public Connection JavaDoc getInnermostDelegate()
69     {
70         Connection JavaDoc c = _conn;
71         while (c != null && c instanceof WrappedConnection)
72         {
73             c = ((WrappedConnection) c).getDelegate();
74             if (this == c)
75             {
76                 return null;
77             }
78         }
79         return c;
80     }
81
82     /** Sets my delegate. */
83     public void setDelegate(Connection JavaDoc c)
84     {
85         _conn = c;
86     }
87
88     protected void checkOpen() throws SQLException JavaDoc
89     {
90         if (_isClosed)
91         {
92             throw new SQLException JavaDoc("Connection is closed. " + this);
93         }
94     }
95
96     /**
97      * Activate the connection
98      */

99     public void activateConnection()
100     {
101         _isClosed = false;
102         if (_conn instanceof WrappedConnection)
103         {
104             ((WrappedConnection) _conn).activateConnection();
105         }
106     }
107
108     /**
109      * Passivate the connection
110      */

111     public void passivateConnection() throws SQLException JavaDoc
112     {
113         _isClosed = true;
114         if (_conn instanceof WrappedConnection)
115         {
116             ((WrappedConnection) _conn).passivateConnection();
117         }
118     }
119
120     public String JavaDoc toString()
121     {
122         return new ToStringBuilder(this, ToStringStyle.MULTI_LINE_STYLE)
123                 .append("wrapped connection", (_conn != null ? _conn.toString() : null))
124                 .toString();
125     }
126
127     /**
128      * Closes the underlying connection, and close
129      * any Statements that were not explicitly closed.
130      */

131     public void close() throws SQLException JavaDoc
132     {
133         passivateConnection();
134         _conn.close();
135     }
136
137     public boolean isClosed() throws SQLException JavaDoc
138     {
139         if (_isClosed || _conn.isClosed())
140         {
141             return true;
142         }
143         return false;
144     }
145
146     public Statement JavaDoc createStatement() throws SQLException JavaDoc
147     {
148         checkOpen();
149         return _conn.createStatement();
150     }
151
152     public Statement JavaDoc createStatement(int resultSetType,
153                                      int resultSetConcurrency)
154             throws SQLException JavaDoc
155     {
156         checkOpen();
157         return _conn.createStatement(resultSetType, resultSetConcurrency);
158     }
159
160     public PreparedStatement JavaDoc prepareStatement(String JavaDoc sql) throws SQLException JavaDoc
161     {
162         checkOpen();
163         return _conn.prepareStatement(sql);
164     }
165
166     public PreparedStatement JavaDoc prepareStatement(String JavaDoc sql,
167                                               int resultSetType,
168                                               int resultSetConcurrency)
169             throws SQLException JavaDoc
170     {
171         checkOpen();
172         return _conn.prepareStatement(sql, resultSetType, resultSetConcurrency);
173     }
174
175     public CallableStatement JavaDoc prepareCall(String JavaDoc sql) throws SQLException JavaDoc
176     {
177         checkOpen();
178         return _conn.prepareCall(sql);
179     }
180
181     public CallableStatement JavaDoc prepareCall(String JavaDoc sql,
182                                          int resultSetType,
183                                          int resultSetConcurrency)
184             throws SQLException JavaDoc
185     {
186         checkOpen();
187         return _conn.prepareCall(sql, resultSetType, resultSetConcurrency);
188     }
189
190     public void clearWarnings() throws SQLException JavaDoc
191     {
192         checkOpen();
193         _conn.clearWarnings();
194     }
195
196     public void commit() throws SQLException JavaDoc
197     {
198         checkOpen();
199         _conn.commit();
200     }
201
202     public boolean getAutoCommit() throws SQLException JavaDoc
203     {
204         checkOpen();
205         return _conn.getAutoCommit();
206     }
207
208     public String JavaDoc getCatalog() throws SQLException JavaDoc
209     {
210         checkOpen();
211         return _conn.getCatalog();
212     }
213
214     public DatabaseMetaData JavaDoc getMetaData() throws SQLException JavaDoc
215     {
216         checkOpen();
217         return _conn.getMetaData();
218     }
219
220     public int getTransactionIsolation() throws SQLException JavaDoc
221     {
222         checkOpen();
223         return _conn.getTransactionIsolation();
224     }
225
226     public Map JavaDoc getTypeMap() throws SQLException JavaDoc
227     {
228         checkOpen();
229         return _conn.getTypeMap();
230     }
231
232     public SQLWarning JavaDoc getWarnings() throws SQLException JavaDoc
233     {
234         checkOpen();
235         return _conn.getWarnings();
236     }
237
238     public boolean isReadOnly() throws SQLException JavaDoc
239     {
240         checkOpen();
241         return _conn.isReadOnly();
242     }
243
244     public String JavaDoc nativeSQL(String JavaDoc sql) throws SQLException JavaDoc
245     {
246         checkOpen();
247         return _conn.nativeSQL(sql);
248     }
249
250     public void rollback() throws SQLException JavaDoc
251     {
252         checkOpen();
253         _conn.rollback();
254     }
255
256     public void setAutoCommit(boolean autoCommit) throws SQLException JavaDoc
257     {
258         checkOpen();
259         _conn.setAutoCommit(autoCommit);
260     }
261
262     public void setCatalog(String JavaDoc catalog) throws SQLException JavaDoc
263     {
264         checkOpen();
265         _conn.setCatalog(catalog);
266     }
267
268     public void setReadOnly(boolean readOnly) throws SQLException JavaDoc
269     {
270         checkOpen();
271         _conn.setReadOnly(readOnly);
272     }
273
274     public void setTransactionIsolation(int level) throws SQLException JavaDoc
275     {
276         checkOpen();
277         _conn.setTransactionIsolation(level);
278     }
279
280     public void setTypeMap(Map JavaDoc map) throws SQLException JavaDoc
281     {
282         checkOpen();
283         _conn.setTypeMap(map);
284     }
285
286
287
288     // ------------------- JDBC 3.0 -----------------------------------------
289
// Will be uncommented by the build process on a JDBC 3.0 system
290

291 //#ifdef JDBC30
292

293     public int getHoldability() throws SQLException JavaDoc {
294         checkOpen();
295         return _conn.getHoldability();
296     }
297
298     public void setHoldability(int holdability) throws SQLException JavaDoc {
299         checkOpen();
300         _conn.setHoldability(holdability);
301     }
302
303     public java.sql.Savepoint JavaDoc setSavepoint() throws SQLException JavaDoc {
304         checkOpen();
305         return _conn.setSavepoint();
306     }
307
308     public java.sql.Savepoint JavaDoc setSavepoint(String JavaDoc name) throws SQLException JavaDoc {
309         checkOpen();
310         return _conn.setSavepoint(name);
311     }
312
313     public void rollback(java.sql.Savepoint JavaDoc savepoint) throws SQLException JavaDoc {
314         checkOpen();
315         _conn.rollback(savepoint);
316     }
317
318     public void releaseSavepoint(java.sql.Savepoint JavaDoc savepoint) throws SQLException JavaDoc {
319         checkOpen();
320         _conn.releaseSavepoint(savepoint);
321     }
322
323     public Statement JavaDoc createStatement(int resultSetType,
324                                      int resultSetConcurrency,
325                                      int resultSetHoldability)
326         throws SQLException JavaDoc {
327         checkOpen();
328         return _conn.createStatement(resultSetType, resultSetConcurrency,
329                                      resultSetHoldability);
330     }
331
332     public PreparedStatement JavaDoc prepareStatement(String JavaDoc sql, int resultSetType,
333                                               int resultSetConcurrency,
334                                               int resultSetHoldability)
335         throws SQLException JavaDoc {
336         checkOpen();
337         return _conn.prepareStatement(sql, resultSetType,
338                                       resultSetConcurrency,
339                                       resultSetHoldability);
340     }
341
342     public CallableStatement JavaDoc prepareCall(String JavaDoc sql, int resultSetType,
343                                          int resultSetConcurrency,
344                                          int resultSetHoldability)
345         throws SQLException JavaDoc {
346         checkOpen();
347         return _conn.prepareCall(sql, resultSetType,
348                                  resultSetConcurrency,
349                                  resultSetHoldability);
350     }
351
352     public PreparedStatement JavaDoc prepareStatement(String JavaDoc sql, int autoGeneratedKeys)
353         throws SQLException JavaDoc {
354         checkOpen();
355         return _conn.prepareStatement(sql, autoGeneratedKeys);
356     }
357
358     public PreparedStatement JavaDoc prepareStatement(String JavaDoc sql, int columnIndexes[])
359         throws SQLException JavaDoc {
360         checkOpen();
361         return _conn.prepareStatement(sql, columnIndexes);
362     }
363
364     public PreparedStatement JavaDoc prepareStatement(String JavaDoc sql, String JavaDoc columnNames[])
365         throws SQLException JavaDoc {
366         checkOpen();
367         return _conn.prepareStatement(sql, columnNames);
368       }
369
370 //#endif
371
}
372
Popular Tags