2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2016 - 2017 AT&T
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
22 * Licensed to the Apache Software Foundation (ASF) under one or more
23 * contributor license agreements. See the NOTICE file distributed with
24 * this work for additional information regarding copyright ownership.
25 * The ASF licenses this file to You under the Apache License, Version 2.0
26 * (the "License"); you may not use this file except in compliance with
27 * the License. You may obtain a copy of the License at
29 * http://www.apache.org/licenses/LICENSE-2.0
31 * Unless required by applicable law or agreed to in writing, software
32 * distributed under the License is distributed on an "AS IS" BASIS,
33 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
34 * See the License for the specific language governing permissions and
35 * limitations under the License.
37 package org.apache.tomcat.jdbc.pool.interceptor;
39 import java.lang.reflect.Method;
41 import org.apache.tomcat.jdbc.pool.ConnectionPool;
42 import org.apache.tomcat.jdbc.pool.JdbcInterceptor;
43 import org.apache.tomcat.jdbc.pool.PooledConnection;
46 * Abstraction interceptor. This component intercepts all calls to create some type of SQL statement.
47 * By extending this class, one can intercept queries and update statements by overriding the {@link #createStatement(Object, Method, Object[], Object, long)}
51 public abstract class AbstractCreateStatementInterceptor extends JdbcInterceptor {
52 protected static final String CREATE_STATEMENT = "createStatement";
53 protected static final int CREATE_STATEMENT_IDX = 0;
54 protected static final String PREPARE_STATEMENT = "prepareStatement";
55 protected static final int PREPARE_STATEMENT_IDX = 1;
56 protected static final String PREPARE_CALL = "prepareCall";
57 protected static final int PREPARE_CALL_IDX = 2;
59 protected static final String[] STATEMENT_TYPES = {CREATE_STATEMENT, PREPARE_STATEMENT, PREPARE_CALL};
60 protected static final int STATEMENT_TYPE_COUNT = STATEMENT_TYPES.length;
62 protected static final String EXECUTE = "execute";
63 protected static final String EXECUTE_QUERY = "executeQuery";
64 protected static final String EXECUTE_UPDATE = "executeUpdate";
65 protected static final String EXECUTE_BATCH = "executeBatch";
67 protected static final String[] EXECUTE_TYPES = {EXECUTE, EXECUTE_QUERY, EXECUTE_UPDATE, EXECUTE_BATCH};
69 public AbstractCreateStatementInterceptor() {
77 public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
78 if (compare(CLOSE_VAL,method)) {
80 return super.invoke(proxy, method, args);
82 boolean process = false;
83 process = isStatement(method, process);
85 long start = System.currentTimeMillis();
86 Object statement = super.invoke(proxy,method,args);
87 long delta = System.currentTimeMillis() - start;
88 return createStatement(proxy,method,args,statement, delta);
90 return super.invoke(proxy,method,args);
96 * This method will be invoked after a successful statement creation. This method can choose to return a wrapper
97 * around the statement or return the statement itself.
98 * If this method returns a wrapper then it should return a wrapper object that implements one of the following interfaces.
99 * {@link java.sql.Statement}, {@link java.sql.PreparedStatement} or {@link java.sql.CallableStatement}
100 * @param proxy the actual proxy object
101 * @param method the method that was called. It will be one of the methods defined in {@link #STATEMENT_TYPES}
102 * @param args the arguments to the method
103 * @param statement the statement that the underlying connection created
104 * @param time Elapsed time
105 * @return a {@link java.sql.Statement} object
107 public abstract Object createStatement(Object proxy, Method method, Object[] args, Object statement, long time);
110 * Method invoked when the operation {@link java.sql.Connection#close()} is invoked.
112 public abstract void closeInvoked();
115 * Returns true if the method that is being invoked matches one of the statement types.
117 * @param method the method being invoked on the proxy
118 * @param process boolean result used for recursion
119 * @return returns true if the method name matched
121 protected boolean isStatement(Method method, boolean process){
122 return process(STATEMENT_TYPES, method, process);
126 * Returns true if the method that is being invoked matches one of the execute types.
128 * @param method the method being invoked on the proxy
129 * @param process boolean result used for recursion
130 * @return returns true if the method name matched
132 protected boolean isExecute(Method method, boolean process){
133 return process(EXECUTE_TYPES, method, process);
137 * Returns true if the method that is being invoked matches one of the method names passed in
138 * @param names list of method names that we want to intercept
139 * @param method the method being invoked on the proxy
140 * @param process boolean result used for recursion
141 * @return returns true if the method name matched
143 protected boolean process(String[] names, Method method, boolean process) {
144 final String name = method.getName();
145 for (int i=0; (!process) && i<names.length; i++) {
146 process = compare(names[i],name);
152 * no-op for this interceptor. no state is stored.
155 public void reset(ConnectionPool parent, PooledConnection con) {