View Javadoc
1   /*
2    * Copyright 2004-2022 the original author or authors.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *    https://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package com.ibatis.common.jdbc.logging;
17  
18  import com.ibatis.common.beans.ClassInfo;
19  import com.ibatis.common.logging.Log;
20  import com.ibatis.common.logging.LogFactory;
21  
22  import java.lang.reflect.InvocationHandler;
23  import java.lang.reflect.Method;
24  import java.lang.reflect.Proxy;
25  import java.sql.ResultSet;
26  import java.sql.Statement;
27  
28  /**
29   * Statement proxy to add logging.
30   */
31  public class StatementLogProxy extends BaseLogProxy implements InvocationHandler {
32  
33    /** The Constant log. */
34    private static final Log log = LogFactory.getLog(Statement.class);
35  
36    /** The statement. */
37    private Statement statement;
38  
39    /**
40     * Instantiates a new statement log proxy.
41     *
42     * @param stmt
43     *          the stmt
44     */
45    private StatementLogProxy(Statement stmt) {
46      super();
47      this.statement = stmt;
48    }
49  
50    public Object invoke(Object proxy, Method method, Object[] params) throws Throwable {
51      try {
52        if (EXECUTE_METHODS.contains(method.getName())) {
53          if (log.isDebugEnabled()) {
54            log.debug("{stmt-" + id + "} Statement: " + removeBreakingWhitespace((String) params[0]));
55          }
56          if ("executeQuery".equals(method.getName())) {
57            ResultSet rs = (ResultSet) method.invoke(statement, params);
58            if (rs != null) {
59              return ResultSetLogProxy.newInstance(rs);
60            } else {
61              return null;
62            }
63          } else {
64            return method.invoke(statement, params);
65          }
66        } else if ("getResultSet".equals(method.getName())) {
67          ResultSet rs = (ResultSet) method.invoke(statement, params);
68          if (rs != null) {
69            return ResultSetLogProxy.newInstance(rs);
70          } else {
71            return null;
72          }
73        } else if ("equals".equals(method.getName())) {
74          Object ps = params[0];
75          if (ps instanceof Proxy) {
76            return new Boolean(proxy == ps);
77          }
78          return new Boolean(false);
79        } else if ("hashCode".equals(method.getName())) {
80          return Integer.valueOf(proxy.hashCode());
81        } else {
82          return method.invoke(statement, params);
83        }
84      } catch (Throwable t) {
85        throw ClassInfo.unwrapThrowable(t);
86      }
87    }
88  
89    /**
90     * Creates a logging version of a Statement.
91     *
92     * @param stmt
93     *          - the statement
94     *
95     * @return - the proxy
96     */
97    public static Statement newInstance(Statement stmt) {
98      InvocationHandler handler = new StatementLogProxy(stmt);
99      ClassLoader cl = Statement.class.getClassLoader();
100     return (Statement) Proxy.newProxyInstance(cl, new Class[] { Statement.class }, handler);
101   }
102 
103   /**
104    * return the wrapped statement.
105    *
106    * @return the statement
107    */
108   public Statement getStatement() {
109     return statement;
110   }
111 
112 }