1 9 package com.vladium.util.exception; 10 11 import java.io.PrintStream ; 12 import java.io.PrintWriter ; 13 import java.text.MessageFormat ; 14 import java.util.Collections ; 15 import java.util.Enumeration ; 16 import java.util.HashMap ; 17 import java.util.Locale ; 18 import java.util.Map ; 19 import java.util.ResourceBundle ; 20 21 import com.vladium.util.IJREVersion; 22 23 25 37 abstract class ExceptionCommon implements IJREVersion 38 { 39 41 89 public static ResourceBundle addExceptionResource (final Class namespace, 90 final String messageResourceBundleName) 91 { 92 if ((namespace != null) && (messageResourceBundleName != null) 93 && (messageResourceBundleName.length () > 0)) 94 { 95 if (! ABSTRACT_EXCEPTION.isAssignableFrom (namespace) 98 && ! ABSTACT_RUNTIME_EXCEPTION.isAssignableFrom (namespace)) 99 { 100 throw new Error ("addExceptionResource(): class [" + namespace + 101 "] is not a subclass of " + ABSTRACT_EXCEPTION.getName () + 102 " or " + ABSTACT_RUNTIME_EXCEPTION.getName ()); 103 } 104 105 107 ResourceBundle temprb = null; 108 String nameInNamespace = null; 109 try 110 { 111 nameInNamespace = getNameInNamespace (namespace, messageResourceBundleName); 112 113 115 ClassLoader loader = namespace.getClassLoader (); 116 if (loader == null) loader = ClassLoader.getSystemClassLoader (); 117 118 temprb = ResourceBundle.getBundle (nameInNamespace, Locale.getDefault (), loader); 119 } 120 catch (Throwable ignore) 121 { 122 temprb = null; 125 } 126 127 if (temprb != null) 128 { 129 synchronized (s_exceptionCodeMap) 130 { 131 final ResourceBundle currentrb = 132 (ResourceBundle ) s_exceptionCodeMap.get (namespace); 133 if (currentrb != null) 134 return currentrb; 135 else 136 { 137 s_exceptionCodeMap.put (namespace, temprb); 138 return temprb; 139 } 140 } 141 } 142 } 143 144 return null; 145 } 146 147 149 151 152 static void printStackTrace (Throwable t, final PrintWriter out) 153 { 154 if (JRE_1_4_PLUS) 155 { 156 if (t instanceof IThrowableWrapper) 157 { 158 final IThrowableWrapper tw = (IThrowableWrapper) t; 159 160 tw.__printStackTrace (out); 161 } 162 else 163 { 164 t.printStackTrace (out); 165 } 166 } 167 else 168 { 169 for (boolean first = true; t != null; ) 170 { 171 if (first) 172 first = false; 173 else 174 { 175 out.println (); 176 out.println (NESTED_THROWABLE_HEADER); 177 } 178 179 if (t instanceof IThrowableWrapper) 180 { 181 final IThrowableWrapper tw = (IThrowableWrapper) t; 182 183 tw.__printStackTrace (out); 184 t = tw.getCause (); 185 } 186 else 187 { 188 t.printStackTrace (out); 189 break; 190 } 191 } 192 } 193 } 194 195 196 static void printStackTrace (Throwable t, final PrintStream out) 197 { 198 if (JRE_1_4_PLUS) 199 { 200 if (t instanceof IThrowableWrapper) 201 { 202 final IThrowableWrapper tw = (IThrowableWrapper) t; 203 204 tw.__printStackTrace (out); 205 } 206 else 207 { 208 t.printStackTrace (out); 209 } 210 } 211 else 212 { 213 for (boolean first = true; t != null; ) 214 { 215 if (first) 216 first = false; 217 else 218 { 219 out.println (); 220 out.println (NESTED_THROWABLE_HEADER); 221 } 222 223 if (t instanceof IThrowableWrapper) 224 { 225 final IThrowableWrapper tw = (IThrowableWrapper) t; 226 227 tw.__printStackTrace (out); 228 t = tw.getCause (); 229 } 230 else 231 { 232 t.printStackTrace (out); 233 break; 234 } 235 } 236 } 237 } 238 239 240 259 static String getMessage (final Class namespace, final String code) 260 { 261 if (code == null) return null; 262 263 try 264 { 265 if (code.length () > 0) 266 { 267 final String msg = lookup (namespace, code); 269 if (msg == null) 270 { 271 return code; 273 } 274 else 275 { 276 return EMBED_ERROR_CODE ? "[" + code + "] " + msg : msg; 277 } 278 } 279 else 280 { 281 return ""; 282 } 283 } 284 catch (Throwable t) 285 { 286 return code; 289 } 290 } 291 292 314 static String getMessage (final Class namespace, final String code, final Object [] arguments) 315 { 316 if (code == null) return null; 317 final String pattern = getMessage (namespace, code); 318 319 321 if ((arguments == null) || (arguments.length == 0)) 322 { 323 return pattern; 324 } 325 else 326 { 327 try 328 { 329 return MessageFormat.format (pattern, arguments); 330 } 331 catch (Throwable t) 332 { 333 336 final StringBuffer msg = new StringBuffer (code + EOL); 337 338 for (int a = 0; a < arguments.length; a ++) 339 { 340 msg.append ("\t{" + a + "} = ["); 341 final Object arg = arguments [a]; 342 try 343 { 344 msg.append (arg.toString ()); 345 } 346 catch (Throwable e) { 348 if (arg != null) 349 msg.append (arg.getClass ().getName ()); 350 else 351 msg.append ("null"); 352 } 353 msg.append ("]"); 354 msg.append (EOL); 355 } 356 357 return msg.toString (); 358 } 359 } 360 } 361 362 364 365 private ExceptionCommon () {} 367 373 private static String lookup (Class namespace, final String propertyName) 374 { 375 if (propertyName == null) return null; 376 377 380 if (namespace != null) 382 { 383 ResourceBundle rb; 384 while (namespace != ABSTRACT_EXCEPTION && namespace != ABSTACT_RUNTIME_EXCEPTION 385 && namespace != THROWABLE && namespace != null) 386 { 387 synchronized (s_exceptionCodeMap) 388 { 389 rb = (ResourceBundle ) s_exceptionCodeMap.get (namespace); 390 if (rb == null) 391 { 392 if ((rb = addExceptionResource (namespace, "exceptions")) == null) 394 { 395 s_exceptionCodeMap.put (namespace, EMPTY_RESOURCE_BUNDLE); 397 } 398 } 399 } 400 401 if (rb != null) 402 { 403 String propertyValue = null; 404 try 405 { 406 propertyValue = rb.getString (propertyName); 407 } 408 catch (Throwable ignore) {} 409 if (propertyValue != null) return propertyValue; 410 } 411 412 namespace = namespace.getSuperclass (); 414 } 415 } 416 417 if (ROOT_RESOURCE_BUNDLE != null) 419 { 420 String propertyValue = null; 421 try 422 { 423 propertyValue = ROOT_RESOURCE_BUNDLE.getString (propertyName); 424 } 425 catch (Throwable ignore) {} 426 if (propertyValue != null) return propertyValue; 427 } 428 429 return null; 430 } 431 432 private static String getNameInNamespace (final Class namespace, final String name) 433 { 434 if (namespace == null) return name; 435 436 final String namespaceName = namespace.getName (); 437 final int lastDot = namespaceName.lastIndexOf ('.'); 438 439 if (lastDot <= 0) 440 return name; 441 else 442 return namespaceName.substring (0, lastDot + 1) + name; 443 } 444 445 446 private static final boolean EMBED_ERROR_CODE = true; 449 450 private static final String ROOT_RESOURCE_BUNDLE_NAME; 454 private static final ResourceBundle ROOT_RESOURCE_BUNDLE; 457 private static final Map s_exceptionCodeMap = new HashMap (); 459 460 462 private static final String NESTED_THROWABLE_HEADER = "[NESTED EXCEPTION]:"; 463 private static final Class THROWABLE = Throwable .class; 464 private static final Class ABSTRACT_EXCEPTION = AbstractException.class; 465 private static final Class ABSTACT_RUNTIME_EXCEPTION = AbstractRuntimeException.class; 466 static final Enumeration EMPTY_ENUMERATION = Collections.enumeration (Collections.EMPTY_SET); 467 private static final ResourceBundle EMPTY_RESOURCE_BUNDLE = new ResourceBundle () 468 { 469 public Object handleGetObject (final String key) 470 { 471 return null; 472 } 473 474 public Enumeration getKeys () 475 { 476 return EMPTY_ENUMERATION; 477 } 478 }; 479 480 private static final String EOL = System.getProperty ("line.separator", "\n"); 482 483 484 static 485 { 486 ROOT_RESOURCE_BUNDLE_NAME = getNameInNamespace (ExceptionCommon.class, "exceptions"); 488 489 ResourceBundle temprb = null; 491 try 492 { 493 temprb = ResourceBundle.getBundle (ROOT_RESOURCE_BUNDLE_NAME); 494 } 495 catch (Throwable ignore) 496 { 497 } 499 ROOT_RESOURCE_BUNDLE = temprb; 500 } 501 502 } | Popular Tags |