// MobyException.java
//
// Created: February 2003
//
// This file is a component of the BioMoby project.
// Copyright Martin Senger (martin.senger@gmail.com).
//

package org.biomoby.shared;

import java.io.StringWriter;
import java.io.PrintWriter;

/** A general exception which can be used as a wrapper
 *  around other exceptions. Using this exception
 *  simplifies the code (but on the other hand it makes it less
 *  "type-safe" - which is usually not a problem when dealing
 *  with exceptions).
 *  <P>
 * @author <A HREF="mailto:martin.senger@gmail.com">Martin Senger</A>
 * @version $Id: MobyException.java,v 1.5 2008/03/02 12:45:26 senger Exp $
 */

public class MobyException extends Exception {

    private static org.apache.commons.logging.Log log =
       org.apache.commons.logging.LogFactory.getLog (MobyException.class);
    
    private static final long serialVersionUID = 3257853190165969203L;

    /** @serial */
    public MobyException ()         { super(); }

    /******************************************************************************
     * A constructor specifying a reason for this exception.
     * @param s message/reason
     ******************************************************************************/
    public MobyException (String s) { super (s); }

    /******************************************************************************
     * A constructor used for storing another exception in MobyException.
     * It allows access to the original (wrapped) exception and its message.
     *
     * @param s message (or reason of)
     * @param e original cause of this exception
     ******************************************************************************/
    public MobyException (String s, Throwable e) {
	super (s, e);
	if (e != null) {
 	    formatAndLog (e, log);
 	}
    }

    /******************************************************************************
     * Retrieve the original exception. 
     *
     * @deprecated Use instead <tt>getCause()</tt> directly.
     *
     * @return an original exception which was wrapped by this BiomonyException, or
     *         null if there was no original exception involved
     ******************************************************************************/
    public Throwable getOriginalException() {
	return getCause();
    }

    /******************************************************************************
     * Format given exception 'e' depending on how serious it it. In
     * same cases add stack trace. Log the result in the given
     * 'log'. <p>
     *
     * @param e an exception to be formatted and logged
     * @param log where to log it
     ******************************************************************************/
    public static void formatAndLog (Throwable e,
				     org.apache.commons.logging.Log log) {
	boolean seriousError =
	    (e instanceof java.lang.RuntimeException);
	if (seriousError || log.isDebugEnabled()) {
	    StringWriter sw = new StringWriter (500);
	    e.printStackTrace (new PrintWriter (sw));
	    if (seriousError)
		log.error (sw.toString());
	    else
		log.debug (sw.toString());
	} else {
	    log.error (e.getMessage());
	}
    }
    
}
