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.Connection;
026: import java.sql.PreparedStatement;
027: import java.sql.Statement;
028:
029: /**
030: * Connection proxy to add logging
031: */
032: public class ConnectionLogProxy extends BaseLogProxy implements
033: InvocationHandler {
034:
035: private static final Log log = LogFactory.getLog(Connection.class);
036:
037: private Connection connection;
038:
039: private ConnectionLogProxy(Connection conn) {
040: super ();
041: this .connection = conn;
042: if (log.isDebugEnabled()) {
043: log.debug("{conn-" + id + "} Connection");
044: }
045: }
046:
047: public Object invoke(Object proxy, Method method, Object[] params)
048: throws Throwable {
049: try {
050: if ("prepareStatement".equals(method.getName())) {
051: if (log.isDebugEnabled()) {
052: log
053: .debug("{conn-"
054: + id
055: + "} Preparing Statement: "
056: + removeBreakingWhitespace((String) params[0]));
057: }
058: PreparedStatement stmt = (PreparedStatement) method
059: .invoke(connection, params);
060: stmt = PreparedStatementLogProxy.newInstance(stmt,
061: (String) params[0]);
062: return stmt;
063: } else if ("prepareCall".equals(method.getName())) {
064: if (log.isDebugEnabled()) {
065: log
066: .debug("{conn-"
067: + id
068: + "} Preparing Call: "
069: + removeBreakingWhitespace((String) params[0]));
070: }
071: PreparedStatement stmt = (PreparedStatement) method
072: .invoke(connection, params);
073: stmt = PreparedStatementLogProxy.newInstance(stmt,
074: (String) params[0]);
075: return stmt;
076: } else if ("createStatement".equals(method.getName())) {
077: Statement stmt = (Statement) method.invoke(connection,
078: params);
079: stmt = StatementLogProxy.newInstance(stmt);
080: return stmt;
081: } else {
082: return method.invoke(connection, params);
083: }
084: } catch (Throwable t) {
085: Throwable t1 = ClassInfo.unwrapThrowable(t);
086: log.error("Error calling Connection." + method.getName()
087: + ':', t1);
088: throw t1;
089: }
090:
091: }
092:
093: /**
094: * Creates a logging version of a connection
095: * @param conn - the original connection
096: * @return - the connection with logging
097: */
098: public static Connection newInstance(Connection conn) {
099: InvocationHandler handler = new ConnectionLogProxy(conn);
100: ClassLoader cl = Connection.class.getClassLoader();
101: return (Connection) Proxy.newProxyInstance(cl,
102: new Class[] { Connection.class }, handler);
103: }
104:
105: }
|