KickJava   Java API By Example, From Geeks To Geeks.

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


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
27 /**
28  * ResultSet proxy to add logging
29  */

30 public class ResultSetLogProxy extends BaseLogProxy implements InvocationHandler JavaDoc {
31
32   private static final Log log = LogFactory.getLog(ResultSet JavaDoc.class);
33
34   boolean first = true;
35   private ResultSet JavaDoc rs;
36
37   private ResultSetLogProxy(ResultSet JavaDoc rs) {
38     super();
39     this.rs = rs;
40     if (log.isDebugEnabled()) {
41       log.debug("{rset-" + id + "} ResultSet");
42     }
43   }
44
45   public Object JavaDoc invoke(Object JavaDoc proxy, Method JavaDoc method, Object JavaDoc[] params) throws Throwable JavaDoc {
46     try {
47       Object JavaDoc o = method.invoke(rs, params);
48       if (GET_METHODS.contains(method.getName())) {
49         if (params[0] instanceof String JavaDoc) {
50           setColumn(params[0], o);
51           // setColumn(params[0], rs.getObject((String) params[0]));
52
// } else {
53
// setColumn(params[0], rs.getObject(((Integer) params[0]).intValue()));
54
}
55       } else if ("next".equals(method.getName())) {
56         String JavaDoc s = getValueString();
57         if (!"[]".equals(s)) {
58           if (first) {
59             first = false;
60             if (log.isDebugEnabled()) {
61               log.debug("{rset-" + id + "} Header: " + getColumnString());
62             }
63           }
64           if (log.isDebugEnabled()) {
65             log.debug("{rset-" + id + "} Result: " + s);
66           }
67         }
68         clearColumnInfo();
69       }
70       return o;
71     } catch (Throwable JavaDoc t) {
72       throw ClassInfo.unwrapThrowable(t);
73     }
74   }
75
76   /**
77    * Creates a logging version of a ResultSet
78    *
79    * @param rs - the ResultSet to proxy
80    * @return - the ResultSet with logging
81    */

82   public static ResultSet JavaDoc newInstance(ResultSet JavaDoc rs) {
83     InvocationHandler JavaDoc handler = new ResultSetLogProxy(rs);
84     ClassLoader JavaDoc cl = ResultSet JavaDoc.class.getClassLoader();
85     return (ResultSet JavaDoc) Proxy.newProxyInstance(cl, new Class JavaDoc[]{ResultSet JavaDoc.class}, handler);
86   }
87
88
89 }
90
Popular Tags