|
| 1 | +/** |
| 2 | + * Copyright 2009-2019 the original author or authors. |
| 3 | + * <p> |
| 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 | + * <p> |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * <p> |
| 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 org.apache.ibatis.logging.jdbc; |
| 17 | + |
| 18 | +import org.apache.ibatis.logging.Log; |
| 19 | +import org.apache.ibatis.reflection.ExceptionUtil; |
| 20 | + |
| 21 | +import java.lang.reflect.InvocationHandler; |
| 22 | +import java.lang.reflect.Method; |
| 23 | +import java.lang.reflect.Proxy; |
| 24 | +import java.sql.Connection; |
| 25 | +import java.sql.PreparedStatement; |
| 26 | +import java.sql.Statement; |
| 27 | + |
| 28 | +/** |
| 29 | + * Connection proxy to add logging. |
| 30 | + * |
| 31 | + * @author Clinton Begin |
| 32 | + * @author Eduardo Macarron |
| 33 | + * |
| 34 | + */ |
| 35 | +public final class ConnectionLogger extends BaseJdbcLogger implements InvocationHandler { |
| 36 | + |
| 37 | + private final Connection connection; |
| 38 | + |
| 39 | + private ConnectionLogger(Connection conn, Log statementLog, int queryStack) { |
| 40 | + super(statementLog, queryStack); |
| 41 | + this.connection = conn; |
| 42 | + } |
| 43 | + |
| 44 | + /** |
| 45 | + * Creates a logging version of a connection. |
| 46 | + * |
| 47 | + * @param conn - the original connection |
| 48 | + * @return - the connection with logging |
| 49 | + */ |
| 50 | + public static Connection newInstance(Connection conn, Log statementLog, int queryStack) { |
| 51 | + InvocationHandler handler = new ConnectionLogger(conn, statementLog, queryStack); |
| 52 | + ClassLoader cl = Connection.class.getClassLoader(); |
| 53 | + return (Connection) Proxy.newProxyInstance(cl, new Class[]{Connection.class}, handler); |
| 54 | + } |
| 55 | + |
| 56 | + @Override |
| 57 | + public Object invoke(Object proxy, Method method, Object[] params) |
| 58 | + throws Throwable { |
| 59 | + try { |
| 60 | + if (Object.class.equals(method.getDeclaringClass())) { |
| 61 | + return method.invoke(this, params); |
| 62 | + } |
| 63 | + if ("prepareStatement".equals(method.getName())) { |
| 64 | + if (isDebugEnabled()) { |
| 65 | + debug(" Preparing: " + removeBreakingWhitespace((String) params[0]), true); |
| 66 | + } |
| 67 | + PreparedStatement stmt = (PreparedStatement) method.invoke(connection, params); |
| 68 | + stmt = PreparedStatementLogger.newInstance(stmt, statementLog, queryStack); |
| 69 | + return stmt; |
| 70 | + } else if ("prepareCall".equals(method.getName())) { |
| 71 | + if (isDebugEnabled()) { |
| 72 | + debug(" Preparing: " + removeBreakingWhitespace((String) params[0]), true); |
| 73 | + } |
| 74 | + PreparedStatement stmt = (PreparedStatement) method.invoke(connection, params); |
| 75 | + stmt = PreparedStatementLogger.newInstance(stmt, statementLog, queryStack); |
| 76 | + return stmt; |
| 77 | + } else if ("createStatement".equals(method.getName())) { |
| 78 | + Statement stmt = (Statement) method.invoke(connection, params); |
| 79 | + stmt = StatementLogger.newInstance(stmt, statementLog, queryStack); |
| 80 | + return stmt; |
| 81 | + } else { |
| 82 | + return method.invoke(connection, params); |
| 83 | + } |
| 84 | + } catch (Throwable t) { |
| 85 | + throw ExceptionUtil.unwrapThrowable(t); |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + /** |
| 90 | + * return the wrapped connection. |
| 91 | + * |
| 92 | + * @return the connection |
| 93 | + */ |
| 94 | + public Connection getConnection() { |
| 95 | + return connection; |
| 96 | + } |
| 97 | + |
| 98 | +} |
0 commit comments