fd4d8a437f5cc1aefb2f680d7ebfa855b186a7b3
[tomcat7.0] /
1 /*
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
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
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.
16  */
17 package org.apache.tomcat.jdbc.pool.interceptor;
18
19 import java.lang.reflect.Method;
20
21 import org.apache.tomcat.jdbc.pool.ConnectionPool;
22 import org.apache.tomcat.jdbc.pool.JdbcInterceptor;
23 import org.apache.tomcat.jdbc.pool.PooledConnection;
24
25 /**
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)}
28  * method.
29  * @author Filip Hanik
30  * @version 1.0
31  */
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"};
35
36     public  AbstractCreateStatementInterceptor() {
37         super();
38     }
39     
40     /**
41      * {@inheritDoc}
42      */
43     @Override
44     public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
45         if (compare(CLOSE_VAL,method)) {
46             closeInvoked();
47             return super.invoke(proxy, method, args);
48         } else {
49             boolean process = false;
50             process = process(statements, method, process);
51             if (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);
56             } else {
57                 return super.invoke(proxy,method,args);
58             }
59         }
60     }
61     
62     /**
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
72      */
73     public abstract Object createStatement(Object proxy, Method method, Object[] args, Object statement, long time);
74     
75     /**
76      * Method invoked when the operation {@link java.sql.Connection#close()} is invoked. 
77      */
78     public abstract void closeInvoked();
79
80     /**
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
86      */
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);
91         }
92         return process;
93     }
94     
95     /**
96      * no-op for this interceptor. no state is stored.
97      */
98     @Override
99     public void reset(ConnectionPool parent, PooledConnection con) {
100         
101     }
102 }