1 6 package org.logicalcobwebs.proxool; 7 8 import org.apache.commons.logging.Log; 9 import org.apache.commons.logging.LogFactory; 10 11 import java.lang.reflect.Constructor ; 12 import java.lang.reflect.InvocationTargetException ; 13 import java.lang.reflect.Method ; 14 import java.sql.SQLException ; 15 import java.util.Iterator ; 16 17 23 class FatalSqlExceptionHelper { 24 25 private static final Log LOG = LogFactory.getLog(FatalSqlExceptionHelper.class); 26 27 36 protected static void throwFatalSQLException(String className, Throwable originalException) throws ProxoolException, SQLException , RuntimeException { 37 if (className != null && className.trim().length() > 0) { 38 Class clazz = null; 39 try { 40 clazz = Class.forName(className); 41 } catch (ClassNotFoundException e) { 42 throw new ProxoolException("Couldn't find class " + className); 43 } 44 if (SQLException .class.isAssignableFrom(clazz)) { 45 } else if (RuntimeException .class.isAssignableFrom(clazz)) { 47 } else { 49 throw new ProxoolException("Couldn't wrap up using " + clazz.getName() + " because it isn't either a RuntimeException or an SQLException"); 50 } 51 Constructor toUse = null; 52 Object [] args = null; 53 String argDescription = ""; 54 Constructor [] constructors = clazz.getConstructors(); 55 for (int i = 0; i < constructors.length; i++) { 56 Constructor constructor = constructors[i]; 57 Class [] parameterTypes = constructor.getParameterTypes(); 58 if (toUse == null && parameterTypes.length == 0) { 59 toUse = constructor; 60 } 61 if (parameterTypes.length == 1 && Exception .class.isAssignableFrom(parameterTypes[0])) { 62 toUse = constructor; 63 args = new Object []{originalException}; 64 argDescription = "Exception"; 65 break; 66 } 67 } 68 try { 69 Object exceptionToThrow = toUse.newInstance(args); 70 if (exceptionToThrow instanceof RuntimeException ) { 71 LOG.debug("Wrapping up a fatal exception: " + originalException.getMessage(), originalException); 72 throw (RuntimeException ) exceptionToThrow; 73 } else if (exceptionToThrow instanceof SQLException ) { 74 throw (SQLException ) exceptionToThrow; 75 } else { 76 throw new ProxoolException("Couldn't throw " + clazz.getName() + " because it isn't either a RuntimeException or an SQLException"); 77 } 78 } catch (InstantiationException e) { 79 throw new ProxoolException("Couldn't create " + clazz.getName() + "(" + argDescription + ")", e); 80 } catch (IllegalAccessException e) { 81 throw new ProxoolException("Couldn't create " + clazz.getName() + "(" + argDescription + ")", e); 82 } catch (InvocationTargetException e) { 83 throw new ProxoolException("Couldn't create " + clazz.getName() + "(" + argDescription + ")", e); 84 } 85 } else { 86 if (originalException instanceof SQLException ) { 87 throw (SQLException ) originalException; 88 } else if (originalException instanceof RuntimeException ) { 89 throw (RuntimeException ) originalException; 90 } else { 91 throw new RuntimeException ("Unexpected exception:" + originalException.getMessage()); 92 } 93 } 94 } 95 96 102 protected static boolean testException(ConnectionPoolDefinitionIF cpd, Throwable t) { 103 return testException(cpd, t, 0); 104 } 105 106 113 protected static boolean testException(ConnectionPoolDefinitionIF cpd, Throwable t, int level) { 114 boolean fatalSqlExceptionDetected = false; 115 Iterator i = cpd.getFatalSqlExceptions().iterator(); 116 while (i.hasNext()) { 117 if (t.getMessage() != null && t.getMessage().indexOf((String ) i.next()) > -1) { 118 fatalSqlExceptionDetected = true; 120 } 121 } 122 123 if (!fatalSqlExceptionDetected && level < 20) { 125 Throwable cause = getCause(t); 126 if (cause != null) { 127 fatalSqlExceptionDetected = testException(cpd, cause, level + 1); 128 } 129 } 130 131 return fatalSqlExceptionDetected; 132 } 133 134 142 protected static Throwable getCause(Throwable t) { 143 Throwable cause = null; 144 Method causeMethod = null; 145 146 try { 147 if (causeMethod == null) { 149 causeMethod = getMethod(t, "getCause"); 150 } 151 if (causeMethod == null) { 152 causeMethod = getMethod(t, "getTargetException"); 153 } 154 if (causeMethod == null) { 155 causeMethod = getMethod(t, "getRootCause"); 156 } 157 if (causeMethod == null) { 158 causeMethod = getMethod(t, "getOriginalException"); 159 } 160 161 if (causeMethod != null) { 163 try { 164 cause = (Throwable ) causeMethod.invoke(t, null); 165 } catch (IllegalAccessException e) { 166 LOG.warn("Problem invoking " + t.getClass().getName() + "." + causeMethod.getName() + ". Ignoring.", e); 167 } catch (IllegalArgumentException e) { 168 LOG.warn("Problem invoking " + t.getClass().getName() + "." + causeMethod.getName() + ". Ignoring.", e); 169 } catch (InvocationTargetException e) { 170 LOG.warn("Problem invoking " + t.getClass().getName() + "." + causeMethod.getName() + ". Ignoring.", e); 171 } 172 } 173 } catch (Exception e) { 174 LOG.warn("Unexpected exception drilling into exception. Ignoring.", e); 175 } 176 return cause; 177 } 178 179 private static Method getMethod(Object o, String methodName) { 180 Method m = null; 181 try { 182 m = o.getClass().getMethod(methodName, null); 183 if (!Throwable .class.isAssignableFrom(m.getReturnType())) { 185 m = null; 186 } 187 } catch (NoSuchMethodException e) { 188 } catch (SecurityException e) { 190 LOG.warn("Problem finding method " + methodName, e); 191 } 192 return m; 193 } 194 195 } 196 197 | Popular Tags |