View Javadoc
1   /*
2    *    Copyright 2009-2021 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    *       http://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 org.apache.ibatis.logging.jdbc;
17  
18  import java.lang.reflect.InvocationHandler;
19  import java.lang.reflect.Method;
20  import java.lang.reflect.Proxy;
21  import java.sql.ResultSet;
22  import java.sql.Statement;
23  
24  import org.apache.ibatis.logging.Log;
25  import org.apache.ibatis.reflection.ExceptionUtil;
26  
27  /**
28   * Statement proxy to add logging.
29   *
30   * @author Clinton Begin
31   * @author Eduardo Macarron
32   *
33   */
34  public final class StatementLogger extends BaseJdbcLogger implements InvocationHandler {
35  
36    private final Statement statement;
37  
38    private StatementLogger(Statement stmt, Log statementLog, int queryStack) {
39      super(statementLog, queryStack);
40      this.statement = stmt;
41    }
42  
43    @Override
44    public Object invoke(Object proxy, Method method, Object[] params) throws Throwable {
45      try {
46        if (Object.class.equals(method.getDeclaringClass())) {
47          return method.invoke(this, params);
48        }
49        if (EXECUTE_METHODS.contains(method.getName())) {
50          if (isDebugEnabled()) {
51            debug(" Executing: " + removeExtraWhitespace((String) params[0]), true);
52          }
53          if ("executeQuery".equals(method.getName())) {
54            ResultSet rs = (ResultSet) method.invoke(statement, params);
55            return rs == null ? null : ResultSetLogger.newInstance(rs, statementLog, queryStack);
56          } else {
57            return method.invoke(statement, params);
58          }
59        } else if ("getResultSet".equals(method.getName())) {
60          ResultSet rs = (ResultSet) method.invoke(statement, params);
61          return rs == null ? null : ResultSetLogger.newInstance(rs, statementLog, queryStack);
62        } else {
63          return method.invoke(statement, params);
64        }
65      } catch (Throwable t) {
66        throw ExceptionUtil.unwrapThrowable(t);
67      }
68    }
69  
70    /**
71     * Creates a logging version of a Statement.
72     *
73     * @param stmt
74     *          the statement
75     * @param statementLog
76     *          the statement log
77     * @param queryStack
78     *          the query stack
79     * @return the proxy
80     */
81    public static Statement newInstance(Statement stmt, Log statementLog, int queryStack) {
82      InvocationHandler handler = new StatementLogger(stmt, statementLog, queryStack);
83      ClassLoader cl = Statement.class.getClassLoader();
84      return (Statement) Proxy.newProxyInstance(cl, new Class[]{Statement.class}, handler);
85    }
86  
87    /**
88     * return the wrapped statement.
89     *
90     * @return the statement
91     */
92    public Statement getStatement() {
93      return statement;
94    }
95  
96  }