View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
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       * The Singleton instance of this class.
41       */
42      private static final ProxyFactory instance = new ProxyFactory();
43  
44      /**
45       * Returns the Singleton instance of this class.
46       *
47       * @return singleton instance
48       */
49      public static ProxyFactory instance() {
50          return instance;
51      }
52  
53      /**
54       * Protected constructor for ProxyFactory subclasses to use.
55       */
56      protected ProxyFactory() {
57          super();
58      }
59  
60      /**
61       * Convenience method to generate a single-interface proxy using the handler's classloader
62       *
63       * @param <T> The type of object to proxy
64       * @param type The type of object to proxy
65       * @param handler The handler that intercepts/overrides method calls.
66       * @return proxied object
67       */
68      public <T> T newProxyInstance(Class<T> type, InvocationHandler handler) {
69          return type.cast(Proxy.newProxyInstance(handler.getClass().getClassLoader(), new Class<?>[] {type}, handler));
70      }
71  
72      /**
73       * Creates a new proxy <code>CallableStatement</code> object.
74       * @param handler The handler that intercepts/overrides method calls.
75       * @return proxied CallableStatement
76       */
77      public CallableStatement createCallableStatement(InvocationHandler handler) {
78          return newProxyInstance(CallableStatement.class, handler);
79      }
80  
81      /**
82       * Creates a new proxy <code>Connection</code> object.
83       * @param handler The handler that intercepts/overrides method calls.
84       * @return proxied Connection
85       */
86      public Connection createConnection(InvocationHandler handler) {
87          return newProxyInstance(Connection.class, handler);
88      }
89  
90      /**
91       * Creates a new proxy <code>Driver</code> object.
92       * @param handler The handler that intercepts/overrides method calls.
93       * @return proxied Driver
94       */
95      public Driver createDriver(InvocationHandler handler) {
96          return newProxyInstance(Driver.class, handler);
97      }
98  
99      /**
100      * Creates a new proxy <code>PreparedStatement</code> object.
101      * @param handler The handler that intercepts/overrides method calls.
102      * @return proxied PreparedStatement
103      */
104     public PreparedStatement createPreparedStatement(InvocationHandler handler) {
105         return newProxyInstance(PreparedStatement.class, handler);
106     }
107 
108     /**
109      * Creates a new proxy <code>ResultSet</code> object.
110      * @param handler The handler that intercepts/overrides method calls.
111      * @return proxied ResultSet
112      */
113     public ResultSet createResultSet(InvocationHandler handler) {
114         return newProxyInstance(ResultSet.class, handler);
115     }
116 
117     /**
118      * Creates a new proxy <code>ResultSetMetaData</code> object.
119      * @param handler The handler that intercepts/overrides method calls.
120      * @return proxied ResultSetMetaData
121      */
122     public ResultSetMetaData createResultSetMetaData(InvocationHandler handler) {
123         return newProxyInstance(ResultSetMetaData.class, handler);
124     }
125 
126     /**
127      * Creates a new proxy <code>Statement</code> object.
128      * @param handler The handler that intercepts/overrides method calls.
129      * @return proxied Statement
130      */
131     public Statement createStatement(InvocationHandler handler) {
132         return newProxyInstance(Statement.class, handler);
133     }
134 
135 }