interceptors = new InterceptorDefinition[0];
} else {
String[] interceptorValues = jdbcInterceptors.split(";");
- InterceptorDefinition[] definitions = new InterceptorDefinition[interceptorValues.length];
+ InterceptorDefinition[] definitions = new InterceptorDefinition[interceptorValues.length+1];
+ //always add the trap interceptor to the mix
+ definitions[0] = new InterceptorDefinition(TrapException.class);
for (int i=0; i<interceptorValues.length; i++) {
int propIndex = interceptorValues[i].indexOf("(");
int endIndex = interceptorValues[i].indexOf(")");
if (propIndex<0 || endIndex<0 || endIndex <= propIndex) {
- definitions[i] = new InterceptorDefinition(interceptorValues[i].trim());
+ definitions[i+1] = new InterceptorDefinition(interceptorValues[i].trim());
} else {
String name = interceptorValues[i].substring(0,propIndex).trim();
- definitions[i] = new InterceptorDefinition(name);
+ definitions[i+1] = new InterceptorDefinition(name);
String propsAsString = interceptorValues[i].substring(propIndex+1, interceptorValues[i].length()-1);
String[] props = propsAsString.split(",");
for (int j=0; j<props.length; j++) {
public InterceptorDefinition(String className) {
this.className = className;
}
+
+ public InterceptorDefinition(Class<?> cl) {
+ this(cl.getName());
+ clazz = cl;
+ }
public String getClassName() {
return className;
--- /dev/null
+/*\r
+ * Licensed to the Apache Software Foundation (ASF) under one or more\r
+ * contributor license agreements. See the NOTICE file distributed with\r
+ * this work for additional information regarding copyright ownership.\r
+ * The ASF licenses this file to You under the Apache License, Version 2.0\r
+ * (the "License"); you may not use this file except in compliance with\r
+ * the License. You may obtain a copy of the License at\r
+ *\r
+ * http://www.apache.org/licenses/LICENSE-2.0\r
+ *\r
+ * Unless required by applicable law or agreed to in writing, software\r
+ * distributed under the License is distributed on an "AS IS" BASIS,\r
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
+ * See the License for the specific language governing permissions and\r
+ * limitations under the License.\r
+ */\r
+\r
+package org.apache.tomcat.jdbc.pool;\r
+\r
+\r
+import java.lang.reflect.InvocationTargetException;\r
+import java.lang.reflect.Method;\r
+import java.sql.SQLException;\r
+/**\r
+ * Interceptor that traps any unhandled exception types and throws an exception that has been declared by the method\r
+ * called, or throw a SQLException if it is declared.\r
+ * If the caught exception is not declared, and the method doesn't throw SQLException, then this interceptor will\r
+ * throw a RuntimeException\r
+ * @author fhanik\r
+ *\r
+ */\r
+public class TrapException extends JdbcInterceptor {\r
+\r
+ \r
+ public TrapException() {\r
+ }\r
+\r
+ @Override\r
+ public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {\r
+ try {\r
+ return super.invoke(proxy, method, args);\r
+ }catch (Throwable t) {\r
+ Throwable exception = t;\r
+ if (t instanceof InvocationTargetException) {\r
+ InvocationTargetException it = (InvocationTargetException)t;\r
+ exception = it.getCause()!=null?it.getCause():it;\r
+ } \r
+ Class<?> exceptionClass = exception.getClass();\r
+ if (!isDeclaredException(method, exceptionClass)) {\r
+ if (isDeclaredException(method,SQLException.class)) {\r
+ SQLException sqlx = new SQLException("Uncaught underlying exception.");\r
+ sqlx.initCause(exception);\r
+ exception = sqlx;\r
+ } else {\r
+ RuntimeException rx = new RuntimeException("Uncaught underlying exception.");\r
+ rx.initCause(exception);\r
+ exception = rx;\r
+ }\r
+ }\r
+ throw exception;\r
+ }\r
+ \r
+ }\r
+ \r
+ public boolean isDeclaredException(Method m, Class<?> clazz) {\r
+ for (Class<?> cl : m.getExceptionTypes()) {\r
+ if (cl.equals(clazz)) return true;\r
+ }\r
+ return false;\r
+ }\r
+ \r
+ /**\r
+ * no-op for this interceptor. no state is stored.\r
+ */\r
+ @Override\r
+ public void reset(ConnectionPool parent, PooledConnection con) {\r
+ // NOOP\r
+ }\r
+ \r
+}\r