Merge "provide ready-use maven setting for easy build"
[sdnc/core.git] / dblib / common / src / main / java / org / apache / tomcat / jdbc / pool / interceptor / AbstractCreateStatementInterceptor.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * openecomp
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
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
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=========================================================
19  */
20
21 /*
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
28  *
29  *      http://www.apache.org/licenses/LICENSE-2.0
30  *
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.
36  */
37 package org.apache.tomcat.jdbc.pool.interceptor;
38
39 import java.lang.reflect.Method;
40
41 import org.apache.tomcat.jdbc.pool.ConnectionPool;
42 import org.apache.tomcat.jdbc.pool.JdbcInterceptor;
43 import org.apache.tomcat.jdbc.pool.PooledConnection;
44
45 /**
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)}
48  * method.
49  * @version 1.0
50  */
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;
58
59     protected static final String[] STATEMENT_TYPES = {CREATE_STATEMENT, PREPARE_STATEMENT, PREPARE_CALL};
60     protected static final int    STATEMENT_TYPE_COUNT = STATEMENT_TYPES.length;
61
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";
66
67     protected static final String[] EXECUTE_TYPES = {EXECUTE, EXECUTE_QUERY, EXECUTE_UPDATE, EXECUTE_BATCH};
68
69     public  AbstractCreateStatementInterceptor() {
70         super();
71     }
72
73     /**
74      * {@inheritDoc}
75      */
76     @Override
77     public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
78         if (compare(CLOSE_VAL,method)) {
79             closeInvoked();
80             return super.invoke(proxy, method, args);
81         } else {
82             boolean process = false;
83             process = isStatement(method, process);
84             if (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);
89             } else {
90                 return super.invoke(proxy,method,args);
91             }
92         }
93     }
94
95     /**
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
106      */
107     public abstract Object createStatement(Object proxy, Method method, Object[] args, Object statement, long time);
108
109     /**
110      * Method invoked when the operation {@link java.sql.Connection#close()} is invoked.
111      */
112     public abstract void closeInvoked();
113
114     /**
115      * Returns true if the method that is being invoked matches one of the statement types.
116      *
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
120      */
121     protected boolean isStatement(Method method, boolean process){
122         return process(STATEMENT_TYPES, method, process);
123     }
124
125     /**
126      * Returns true if the method that is being invoked matches one of the execute types.
127      *
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
131      */
132     protected boolean isExecute(Method method, boolean process){
133         return process(EXECUTE_TYPES, method, process);
134     }
135
136     /*
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
142      */
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);
147         }
148         return process;
149     }
150
151     /**
152      * no-op for this interceptor. no state is stored.
153      */
154     @Override
155     public void reset(ConnectionPool parent, PooledConnection con) {
156         // NOOP
157     }
158 }