5c597f34dca8d10bfad285f98912a65c5d386994
[sdnc/core.git] / dblib / common / src / main / java / org / apache / tomcat / jdbc / pool / TrapException.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
38 package org.apache.tomcat.jdbc.pool;
39
40
41 import java.lang.reflect.InvocationTargetException;
42 import java.lang.reflect.Method;
43 import java.sql.SQLException;
44 /**
45  * Interceptor that traps any unhandled exception types and throws an exception that has been declared by the method
46  * called, or throw a SQLException if it is declared.
47  * If the caught exception is not declared, and the method doesn't throw SQLException, then this interceptor will
48  * throw a RuntimeException
49  *
50  */
51 public class TrapException extends JdbcInterceptor {
52
53
54     public TrapException() {
55     }
56
57     @Override
58     public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
59         try {
60             return super.invoke(proxy, method, args);
61         }catch (Exception t) {
62             Throwable exception = t;
63             if (t instanceof InvocationTargetException && t.getCause() != null) {
64                 exception = t.getCause();
65                 if (exception instanceof Error) {
66                     throw exception;
67                 }
68             }
69             Class<?> exceptionClass = exception.getClass();
70             if (!isDeclaredException(method, exceptionClass)) {
71                 if (isDeclaredException(method,SQLException.class)) {
72                     SQLException sqlx = new SQLException("Uncaught underlying exception.");
73                     sqlx.initCause(exception);
74                     exception = sqlx;
75                 } else {
76                     RuntimeException rx = new RuntimeException("Uncaught underlying exception.");
77                     rx.initCause(exception);
78                     exception = rx;
79                 }
80             }
81             throw exception;
82         }
83
84     }
85
86     public boolean isDeclaredException(Method m, Class<?> clazz) {
87         for (Class<?> cl : m.getExceptionTypes()) {
88             if (cl.equals(clazz) || cl.isAssignableFrom(clazz)) return true;
89         }
90         return false;
91     }
92
93     /**
94      * no-op for this interceptor. no state is stored.
95      */
96     @Override
97     public void reset(ConnectionPool parent, PooledConnection con) {
98         // NOOP
99     }
100
101 }