ConnectionLogger.java

  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. import java.lang.reflect.InvocationHandler;
  18. import java.lang.reflect.Method;
  19. import java.lang.reflect.Proxy;
  20. import java.sql.Connection;
  21. import java.sql.PreparedStatement;
  22. import java.sql.Statement;

  23. import org.apache.ibatis.logging.Log;
  24. import org.apache.ibatis.reflection.ExceptionUtil;

  25. /**
  26.  * Connection proxy to add logging.
  27.  *
  28.  * @author Clinton Begin
  29.  * @author Eduardo Macarron
  30.  */
  31. public final class ConnectionLogger extends BaseJdbcLogger implements InvocationHandler {

  32.   private final Connection connection;

  33.   private ConnectionLogger(Connection conn, Log statementLog, int queryStack) {
  34.     super(statementLog, queryStack);
  35.     this.connection = conn;
  36.   }

  37.   @Override
  38.   public Object invoke(Object proxy, Method method, Object[] params) throws Throwable {
  39.     try {
  40.       if (Object.class.equals(method.getDeclaringClass())) {
  41.         return method.invoke(this, params);
  42.       }
  43.       if ("prepareStatement".equals(method.getName()) || "prepareCall".equals(method.getName())) {
  44.         if (isDebugEnabled()) {
  45.           debug(" Preparing: " + removeExtraWhitespace((String) params[0]), true);
  46.         }
  47.         PreparedStatement stmt = (PreparedStatement) method.invoke(connection, params);
  48.         return PreparedStatementLogger.newInstance(stmt, statementLog, queryStack);
  49.       }
  50.       if ("createStatement".equals(method.getName())) {
  51.         Statement stmt = (Statement) method.invoke(connection, params);
  52.         return StatementLogger.newInstance(stmt, statementLog, queryStack);
  53.       }
  54.       return method.invoke(connection, params);
  55.     } catch (Throwable t) {
  56.       throw ExceptionUtil.unwrapThrowable(t);
  57.     }
  58.   }

  59.   /**
  60.    * Creates a logging version of a connection.
  61.    *
  62.    * @param conn
  63.    *          the original connection
  64.    * @param statementLog
  65.    *          the statement log
  66.    * @param queryStack
  67.    *          the query stack
  68.    *
  69.    * @return the connection with logging
  70.    */
  71.   public static Connection newInstance(Connection conn, Log statementLog, int queryStack) {
  72.     InvocationHandler handler = new ConnectionLogger(conn, statementLog, queryStack);
  73.     ClassLoader cl = Connection.class.getClassLoader();
  74.     return (Connection) Proxy.newProxyInstance(cl, new Class[] { Connection.class }, handler);
  75.   }

  76.   /**
  77.    * return the wrapped connection.
  78.    *
  79.    * @return the connection
  80.    */
  81.   public Connection getConnection() {
  82.     return connection;
  83.   }

  84. }