KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > ibatis > common > jdbc > logging > StatementLogProxy


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 com.ibatis.common.logging.Log;
20 import com.ibatis.common.logging.LogFactory;
21
22 import java.lang.reflect.InvocationHandler JavaDoc;
23 import java.lang.reflect.Method JavaDoc;
24 import java.lang.reflect.Proxy JavaDoc;
25 import java.sql.ResultSet JavaDoc;
26 import java.sql.Statement JavaDoc;
27
28 /**
29  * Statement proxy to add logging
30  */

31 public class StatementLogProxy extends BaseLogProxy implements InvocationHandler JavaDoc {
32
33   private static final Log log = LogFactory.getLog(Statement JavaDoc.class);
34
35   private Statement JavaDoc statement;
36
37   private StatementLogProxy(Statement JavaDoc stmt) {
38     super();
39     this.statement = stmt;
40   }
41
42   public Object JavaDoc invoke(Object JavaDoc proxy, Method JavaDoc method, Object JavaDoc[] params) throws Throwable JavaDoc {
43     try {
44       if (EXECUTE_METHODS.contains(method.getName())) {
45         if (log.isDebugEnabled()) {
46           log.debug("{stmt-" + id + "} Statement: " + removeBreakingWhitespace((String JavaDoc) params[0]));
47         }
48         if ("executeQuery".equals(method.getName())) {
49           ResultSet JavaDoc rs = (ResultSet JavaDoc) method.invoke(statement, params);
50           if ( rs != null ) {
51             return ResultSetLogProxy.newInstance(rs);
52           }
53           else {
54             return null;
55           }
56         } else {
57           return method.invoke(statement, params);
58         }
59       } else if ("getResultSet".equals(method.getName())) {
60         ResultSet JavaDoc rs = (ResultSet JavaDoc) method.invoke(statement, params);
61         if ( rs != null ) {
62           return ResultSetLogProxy.newInstance(rs);
63         }
64         else {
65           return null;
66         }
67       } else {
68         return method.invoke(statement, params);
69       }
70     } catch (Throwable JavaDoc t) {
71       throw ClassInfo.unwrapThrowable(t);
72     }
73   }
74
75   /**
76    * Creates a logging version of a Statement
77    * @param stmt - the statement
78    * @return - the proxy
79    */

80   public static Statement JavaDoc newInstance(Statement JavaDoc stmt) {
81     InvocationHandler JavaDoc handler = new StatementLogProxy(stmt);
82     ClassLoader JavaDoc cl = Statement JavaDoc.class.getClassLoader();
83     return (Statement JavaDoc) Proxy.newProxyInstance(cl, new Class JavaDoc[]{Statement JavaDoc.class}, handler);
84   }
85
86 }
87
Popular Tags