KickJava   Java API By Example, From Geeks To Geeks.

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


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

32 public class ConnectionLogProxy extends BaseLogProxy implements InvocationHandler JavaDoc {
33
34   private static final Log log = LogFactory.getLog(Connection JavaDoc.class);
35
36   private Connection JavaDoc connection;
37
38   private ConnectionLogProxy(Connection JavaDoc conn) {
39     super();
40     this.connection = conn;
41     if (log.isDebugEnabled()) {
42       log.debug("{conn-" + id + "} Connection");
43     }
44   }
45
46   public Object JavaDoc invoke(Object JavaDoc proxy, Method JavaDoc method, Object JavaDoc[] params)
47       throws Throwable JavaDoc {
48     try {
49       if ("prepareStatement".equals(method.getName())) {
50         PreparedStatement JavaDoc stmt = (PreparedStatement JavaDoc) method.invoke(connection, params);
51         stmt = PreparedStatementLogProxy.newInstance(stmt, (String JavaDoc) params[0]);
52         return stmt;
53       } else if ("prepareCall".equals(method.getName())) {
54         PreparedStatement JavaDoc stmt = (PreparedStatement JavaDoc) method.invoke(connection, params);
55         stmt = PreparedStatementLogProxy.newInstance(stmt, (String JavaDoc) params[0]);
56         return stmt;
57       } else if ("createStatement".equals(method.getName())) {
58         Statement JavaDoc stmt = (Statement JavaDoc) method.invoke(connection, params);
59         stmt = StatementLogProxy.newInstance(stmt);
60         return stmt;
61       } else {
62         return method.invoke(connection, params);
63       }
64     } catch (Throwable JavaDoc t) {
65       throw ClassInfo.unwrapThrowable(t);
66     }
67
68   }
69
70   /**
71    * Creates a logging version of a connection
72    * @param conn - the original connection
73    * @return - the connection with logging
74    */

75   public static Connection JavaDoc newInstance(Connection JavaDoc conn) {
76     InvocationHandler JavaDoc handler = new ConnectionLogProxy(conn);
77     ClassLoader JavaDoc cl = Connection JavaDoc.class.getClassLoader();
78     return (Connection JavaDoc) Proxy.newProxyInstance(cl, new Class JavaDoc[]{Connection JavaDoc.class}, handler);
79   }
80
81 }
82
Popular Tags