Open Source Repository

Home /ibatis/ibatis-sqlmap-3.0-beta8 | Repository Home



org/apache/ibatis/logging/jdbc/StatementLogger.java
package org.apache.ibatis.logging.jdbc;

import org.apache.ibatis.logging.Log;
import org.apache.ibatis.logging.LogFactory;
import org.apache.ibatis.reflection.ExceptionUtil;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.sql.ResultSet;
import java.sql.Statement;

/**
 * Statement proxy to add logging
 */
public class StatementLogger extends BaseJdbcLogger implements InvocationHandler {

  private static final Log log = LogFactory.getLog(Statement.class);

  private Statement statement;

  private StatementLogger(Statement stmt) {
    super();
    this.statement = stmt;
  }

  public Object invoke(Object proxy, Method method, Object[] paramsthrows Throwable {
    try {
      if (EXECUTE_METHODS.contains(method.getName())) {
        if (log.isDebugEnabled()) {
          log.debug("==>  Executing: " + removeBreakingWhitespace((Stringparams[0]));
        }
        if ("executeQuery".equals(method.getName())) {
          ResultSet rs = (ResultSetmethod.invoke(statement, params);
          if (rs != null) {
            return ResultSetLogger.newInstance(rs);
          else {
            return null;
          }
        else {
          return method.invoke(statement, params);
        }
      else if ("getResultSet".equals(method.getName())) {
        ResultSet rs = (ResultSetmethod.invoke(statement, params);
        if (rs != null) {
          return ResultSetLogger.newInstance(rs);
        else {
          return null;
        }
      else if ("equals".equals(method.getName())) {
        Object ps = params[0];
        return ps instanceof Proxy && proxy == ps;
      else if ("hashCode".equals(method.getName())) {
        return proxy.hashCode();
      else {
        return method.invoke(statement, params);
      }
    catch (Throwable t) {
      throw ExceptionUtil.unwrapThrowable(t);
    }
  }

  /**
   * Creates a logging version of a Statement
   *
   @param stmt - the statement
   @return - the proxy
   */
  public static Statement newInstance(Statement stmt) {
    InvocationHandler handler = new StatementLogger(stmt);
    ClassLoader cl = Statement.class.getClassLoader();
    return (StatementProxy.newProxyInstance(cl, new Class[]{Statement.class}, handler);
  }

  /**
   * return the wrapped statement
   *
   @return the statement
   */
  public Statement getStatement() {
    return statement;
  }

}