001: /*
002: * Copyright 2004 Clinton Begin
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package com.ibatis.common.jdbc.logging;
017:
018: import com.ibatis.common.beans.ClassInfo;
019: import com.ibatis.common.logging.Log;
020: import com.ibatis.common.logging.LogFactory;
021:
022: import java.lang.reflect.InvocationHandler;
023: import java.lang.reflect.Method;
024: import java.lang.reflect.Proxy;
025: import java.sql.ResultSet;
026: import java.sql.Statement;
027:
028: /**
029: * Statement proxy to add logging
030: */
031: public class StatementLogProxy extends BaseLogProxy implements
032: InvocationHandler {
033:
034: private static final Log log = LogFactory.getLog(Statement.class);
035:
036: private Statement statement;
037:
038: private StatementLogProxy(Statement stmt) {
039: super ();
040: this .statement = stmt;
041: }
042:
043: public Object invoke(Object proxy, Method method, Object[] params)
044: throws Throwable {
045: try {
046: if (EXECUTE_METHODS.contains(method.getName())) {
047: if (log.isDebugEnabled()) {
048: log
049: .debug("{stmt-"
050: + id
051: + "} Statement: "
052: + removeBreakingWhitespace((String) params[0]));
053: }
054: if ("executeQuery".equals(method.getName())) {
055: ResultSet rs = (ResultSet) method.invoke(statement,
056: params);
057: if (rs != null) {
058: return ResultSetLogProxy.newInstance(rs);
059: } else {
060: return null;
061: }
062: } else {
063: return method.invoke(statement, params);
064: }
065: } else if ("getResultSet".equals(method.getName())) {
066: ResultSet rs = (ResultSet) method.invoke(statement,
067: params);
068: if (rs != null) {
069: return ResultSetLogProxy.newInstance(rs);
070: } else {
071: return null;
072: }
073: } else if ("equals".equals(method.getName())) {
074: Object ps = params[0];
075: if (ps instanceof Proxy) {
076: return new Boolean(proxy == ps);
077: }
078: return new Boolean(false);
079: } else if ("hashCode".equals(method.getName())) {
080: return new Integer(proxy.hashCode());
081: } else {
082: return method.invoke(statement, params);
083: }
084: } catch (Throwable t) {
085: throw ClassInfo.unwrapThrowable(t);
086: }
087: }
088:
089: /**
090: * Creates a logging version of a Statement
091: * @param stmt - the statement
092: * @return - the proxy
093: */
094: public static Statement newInstance(Statement stmt) {
095: InvocationHandler handler = new StatementLogProxy(stmt);
096: ClassLoader cl = Statement.class.getClassLoader();
097: return (Statement) Proxy.newProxyInstance(cl,
098: new Class[] { Statement.class }, handler);
099: }
100:
101: }
|