001: /*
002: * Copyright 2001-2006 C:1 Financial Services GmbH
003: *
004: * This software is free software; you can redistribute it and/or
005: * modify it under the terms of the GNU Lesser General Public
006: * License Version 2.1, as published by the Free Software Foundation.
007: *
008: * This software is distributed in the hope that it will be useful,
009: * but WITHOUT ANY WARRANTY; without even the implied warranty of
010: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
011: * Lesser General Public License for more details.
012: *
013: * You should have received a copy of the GNU Lesser General Public
014: * License along with this library; if not, write to the Free Software
015: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA
016: */
017:
018: package de.finix.contelligent.exception;
019:
020: import java.util.Arrays;
021: import java.util.Locale;
022:
023: import de.finix.contelligent.i18n.LocalizedMessage;
024:
025: /**
026: * The main runtime exception class.
027: */
028: public class ContelligentRuntimeException extends RuntimeException {
029:
030: private Throwable throwable;
031:
032: private Object[] info;
033:
034: /**
035: * Creates a new <code>ContelligentException</code> instance.
036: *
037: * @param message
038: * exception message for debug purposes
039: */
040: public ContelligentRuntimeException(String message) {
041: this (message, null, null);
042: }
043:
044: /**
045: * Creates a new <code>ContelligentException</code> instance.
046: *
047: * @param message
048: * exception message for debug purposes
049: * @param info
050: * array of objects to describe the exception in more detail
051: */
052: public ContelligentRuntimeException(String message, Object[] info) {
053: this (message, info, null);
054: }
055:
056: /**
057: * Creates a new <code>ContelligentException</code> instance.
058: *
059: * @param message
060: * exception message for debug purposes
061: * @param t
062: * <code>Throwable</code> which caused this exception
063: */
064: public ContelligentRuntimeException(String message, Throwable t) {
065: this (message, null, t);
066: }
067:
068: /**
069: * Creates a new <code>ContelligentException</code> instance.
070: *
071: * @param message
072: * exception message for debug purposes
073: * @param info
074: * array of objects to describe the exception in more detail
075: * @param t
076: * <code>Throwable</code> which caused this exception
077: */
078: public ContelligentRuntimeException(String message, Object[] info,
079: Throwable t) {
080: super (message);
081: throwable = t;
082: this .info = info;
083: }
084:
085: public Object[] getInfo() {
086: return info;
087: }
088:
089: public Throwable getNested() {
090: return throwable;
091: }
092:
093: public String toString() {
094: StringBuffer buffer = new StringBuffer(getClass().getName());
095: String message = getMessage();
096: if (message != null) {
097: buffer.append(": ");
098: buffer.append(getLocalizedMessage(Locale.ENGLISH));
099: }
100:
101: if (throwable != null) {
102: buffer.append("\n->embedded exception: ");
103: buffer.append(throwable);
104: }
105: return buffer.toString();
106: }
107:
108: public void printStackTrace(java.io.PrintWriter s) {
109: if (throwable == null) {
110: super .printStackTrace(s);
111: } else {
112: s.println(toString());
113: throwable.printStackTrace(s);
114: }
115: }
116:
117: public String getLocalizedMessage() {
118: return getLocalizedMessage(Locale.ENGLISH);
119: }
120:
121: /**
122: * Produce a localized error message for the given locale
123: */
124: public String getLocalizedMessage(Locale locale) {
125: String message = LocalizedMessage.getMessage(getMessage(),
126: info, locale);
127:
128: if (message == null) {
129: if (info == null) {
130: return getMessage();
131: } else {
132: return getMessage() + " " + Arrays.asList(info);
133: }
134: }
135: return message;
136: }
137:
138: public int getLevel() {
139: return ContelligentException.ERROR;
140: }
141: }
|