View Javadoc
1   /*
2    *    Copyright 2009-2023 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 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.Connection;
22  import java.sql.PreparedStatement;
23  import java.sql.Statement;
24  
25  import org.apache.ibatis.logging.Log;
26  import org.apache.ibatis.reflection.ExceptionUtil;
27  
28  /**
29   * Connection proxy to add logging.
30   *
31   * @author Clinton Begin
32   * @author Eduardo Macarron
33   */
34  public final class ConnectionLogger extends BaseJdbcLogger implements InvocationHandler {
35  
36    private final Connection connection;
37  
38    private ConnectionLogger(Connection conn, Log statementLog, int queryStack) {
39      super(statementLog, queryStack);
40      this.connection = conn;
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 ("prepareStatement".equals(method.getName()) || "prepareCall".equals(method.getName())) {
50          if (isDebugEnabled()) {
51            debug(" Preparing: " + removeExtraWhitespace((String) params[0]), true);
52          }
53          PreparedStatement stmt = (PreparedStatement) method.invoke(connection, params);
54          return PreparedStatementLogger.newInstance(stmt, statementLog, queryStack);
55        }
56        if ("createStatement".equals(method.getName())) {
57          Statement stmt = (Statement) method.invoke(connection, params);
58          return StatementLogger.newInstance(stmt, statementLog, queryStack);
59        }
60        return method.invoke(connection, params);
61      } catch (Throwable t) {
62        throw ExceptionUtil.unwrapThrowable(t);
63      }
64    }
65  
66    /**
67     * Creates a logging version of a connection.
68     *
69     * @param conn
70     *          the original connection
71     * @param statementLog
72     *          the statement log
73     * @param queryStack
74     *          the query stack
75     *
76     * @return the connection with logging
77     */
78    public static Connection newInstance(Connection conn, Log statementLog, int queryStack) {
79      InvocationHandler handler = new ConnectionLogger(conn, statementLog, queryStack);
80      ClassLoader cl = Connection.class.getClassLoader();
81      return (Connection) Proxy.newProxyInstance(cl, new Class[] { Connection.class }, handler);
82    }
83  
84    /**
85     * return the wrapped connection.
86     *
87     * @return the connection
88     */
89    public Connection getConnection() {
90      return connection;
91    }
92  
93  }