// Collection of code snippets by Arne Vajhøj 
// (likely posted to comp.os.vms/INFO-VAX, INFO-TPU, MACRO32, eksperten.dk, newz.dk, LinkedIn or other place sometime between 1990 and now) 
import java.io.PrintStream;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.HashSet;
import java.util.Set;

public class Reveal {
    public static interface TrackableResource {
        public String getId() throws SQLException;
    }
    private static Set<TrackableResource> reslst = new HashSet<TrackableResource>();
    public static class ResultSetWrapper implements InvocationHandler, TrackableResource {
        private static int count = 0;
        private int num;
        private ResultSet rs;
        private String parent;
        private String sql;
        public ResultSetWrapper(ResultSet rs, String parent) {
            this.rs = rs;
            this.parent = parent;
            this.sql = null;
            synchronized(reslst) {
                count++;
                num = count;
                reslst.add(this);
            }
        }
        public ResultSetWrapper(ResultSet rs, String parent, String sql) {
            this.rs = rs;
            this.parent = parent;
            this.sql = sql;
            synchronized(reslst) {
                count++;
                num = count;
                reslst.add(this);
            }
        }
        public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
            if(method.getName().equals("close")) {
                synchronized(reslst) {
                    reslst.remove(this);
                }
                return method.invoke(rs, args);
            } else {
                return method.invoke(rs, args);
            }
        }
        public String getId() throws SQLException {
            if(sql == null) {
                return "ResultSet#" + num + " from " + parent;
            } else {
                return "ResultSet#" + num + " (" + sql + ") from " + parent;
            }
        }
    }
    public static class PreparedStatementWrapper implements InvocationHandler, TrackableResource {
        private static int count = 0;
        private int num;
        private Statement stmt;
        private String parent;
        private String sql;
        public PreparedStatementWrapper(Statement stmt, String parent, String sql) {
            this.stmt = stmt;
            this.parent = parent;
            this.sql = sql;
            synchronized(reslst) {
                count++;
                num = count;
                reslst.add(this);
            }
        }
        public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
            if(method.getName().equals("close")) {
                synchronized(reslst) {
                    reslst.remove(this);
                }
                return method.invoke(stmt, args);
            } else if(method.getName().equals("executeQuery")) {
                return (ResultSet)Proxy.newProxyInstance(getClass().getClassLoader(), new Class<?>[] { ResultSet.class },
                                                         new ResultSetWrapper((ResultSet)method.invoke(stmt, args), getId()));
            } else {
                return method.invoke(stmt, args);
            }
        }
        public String getId() throws SQLException {
            return "PreparedStatement#" + num + " (" + sql + ") from " + parent;
        }
    }
    public static class StatementWrapper implements InvocationHandler, TrackableResource {
        private static int count = 0;
        private int num;
        private Statement stmt;
        private String parent;
        public StatementWrapper(Statement stmt, String parent) {
            this.stmt = stmt;
            this.parent = parent;
            synchronized(reslst) {
                count++;
                num = count;
                reslst.add(this);
            }
        }
        public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
            if(method.getName().equals("close")) {
                synchronized(reslst) {
                    reslst.remove(this);
                }
                return method.invoke(stmt, args);
            } else if(method.getName().equals("executeQuery")) {
                return (ResultSet)Proxy.newProxyInstance(getClass().getClassLoader(), new Class<?>[] { ResultSet.class },
                                                         new ResultSetWrapper((ResultSet)method.invoke(stmt, args), getId(), (String)args[0]));
            } else {
                return method.invoke(stmt, args);
            }
        }
        public String getId() throws SQLException {
            return "Statement#" + num + " from " + parent;
        }
    }
    public static class ConnectionWrapper implements InvocationHandler, TrackableResource {
        private static int count = 0;
        private int num;
        private Connection con;
        public ConnectionWrapper(Connection con) {
            this.con = con;
            synchronized(reslst) {
                count++;
                num = count;
                reslst.add(this);
            }
        }
        public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
            if(method.getName().equals("close")) {
                synchronized(reslst) {
                    reslst.remove(this);
                }
                return method.invoke(con, args);
            } else if(method.getName().equals("createStatement")) {
                return (Statement)Proxy.newProxyInstance(con.getClass().getClassLoader(), new Class<?>[] { Statement.class },
                                                         new StatementWrapper((Statement)method.invoke(con, args), getId()));
            } else if(method.getName().equals("prepareStatement")) {
                return (PreparedStatement)Proxy.newProxyInstance(con.getClass().getClassLoader(), new Class<?>[] { PreparedStatement.class },
                                                                 new PreparedStatementWrapper((PreparedStatement)method.invoke(con, args), getId(), (String)args[0]));
            } else {
                return method.invoke(con, args);
            }
        }
        public String getId() throws SQLException {
            return "Connection#" + num;
        }
    }
    public static Connection wrap(Connection con) {
        return (Connection)Proxy.newProxyInstance(con.getClass().getClassLoader(), new Class<?>[] { Connection.class },
                                                  new ConnectionWrapper(con));
    }
    public static void dumpOpen(PrintStream ps) throws SQLException {
        synchronized(reslst) {
            for(TrackableResource obj : reslst.toArray(new TrackableResource[0])) {
                System.out.println("Unclosed: " + obj.getId());
            }
        }
    }
}
