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 exception class.
027: */
028: public class ContelligentException extends Exception {
029:
030: public final static int INFO = 1;
031:
032: public final static int WARNING = 2;
033:
034: public final static int ERROR = 3;
035:
036: private Throwable throwable;
037:
038: private Object[] info;
039:
040: public ContelligentException() {
041: this (ContelligentExceptionID.generic_generic);
042: }
043:
044: /**
045: * Creates a new <code>ContelligentException</code> instance.
046: *
047: * @param message
048: * exception message for debug purposes
049: */
050: public ContelligentException(String message) {
051: this (message, null, null);
052: }
053:
054: public ContelligentException(Throwable t) {
055: this (ContelligentExceptionID.generic_generic, t);
056: }
057:
058: /**
059: * Creates a new <code>ContelligentException</code> instance.
060: *
061: * @param message
062: * exception message for debug purposes
063: * @param info
064: * array of objects to describe the exception in more detail
065: */
066: public ContelligentException(String message, Object[] info) {
067: this (message, info, null);
068: }
069:
070: /**
071: * Creates a new <code>ContelligentException</code> instance.
072: *
073: * @param message
074: * exception message for debug purposes
075: * @param t
076: * <code>Throwable</code> which caused this exception
077: */
078: public ContelligentException(String message, Throwable t) {
079: this (message, null, t);
080: }
081:
082: /**
083: * Creates a new <code>ContelligentException</code> instance.
084: *
085: * @param message
086: * exception message for debug purposes
087: * @param info
088: * array of objects to describe the exception in more detail
089: * @param t
090: * <code>Throwable</code> which caused this exception
091: */
092: public ContelligentException(String message, Object[] info,
093: Throwable t) {
094: super (message);
095: throwable = t;
096: this .info = info;
097: }
098:
099: public Object[] getInfo() {
100: return info;
101: }
102:
103: public Throwable getNested() {
104: return throwable;
105: }
106:
107: public String toString() {
108: StringBuffer buffer = new StringBuffer(getClass().getName());
109: String message = getMessage();
110: if (message != null) {
111: buffer.append(": ");
112: buffer.append(getLocalizedMessage(Locale.ENGLISH));
113: }
114:
115: if (throwable != null) {
116: buffer.append("\n->embedded exception: ");
117: buffer.append(throwable);
118: }
119: return buffer.toString();
120: }
121:
122: public void printStackTrace(java.io.PrintWriter s) {
123: if (throwable == null) {
124: super .printStackTrace(s);
125: } else {
126: s.println(toString());
127: throwable.printStackTrace(s);
128: }
129: }
130:
131: public String getLocalizedMessage() {
132: return getLocalizedMessage(Locale.ENGLISH);
133: }
134:
135: /**
136: * Produce a localized error message for the given locale
137: */
138: public String getLocalizedMessage(Locale locale) {
139: String message = LocalizedMessage.getMessage(getMessage(),
140: info, locale);
141:
142: if (message == null) {
143: if (info == null) {
144: return getMessage();
145: } else {
146: return getMessage() + " " + Arrays.asList(info);
147: }
148: }
149: return message;
150: }
151:
152: public int getLevel() {
153: return ERROR;
154: }
155: }
|