1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.dbutils;
18
19 import java.lang.reflect.InvocationHandler;
20 import java.lang.reflect.Proxy;
21 import java.sql.CallableStatement;
22 import java.sql.Connection;
23 import java.sql.Driver;
24 import java.sql.PreparedStatement;
25 import java.sql.ResultSet;
26 import java.sql.ResultSetMetaData;
27 import java.sql.Statement;
28
29 /**
30 * Creates proxy implementations of JDBC interfaces. This avoids
31 * incompatibilities between the JDBC 2 and JDBC 3 interfaces. This class is
32 * thread safe.
33 *
34 * @see java.lang.reflect.Proxy
35 * @see java.lang.reflect.InvocationHandler
36 */
37 public class ProxyFactory {
38
39 /**
40 * Class[] for CallableStatement interface.
41 */
42 private static final Class[] callableStatementClass =
43 new Class[] { CallableStatement.class };
44
45 /**
46 * Class[] for Connection interface.
47 */
48 private static final Class[] connectionClass =
49 new Class[] { Connection.class };
50
51 /**
52 * Class[] for Driver interface.
53 */
54 private static final Class[] driverClass = new Class[] { Driver.class };
55
56 /**
57 * The Singleton instance of this class.
58 */
59 private static final ProxyFactory instance = new ProxyFactory();
60
61 /**
62 * Class[] for ResultSetMetaData interface.
63 */
64 private static final Class[] metaClass =
65 new Class[] { ResultSetMetaData.class };
66
67 /**
68 * Class[] for PreparedStatement interface.
69 */
70 private static final Class[] preparedStatementClass =
71 new Class[] { PreparedStatement.class };
72
73 /**
74 * Class[] for ResultSet interface.
75 */
76 private static final Class[] resultSetClass =
77 new Class[] { ResultSet.class };
78
79 /**
80 * Class[] for Statement interface.
81 */
82 private static final Class[] statementClass =
83 new Class[] { Statement.class };
84
85 /**
86 * Returns the Singleton instance of this class.
87 *
88 * @return singleton instance
89 */
90 public static ProxyFactory instance() {
91 return instance;
92 }
93
94 /**
95 * Protected constructor for ProxyFactory subclasses to use.
96 */
97 protected ProxyFactory() {
98 super();
99 }
100
101 /**
102 * Creates a new proxy <code>CallableStatement</code> object.
103 * @param handler The handler that intercepts/overrides method calls.
104 * @return proxied CallableStatement
105 */
106 public CallableStatement createCallableStatement(InvocationHandler handler) {
107 return (CallableStatement) Proxy.newProxyInstance(
108 handler.getClass().getClassLoader(),
109 callableStatementClass,
110 handler);
111 }
112
113 /**
114 * Creates a new proxy <code>Connection</code> object.
115 * @param handler The handler that intercepts/overrides method calls.
116 * @return proxied Connection
117 */
118 public Connection createConnection(InvocationHandler handler) {
119 return (Connection) Proxy.newProxyInstance(
120 handler.getClass().getClassLoader(),
121 connectionClass,
122 handler);
123 }
124
125 /**
126 * Creates a new proxy <code>Driver</code> object.
127 * @param handler The handler that intercepts/overrides method calls.
128 * @return proxied Driver
129 */
130 public Driver createDriver(InvocationHandler handler) {
131 return (Driver) Proxy.newProxyInstance(
132 handler.getClass().getClassLoader(),
133 driverClass,
134 handler);
135 }
136
137 /**
138 * Creates a new proxy <code>PreparedStatement</code> object.
139 * @param handler The handler that intercepts/overrides method calls.
140 * @return proxied PreparedStatement
141 */
142 public PreparedStatement createPreparedStatement(InvocationHandler handler) {
143 return (PreparedStatement) Proxy.newProxyInstance(
144 handler.getClass().getClassLoader(),
145 preparedStatementClass,
146 handler);
147 }
148
149 /**
150 * Creates a new proxy <code>ResultSet</code> object.
151 * @param handler The handler that intercepts/overrides method calls.
152 * @return proxied ResultSet
153 */
154 public ResultSet createResultSet(InvocationHandler handler) {
155 return (ResultSet) Proxy.newProxyInstance(
156 handler.getClass().getClassLoader(),
157 resultSetClass,
158 handler);
159 }
160
161 /**
162 * Creates a new proxy <code>ResultSetMetaData</code> object.
163 * @param handler The handler that intercepts/overrides method calls.
164 * @return proxied ResultSetMetaData
165 */
166 public ResultSetMetaData createResultSetMetaData(InvocationHandler handler) {
167 return (ResultSetMetaData) Proxy.newProxyInstance(
168 handler.getClass().getClassLoader(),
169 metaClass,
170 handler);
171 }
172
173 /**
174 * Creates a new proxy <code>Statement</code> object.
175 * @param handler The handler that intercepts/overrides method calls.
176 * @return proxied Statement
177 */
178 public Statement createStatement(InvocationHandler handler) {
179 return (Statement) Proxy.newProxyInstance(
180 handler.getClass().getClassLoader(),
181 statementClass,
182 handler);
183 }
184
185 }