001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. 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: package org.apache.commons.dbutils;
018:
019: import java.lang.reflect.InvocationHandler;
020: import java.lang.reflect.Proxy;
021: import java.sql.CallableStatement;
022: import java.sql.Connection;
023: import java.sql.Driver;
024: import java.sql.PreparedStatement;
025: import java.sql.ResultSet;
026: import java.sql.ResultSetMetaData;
027: import java.sql.Statement;
028:
029: /**
030: * Creates proxy implementations of JDBC interfaces. This avoids
031: * incompatibilities between the JDBC 2 and JDBC 3 interfaces. This class is
032: * thread safe.
033: *
034: * @see java.lang.reflect.Proxy
035: * @see java.lang.reflect.InvocationHandler
036: */
037: public class ProxyFactory {
038:
039: /**
040: * Class[] for CallableStatement interface.
041: */
042: private static final Class[] callableStatementClass = new Class[] { CallableStatement.class };
043:
044: /**
045: * Class[] for Connection interface.
046: */
047: private static final Class[] connectionClass = new Class[] { Connection.class };
048:
049: /**
050: * Class[] for Driver interface.
051: */
052: private static final Class[] driverClass = new Class[] { Driver.class };
053:
054: /**
055: * The Singleton instance of this class.
056: */
057: private static final ProxyFactory instance = new ProxyFactory();
058:
059: /**
060: * Class[] for ResultSetMetaData interface.
061: */
062: private static final Class[] metaClass = new Class[] { ResultSetMetaData.class };
063:
064: /**
065: * Class[] for PreparedStatement interface.
066: */
067: private static final Class[] preparedStatementClass = new Class[] { PreparedStatement.class };
068:
069: /**
070: * Class[] for ResultSet interface.
071: */
072: private static final Class[] resultSetClass = new Class[] { ResultSet.class };
073:
074: /**
075: * Class[] for Statement interface.
076: */
077: private static final Class[] statementClass = new Class[] { Statement.class };
078:
079: /**
080: * Returns the Singleton instance of this class.
081: *
082: * @return singleton instance
083: */
084: public static ProxyFactory instance() {
085: return instance;
086: }
087:
088: /**
089: * Protected constructor for ProxyFactory subclasses to use.
090: */
091: protected ProxyFactory() {
092: super ();
093: }
094:
095: /**
096: * Creates a new proxy <code>CallableStatement</code> object.
097: * @param handler The handler that intercepts/overrides method calls.
098: * @return proxied CallableStatement
099: */
100: public CallableStatement createCallableStatement(
101: InvocationHandler handler) {
102: return (CallableStatement) Proxy.newProxyInstance(handler
103: .getClass().getClassLoader(), callableStatementClass,
104: handler);
105: }
106:
107: /**
108: * Creates a new proxy <code>Connection</code> object.
109: * @param handler The handler that intercepts/overrides method calls.
110: * @return proxied Connection
111: */
112: public Connection createConnection(InvocationHandler handler) {
113: return (Connection) Proxy.newProxyInstance(handler.getClass()
114: .getClassLoader(), connectionClass, handler);
115: }
116:
117: /**
118: * Creates a new proxy <code>Driver</code> object.
119: * @param handler The handler that intercepts/overrides method calls.
120: * @return proxied Driver
121: */
122: public Driver createDriver(InvocationHandler handler) {
123: return (Driver) Proxy.newProxyInstance(handler.getClass()
124: .getClassLoader(), driverClass, handler);
125: }
126:
127: /**
128: * Creates a new proxy <code>PreparedStatement</code> object.
129: * @param handler The handler that intercepts/overrides method calls.
130: * @return proxied PreparedStatement
131: */
132: public PreparedStatement createPreparedStatement(
133: InvocationHandler handler) {
134: return (PreparedStatement) Proxy.newProxyInstance(handler
135: .getClass().getClassLoader(), preparedStatementClass,
136: handler);
137: }
138:
139: /**
140: * Creates a new proxy <code>ResultSet</code> object.
141: * @param handler The handler that intercepts/overrides method calls.
142: * @return proxied ResultSet
143: */
144: public ResultSet createResultSet(InvocationHandler handler) {
145: return (ResultSet) Proxy.newProxyInstance(handler.getClass()
146: .getClassLoader(), resultSetClass, handler);
147: }
148:
149: /**
150: * Creates a new proxy <code>ResultSetMetaData</code> object.
151: * @param handler The handler that intercepts/overrides method calls.
152: * @return proxied ResultSetMetaData
153: */
154: public ResultSetMetaData createResultSetMetaData(
155: InvocationHandler handler) {
156: return (ResultSetMetaData) Proxy.newProxyInstance(handler
157: .getClass().getClassLoader(), metaClass, handler);
158: }
159:
160: /**
161: * Creates a new proxy <code>Statement</code> object.
162: * @param handler The handler that intercepts/overrides method calls.
163: * @return proxied Statement
164: */
165: public Statement createStatement(InvocationHandler handler) {
166: return (Statement) Proxy.newProxyInstance(handler.getClass()
167: .getClassLoader(), statementClass, handler);
168: }
169:
170: }
|