Source code: com/ibatis/common/jdbc/logging/StatementLogProxy.java
1 /*
2 * Copyright 2004 Clinton Begin
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 com.ibatis.common.jdbc.logging;
17
18 import com.ibatis.common.beans.ClassInfo;
19 import org.apache.commons.logging.Log;
20 import org.apache.commons.logging.LogFactory;
21
22 import java.lang.reflect.InvocationHandler;
23 import java.lang.reflect.Method;
24 import java.lang.reflect.Proxy;
25 import java.sql.ResultSet;
26 import java.sql.Statement;
27
28 /**
29 * Statement proxy to add logging
30 */
31 public class StatementLogProxy extends BaseLogProxy implements InvocationHandler {
32
33 private static final Log log = LogFactory.getLog(Statement.class);
34
35 private Statement statement;
36
37 private StatementLogProxy(Statement stmt) {
38 super();
39 this.statement = stmt;
40 }
41
42 public Object invoke(Object proxy, Method method, Object[] params) throws Throwable {
43 try {
44 if (EXECUTE_METHODS.contains(method.getName())) {
45 if (log.isDebugEnabled()) {
46 log.debug("{stmt-" + id + "} Statement: " + removeBreakingWhitespace((String) params[0]));
47 }
48 if ("executeQuery".equals(method.getName())) {
49 ResultSet rs = (ResultSet) method.invoke(statement, params);
50 return ResultSetLogProxy.newInstance(rs);
51 } else {
52 return method.invoke(statement, params);
53 }
54 } else if ("getResultSet".equals(method.getName())) {
55 ResultSet rs = (ResultSet) method.invoke(statement, params);
56 return ResultSetLogProxy.newInstance(rs);
57 } else {
58 return method.invoke(statement, params);
59 }
60 } catch (Throwable t) {
61 throw ClassInfo.unwrapThrowable(t);
62 }
63 }
64
65 /**
66 * Creates a logging version of a Statement
67 * @param stmt - the statement
68 * @return - the proxy
69 */
70 public static Statement newInstance(Statement stmt) {
71 InvocationHandler handler = new StatementLogProxy(stmt);
72 ClassLoader cl = Statement.class.getClassLoader();
73 return (Statement) Proxy.newProxyInstance(cl, new Class[]{Statement.class}, handler);
74 }
75
76 }