1 19 package org.enhydra.zeus; 20 21 import java.io.PrintStream ; 22 import java.io.PrintWriter ; 23 import java.lang.ExceptionInInitializerError ; 24 import java.lang.reflect.Method ; 25 import java.lang.reflect.InvocationTargetException ; 26 import java.rmi.RemoteException ; 27 28 import org.xml.sax.SAXException ; 30 31 43 public class ZeusException extends Exception { 44 45 46 protected Throwable cause; 47 48 53 public ZeusException() { 54 super("An exception occurred in Zeus processing."); 55 } 56 57 65 public ZeusException(String message) { 66 super(message); 67 } 68 69 81 public ZeusException(String message, Throwable cause) { 82 super(message); 83 this.cause = cause; 84 } 85 86 93 public Throwable initCause(Throwable cause) { 94 this.cause = cause; 95 return cause; 96 } 97 98 107 public String getMessage() { 108 String msg = super.getMessage(); 110 111 Throwable parent = this; 112 Throwable child; 113 114 while((child = getNestedException(parent)) != null) { 116 String msg2 = child.getMessage(); 118 119 125 if (child instanceof SAXException ) { 126 Throwable grandchild = ((SAXException )child).getException(); 127 132 if ((grandchild != null) && 133 (msg2 != null) && 134 (msg2.equals(grandchild.getMessage()))) { 135 msg2 = null; 136 } 137 } 138 139 if (msg2 != null) { 141 if (msg != null) { 142 msg += ": " + msg2; 143 } else { 144 msg = msg2; 145 } 146 } 147 148 152 if (child instanceof ZeusException) { 153 break; 154 } 155 parent = child; 156 } 157 158 return msg; 160 } 161 162 169 public void printStackTrace() { 170 super.printStackTrace(); 172 173 Throwable parent = this; 174 Throwable child; 175 176 while((child = getNestedException(parent)) != null) { 178 if (child != null) { 179 System.err.print("Caused by: "); 180 child.printStackTrace(); 181 185 if (child instanceof ZeusException) { 186 break; 187 } 188 parent = child; 189 } 190 } 191 } 192 193 200 public void printStackTrace(PrintStream s) { 201 super.printStackTrace(s); 203 204 Throwable parent = this; 205 Throwable child; 206 207 while((child = getNestedException(parent)) != null) { 209 if (child != null) { 210 System.err.print("Caused by: "); 211 child.printStackTrace(s); 212 216 if (child instanceof ZeusException) { 217 break; 218 } 219 parent = child; 220 } 221 } 222 } 223 224 231 public void printStackTrace(PrintWriter w) { 232 super.printStackTrace(w); 234 235 Throwable parent = this; 236 Throwable child; 237 238 while((child = getNestedException(parent)) != null) { 240 if (child != null) { 241 System.err.print("Caused by: "); 242 child.printStackTrace(w); 243 247 if (child instanceof ZeusException) { 248 break; 249 } 250 parent = child; 251 } 252 } 253 } 254 255 263 public Throwable getCause() { 264 return cause; 265 } 266 267 277 private static Throwable getNestedException(Throwable parent) { 278 if (parent instanceof ZeusException) { 279 return ((ZeusException)parent).getCause(); 280 } 281 282 if (parent instanceof SAXException ) { 283 return ((SAXException )parent).getException(); 284 } 285 286 if (parent instanceof InvocationTargetException ) { 287 return ((InvocationTargetException )parent).getTargetException(); 288 } 289 290 if (parent instanceof ExceptionInInitializerError ) { 291 return ((ExceptionInInitializerError )parent).getException(); 292 } 293 294 if (parent instanceof RemoteException ) { 295 return ((RemoteException )parent).detail; 296 } 297 298 302 Throwable nestedException = getNestedException(parent, 303 "javax.naming.NamingException", "getRootCause"); 304 if (nestedException != null) { 305 return nestedException; 306 } 307 308 return null; 309 } 310 311 323 private static Throwable getNestedException(Throwable parent, 324 String className, 325 String methodName) { 326 try { 327 331 Class testClass = Class.forName(className); 332 Class objectClass = parent.getClass(); 333 if (testClass.isAssignableFrom(objectClass)) { 334 Class [] argClasses = new Class [0]; 336 Method method = testClass.getMethod(methodName, argClasses); 337 Object [] args = new Object [0]; 338 return (Throwable )method.invoke(parent, args); 339 } 340 } 341 catch(Exception ex) { 342 349 } 350 351 return null; 352 } 353 } 354 | Popular Tags |