1 23 24 package org.enhydra.error; 25 26 import java.io.PrintStream ; 27 import java.io.PrintWriter ; 28 import java.lang.reflect.InvocationTargetException ; 29 import java.lang.reflect.Method ; 30 31 34 public final class ChainedThrowableSupport { 35 36 private static final String SAX_EXCEPTION_CLASS = "org.xml.sax.SAXException"; 37 38 41 private ChainedThrowableSupport() { 42 } 43 44 53 public static String makeMessage(Throwable cause) { 54 if (cause == null) { 55 return null; 56 } else { 57 String causeMsg = cause.getMessage(); 58 if (causeMsg == null) { 59 return cause.getClass().getName(); 60 } else { 61 return causeMsg; 62 } 63 } 64 } 65 66 73 public static String getMessage(ChainedThrowable except, 74 String superMsg) { 75 if (superMsg != null) { 76 return superMsg; 77 } 78 Thread thread = Thread.currentThread(); 79 String threadMsg = " " + thread + " (" + thread.getThreadGroup().getName() + ", " + thread.getName() + ")"; 80 Throwable cause = except.getCause(); 81 if (cause == null) { 82 return except.getClass().getName() + threadMsg; 83 } else { 84 String causeMsg = cause.getMessage(); 85 if ((causeMsg == null) || (causeMsg.length() == 0)) { 86 causeMsg = cause.getClass().getName(); 87 } 88 return causeMsg + threadMsg; 89 } 90 } 91 92 98 public static String getLocalizedMessage(ChainedThrowable except, 99 String superMsg) { 100 if (superMsg != null) { 101 return superMsg; 102 } 103 Throwable cause = except.getCause(); 104 if (cause == null) { 105 return except.getClass().getName(); 106 } else { 107 String causeMsg = cause.getLocalizedMessage(); 108 if ((causeMsg == null) || (causeMsg.length() == 0)) { 109 causeMsg = cause.getClass().getName(); 110 } 111 return causeMsg; 112 } 113 } 114 115 119 private static Throwable getJavaChain(Throwable cause) { 120 if (cause instanceof java.io.WriteAbortedException ) { 121 return ((java.io.WriteAbortedException )cause).detail; 122 } else if (cause instanceof java.lang.ClassNotFoundException ) { 123 return ((java.lang.ClassNotFoundException )cause).getException(); 124 } else if (cause instanceof java.lang.ExceptionInInitializerError ) { 125 return ((java.lang.ExceptionInInitializerError )cause).getException(); 126 } else if (cause instanceof java.lang.reflect.InvocationTargetException ) { 127 return ((java.lang.reflect.InvocationTargetException )cause).getTargetException(); 128 } else if (cause instanceof java.rmi.RemoteException ) { 129 return ((java.rmi.RemoteException )cause).detail; 130 } else if (cause instanceof java.rmi.activation.ActivationException ) { 131 return ((java.rmi.activation.ActivationException )cause).detail; 132 } else if (cause instanceof java.rmi.server.ServerCloneException ) { 133 return ((java.rmi.server.ServerCloneException )cause).detail; 134 } else if (cause instanceof java.security.PrivilegedActionException ) { 135 return ((java.security.PrivilegedActionException )cause).getException(); 136 } else if (cause instanceof java.sql.SQLException ) { 137 return ((java.sql.SQLException )cause).getNextException(); 138 } else { 139 return null; 140 } 141 } 142 143 147 private static Throwable getChainWithAccessor(Throwable cause, 148 String accessorName) 149 throws NoSuchMethodException , IllegalAccessException , 150 InvocationTargetException { 151 Method accessor = cause.getClass().getMethod(accessorName, (Class [])null); 152 return (Throwable )accessor.invoke(cause, (Object [])null); 153 } 154 155 160 private static Throwable getAWTChain(Throwable cause) 161 throws NoSuchMethodException , IllegalAccessException , 162 InvocationTargetException { 163 if (cause.getClass().getName().equals("java.awt.print.PrinterIOException")) { 164 return getChainWithAccessor(cause, "getIOException"); 165 } else { 166 return null; 167 } 168 } 169 170 174 private static boolean isSAXException(Throwable except) { 175 return except.getClass().getName().equals(SAX_EXCEPTION_CLASS); 176 } 177 178 181 private static Throwable getSAXExceptionChain(Throwable cause) 182 throws NoSuchMethodException , IllegalAccessException , 183 InvocationTargetException { 184 if (isSAXException(cause)) { 185 return getChainWithAccessor(cause, "getException"); 186 } else { 187 return null; 188 } 189 } 190 191 199 private static void printChainedCauses(Throwable cause, 200 PrintWriter out) { 201 if (cause != null) { 202 out.println("*** Caused by:"); 204 if (isSAXException(cause)) { 205 out.print(SAX_EXCEPTION_CLASS); 209 out.print(": "); 210 } 211 212 cause.printStackTrace(out); 213 try { 214 if (!(cause instanceof ChainedThrowable)) { 217 Throwable nextCause = getJavaChain(cause); 218 if (nextCause == null) { 219 nextCause = getAWTChain(cause); 220 } 221 if (nextCause == null) { 222 nextCause = getSAXExceptionChain(cause); 223 } 224 if (nextCause != null) { 225 printChainedCauses(nextCause, out); 226 } 227 } 228 } catch (Throwable except) { 229 out.println("WARNING: can't print rest of exception chain: " 231 + except); 232 } 233 } 234 } 235 236 239 public static void printCauseTrace(ChainedThrowable except) { 240 PrintWriter pw = new PrintWriter (System.err); 241 printChainedCauses(except.getCause(), pw); 242 pw.flush(); 243 } 244 245 248 public static void printCauseTrace(ChainedThrowable except, 249 PrintStream s) { 250 PrintWriter pw = new PrintWriter (s); 251 printChainedCauses(except.getCause(), pw); 252 pw.flush(); 253 } 254 255 258 public static void printCauseTrace(ChainedThrowable except, 259 PrintWriter out) { 260 printChainedCauses(except.getCause(), out); 261 out.flush(); 262 } 263 264 265 } 266 | Popular Tags |