View Javadoc

1   /*
2    * Personal Book Database, a simple database for book and loan registration. 
3    * Copyright (C) 2005 Jan Magne Tjensvold
4    * 
5    * This program is free software; you can redistribute it and/or
6    * modify it under the terms of the GNU General Public License
7    * as published by the Free Software Foundation; either version 2
8    * of the License, or (at your option) any later version.
9    * 
10   * This program is distributed in the hope that it will be useful,
11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13   * GNU General Public License for more details.
14   * 
15   * You should have received a copy of the GNU General Public License
16   * along with this program; if not, write to the Free Software
17   * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
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  // TODO Modify for compression testing with byte counting.
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  	        // Print SQL argument for method calls.
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          // Dynamically creates new proxies.
67          Statement proxyObj = (Statement)Proxy.newProxyInstance(
68          		objClass.getClassLoader(), objClass.getInterfaces(),
69          		new DBDebugProxy(obj));
70          return proxyObj;
71      }
72  }