001: /*
002: * $Id: MuleException.java 10489 2008-01-23 17:53:38Z dfeist $
003: * --------------------------------------------------------------------------------------
004: * Copyright (c) MuleSource, Inc. All rights reserved. http://www.mulesource.com
005: *
006: * The software in this package is published under the terms of the CPAL v1.0
007: * license, a copy of which has been included with this distribution in the
008: * LICENSE.txt file.
009: */
010:
011: package org.mule.api;
012:
013: import org.mule.config.ExceptionHelper;
014: import org.mule.config.i18n.CoreMessages;
015: import org.mule.config.i18n.Message;
016: import org.mule.config.i18n.MessageFactory;
017: import org.mule.util.StringUtils;
018: import org.mule.util.SystemUtils;
019:
020: import java.io.PrintWriter;
021: import java.io.StringWriter;
022: import java.lang.reflect.InvocationTargetException;
023: import java.util.HashMap;
024: import java.util.Iterator;
025: import java.util.Map;
026:
027: /**
028: * <code>MuleException</code> is the base exception type for the Mule server any
029: * other exceptions thrown by Mule code will be based on this exception,
030: */
031: public abstract class MuleException extends Exception {
032: private Map info = new HashMap();
033: private int errorCode = -1;
034: private String message = null;
035: private Message i18nMessage;
036:
037: /**
038: * @param message the exception message
039: */
040: public MuleException(Message message) {
041: super ();
042: setMessage(message);
043: }
044:
045: /**
046: * @param message the exception message
047: * @param cause the exception that cause this exception to be thrown
048: */
049: public MuleException(Message message, Throwable cause) {
050: super (
051: (cause instanceof InvocationTargetException ? ((InvocationTargetException) cause)
052: .getTargetException()
053: : cause));
054: setMessage(message);
055: }
056:
057: public MuleException(Throwable cause) {
058: super (
059: (cause instanceof InvocationTargetException ? ((InvocationTargetException) cause)
060: .getTargetException()
061: : cause));
062: if (cause != null) {
063: setMessage(MessageFactory.createStaticMessage(cause
064: .getMessage()
065: + " (" + cause.getClass().getName() + ")"));
066: } else {
067: initialise();
068: }
069: }
070:
071: protected MuleException() {
072: super ();
073: initialise();
074: }
075:
076: protected void setMessage(Message message) {
077: initialise();
078: this .message = message.getMessage();
079: i18nMessage = message;
080: }
081:
082: protected void setMessage(String message) {
083: initialise();
084: this .message = message;
085: if (i18nMessage == null) {
086: i18nMessage = MessageFactory.createStaticMessage(message);
087: }
088: }
089:
090: public int getExceptionCode() {
091: return errorCode;
092: }
093:
094: public Message getI18nMessage() {
095: return i18nMessage;
096: }
097:
098: public int getMessageCode() {
099: return (i18nMessage == null ? 0 : i18nMessage.getCode());
100: }
101:
102: public void addInfo(String name, Object info) {
103: this .info.put(name, info);
104: }
105:
106: protected void appendMessage(String s) {
107: message += s;
108: }
109:
110: protected void prependMessage(String s) {
111: message = message + ". " + s;
112: }
113:
114: protected void setExceptionCode(int code) {
115: errorCode = code;
116: }
117:
118: public final String getMessage() {
119: return message;
120: }
121:
122: protected void initialise() {
123: setExceptionCode(ExceptionHelper.getErrorCode(getClass()));
124: String javadoc = ExceptionHelper.getJavaDocUrl(getClass());
125: String doc = ExceptionHelper.getDocUrl(getClass());
126: if (javadoc != null) {
127: // info.put(ClassHelper.getClassName(getClass()) + " JavaDoc", javadoc);
128: info.put("JavaDoc", javadoc);
129: }
130: if (doc != null) {
131: // info.put(ClassHelper.getClassName(getClass()) + " Other Doc", doc);
132: info.put("Other Doc", doc);
133: }
134: }
135:
136: public String getDetailedMessage() {
137: MuleException e = ExceptionHelper.getRootMuleException(this );
138: if (!e.equals(this )) {
139: return getMessage();
140: }
141: StringBuffer buf = new StringBuffer(1024);
142: buf.append(SystemUtils.LINE_SEPARATOR).append(
143: StringUtils.repeat('*', 80)).append(
144: SystemUtils.LINE_SEPARATOR);
145: buf.append("Message : ").append(message).append(
146: SystemUtils.LINE_SEPARATOR);
147: buf.append("Type : ").append(
148: getClass().getName())
149: .append(SystemUtils.LINE_SEPARATOR);
150: buf.append("Code : ").append("MULE_ERROR-")
151: .append(getExceptionCode() + getMessageCode()).append(
152: SystemUtils.LINE_SEPARATOR);
153: // buf.append("Msg Code :
154: // ").append(getMessageCode()).append(SystemUtils.LINE_SEPARATOR);
155:
156: Map info = ExceptionHelper.getExceptionInfo(this );
157: for (Iterator iterator = info.keySet().iterator(); iterator
158: .hasNext();) {
159: String s = (String) iterator.next();
160: int pad = 22 - s.length();
161: buf.append(s);
162: if (pad > 0) {
163: buf.append(StringUtils.repeat(' ', pad));
164: }
165: buf.append(": ");
166: buf.append(info.get(s)).append(SystemUtils.LINE_SEPARATOR);
167: }
168:
169: // print exception stack
170: buf.append(StringUtils.repeat('*', 80)).append(
171: SystemUtils.LINE_SEPARATOR);
172: buf.append(CoreMessages.exceptionStackIs()).append(
173: SystemUtils.LINE_SEPARATOR);
174: buf.append(ExceptionHelper.getExceptionStack(this ));
175:
176: buf.append(StringUtils.repeat('*', 80)).append(
177: SystemUtils.LINE_SEPARATOR);
178: buf.append(CoreMessages.rootStackTrace()).append(
179: SystemUtils.LINE_SEPARATOR);
180: Throwable root = ExceptionHelper.getRootException(this );
181: StringWriter w = new StringWriter();
182: PrintWriter p = new PrintWriter(w);
183: root.printStackTrace(p);
184: buf.append(w.toString()).append(SystemUtils.LINE_SEPARATOR);
185: buf.append(StringUtils.repeat('*', 80)).append(
186: SystemUtils.LINE_SEPARATOR);
187:
188: return buf.toString();
189: }
190:
191: public boolean equals(Object o) {
192: if (this == o) {
193: return true;
194: }
195: if (!(o instanceof MuleException)) {
196: return false;
197: }
198:
199: final MuleException umoException = (MuleException) o;
200:
201: if (errorCode != umoException.errorCode) {
202: return false;
203: }
204: if (i18nMessage != null ? !i18nMessage
205: .equals(umoException.i18nMessage)
206: : umoException.i18nMessage != null) {
207: return false;
208: }
209: if (message != null ? !message.equals(umoException.message)
210: : umoException.message != null) {
211: return false;
212: }
213:
214: return true;
215: }
216:
217: public int hashCode() {
218: int result;
219: result = errorCode;
220: result = 29 * result
221: + (message != null ? message.hashCode() : 0);
222: result = 29 * result
223: + (i18nMessage != null ? i18nMessage.hashCode() : 0);
224: return result;
225: }
226:
227: public Map getInfo() {
228: return info;
229: }
230: }
|