1. /*
  2. * $Header: /home/cvs/jakarta-commons/dbutils/src/java/org/apache/commons/dbutils/ProxyFactory.java,v 1.3 2003/11/09 04:54:32 dgraham Exp $
  3. * $Revision: 1.3 $
  4. * $Date: 2003/11/09 04:54:32 $
  5. *
  6. * ====================================================================
  7. *
  8. * The Apache Software License, Version 1.1
  9. *
  10. * Copyright (c) 2003 The Apache Software Foundation. All rights
  11. * reserved.
  12. *
  13. * Redistribution and use in source and binary forms, with or without
  14. * modification, are permitted provided that the following conditions
  15. * are met:
  16. *
  17. * 1. Redistributions of source code must retain the above copyright
  18. * notice, this list of conditions and the following disclaimer.
  19. *
  20. * 2. Redistributions in binary form must reproduce the above copyright
  21. * notice, this list of conditions and the following disclaimer in
  22. * the documentation and/or other materials provided with the
  23. * distribution.
  24. *
  25. * 3. The end-user documentation included with the redistribution, if
  26. * any, must include the following acknowledgement:
  27. * "This product includes software developed by the
  28. * Apache Software Foundation (http://www.apache.org/)."
  29. * Alternately, this acknowledgement may appear in the software itself,
  30. * if and wherever such third-party acknowledgements normally appear.
  31. *
  32. * 4. The names "The Jakarta Project", "Commons", and "Apache Software
  33. * Foundation" must not be used to endorse or promote products derived
  34. * from this software without prior written permission. For written
  35. * permission, please contact apache@apache.org.
  36. *
  37. * 5. Products derived from this software may not be called "Apache"
  38. * nor may "Apache" appear in their names without prior written
  39. * permission of the Apache Software Foundation.
  40. *
  41. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
  42. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  43. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  44. * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
  45. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  46. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  47. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
  48. * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  49. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  50. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  51. * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  52. * SUCH DAMAGE.
  53. * ====================================================================
  54. *
  55. * This software consists of voluntary contributions made by many
  56. * individuals on behalf of the Apache Software Foundation. For more
  57. * information on the Apache Software Foundation, please see
  58. * <http://www.apache.org/>.
  59. *
  60. */
  61. package org.apache.commons.dbutils;
  62. import java.lang.reflect.InvocationHandler;
  63. import java.lang.reflect.Proxy;
  64. import java.sql.CallableStatement;
  65. import java.sql.Connection;
  66. import java.sql.Driver;
  67. import java.sql.PreparedStatement;
  68. import java.sql.ResultSet;
  69. import java.sql.ResultSetMetaData;
  70. import java.sql.Statement;
  71. /**
  72. * Creates proxy implementations of JDBC interfaces. This avoids
  73. * incompatibilities between the JDBC 2 and JDBC 3 interfaces. This class is
  74. * thread safe.
  75. *
  76. * @see java.lang.reflect.Proxy
  77. * @see java.lang.reflect.InvocationHandler
  78. *
  79. * @author David Graham
  80. */
  81. public class ProxyFactory {
  82. /**
  83. * Class[] for CallableStatement interface.
  84. */
  85. private static final Class[] callableStatementClass =
  86. new Class[] { CallableStatement.class };
  87. /**
  88. * Class[] for Connection interface.
  89. */
  90. private static final Class[] connectionClass =
  91. new Class[] { Connection.class };
  92. /**
  93. * Class[] for Driver interface.
  94. */
  95. private static final Class[] driverClass = new Class[] { Driver.class };
  96. /**
  97. * The Singleton instance of this class.
  98. */
  99. private static final ProxyFactory instance = new ProxyFactory();
  100. /**
  101. * Class[] for ResultSetMetaData interface.
  102. */
  103. private static final Class[] metaClass =
  104. new Class[] { ResultSetMetaData.class };
  105. /**
  106. * Class[] for PreparedStatement interface.
  107. */
  108. private static final Class[] preparedStatementClass =
  109. new Class[] { PreparedStatement.class };
  110. /**
  111. * Class[] for ResultSet interface.
  112. */
  113. private static final Class[] resultSetClass =
  114. new Class[] { ResultSet.class };
  115. /**
  116. * Class[] for Statement interface.
  117. */
  118. private static final Class[] statementClass =
  119. new Class[] { Statement.class };
  120. /**
  121. * Returns the Singleton instance of this class.
  122. */
  123. public static ProxyFactory instance() {
  124. return instance;
  125. }
  126. /**
  127. * Protected constructor for ProxyFactory subclasses to use.
  128. */
  129. protected ProxyFactory() {
  130. super();
  131. }
  132. /**
  133. * Creates a new proxy <code>CallableStatement</code> object.
  134. * @param handler The handler that intercepts/overrides method calls.
  135. */
  136. public CallableStatement createCallableStatement(InvocationHandler handler) {
  137. return (CallableStatement) Proxy.newProxyInstance(
  138. handler.getClass().getClassLoader(),
  139. callableStatementClass,
  140. handler);
  141. }
  142. /**
  143. * Creates a new proxy <code>Connection</code> object.
  144. * @param handler The handler that intercepts/overrides method calls.
  145. */
  146. public Connection createConnection(InvocationHandler handler) {
  147. return (Connection) Proxy.newProxyInstance(
  148. handler.getClass().getClassLoader(),
  149. connectionClass,
  150. handler);
  151. }
  152. /**
  153. * Creates a new proxy <code>Driver</code> object.
  154. * @param handler The handler that intercepts/overrides method calls.
  155. */
  156. public Driver createDriver(InvocationHandler handler) {
  157. return (Driver) Proxy.newProxyInstance(
  158. handler.getClass().getClassLoader(),
  159. driverClass,
  160. handler);
  161. }
  162. /**
  163. * Creates a new proxy <code>PreparedStatement</code> object.
  164. * @param handler The handler that intercepts/overrides method calls.
  165. */
  166. public PreparedStatement createPreparedStatement(InvocationHandler handler) {
  167. return (PreparedStatement) Proxy.newProxyInstance(
  168. handler.getClass().getClassLoader(),
  169. preparedStatementClass,
  170. handler);
  171. }
  172. /**
  173. * Creates a new proxy <code>ResultSet</code> object.
  174. * @param handler The handler that intercepts/overrides method calls.
  175. */
  176. public ResultSet createResultSet(InvocationHandler handler) {
  177. return (ResultSet) Proxy.newProxyInstance(
  178. handler.getClass().getClassLoader(),
  179. resultSetClass,
  180. handler);
  181. }
  182. /**
  183. * Creates a new proxy <code>ResultSetMetaData</code> object.
  184. * @param handler The handler that intercepts/overrides method calls.
  185. */
  186. public ResultSetMetaData createResultSetMetaData(InvocationHandler handler) {
  187. return (ResultSetMetaData) Proxy.newProxyInstance(
  188. handler.getClass().getClassLoader(),
  189. metaClass,
  190. handler);
  191. }
  192. /**
  193. * Creates a new proxy <code>Statement</code> object.
  194. * @param handler The handler that intercepts/overrides method calls.
  195. */
  196. public Statement createStatement(InvocationHandler handler) {
  197. return (Statement) Proxy.newProxyInstance(
  198. handler.getClass().getClassLoader(),
  199. statementClass,
  200. handler);
  201. }
  202. }