Example usage for java.sql Wrapper isWrapperFor

List of usage examples for java.sql Wrapper isWrapperFor

Introduction

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

Prototype

boolean isWrapperFor(java.lang.Class<?> iface) throws java.sql.SQLException;

Source Link

Document

Returns true if this either implements the interface argument or is directly or indirectly a wrapper for an object that does.

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  www  .j  a va  2  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;
}