1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package triptracker.testing.compress;
21
22 import java.lang.reflect.*;
23 import java.sql.*;
24
25 /***
26 * Proxy class for logging database
27 *
28 * @author Jan Magne Tjensvold {@code <janmagne at broadpark dot no>}
29 */
30
31 public class DBDebugProxy extends DebugProxy {
32 /***
33 * Creates an object of the super proxy class.
34 *
35 * @param obj points to the object to create a proxy for.
36 */
37 protected DBDebugProxy(Statement obj) {
38 super(obj);
39 }
40
41 /***
42 * Captures all calls to the SQL query methods in order to print their
43 * arguments to stdout.
44 *
45 * @see java.lang.reflect.InvocationHandler#invoke(java.lang.Object,
46 * java.lang.reflect.Method, java.lang.Object[])
47 */
48 @Override
49 public Object invoke(Object proxy, Method method, Object[] args)
50 throws Throwable {
51 if (debugEnabled) {
52
53 if (method.getName() == "execute" ||
54 method.getName() == "executeQuery" ||
55 method.getName() == "executeUpdate") {
56 System.out.println(args[0]);
57 }
58 }
59
60 return super.invoke(proxy, method, args);
61 }
62
63 public static synchronized Statement proxyFor(Statement obj) {
64 Class objClass = obj.getClass();
65
66
67 Statement proxyObj = (Statement)Proxy.newProxyInstance(
68 objClass.getClassLoader(), objClass.getInterfaces(),
69 new DBDebugProxy(obj));
70 return proxyObj;
71 }
72 }