11acf01ab2cf4269ca2d635949a0e920eb16b1bf
[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  * @author Filip Hanik
27  * @version 1.0
28  */
29 public abstract class  AbstractCreateStatementInterceptor extends JdbcInterceptor {
30     public static final String[] statements = {"createStatement","prepareStatement","prepareCall"};
31     public static final String[] executes = {"execute","executeQuery","executeUpdate","executeBatch"};
32
33     public  AbstractCreateStatementInterceptor() {
34         super();
35     }
36     
37     @Override
38     public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
39         if (compare(CLOSE_VAL,method)) {
40             closeInvoked();
41             return super.invoke(proxy, method, args);
42         } else {
43             boolean process = false;
44             process = process(statements, method, process);
45             if (process) {
46                 long start = System.currentTimeMillis();
47                 Object statement = super.invoke(proxy,method,args);
48                 long delta = System.currentTimeMillis() - start;
49                 return createStatement(proxy,method,args,statement, delta);
50             } else {
51                 return super.invoke(proxy,method,args);
52             }
53         }
54     }
55     
56     /**
57      * This method should return a wrapper object around a
58      * java.sql.Statement, java.sql.PreparedStatement or java.sql.CallableStatement
59      * @param proxy
60      * @param method
61      * @param args
62      * @param statement
63      * @return
64      */
65     public abstract Object createStatement(Object proxy, Method method, Object[] args, Object statement, long time);
66     
67     public abstract void closeInvoked();
68
69     protected boolean process(String[] names, Method method, boolean process) {
70         final String name = method.getName();
71         for (int i=0; (!process) && i<names.length; i++) {
72             process = compare(names[i],name);
73         }
74         return process;
75     }
76     
77     public void reset(ConnectionPool parent, PooledConnection con) {
78
79     }
80 }