001: /**
002: * Title: OpenUSS - Open Source University Support System
003: * My Piggy Bank Example
004: * Description: Enhydra Presentation Object
005: * Copyright: Copyright (c) 2003 by B. Lofi Dewanto, T. Menzel
006: * Company: University of Muenster, HTWK Leipzig
007: * @author B. Lofi Dewanto, T. Menzel
008: * @version 1.1
009: */package net.sourceforge.ejosa.piggybank.presentation.enhydra;
010:
011: import com.lutris.appserver.server.*;
012: import com.lutris.appserver.server.httpPresentation.*;
013:
014: import com.lutris.logging.*;
015:
016: import java.io.*;
017:
018: import java.text.*;
019:
020: import java.util.*;
021:
022: import org.openuss.utility.*;
023:
024: import org.w3c.dom.*;
025: import org.w3c.dom.html.*;
026:
027: /**
028: * Class to handle exceptions not caught anywhere else in the
029: * framework of our application
030: *
031: * @author B. Lofi Dewanto, T. Menzel
032: * @version 1.1
033: */
034: public class ErrorHandler implements HttpPresentation {
035: // Page width
036: private static int PAGE_WIDTH = 50;
037:
038: /**
039: * Handling the error and special handling for FilePresentationException.
040: * @author B. Lofi Dewanto
041: */
042: public void run(HttpPresentationComms comms)
043: throws HttpPresentationException {
044: // Check for exception
045: if (comms.exception != null) {
046: // --- Check for file not found exeception ---
047: // This only happens if a html file has many links on
048: // it and try to get the file from the server...
049: if (comms.exception instanceof FilePresentationException) {
050: // Do nothing
051: } else {
052: // --- Handle the rest of the error ---
053: // Create the error page
054: ErrorHTML errorPage = (ErrorHTML) comms.xmlcFactory
055: .create(ErrorHTML.class);
056:
057: StringWriter stringWriter = new StringWriter();
058: comms.exception.printStackTrace(new PrintWriter(
059: stringWriter));
060:
061: LogChannel logChannel = Enhydra.getLogChannel();
062:
063: // Use "ERROR" when going into release mode
064: // int level = logChannel.getLevel("ERROR");
065: int level = logChannel.getLevel("DEBUG");
066:
067: logChannel.write(level, "EJOSA error = ");
068: logChannel.write(level, stringWriter.toString());
069: logChannel.write(level, "EJOSA caught an exception - "
070: + comms.exception.toString(), comms.exception);
071:
072: System.out.println(comms.exception);
073:
074: // Text message
075: Element elementTextErrorMessaqeFormatted = EnhydraPresentationUtility
076: .formatTextToHtml(comms.exception.getMessage(),
077: errorPage, PAGE_WIDTH);
078:
079: Element elementTextErrorMessaqe = errorPage
080: .getElementErrorMessage();
081: elementTextErrorMessaqe.removeAttribute("id");
082:
083: Node deleteNode = elementTextErrorMessaqe
084: .getFirstChild();
085: elementTextErrorMessaqe.removeChild(deleteNode);
086: elementTextErrorMessaqe
087: .appendChild(elementTextErrorMessaqeFormatted);
088:
089: // Stack trace message
090: Element elementStackTraceFormatted = EnhydraPresentationUtility
091: .formatTextToHtml(stringWriter.toString(),
092: errorPage, PAGE_WIDTH);
093:
094: Element elementTextStackTrace = errorPage
095: .getElementStackTrace();
096: elementTextStackTrace.removeAttribute("id");
097:
098: deleteNode = elementTextStackTrace.getFirstChild();
099: elementTextStackTrace.removeChild(deleteNode);
100: elementTextStackTrace
101: .appendChild(elementStackTraceFormatted);
102:
103: // Write this out
104: comms.response.writeHTML(errorPage.toDocument());
105: }
106: }
107: }
108: }
|