2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements. See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
17 package org.apache.tomcat.jdbc.pool.interceptor;
19 import java.lang.reflect.Method;
21 import org.apache.tomcat.jdbc.pool.ConnectionPool;
22 import org.apache.tomcat.jdbc.pool.JdbcInterceptor;
23 import org.apache.tomcat.jdbc.pool.PooledConnection;
26 * Abstraction interceptor. This component intercepts all calls to create some type of SQL statement.
27 * By extending this class, one can intercept queries and update statements by overriding the {@link #createStatement(Object, Method, Object[], Object, long)}
32 public abstract class AbstractCreateStatementInterceptor extends JdbcInterceptor {
33 public static final String[] statements = {"createStatement","prepareStatement","prepareCall"};
34 public static final String[] executes = {"execute","executeQuery","executeUpdate","executeBatch"};
36 public AbstractCreateStatementInterceptor() {
44 public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
45 if (compare(CLOSE_VAL,method)) {
47 return super.invoke(proxy, method, args);
49 boolean process = false;
50 process = process(statements, method, process);
52 long start = System.currentTimeMillis();
53 Object statement = super.invoke(proxy,method,args);
54 long delta = System.currentTimeMillis() - start;
55 return createStatement(proxy,method,args,statement, delta);
57 return super.invoke(proxy,method,args);
63 * This method will be invoked after a successful statement creation. This method can choose to return a wrapper
64 * around the statement or return the statement itself.
65 * If this method returns a wrapper then it should return a wrapper object that implements one of the following interfaces.
66 * {@link java.sql.Statement}, {@link java.sql.PreparedStatement} or {@link java.sql.CallableStatement}
67 * @param proxy the actual proxy object
68 * @param method the method that was called. It will be one of the methods defined in {@link #statements}
69 * @param args the arguments to the method
70 * @param statement the statement that the underlying connection created
71 * @return a {@link java.sql.Statement} object
73 public abstract Object createStatement(Object proxy, Method method, Object[] args, Object statement, long time);
76 * Method invoked when the operation {@link java.sql.Connection#close()} is invoked.
78 public abstract void closeInvoked();
81 * Returns true if the method that is being invoked matches one of the method names passed in
82 * @param names list of method names that we want to intercept
83 * @param method the method being invoked on the proxy
84 * @param process boolean result used for recursion
85 * @return returns true if the method name matched
87 protected boolean process(String[] names, Method method, boolean process) {
88 final String name = method.getName();
89 for (int i=0; (!process) && i<names.length; i++) {
90 process = compare(names[i],name);
96 * no-op for this interceptor. no state is stored.
99 public void reset(ConnectionPool parent, PooledConnection con) {