Example usage for java.sql Wrapper unwrap

List of usage examples for java.sql Wrapper unwrap

Introduction

In this page you can find the example usage for java.sql Wrapper unwrap.

Prototype

<T> T unwrap(java.lang.Class<T> iface) throws java.sql.SQLException;

Source Link

Document

Returns an object that implements the given interface to allow access to non-standard methods, or standard methods not exposed by the proxy.

Usage

From source file:org.nuclos.server.dblayer.impl.SQLUtils2.java

public static <T> T unwrap(Object obj, Class<T> iface) throws SQLException {
    if (obj.getClass().getName()
            .equals("org.apache.commons.dbcp.PoolingDataSource$PoolGuardConnectionWrapper")) {
        Method method;//from  w  w w .  ja  v  a2 s.com
        try {
            method = obj.getClass().getMethod("getInnermostDelegate");
            method.setAccessible(true);

            return unwrap(method.invoke(obj), iface);
        } catch (Exception e) {
            throw new CommonFatalException(
                    "Error calling org.jboss.resource.adapter.jdbc.WrappedConnection#getUnderlyingConnection()",
                    e);
        }
    }
    if (iface.isInstance(obj)) {
        return iface.cast(obj);
    }
    if (obj instanceof Wrapper) {
        try {
            Wrapper wrapper = (Wrapper) obj;
            if (wrapper.isWrapperFor(iface)) {
                obj = wrapper.unwrap(iface);
            }

        } catch (AbstractMethodError e) {
            // this class didn't implement the interface completely (pre JDBC 4.0 implementation)
        }
    }
    return null;
}