1 18 package org.apache.geronimo.interop.util; 19 20 import java.io.BufferedReader ; 21 import java.io.StringReader ; 22 import java.util.ArrayList ; 23 import java.util.Iterator ; 24 import java.util.List ; 25 26 import org.apache.geronimo.interop.CheckedException; 27 import org.apache.geronimo.interop.SystemException; 28 29 30 public abstract class ExceptionUtil { 31 public static List addException(List exceptions, Throwable ex) { 32 if (exceptions == null) { 33 exceptions = new ArrayList (1); 34 } 35 exceptions.add(ex); 36 return exceptions; 37 } 38 39 public static void checkExceptions(List exceptions) { 40 if (exceptions != null) { 41 int n = exceptions.size(); 42 if (n == 1) { 43 Throwable ex = (Throwable ) exceptions.get(0); 44 if (ex instanceof Error ) { 45 throw (Error ) ex; 46 } 47 if (ex instanceof RuntimeException ) { 48 throw (RuntimeException ) ex; 49 } 50 throw new SystemException(ex); 51 } else { 52 StringBuffer buffer = new StringBuffer (); 53 for (Iterator i = exceptions.iterator(); i.hasNext();) { 54 Exception ex = (Exception ) i.next(); 55 if (buffer.length() > 0) { 56 buffer.append("\n______________________________________________________________\n\n"); 57 } 58 buffer.append(ExceptionUtil.getStackTrace(ex)); 59 } 60 throw new SystemException(buffer.toString()); 61 } 62 } 63 } 64 65 public static String getDivider() { 66 return "\n ______________________________________________________________\n"; 67 } 68 69 73 public static String causedBy(Throwable ex) { 74 return "\nCaused by: " + getStackTrace(ex) + getDivider(); 75 } 76 77 public static String causedBy(String stackTrace) 78 { 79 return "\nCaused by: " + getTraceLines(stackTrace); 80 } 81 82 public static Throwable getCause(Throwable ex) 83 { 84 for (;;) 85 { 86 if (ex instanceof SystemException) 87 { 88 SystemException se = (SystemException)ex; 89 if (se.getCause() != null && se.getMessage() == null) 90 { 91 ex = se.getCause(); 92 } 93 else 94 { 95 break; 96 } 97 } 98 else 99 { 100 break; 101 } 102 } 103 return ex; 104 } 105 106 public static String getCauseChain(Throwable ex) { 107 String stackTrace = getStackTrace(ex); 108 return getCauseChain(stackTrace); 109 } 110 111 public static String getCauseChain(String stackTrace) { 112 try { 113 BufferedReader input = new BufferedReader (new StringReader (stackTrace)); 114 StringBuffer output = new StringBuffer (100); 115 String line; 116 while ((line = input.readLine()) != null) { 117 line = line.trim(); 118 if (!line.startsWith("at ") && !line.startsWith("... ")) { 119 output.append(line); 120 output.append('\n'); 121 } 122 } 123 return output.toString(); 124 } catch (Exception ex2) { 125 ex2.printStackTrace(); 126 return stackTrace; 127 } 128 } 129 130 public static String getStackTrace(Throwable ex) { 131 java.io.StringWriter sw = new java.io.StringWriter (); 132 java.io.PrintWriter pw = new java.io.PrintWriter (sw); 133 ex.printStackTrace(pw); 134 return sw.toString().trim(); 135 } 136 137 public static String getTraceLines(String stackTrace) 138 { 139 try 140 { 141 BufferedReader input = new BufferedReader (new StringReader (stackTrace)); 142 StringBuffer output = new StringBuffer (100); 143 String line; 144 boolean first = true; 145 while ((line = input.readLine()) != null) 146 { 147 line = line.trim(); 148 if (line.length() != 0) 149 { 150 if (! first) 151 { 152 output.append("| "); 153 } 154 first = false; 155 output.append(line); 156 output.append('\n'); 157 } 158 } 159 return output.toString(); 160 } 161 catch (Exception ex2) 162 { 163 ex2.printStackTrace(); 164 return stackTrace; 165 } 166 } 167 168 public static String getCurrentStackTrace() { 169 return StringUtil.removePrefix(getStackTrace(new Exception ()), "java.lang.Exception:"); 170 } 171 172 public static String indentLines(String lines) { 173 return " " + StringUtil.replace(lines.trim(), "\n", "\n "); 174 } 175 176 public static boolean isApplicationException(Throwable ex) { 177 return !isSystemException(ex); 178 } 179 180 public static boolean isSystemException(Throwable ex) { 181 Class exClass = ex.getClass(); 182 return Error .class.isAssignableFrom(exClass) 183 || RuntimeException .class.isAssignableFrom(exClass); 184 } 185 186 public static boolean isUserException(Class exClass) { 187 if (RuntimeException .class.isAssignableFrom(exClass) 188 || Error .class.isAssignableFrom(exClass)) { 189 return false; 190 } 191 return true; 192 } 193 194 public static RuntimeException getRuntimeException(Exception ex) { 195 if (ex instanceof RuntimeException ) { 196 return (RuntimeException ) ex; 197 } else { 198 return new SystemException(ex); 199 } 200 } 201 202 public static RuntimeException rethrow(Throwable ex) { 203 if (ex instanceof Error ) { 204 throw (Error ) ex; 205 } else if (ex instanceof RuntimeException ) { 206 return (RuntimeException ) ex; 207 } else { 208 return new CheckedException(ex); 209 } 210 } 211 } 212 | Popular Tags |