001: package org.apache.ojb.broker.util;
002:
003: /* Copyright 2002-2005 The Apache Software Foundation
004: *
005: * Licensed under the Apache License, Version 2.0 (the "License");
006: * you may not use this file except in compliance with the License.
007: * You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: import java.sql.CallableStatement;
019: import java.sql.Connection;
020: import java.sql.DatabaseMetaData;
021: import java.sql.PreparedStatement;
022: import java.sql.SQLException;
023: import java.sql.SQLWarning;
024: import java.sql.Statement;
025: import java.util.Map;
026:
027: import org.apache.commons.lang.builder.ToStringBuilder;
028: import org.apache.commons.lang.builder.ToStringStyle;
029:
030: /**
031: * Wrapper class for connections.
032: * Simplified version of {@link org.apache.commons.dbcp.DelegatingConnection}
033: */
034: public class WrappedConnection implements Connection {
035: private Connection _conn = null;
036: private boolean _isClosed = false;
037:
038: public WrappedConnection(Connection c) {
039: _conn = c;
040: }
041:
042: /**
043: * Returns my underlying {@link Connection}.
044: * @return my underlying {@link Connection}.
045: */
046: public Connection getDelegate() {
047: return _conn;
048: }
049:
050: /**
051: * If my underlying <tt>Connection</tt> is not a
052: * <tt>WrappedConnection</tt>, returns it,
053: * otherwise recursively invokes this method on
054: * my delegate.
055: * <p>
056: * Hence this method will return the first
057: * delegate that is not a <tt>WrappedConnection</tt>,
058: * or <tt>null</tt> when no non-<tt>WrappedConnection</tt>
059: * delegate can be found by transversing this chain.
060: * <p>
061: * This method is useful when you may have nested
062: * <tt>WrappedConnection</tt>s, and you want to make
063: * sure to obtain a "genuine" {@link java.sql.Connection}.
064: */
065: public Connection getInnermostDelegate() {
066: Connection c = _conn;
067: while (c != null && c instanceof WrappedConnection) {
068: c = ((WrappedConnection) c).getDelegate();
069: if (this == c) {
070: return null;
071: }
072: }
073: return c;
074: }
075:
076: /** Sets my delegate. */
077: public void setDelegate(Connection c) {
078: _conn = c;
079: }
080:
081: protected void checkOpen() throws SQLException {
082: if (_isClosed) {
083: throw new SQLException("Connection is closed. " + this );
084: }
085: }
086:
087: /**
088: * Activate the connection
089: */
090: public void activateConnection() {
091: _isClosed = false;
092: if (_conn instanceof WrappedConnection) {
093: ((WrappedConnection) _conn).activateConnection();
094: }
095: }
096:
097: /**
098: * Passivate the connection
099: */
100: public void passivateConnection() throws SQLException {
101: _isClosed = true;
102: if (_conn instanceof WrappedConnection) {
103: ((WrappedConnection) _conn).passivateConnection();
104: }
105: }
106:
107: public String toString() {
108: return new ToStringBuilder(this , ToStringStyle.MULTI_LINE_STYLE)
109: .append("wrapped connection",
110: (_conn != null ? _conn.toString() : null))
111: .toString();
112: }
113:
114: /**
115: * Closes the underlying connection, and close
116: * any Statements that were not explicitly closed.
117: */
118: public void close() throws SQLException {
119: passivateConnection();
120: _conn.close();
121: }
122:
123: public boolean isClosed() throws SQLException {
124: if (_isClosed || _conn.isClosed()) {
125: return true;
126: }
127: return false;
128: }
129:
130: public Statement createStatement() throws SQLException {
131: checkOpen();
132: return _conn.createStatement();
133: }
134:
135: public Statement createStatement(int resultSetType,
136: int resultSetConcurrency) throws SQLException {
137: checkOpen();
138: return _conn.createStatement(resultSetType,
139: resultSetConcurrency);
140: }
141:
142: public PreparedStatement prepareStatement(String sql)
143: throws SQLException {
144: checkOpen();
145: return _conn.prepareStatement(sql);
146: }
147:
148: public PreparedStatement prepareStatement(String sql,
149: int resultSetType, int resultSetConcurrency)
150: throws SQLException {
151: checkOpen();
152: return _conn.prepareStatement(sql, resultSetType,
153: resultSetConcurrency);
154: }
155:
156: public CallableStatement prepareCall(String sql)
157: throws SQLException {
158: checkOpen();
159: return _conn.prepareCall(sql);
160: }
161:
162: public CallableStatement prepareCall(String sql, int resultSetType,
163: int resultSetConcurrency) throws SQLException {
164: checkOpen();
165: return _conn.prepareCall(sql, resultSetType,
166: resultSetConcurrency);
167: }
168:
169: public void clearWarnings() throws SQLException {
170: checkOpen();
171: _conn.clearWarnings();
172: }
173:
174: public void commit() throws SQLException {
175: checkOpen();
176: _conn.commit();
177: }
178:
179: public boolean getAutoCommit() throws SQLException {
180: checkOpen();
181: return _conn.getAutoCommit();
182: }
183:
184: public String getCatalog() throws SQLException {
185: checkOpen();
186: return _conn.getCatalog();
187: }
188:
189: public DatabaseMetaData getMetaData() throws SQLException {
190: checkOpen();
191: return _conn.getMetaData();
192: }
193:
194: public int getTransactionIsolation() throws SQLException {
195: checkOpen();
196: return _conn.getTransactionIsolation();
197: }
198:
199: public Map getTypeMap() throws SQLException {
200: checkOpen();
201: return _conn.getTypeMap();
202: }
203:
204: public SQLWarning getWarnings() throws SQLException {
205: checkOpen();
206: return _conn.getWarnings();
207: }
208:
209: public boolean isReadOnly() throws SQLException {
210: checkOpen();
211: return _conn.isReadOnly();
212: }
213:
214: public String nativeSQL(String sql) throws SQLException {
215: checkOpen();
216: return _conn.nativeSQL(sql);
217: }
218:
219: public void rollback() throws SQLException {
220: checkOpen();
221: _conn.rollback();
222: }
223:
224: public void setAutoCommit(boolean autoCommit) throws SQLException {
225: checkOpen();
226: _conn.setAutoCommit(autoCommit);
227: }
228:
229: public void setCatalog(String catalog) throws SQLException {
230: checkOpen();
231: _conn.setCatalog(catalog);
232: }
233:
234: public void setReadOnly(boolean readOnly) throws SQLException {
235: checkOpen();
236: _conn.setReadOnly(readOnly);
237: }
238:
239: public void setTransactionIsolation(int level) throws SQLException {
240: checkOpen();
241: _conn.setTransactionIsolation(level);
242: }
243:
244: public void setTypeMap(Map map) throws SQLException {
245: checkOpen();
246: _conn.setTypeMap(map);
247: }
248:
249: // ------------------- JDBC 3.0 -----------------------------------------
250: // Will be uncommented by the build process on a JDBC 3.0 system
251:
252: //#ifdef JDBC30
253:
254: public int getHoldability() throws SQLException {
255: checkOpen();
256: return _conn.getHoldability();
257: }
258:
259: public void setHoldability(int holdability) throws SQLException {
260: checkOpen();
261: _conn.setHoldability(holdability);
262: }
263:
264: public java.sql.Savepoint setSavepoint() throws SQLException {
265: checkOpen();
266: return _conn.setSavepoint();
267: }
268:
269: public java.sql.Savepoint setSavepoint(String name)
270: throws SQLException {
271: checkOpen();
272: return _conn.setSavepoint(name);
273: }
274:
275: public void rollback(java.sql.Savepoint savepoint)
276: throws SQLException {
277: checkOpen();
278: _conn.rollback(savepoint);
279: }
280:
281: public void releaseSavepoint(java.sql.Savepoint savepoint)
282: throws SQLException {
283: checkOpen();
284: _conn.releaseSavepoint(savepoint);
285: }
286:
287: public Statement createStatement(int resultSetType,
288: int resultSetConcurrency, int resultSetHoldability)
289: throws SQLException {
290: checkOpen();
291: return _conn.createStatement(resultSetType,
292: resultSetConcurrency, resultSetHoldability);
293: }
294:
295: public PreparedStatement prepareStatement(String sql,
296: int resultSetType, int resultSetConcurrency,
297: int resultSetHoldability) throws SQLException {
298: checkOpen();
299: return _conn.prepareStatement(sql, resultSetType,
300: resultSetConcurrency, resultSetHoldability);
301: }
302:
303: public CallableStatement prepareCall(String sql, int resultSetType,
304: int resultSetConcurrency, int resultSetHoldability)
305: throws SQLException {
306: checkOpen();
307: return _conn.prepareCall(sql, resultSetType,
308: resultSetConcurrency, resultSetHoldability);
309: }
310:
311: public PreparedStatement prepareStatement(String sql,
312: int autoGeneratedKeys) throws SQLException {
313: checkOpen();
314: return _conn.prepareStatement(sql, autoGeneratedKeys);
315: }
316:
317: public PreparedStatement prepareStatement(String sql,
318: int columnIndexes[]) throws SQLException {
319: checkOpen();
320: return _conn.prepareStatement(sql, columnIndexes);
321: }
322:
323: public PreparedStatement prepareStatement(String sql,
324: String columnNames[]) throws SQLException {
325: checkOpen();
326: return _conn.prepareStatement(sql, columnNames);
327: }
328:
329: //#endif
330: }
|