001: /*
002: * @(#)ErrorImpl.java
003: *
004: * Copyright (C) 2002-2003 Matt Albrecht
005: * groboclown@users.sourceforge.net
006: * http://groboutils.sourceforge.net
007: *
008: * Part of the GroboUtils package at:
009: * http://groboutils.sourceforge.net
010: *
011: * Permission is hereby granted, free of charge, to any person obtaining a
012: * copy of this software and associated documentation files (the "Software"),
013: * to deal in the Software without restriction, including without limitation
014: * the rights to use, copy, modify, merge, publish, distribute, sublicense,
015: * and/or sell copies of the Software, and to permit persons to whom the
016: * Software is furnished to do so, subject to the following conditions:
017: *
018: * The above copyright notice and this permission notice shall be included in
019: * all copies or substantial portions of the Software.
020: *
021: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
022: * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
023: * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
024: * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
025: * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
026: * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
027: * DEALINGS IN THE SOFTWARE.
028: */
029: package net.sourceforge.groboutils.mbtf.v1.engine;
030:
031: import java.io.StringWriter;
032: import java.io.PrintWriter;
033:
034: import net.sourceforge.groboutils.mbtf.v1.IError;
035: import net.sourceforge.groboutils.mbtf.v1.IPathHistory;
036:
037: /**
038: * Immutible IError implementation.
039: *
040: * @author Matt Albrecht <a href="mailto:groboclown@users.sourceforge.net">groboclown@users.sourceforge.net</a>
041: * @version $Date: 2003/02/10 22:52:26 $
042: * @since June 12, 2002
043: */
044: public class ErrorImpl implements IError {
045: private IPathHistory pathHistory;
046: private Throwable err;
047: private String msg;
048:
049: public ErrorImpl(String msg, Throwable err, IPathHistory ph) {
050: this .msg = msg;
051: this .err = err;
052: this .pathHistory = ph;
053: }
054:
055: /**
056: * The path history at the time of the error, if any history was known.
057: *
058: * @return the path history, or <tt>null</tt>.
059: */
060: public IPathHistory getPathHistory() {
061: return this .pathHistory;
062: }
063:
064: /**
065: * Returns the <tt>Throwable</tt> registered with the error report, if
066: * any.
067: *
068: * @return the registered throwable, or <tt>null</tt>.
069: */
070: public Throwable getThrowable() {
071: return this .err;
072: }
073:
074: /**
075: * Returns the human-readable message associated with the error, if any.
076: *
077: * @return the human-readable message, or <tt>null</tt>.
078: */
079: public String getMessage() {
080: return this .msg;
081: }
082:
083: public String toString() {
084: String ls = System.getProperty("line.separator");
085: if (ls == null) {
086: ls = "\n";
087: }
088: StringBuffer sb = new StringBuffer("ERROR: ");
089: if (getMessage() != null) {
090: sb.append(getMessage());
091: sb.append(":");
092: }
093: sb.append(ls);
094: if (getThrowable() != null) {
095: StringWriter sw = new StringWriter();
096: PrintWriter pw = new PrintWriter(sw);
097: getThrowable().printStackTrace(pw);
098:
099: sb.append(sw.toString());
100: }
101: if (getPathHistory() != null) {
102: sb.append("at").append(ls).append(getPathHistory());
103: }
104:
105: return sb.toString();
106: }
107: }
|