ConnectionLogger.java

  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. 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.  */
  32. public final class ConnectionLogger extends BaseJdbcLogger implements InvocationHandler {

  33.   private final Connection connection;

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

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

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

  79.   /**
  80.    * return the wrapped connection.
  81.    *
  82.    * @return the connection
  83.    */
  84.   public Connection getConnection() {
  85.     return connection;
  86.   }

  87. }