KickJava   Java API By Example, From Geeks To Geeks.

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


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.CallableStatement JavaDoc;
26 import java.sql.PreparedStatement JavaDoc;
27 import java.sql.ResultSet JavaDoc;
28
29 /**
30  * PreparedStatement proxy to add logging
31  */

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

93   public static PreparedStatement JavaDoc newInstance(PreparedStatement JavaDoc stmt, String JavaDoc sql) {
94     InvocationHandler JavaDoc handler = new PreparedStatementLogProxy(stmt, sql);
95     ClassLoader JavaDoc cl = PreparedStatement JavaDoc.class.getClassLoader();
96     return (PreparedStatement JavaDoc) Proxy.newProxyInstance(cl, new Class JavaDoc[]{PreparedStatement JavaDoc.class, CallableStatement JavaDoc.class}, handler);
97   }
98
99 }
100
Popular Tags