Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: safejdbc/StatementProxy.java


1   // Copyright (c) 2001, Jan Hermanns & Arno Haase
2   // All rights reserved.
3   //
4   // Redistribution and use in source and binary forms, with or
5   // without modification, are permitted provided that the
6   // following conditions are met:
7   //
8   //   * Redistributions of source code must retain the above
9   //     copyright notice, this list of conditions and the
10  //     following disclaimer.
11  //   * Redistributions in binary form must reproduce the above
12  //     copyright notice, this list of conditions and the
13  //     following disclaimer in the documentation and/or other
14  //     materials provided with the distribution.
15  //
16  // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
17  // CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
18  // INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
19  // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20  // DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE
21  // LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
22  // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23  // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
24  // OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
25  // CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
26  // STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27  // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
28  // ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  
30  package safejdbc;
31  
32  import java.lang.reflect.*;
33  import java.sql.*;
34  import java.io.InputStream;
35  import java.io.Reader;
36  import java.util.Map;
37  import java.util.HashMap;
38  import java.util.Calendar;
39  import java.math.BigDecimal;
40  
41  /**
42   * This class wrapps a java.sql.Statement, so that calls to the methods execute(), executeQuery() and executeUpdate() are
43   * delegated to the LogListener.
44   */
45  class StatementProxy implements InvocationHandler {
46  
47      private Statement _inner;
48      private Map               _params = new HashMap();
49      private HashMap           _parameters = new HashMap();
50      private LogListener       _loglistener;
51      
52      private static final ClassLoader _classLoader = Statement.class.getClassLoader();
53  
54      public static Statement getStatementProxy(Statement statement, LogListener loglistener) {
55          InvocationHandler handler = new StatementProxy(statement, loglistener);
56          Statement statementProxy = (Statement) Proxy.newProxyInstance (_classLoader, new Class[]{Statement.class}, handler);
57          return statementProxy;
58      }
59      
60      public StatementProxy(Statement inner, LogListener loglistener) {
61          _inner = inner;
62          _loglistener = loglistener;
63      }
64  
65      public Object invoke (Object proxy, Method method, Object[] args) throws Throwable {
66          try {
67              if (method.getName().startsWith("execute") && args.length == 1) {
68                  if (method.getName().equals("execute") ||
69                      method.getName().equals("executeQuery") ||
70                      method.getName().equals("executeUpdate")) {
71                      
72                      _loglistener.log((String) args[0]); // args[0] must be the sqlStmt
73                  }
74              }           
75              return method.invoke (_inner, args);
76          }
77          catch (IllegalAccessException e) {
78              throw new InternalError (e.toString ());
79          }
80          catch (IllegalArgumentException e) {
81              throw new InternalError (e.toString ());
82          }
83          catch (InvocationTargetException e) {
84              throw e.getTargetException ();
85          }
86      }
87          
88  
89      
90      
91  }
92