KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > j2biz > blogunity > exception > BlogunityException


1 /*
2  * $Id: BlogunityException.java,v 1.3 2005/01/17 11:21:24 keyboardsamurai Exp $
3  *
4  * Copyright (c) 2004 j2biz Group, http://www.j2biz.com
5  * Koeln / Duesseldorf , Germany
6  *
7  * @author Max Kalina
8  *
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23  *
24  */

25
26 package com.j2biz.blogunity.exception;
27
28 import java.io.PrintStream JavaDoc;
29 import java.io.PrintWriter JavaDoc;
30 import java.text.MessageFormat JavaDoc;
31
32 import org.apache.commons.lang.StringUtils;
33
34 import com.j2biz.blogunity.i18n.I18N;
35 import com.j2biz.blogunity.i18n.I18NMessageManager;
36 import com.j2biz.blogunity.i18n.I18NStatus;
37
38 /**
39  * @author michelson
40  * @version $$
41  * @since 0.1
42  *
43  *
44  */

45 public class BlogunityException extends Exception JavaDoc {
46
47     /**
48      * Comment for <code>serialVersionUID</code>
49      */

50     private static final long serialVersionUID = 3617008659184824880L;
51     private I18NStatus status;
52
53     public BlogunityException(I18NStatus status) {
54
55         super((StringUtils.isNotEmpty(status.getKey())) ? status.getKey() : I18N.ERRORS.UNKNOWN);
56         this.status = status;
57     }
58
59     public final I18NStatus getStatus() {
60         return status;
61     }
62
63     /**
64      * Prints a stack trace out for the exception, and any nested exception that
65      * it may have embedded in its Status object.
66      */

67     public void printStackTrace() {
68         printStackTrace(System.err);
69     }
70
71     /**
72      * Prints a stack trace out for the exception, and any nested exception that
73      * it may have embedded in its Status object.
74      *
75      * @param output
76      * the stream to write to
77      */

78     public void printStackTrace(PrintStream JavaDoc output) {
79         PrintWriter JavaDoc writer = new PrintWriter JavaDoc(output);
80         printStackTrace(writer);
81     }
82
83     /**
84      * Prints a stack trace out for the exception, and any nested exception that
85      * it may have embedded in its Status object.
86      *
87      * @param output
88      * the stream to write to
89      */

90     public void printStackTrace(PrintWriter JavaDoc output) {
91         synchronized (output) {
92             if (status.getCause() != null) {
93                 output.println(getClass().getName() + "[ErrorKey=" + status.getKey() + "]: ");
94                 status.getCause().printStackTrace(output);
95             } else
96                 super.printStackTrace(output);
97         }
98         output.flush();
99     }
100
101     /*
102      * (non-Javadoc)
103      *
104      * @see java.lang.Throwable#getLocalizedMessage()
105      */

106     public String JavaDoc getMessage() {
107
108         String JavaDoc msg = I18NMessageManager.getInstance().getError(status.getKey());
109         String JavaDoc[] params = status.getParameters();
110
111         if (params != null && params.length > 0) {
112             msg = MessageFormat.format(msg, params);
113         }
114
115         return msg;
116     }
117
118 }
Popular Tags