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.
38 package org.apache.tomcat.jdbc.pool;
41 import java.lang.reflect.InvocationTargetException;
42 import java.lang.reflect.Method;
43 import java.sql.SQLException;
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
51 public class TrapException extends JdbcInterceptor {
54 public TrapException() {
58 public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
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) {
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);
76 RuntimeException rx = new RuntimeException("Uncaught underlying exception.");
77 rx.initCause(exception);
86 public boolean isDeclaredException(Method m, Class<?> clazz) {
87 for (Class<?> cl : m.getExceptionTypes()) {
88 if (cl.equals(clazz) || cl.isAssignableFrom(clazz)) return true;
94 * no-op for this interceptor. no state is stored.
97 public void reset(ConnectionPool parent, PooledConnection con) {