ErrorHandler.java :  » J2EE » Enhydra-Demos » net » sourceforge » ejosa » piggybank » presentation » enhydra » Java Open Source

Java Open Source » J2EE » Enhydra Demos 
Enhydra Demos » net » sourceforge » ejosa » piggybank » presentation » enhydra » ErrorHandler.java
/**
 * Title:        OpenUSS - Open Source University Support System
 *               My Piggy Bank Example
 * Description:  Enhydra Presentation Object
 * Copyright:    Copyright (c) 2003 by B. Lofi Dewanto, T. Menzel
 * Company:      University of Muenster, HTWK Leipzig
 * @author  B. Lofi Dewanto, T. Menzel
 * @version 1.1
 */
package net.sourceforge.ejosa.piggybank.presentation.enhydra;

import com.lutris.appserver.server.*;
import com.lutris.appserver.server.httpPresentation.*;

import com.lutris.logging.*;

import java.io.*;

import java.text.*;

import java.util.*;

import org.openuss.utility.*;

import org.w3c.dom.*;
import org.w3c.dom.html.*;


/**
 * Class to handle exceptions not caught anywhere else in the
 * framework of our application
 *
 * @author  B. Lofi Dewanto, T. Menzel
 * @version 1.1
 */
public class ErrorHandler implements HttpPresentation {
    // Page width
    private static int PAGE_WIDTH = 50;

    /**
     * Handling the error and special handling for FilePresentationException.
     * @author  B. Lofi Dewanto
     */
    public void run(HttpPresentationComms comms)
             throws HttpPresentationException {
        // Check for exception
        if (comms.exception != null) {
            // --- Check for file not found exeception ---
            // This only happens if a html file has many links on
            // it and try to get the file from the server...
            if (comms.exception instanceof FilePresentationException) {
                // Do nothing
            } else {
                // --- Handle the rest of the error ---
                // Create the error page
                ErrorHTML errorPage = (ErrorHTML) comms.xmlcFactory.create(
                                              ErrorHTML.class);

                StringWriter stringWriter = new StringWriter();
                comms.exception.printStackTrace(new PrintWriter(stringWriter));

                LogChannel logChannel = Enhydra.getLogChannel();

                // Use "ERROR" when going into release mode
                // int level = logChannel.getLevel("ERROR");
                int level = logChannel.getLevel("DEBUG");

                logChannel.write(level, "EJOSA error = ");
                logChannel.write(level, stringWriter.toString());
                logChannel.write(level, 
                                 "EJOSA caught an exception - " + 
                                 comms.exception.toString(), comms.exception);

                System.out.println(comms.exception);

                // Text message
                Element elementTextErrorMessaqeFormatted = 
                        EnhydraPresentationUtility.formatTextToHtml(
                                comms.exception.getMessage(), errorPage, 
                                PAGE_WIDTH);

                Element elementTextErrorMessaqe = errorPage.getElementErrorMessage();
                elementTextErrorMessaqe.removeAttribute("id");

                Node deleteNode = elementTextErrorMessaqe.getFirstChild();
                elementTextErrorMessaqe.removeChild(deleteNode);
                elementTextErrorMessaqe.appendChild(
                        elementTextErrorMessaqeFormatted);

                // Stack trace message
                Element elementStackTraceFormatted = EnhydraPresentationUtility.formatTextToHtml(
                                                             stringWriter.toString(), 
                                                             errorPage, 
                                                             PAGE_WIDTH);

                Element elementTextStackTrace = errorPage.getElementStackTrace();
                elementTextStackTrace.removeAttribute("id");

                deleteNode = elementTextStackTrace.getFirstChild();
                elementTextStackTrace.removeChild(deleteNode);
                elementTextStackTrace.appendChild(elementStackTraceFormatted);


                // Write this out
                comms.response.writeHTML(errorPage.toDocument());
            }
        }
    }
}
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.