1 10 11 package org.mule.config; 12 13 import org.apache.commons.collections.MapUtils; 14 import org.apache.commons.logging.Log; 15 import org.apache.commons.logging.LogFactory; 16 import org.mule.MuleRuntimeException; 17 import org.mule.config.i18n.Message; 18 import org.mule.umo.UMOException; 19 import org.mule.util.ClassUtils; 20 import org.mule.util.SpiUtils; 21 import org.mule.util.StringUtils; 22 23 import java.io.IOException ; 24 import java.io.InputStream ; 25 import java.util.ArrayList ; 26 import java.util.HashMap ; 27 import java.util.Iterator ; 28 import java.util.List ; 29 import java.util.Map ; 30 import java.util.Properties ; 31 32 43 44 public class ExceptionHelper 45 { 46 51 public static final String ERROR_CODE_PROPERTY = "error.code.property"; 52 53 57 public static final String APPLY_TO_PROPERTY = "apply.to"; 58 59 62 protected static final Log logger = LogFactory.getLog(ExceptionHelper.class); 63 64 private static Properties errorDocs = new Properties (); 65 private static Properties errorCodes = new Properties (); 66 private static Map reverseErrorCodes = null; 67 private static Map errorMappings = new HashMap (); 68 69 private static int exceptionThreshold = 0; 70 private static boolean verbose = true; 71 72 private static boolean initialised = false; 73 74 private static String J2SE_VERSION = ""; 75 76 77 private static String J2EE_VERSION = "1.3ee"; 78 79 82 private static List exceptionReaders = new ArrayList (); 83 84 87 private static ExceptionReader defaultExceptionReader = new DefaultExceptionReader(); 88 89 static 90 { 91 initialise(); 92 } 93 94 public static void initialise() 95 { 96 try 97 { 98 if (initialised) 99 { 100 return; 101 } 102 103 registerExceptionReader(new UMOExceptionReader()); 104 registerExceptionReader(new NamingExceptionReader()); 105 J2SE_VERSION = System.getProperty("java.specification.version"); 106 107 InputStream is = SpiUtils.findServiceDescriptor("org/mule/config", 108 "mule-exception-codes.properties", ExceptionHelper.class); 109 if (is == null) 110 { 111 throw new NullPointerException ( 112 "Failed to load resource: META_INF/services/org/mule/config/mule-exception-codes.properties"); 113 } 114 errorCodes.load(is); 115 reverseErrorCodes = MapUtils.invertMap(errorCodes); 116 is = SpiUtils.findServiceDescriptor("org/mule/config", "mule-exception-config.properties", 117 ExceptionHelper.class); 118 if (is == null) 119 { 120 throw new NullPointerException ( 121 "Failed to load resource: META_INF/services/org/mule/config/mule-exception-config.properties"); 122 } 123 errorDocs.load(is); 124 125 initialised = true; 126 } 127 catch (IOException e) 128 { 129 throw new MuleRuntimeException(Message.createStaticMessage("Failed to load Exception resources"), 130 e); 131 } 132 } 133 134 public static int getErrorCode(Class exception) 135 { 136 String code = errorCodes.getProperty(exception.getName(), "-1"); 137 return Integer.parseInt(code); 138 } 139 140 public static Class getErrorClass(int code) 141 { 142 String key = String.valueOf(code); 143 Object clazz = reverseErrorCodes.get(key); 144 if (clazz == null) 145 { 146 return null; 147 } 148 else if (clazz instanceof Class ) 149 { 150 return (Class )clazz; 151 } 152 else 153 { 154 try 155 { 156 clazz = ClassUtils.loadClass(clazz.toString(), ExceptionHelper.class); 157 } 158 catch (ClassNotFoundException e) 159 { 160 logger.error(e.getMessage(), e); 161 return null; 162 } 163 reverseErrorCodes.put(key, clazz); 164 return (Class )clazz; 165 } 166 } 167 168 public static String getErrorMapping(String protocol, int code) 169 { 170 Class c = getErrorClass(code); 171 if (c != null) 172 { 173 return getErrorMapping(protocol, c); 174 } 175 else 176 { 177 logger.error("Class not known for code: " + code); 178 return "-1"; 179 } 180 } 181 182 private static Properties getErrorMappings(String protocol) 183 { 184 Object m = errorMappings.get(protocol); 185 if (m != null) 186 { 187 if (m instanceof Properties ) 188 { 189 return (Properties )m; 190 } 191 else 192 { 193 return null; 194 } 195 } 196 else 197 { 198 InputStream is = SpiUtils.findServiceDescriptor("org/mule/config", 199 protocol + "-exception-mappings.properties", ExceptionHelper.class); 200 if (is == null) 201 { 202 errorMappings.put(protocol, "not found"); 203 logger.warn("Failed to load error mappings from: META-INF/services/org/mule/config/" 204 + protocol 205 + "-exception-mappings.properties. This may be because there are no error code mappings for protocol: " 206 + protocol); 207 return null; 208 } 209 Properties p = new Properties (); 210 try 211 { 212 p.load(is); 213 } 214 catch (IOException e) 215 { 216 throw new MuleRuntimeException( 217 Message.createStaticMessage("Failed to load Exception resources"), e); 218 } 219 errorMappings.put(protocol, p); 220 String applyTo = p.getProperty(APPLY_TO_PROPERTY, null); 221 if (applyTo != null) 222 { 223 String [] protocols = StringUtils.splitAndTrim(applyTo, ","); 224 for (int i = 0; i < protocols.length; i++) 225 { 226 errorMappings.put(protocols[i], p); 227 } 228 } 229 return p; 230 } 231 } 232 233 public static String getErrorCodePropertyName(String protocol) 234 { 235 protocol = protocol.toLowerCase(); 236 Properties mappings = getErrorMappings(protocol); 237 if (mappings == null) 238 { 239 return null; 240 } 241 return mappings.getProperty(ERROR_CODE_PROPERTY); 242 } 243 244 public static String getErrorMapping(String protocol, Class exception) 245 { 246 protocol = protocol.toLowerCase(); 247 Properties mappings = getErrorMappings(protocol); 248 if (mappings == null) 249 { 250 logger.info("No mappings found for protocol: " + protocol); 251 return String.valueOf(getErrorCode(exception)); 252 } 253 254 Class clazz = exception; 255 String code = null; 256 while (!clazz.equals(Object .class)) 257 { 258 code = mappings.getProperty(clazz.getName()); 259 if (code == null) 260 { 261 clazz = clazz.getSuperclass(); 262 } 263 else 264 { 265 return code; 266 } 267 } 268 code = String.valueOf(getErrorCode(exception)); 269 return mappings.getProperty(code, code); 272 } 273 274 public static String getJavaDocUrl(Class exception) 275 { 276 return getDocUrl("javadoc.", exception.getName()); 277 } 278 279 public static String getDocUrl(Class exception) 280 { 281 return getDocUrl("doc.", exception.getName()); 282 } 283 284 private static String getDocUrl(String prefix, String packageName) 285 { 286 String key = prefix; 287 if (packageName.startsWith("java.") || packageName.startsWith("javax.")) 288 { 289 key += J2SE_VERSION; 290 } 291 String url = getUrl(key, packageName); 292 if (url == null && (packageName.startsWith("java.") || packageName.startsWith("javax."))) 293 { 294 key = prefix + J2EE_VERSION; 295 url = getUrl(key, packageName); 296 } 297 if (url != null) 298 { 299 if (!url.endsWith("/")) 300 { 301 url += "/"; 302 } 303 String s = packageName.replaceAll("[.]", "/"); 304 s += ".html"; 305 url += s; 306 } 307 308 318 return url; 319 } 320 321 private static String getUrl(String key, String packageName) 322 { 323 String url = null; 324 if (!key.endsWith(".")) 325 { 326 key += "."; 327 } 328 while (packageName.length() > 0) 329 { 330 url = errorDocs.getProperty(key + packageName, null); 331 if (url == null) 332 { 333 int i = packageName.lastIndexOf("."); 334 if (i == -1) 335 { 336 packageName = ""; 337 } 338 else 339 { 340 packageName = packageName.substring(0, i); 341 } 342 } 343 else 344 { 345 break; 346 } 347 } 348 return url; 349 } 350 351 public static Throwable getRootException(Throwable t) 352 { 353 Throwable cause = t; 354 Throwable root = null; 355 while (cause != null) 356 { 357 root = cause; 358 cause = getExceptionReader(cause).getCause(cause); 359 if (t == cause) 361 { 362 break; 363 } 364 } 365 return root; 366 } 367 368 public static Throwable getRootParentException(Throwable t) 369 { 370 Throwable cause = t; 371 Throwable parent = t; 372 while (cause != null) 373 { 374 if (cause.getCause() == null) 375 { 376 return parent; 377 } 378 parent = cause; 379 cause = getExceptionReader(cause).getCause(cause); 380 if (t == cause) 382 { 383 break; 384 } 385 } 386 return t; 387 } 388 389 public static UMOException getRootMuleException(Throwable t) 390 { 391 Throwable cause = t; 392 UMOException umoException = null; 393 while (cause != null) 394 { 395 if (cause instanceof UMOException) 396 { 397 umoException = (UMOException)cause; 398 } 399 cause = getExceptionReader(cause).getCause(cause); 400 if (t == cause) 402 { 403 break; 404 } 405 } 406 return umoException; 407 } 408 409 public static List getExceptionsAsList(Throwable t) 410 { 411 List exceptions = new ArrayList (); 412 Throwable cause = t; 413 while (cause != null) 414 { 415 exceptions.add(0, cause); 416 cause = getExceptionReader(cause).getCause(cause); 417 if (t == cause) 419 { 420 break; 421 } 422 } 423 return exceptions; 424 } 425 426 public static Map getExceptionInfo(Throwable t) 427 { 428 Map info = new HashMap (); 429 Throwable cause = t; 430 while (cause != null) 431 { 432 info.putAll(getExceptionReader(cause).getInfo(cause)); 433 cause = getExceptionReader(cause).getCause(cause); 434 if (t == cause) 436 { 437 break; 438 } 439 } 440 return info; 441 } 442 443 public static String getExceptionStack(Throwable t) 444 { 445 StringBuffer buf = new StringBuffer (); 446 List exceptions = getExceptionsAsList(t); 448 449 int i = 1; 450 for (Iterator iterator = exceptions.iterator(); iterator.hasNext(); i++) 451 { 452 if (i > exceptionThreshold && exceptionThreshold > 0) 453 { 454 buf.append("(").append(exceptions.size() - i + 1).append(" more...)"); 455 break; 456 } 457 Throwable throwable = (Throwable )iterator.next(); 458 ExceptionReader er = getExceptionReader(throwable); 459 buf.append(i).append(". ").append(er.getMessage(throwable)).append(" ("); 460 buf.append(throwable.getClass().getName()).append(")\n"); 461 if (verbose && throwable.getStackTrace().length > 0) 462 { 463 StackTraceElement e = throwable.getStackTrace()[0]; 464 buf.append(" ") 465 .append(e.getClassName()) 466 .append(":") 467 .append(e.getLineNumber()) 468 .append(" (") 469 .append(getJavaDocUrl(throwable.getClass())) 470 .append(")\n"); 471 } 472 } 473 return buf.toString(); 474 } 475 476 481 public static void registerExceptionReader(ExceptionReader reader) 482 { 483 exceptionReaders.add(reader); 484 } 485 486 493 public static ExceptionReader getExceptionReader(Throwable t) 494 { 495 for (Iterator iterator = exceptionReaders.iterator(); iterator.hasNext();) 496 { 497 ExceptionReader exceptionReader = (ExceptionReader)iterator.next(); 498 if (exceptionReader.getExceptionType().isInstance(t)) 499 { 500 return exceptionReader; 501 } 502 } 503 return defaultExceptionReader; 504 } 505 506 public static String writeException(Throwable t) 507 { 508 ExceptionReader er = getExceptionReader(t); 509 StringBuffer msg = new StringBuffer (); 510 msg.append(er.getMessage(t)).append(". Type: ").append(t.getClass()); 511 return msg.toString(); 512 } 513 } 514 | Popular Tags |