Implement exception traps as suggested by Eiji Takahashi
authorfhanik <fhanik@13f79535-47bb-0310-9956-ffa450edef68>
Thu, 31 Mar 2011 22:28:54 +0000 (22:28 +0000)
committerfhanik <fhanik@13f79535-47bb-0310-9956-ffa450edef68>
Thu, 31 Mar 2011 22:28:54 +0000 (22:28 +0000)
http://markmail.org/message/c7hrhky4jtgcto76

git-svn-id: https://svn.apache.org/repos/asf/tomcat/trunk@1087467 13f79535-47bb-0310-9956-ffa450edef68

modules/jdbc-pool/src/main/java/org/apache/tomcat/jdbc/pool/PoolProperties.java
modules/jdbc-pool/src/main/java/org/apache/tomcat/jdbc/pool/TrapException.java [new file with mode: 0644]

index 6fd20d4..be01339 100644 (file)
@@ -401,15 +401,17 @@ public class PoolProperties implements PoolConfiguration {
                 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++) {
@@ -810,6 +812,11 @@ public class PoolProperties implements PoolConfiguration {
         public InterceptorDefinition(String className) {
             this.className = className;
         }
+        
+        public InterceptorDefinition(Class<?> cl) {
+            this(cl.getName());
+            clazz = cl;
+        }
 
         public String getClassName() {
             return className;
diff --git a/modules/jdbc-pool/src/main/java/org/apache/tomcat/jdbc/pool/TrapException.java b/modules/jdbc-pool/src/main/java/org/apache/tomcat/jdbc/pool/TrapException.java
new file mode 100644 (file)
index 0000000..a075fab
--- /dev/null
@@ -0,0 +1,80 @@
+/*\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