01: package org.geotools.data.jdbc.datasource;
02:
03: import java.sql.Connection;
04: import java.sql.Statement;
05:
06: import org.apache.commons.dbcp.DelegatingConnection;
07: import org.apache.commons.dbcp.DelegatingStatement;
08:
09: /**
10: * Unwraps DBCP managed connections
11: *
12: * @author Andrea Aime - TOPP
13: *
14: */
15: public class DBCPUnWrapper implements UnWrapper {
16:
17: public boolean canUnwrap(Connection conn) {
18: return conn instanceof DelegatingConnection;
19: }
20:
21: public Connection unwrap(Connection conn) {
22: if (!canUnwrap(conn))
23: throw new IllegalArgumentException(
24: "This unwrapper can only handle instances of "
25: + DelegatingConnection.class);
26: Connection unwrapped = ((DelegatingConnection) conn)
27: .getInnermostDelegate();
28: if (unwrapped == null)
29: throw new RuntimeException(
30: "Could not unwrap connection. Is the DBCP pool configured "
31: + "to allow access to underlying connections?");
32: return unwrapped;
33: }
34:
35: public boolean canUnwrap(Statement st) {
36: return st instanceof DelegatingStatement;
37: }
38:
39: public Statement unwrap(Statement statement) {
40: if (!canUnwrap(statement))
41: throw new IllegalArgumentException(
42: "This unwrapper can only handle instances of "
43: + DelegatingStatement.class);
44: Statement unwrapped = ((DelegatingStatement) statement)
45: .getInnermostDelegate();
46: if (unwrapped == null)
47: throw new RuntimeException(
48: "Could not unwrap connection. Is the DBCP pool configured "
49: + "to allow access to underlying connections?");
50: return unwrapped;
51: }
52:
53: }
|