01: /*
02: * Created on 5 Jul 2006
03: */
04: package uk.org.ponder.rsf.processor;
05:
06: import java.io.PrintWriter;
07: import java.io.StringWriter;
08:
09: import uk.org.ponder.streamutil.write.PrintOutputStream;
10: import uk.org.ponder.util.Logger;
11: import uk.org.ponder.util.UniversalRuntimeException;
12: import uk.org.ponder.xml.XMLWriter;
13:
14: /**
15: * The default FatalErrorHandler for RSF. In addition to logic to print a hardwired
16: * completelly fail-safe message, also contains definition of the strategy to
17: * be used for user-defined strategies as a first line.
18: *
19: * @author Antranig Basman (antranig@caret.cam.ac.uk)
20: */
21:
22: public class DefaultFatalErrorHandler implements FatalErrorHandler {
23: private Class[] propagated;
24:
25: /** Set a list of exception classes which will cause no action to be
26: * performed by the fatal handler.
27: */
28: public void setPropagatedExceptions(Class[] propagated) {
29: this .propagated = propagated;
30: }
31:
32: public static String handleFatalErrorStatic(Throwable t,
33: PrintOutputStream pos) {
34: // We may have such a fatal misconfiguration that we can't even rely on
35: // IKAT to format this error message
36: Logger.log.fatal("Completely fatal error populating view root",
37: t);
38:
39: pos
40: .println("<html><head><title>Internal Error</title></head></body><pre>");
41: pos.println("Fatal internal error handling request: ");
42: StringWriter todump = new StringWriter();
43: t.printStackTrace(new PrintWriter(todump));
44: XMLWriter xml = new XMLWriter(pos);
45: xml.write(todump.toString());
46: pos.println("</pre></body></html>");
47: pos.close();
48: return HANDLED;
49: }
50:
51: public static String handleFatalErrorStrategy(
52: FatalErrorHandler handler, Throwable t,
53: PrintOutputStream pos) {
54: String rendered = null;
55: Throwable failed = null;
56: try {
57: rendered = handler.handleFatalError(t, pos);
58: } catch (Throwable t2) {
59: failed = t2;
60: }
61: if (rendered == null || failed != null) {
62: return handleFatalErrorStatic(t, pos);
63: }
64: return rendered;
65: }
66:
67: public String handleFatalError(Throwable t, PrintOutputStream pos) {
68: Exception unwrapped = (Exception) UniversalRuntimeException
69: .unwrapException(t);
70: if (propagated != null) {
71: for (int i = 0; i < propagated.length; ++i) {
72: if (propagated[i].isInstance(unwrapped))
73: return HANDLE_EXCEPTION_UPSTAIRS;
74: }
75: }
76: return handleFatalErrorStatic(t, pos);
77: }
78:
79: }
|