private static InvocationHandler wrapperIh(Object h) { return (proxy, method, args) -> { if (method.getDeclaringClass() == Wrapper.class) { switch (method.getName()) { case "unwrap": if (((Class) args[0]).isInstance(h)) { return ((Class) args[0]).cast(h); } else { return method.invoke(h, args); } case "isWrapperFor": if (((Class) args[0]).isInstance(h)) { return true; } else { return method.invoke(h, args); } } } return UNHANLED; }; }
@Override public <T> T unwrap(Class<T> iface) throws SQLException { final Object result; if (iface.isAssignableFrom(getClass())) { // if the proxy directly implements the interface or extends it, return the proxy result = this; } else if (iface.isAssignableFrom(delegate.getClass())) { // if the proxied object directly implements the interface or extends it, return // the proxied object result = unwrapP6SpyProxy(); } else if (Wrapper.class.isAssignableFrom(delegate.getClass())) { // if the proxied object implements the wrapper interface, then // return the result of it's unwrap method. result = ((Wrapper) unwrapP6SpyProxy()).unwrap(iface); } else { /* This line of code can only be reached when the underlying object does not implement the wrapper interface. This would mean that either the JDBC driver or the wrapper of the underlying object does not implement the JDBC 4.0 API. */ throw new SQLException("Can not unwrap to " + iface.getName()); } return iface.cast(result); }
@Override public String readGeometry(Wrapper aRs, int aColumnIndex, Connection aConnection) throws SQLException { Object read = aRs instanceof ResultSet ? ((ResultSet) aRs).getObject(aColumnIndex) : ((CallableStatement) aRs).getObject(aColumnIndex); boolean wasNull = aRs instanceof ResultSet ? ((ResultSet) aRs).wasNull() : ((CallableStatement) aRs).wasNull(); if (wasNull) { return null; } else { if (read instanceof STRUCT) { STRUCT struct = (STRUCT) read; GeometryConverter reader = new GeometryConverter(struct.getInternalConnection()); Geometry geometry = reader.asGeometry(struct); WKTWriter writer = new WKTWriter(); return writer.write(geometry); } else { return null; } } }
@Override public String readGeometry(Wrapper aRs, int aColumnIndex, Connection aConnection) throws SQLException { Object read = aRs instanceof ResultSet ? ((ResultSet) aRs).getObject(aColumnIndex) : ((CallableStatement) aRs).getObject(aColumnIndex); boolean wasNull = aRs instanceof ResultSet ? ((ResultSet) aRs).wasNull() : ((CallableStatement) aRs).wasNull(); if (wasNull) { return null; } else { if (read instanceof PGgeometry) { PGgeometry pgg = (PGgeometry) read; read = pgg.getGeometry(); }else if(read.getClass().getName().equals(PGgeometry.class.getName())){// Crazy netbeans designer! return read.toString(); } if (read instanceof org.postgis.Geometry) { org.postgis.Geometry g = (org.postgis.Geometry) read; StringBuffer sb = new StringBuffer(); g.outerWKT(sb); return sb.toString(); } else { return null; } } }
/** * Given a {@link java.lang.reflect.InvocationHandler}, generate a proxy for a {@link java.sql.Connection}. * This performs the actual {@link java.lang.reflect.Proxy} logic, along with wrapping the proxy in a * {@link funjava.lang.reflect.WrapperInvocationHandler} proxy. * * @param connection the connection to proxy * @param handlerFactory the function that will generate a handler for connection method calls. * @return A {@link java.sql.Connection} proxy that delegates to the handler; never {@code null} */ public static Connection createProxy(Connection connection, Function<Connection, InvocationHandler> handlerFactory) { Objects.requireNonNull(connection, "the connection to create"); Objects.requireNonNull(handlerFactory, "the handler for connection method calls"); ClassLoader classLoader = connection.getClass().getClassLoader(); Connection proxy = Connection.class.cast(Proxy.newProxyInstance( classLoader, new Class[]{Connection.class, Wrapper.class}, new WrapperInvocationHandler(connection) ) ); proxy = Connection.class.cast(Proxy.newProxyInstance( classLoader, new Class[]{Connection.class, Wrapper.class}, handlerFactory.apply(proxy) ) ); return proxy; }
@Override public boolean isWrapperFor(Class<?> iface) throws SQLException { if (iface.isAssignableFrom(getClass())) { // if the proxy directly proxy the interface or extends it, return true return true; } else if (iface.isAssignableFrom(delegate.getClass())) { // if the proxied object directly implements the interface or extends it, return true return true; } else if (Wrapper.class.isAssignableFrom(delegate.getClass())) { // if the proxied object implements the wrapper interface, then // return the result of it's isWrapperFor method. return ((Wrapper) unwrapP6SpyProxy()).isWrapperFor(iface); } return false; }
/** {@inheritDoc} */ @Override @SuppressWarnings("unchecked") public final <T> T unwrap(Class<T> iface) throws SQLException { if (iface.isInstance(delegate)) { return (T) delegate; } else if (delegate instanceof Wrapper) { return (T) delegate.unwrap(iface); } throw new SQLException("Wrapped connection is not an instance of " + iface); }
/** {@inheritDoc} */ @Override @SuppressWarnings("unchecked") public final <T> T unwrap(Class<T> iface) throws SQLException { if (iface.isInstance(delegate)) { return (T) delegate; } else if (delegate instanceof Wrapper) { return (T) delegate.unwrap(iface); } throw new SQLException("Wrapped ResultSet is not an instance of " + iface); }
/** {@inheritDoc} */ @Override @SuppressWarnings("unchecked") public final <T> T unwrap(Class<T> iface) throws SQLException { if (iface.isInstance(delegate)) { return (T) delegate; } else if (delegate instanceof Wrapper) { return (T) delegate.unwrap(iface); } throw new SQLException("Wrapped statement is not an instance of " + iface); }
@Override public <T> T unwrap(Class<T> iface) throws SQLException { if (xaDataSource == null) { throw new SQLException("The underlying XADataSource is invalid or cannot be found"); } else if (iface.isInstance(xaDataSource)) { return iface.cast(xaDataSource); } else if (xaDataSource instanceof Wrapper) { return ((java.sql.Wrapper) xaDataSource).unwrap(iface); } else { throw new SQLException("The requested interface cannot be unwrapped"); } }
@Override public boolean isWrapperFor(Class<?> iface) throws SQLException { if (xaDataSource == null) { throw new SQLException("The underlying XADataSource is invalid or cannot be found"); } else if (iface.isInstance(xaDataSource)) { return true; } else if (xaDataSource instanceof Wrapper) { return ((java.sql.Wrapper) xaDataSource).isWrapperFor(iface); } return false; }
@Override protected JdbcReader obtainJdbcReader() { return new JdbcReader(expectedFields, (Wrapper aRsultSetOrCallableStatement, int aColumnIndex, Connection aConnection) -> { return sqlDriver.readGeometry(aRsultSetOrCallableStatement, aColumnIndex, aConnection); }, (int aJdbcType, String aRDBMSType) -> { return sqlDriver.getTypesResolver().toApplicationType(aJdbcType, aRDBMSType); }); }
private <A> A unwrapTo(Class<A> clazz) throws SQLException { Objects.requireNonNull(clazz, "class to unwrap to"); if (wrappedIsWrapper) { Wrapper w = ((Wrapper) wrapped); try { if (w.isWrapperFor(clazz)) return w.unwrap(clazz); } catch(SQLException | UndeclaredThrowableException e) { // Driver doesn't implement the wrapper functionality } } if (clazz.isAssignableFrom(wrapped.getClass())) return clazz.cast(wrapped); throw new SQLException("Could not unwrap " + wrapped + " to " + clazz); }
protected Object unwrap(MethodInvocation<T> methodInvocation) throws SQLException { final Class iface = getClassArg(methodInvocation); final Wrapper delegateWrapper = (Wrapper) delegate; Object result; if (isDelegateType(iface)) { result = delegateWrapper.isWrapperFor(iface) ? delegateWrapper.unwrap(iface) : iface.cast(delegateWrapper); } else { result = delegateWrapper.unwrap(iface); } return result; }
@Override public boolean isWrapperFor(Class<?> iface) throws SQLException { return ((Wrapper) realDataSource).isWrapperFor(iface); }
/** {@inheritDoc} */ @Override public final boolean isWrapperFor(Class<?> iface) throws SQLException { return iface.isInstance(delegate) || (delegate instanceof Wrapper && delegate.isWrapperFor(iface)); }
@Override public String readGeometry(Wrapper aRs, int aColumnIndex, Connection aConnection) throws SQLException { return null; }
public WrapperInvocationHandler(final T wrapped) { Objects.requireNonNull(wrapped, "object to be wrapped"); this.wrapped = wrapped; this.wrappedIsWrapper = this.wrapped instanceof Wrapper; }
private boolean isWrapped(Class<?> clazz) throws SQLException { Objects.requireNonNull(clazz, "class to check for wrapping"); if (clazz.isAssignableFrom(wrapped.getClass())) return true; if (wrappedIsWrapper) return ((Wrapper) wrapped).isWrapperFor(clazz); return false; }
/** * Returns an object that implements the given interface to allow access to * non-standard methods, or standard methods not exposed by the proxy. * * @param <T> * the type of the class modeled by this Class object. * @param wrapper * the wrapper class. * @param iFace * A Class defining an interface that the result must implement. * @return an object that implements the interface. May be a proxy for the * actual implementing object. * @throws java.sql.SQLException * If no object found that implements the interface. * @since 1.2 */ @SuppressWarnings("unchecked") public static <T> T unwrap(final Wrapper wrapper, final Class<T> iFace) throws SQLException { if (wrapper.isWrapperFor(iFace)) { return (T) wrapper; } throw new SQLException("Type not found.", SQLStates.TYPE_NOT_FOUND.getValue()); }
/** * Returns true if this either implements the interface argument or is * directly or indirectly a wrapper for an object that does. Returns false * otherwise.. * * @param wrapper * wrapper to test for. * @param iFace * a Class defining an interface. * @return true if this implements the interface or directly or indirectly * wraps an object that does. * @since 1.2 */ public static boolean isWrapperFor(final Wrapper wrapper, final Class<?> iFace) { return wrapper.getClass().isAssignableFrom(iFace); }
public String readGeometry(Wrapper aRs, int aColumnIndex, Connection aConnection) throws SQLException;
public abstract String readGeometry(Wrapper aRs, int aColumnIndex, Connection aConnection) throws SQLException;