ProxyFactory.java

  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. import java.lang.reflect.InvocationHandler;
  19. import java.lang.reflect.Proxy;
  20. import java.sql.CallableStatement;
  21. import java.sql.Connection;
  22. import java.sql.Driver;
  23. import java.sql.PreparedStatement;
  24. import java.sql.ResultSet;
  25. import java.sql.ResultSetMetaData;
  26. import java.sql.Statement;

  27. /**
  28.  * Creates proxy implementations of JDBC interfaces.  This avoids
  29.  * incompatibilities between the JDBC 2 and JDBC 3 interfaces.  This class is
  30.  * thread safe.
  31.  *
  32.  * @see java.lang.reflect.Proxy
  33.  * @see java.lang.reflect.InvocationHandler
  34.  */
  35. public class ProxyFactory {

  36.     /**
  37.      * The Singleton instance of this class.
  38.      */
  39.     private static final ProxyFactory instance = new ProxyFactory();

  40.     /**
  41.      * Returns the Singleton instance of this class.
  42.      *
  43.      * @return singleton instance
  44.      */
  45.     public static ProxyFactory instance() {
  46.         return instance;
  47.     }

  48.     /**
  49.      * Protected constructor for ProxyFactory subclasses to use.
  50.      */
  51.     protected ProxyFactory() {
  52.     }

  53.     /**
  54.      * Creates a new proxy {@code CallableStatement} object.
  55.      * @param handler The handler that intercepts/overrides method calls.
  56.      * @return proxied CallableStatement
  57.      */
  58.     public CallableStatement createCallableStatement(final InvocationHandler handler) {
  59.         return newProxyInstance(CallableStatement.class, handler);
  60.     }

  61.     /**
  62.      * Creates a new proxy {@code Connection} object.
  63.      * @param handler The handler that intercepts/overrides method calls.
  64.      * @return proxied Connection
  65.      */
  66.     public Connection createConnection(final InvocationHandler handler) {
  67.         return newProxyInstance(Connection.class, handler);
  68.     }

  69.     /**
  70.      * Creates a new proxy {@code Driver} object.
  71.      * @param handler The handler that intercepts/overrides method calls.
  72.      * @return proxied Driver
  73.      */
  74.     public Driver createDriver(final InvocationHandler handler) {
  75.         return newProxyInstance(Driver.class, handler);
  76.     }

  77.     /**
  78.      * Creates a new proxy {@code PreparedStatement} object.
  79.      * @param handler The handler that intercepts/overrides method calls.
  80.      * @return proxied PreparedStatement
  81.      */
  82.     public PreparedStatement createPreparedStatement(final InvocationHandler handler) {
  83.         return newProxyInstance(PreparedStatement.class, handler);
  84.     }

  85.     /**
  86.      * Creates a new proxy {@code ResultSet} object.
  87.      * @param handler The handler that intercepts/overrides method calls.
  88.      * @return proxied ResultSet
  89.      */
  90.     public ResultSet createResultSet(final InvocationHandler handler) {
  91.         return newProxyInstance(ResultSet.class, handler);
  92.     }

  93.     /**
  94.      * Creates a new proxy {@code ResultSetMetaData} object.
  95.      * @param handler The handler that intercepts/overrides method calls.
  96.      * @return proxied ResultSetMetaData
  97.      */
  98.     public ResultSetMetaData createResultSetMetaData(final InvocationHandler handler) {
  99.         return newProxyInstance(ResultSetMetaData.class, handler);
  100.     }

  101.     /**
  102.      * Creates a new proxy {@code Statement} object.
  103.      * @param handler The handler that intercepts/overrides method calls.
  104.      * @return proxied Statement
  105.      */
  106.     public Statement createStatement(final InvocationHandler handler) {
  107.         return newProxyInstance(Statement.class, handler);
  108.     }

  109.     /**
  110.      * Convenience method to generate a single-interface proxy using the handler's classloader
  111.      *
  112.      * @param <T> The type of object to proxy
  113.      * @param type The type of object to proxy
  114.      * @param handler The handler that intercepts/overrides method calls.
  115.      * @return proxied object
  116.      */
  117.     public <T> T newProxyInstance(final Class<T> type, final InvocationHandler handler) {
  118.         return type.cast(Proxy.newProxyInstance(handler.getClass().getClassLoader(), new Class<?>[] {type}, handler));
  119.     }

  120. }