01: package org.wings.session;
02:
03: import org.wings.*;
04: import org.wings.io.ServletDevice;
05: import org.wings.io.Device;
06: import org.wings.util.SStringBuilder;
07:
08: import java.io.*;
09:
10: /**
11: * This implementation queries a property <code>wings.error.template</code> for a resource name relative to the
12: * webapp of a wingS template. In this template, placeholders must be defined for wingS components named
13: * <code>EXCEPTION_STACK_TRACE</code>, <code>EXCEPTION_MESSAGE</code> and <code>WINGS_VERSION</code>.
14: */
15: public class DefaultExceptionHandler implements ExceptionHandler {
16: public void handle(Device device, Throwable thrown) {
17: try {
18: String errorTemplateFile = (String) SessionManager
19: .getSession().getProperty("wings.error.template");
20: if (errorTemplateFile == null)
21: throw new RuntimeException(
22: "Unable to display error page. "
23: + "In web.xml define wings.error.template to see the stacktrace online "
24: + "or wings.error.handler to replace the default exception handler");
25:
26: String resourcePath = SessionManager.getSession()
27: .getServletContext().getRealPath(errorTemplateFile);
28: STemplateLayout layout = new STemplateLayout(resourcePath);
29: final SFrame errorFrame = new SFrame();
30: errorFrame.getContentPane().setLayout(layout);
31:
32: final SLabel errorStackTraceLabel = new SLabel();
33: errorFrame.getContentPane().add(errorStackTraceLabel,
34: "EXCEPTION_STACK_TRACE");
35:
36: final SLabel errorMessageLabel = new SLabel();
37: errorFrame.getContentPane().add(errorMessageLabel,
38: "EXCEPTION_MESSAGE");
39:
40: final SLabel versionLabel = new SLabel();
41: errorFrame.getContentPane().add(versionLabel,
42: "WINGS_VERSION");
43:
44: versionLabel.setText("wingS " + Version.getVersion()
45: + " / " + Version.getCompileTime());
46:
47: // build the stacktrace wrapped by pre'device so line breaks are preserved
48: SStringBuilder stackTrace = new SStringBuilder(
49: "<html><pre>");
50: stackTrace.append(getStackTraceString(thrown));
51: stackTrace.append("</pre>");
52: errorStackTraceLabel.setText(stackTrace.toString());
53:
54: // if there is a message, print it, otherwise print "none".
55: errorMessageLabel
56: .setText(thrown.getMessage() != null ? thrown
57: .getMessage() : "none");
58: errorFrame.setVisible(true);
59: errorFrame.write(device);
60: errorFrame.setVisible(false);
61: } catch (IOException e) {
62: throw new RuntimeException(e);
63: }
64: }
65:
66: private String getStackTraceString(Throwable e) {
67: final StringWriter stringWriter = new StringWriter();
68: final PrintWriter printWriter = new PrintWriter(stringWriter);
69: e.printStackTrace(printWriter);
70: return stringWriter.toString();
71: }
72: }
|