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:
027: /**
028: * ResultSet proxy to add logging
029: */
030: public class ResultSetLogProxy extends BaseLogProxy implements
031: InvocationHandler {
032:
033: private static final Log log = LogFactory.getLog(ResultSet.class);
034:
035: boolean first = true;
036: private ResultSet rs;
037:
038: private ResultSetLogProxy(ResultSet rs) {
039: super ();
040: this .rs = rs;
041: if (log.isDebugEnabled()) {
042: log.debug("{rset-" + id + "} ResultSet");
043: }
044: }
045:
046: public Object invoke(Object proxy, Method method, Object[] params)
047: throws Throwable {
048: try {
049: Object o = method.invoke(rs, params);
050: if (GET_METHODS.contains(method.getName())) {
051: if (params[0] instanceof String) {
052: setColumn(params[0], o);
053: // setColumn(params[0], rs.getObject((String) params[0]));
054: // } else {
055: // setColumn(params[0], rs.getObject(((Integer) params[0]).intValue()));
056: }
057: } else if ("next".equals(method.getName())
058: || "close".equals(method.getName())) {
059: String s = getValueString();
060: if (!"[]".equals(s)) {
061: if (first) {
062: first = false;
063: if (log.isDebugEnabled()) {
064: log.debug("{rset-" + id + "} Header: "
065: + getColumnString());
066: }
067: }
068: if (log.isDebugEnabled()) {
069: log.debug("{rset-" + id + "} Result: " + s);
070: }
071: }
072: clearColumnInfo();
073: }
074: return o;
075: } catch (Throwable t) {
076: throw ClassInfo.unwrapThrowable(t);
077: }
078: }
079:
080: /**
081: * Creates a logging version of a ResultSet
082: *
083: * @param rs - the ResultSet to proxy
084: * @return - the ResultSet with logging
085: */
086: public static ResultSet newInstance(ResultSet rs) {
087: InvocationHandler handler = new ResultSetLogProxy(rs);
088: ClassLoader cl = ResultSet.class.getClassLoader();
089: return (ResultSet) Proxy.newProxyInstance(cl,
090: new Class[] { ResultSet.class }, handler);
091: }
092:
093: /**
094: * Get the wrapped result set
095: * @return the resultSet
096: */
097: public ResultSet getRs() {
098: return rs;
099: }
100:
101: }
|