KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > opencms > main > CmsException


1 /*
2  * File : $Source: /usr/local/cvs/opencms/src/org/opencms/main/CmsException.java,v $
3  * Date : $Date: 2005/07/03 09:41:52 $
4  * Version: $Revision: 1.36 $
5  *
6  * This library is part of OpenCms -
7  * the Open Source Content Mananagement System
8  *
9  * Copyright (c) 2005 Alkacon Software GmbH (http://www.alkacon.com)
10  *
11  * This library is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU Lesser General Public
13  * License as published by the Free Software Foundation; either
14  * version 2.1 of the License, or (at your option) any later version.
15  *
16  * This library is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19  * Lesser General Public License for more details.
20  *
21  * For further information about Alkacon Software GmbH, please see the
22  * company website: http://www.alkacon.com
23  *
24  * For further information about OpenCms, please see the
25  * project website: http://www.opencms.org
26  *
27  * You should have received a copy of the GNU Lesser General Public
28  * License along with this library; if not, write to the Free Software
29  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
30  */

31
32 package org.opencms.main;
33
34 import org.opencms.i18n.CmsEncoder;
35 import org.opencms.i18n.CmsMessageContainer;
36 import org.opencms.util.CmsStringUtil;
37
38 import java.io.PrintWriter JavaDoc;
39 import java.io.StringWriter JavaDoc;
40 import java.util.Locale JavaDoc;
41
42 /**
43  * Master exception type for all exceptions caused in OpenCms.<p>
44  *
45  * @author Alexander Kandzior
46  * @author Michael Emmerich
47  * @author Michael Moossen
48  * @author Jan Baudisch
49  *
50  * @version $Revision: 1.36 $
51  *
52  * @since 6.0.0
53  */

54 public class CmsException extends Exception JavaDoc implements I_CmsThrowable {
55
56     /** Serial version UID required for safe serialization. */
57     private static final long serialVersionUID = -1372556209321406104L;
58
59     /** The container for the localized message. */
60     protected CmsMessageContainer m_message;
61
62     /**
63      * Creates a new localized Exception.<p>
64      *
65      * @param message the localized message container to use
66      */

67     public CmsException(CmsMessageContainer message) {
68
69         super(message.getKey());
70         m_message = message;
71     }
72
73     /**
74      * Creates a new localized Exception that also containes a root cause.<p>
75      *
76      * @param message the localized message container to use
77      * @param cause the Exception root cause
78      */

79     public CmsException(CmsMessageContainer message, Throwable JavaDoc cause) {
80
81         super(message.getKey(), cause);
82         m_message = message;
83     }
84
85     /**
86      * Returns the HTML formatted error stack of a Throwable.<p>
87      *
88      * The error stack is used by the common error screen
89      * that is displayed if an error occurs.<p>
90      *
91      * @param t the throwable to get the errorstack from
92      * @return the formatted value of the errorstack parameter
93      */

94     public static String JavaDoc getFormattedErrorstack(Throwable JavaDoc t) {
95
96         String JavaDoc stacktrace = CmsException.getStackTraceAsString(t);
97         if (CmsStringUtil.isEmpty(stacktrace)) {
98             return "";
99         } else {
100             stacktrace = CmsStringUtil.escapeJavaScript(stacktrace);
101             stacktrace = CmsEncoder.escapeXml(stacktrace);
102             StringBuffer JavaDoc result = new StringBuffer JavaDoc(256);
103             result.append("<html><body style='background-color: Window; overflow: scroll;'><pre>");
104             result.append(stacktrace);
105             result.append("</pre></body></html>");
106             return result.toString();
107         }
108     }
109
110     /**
111      * Returns the stack trace (including the message) of an exception as a String.<p>
112      *
113      * If the exception is a CmsException,
114      * also writes the root cause to the String.<p>
115      *
116      * @param e the exception to get the stack trace from
117      * @return the stack trace of an exception as a String
118      */

119     public static String JavaDoc getStackTraceAsString(Throwable JavaDoc e) {
120
121         StringWriter JavaDoc stringWriter = new StringWriter JavaDoc();
122         e.printStackTrace(new PrintWriter JavaDoc(stringWriter));
123         return stringWriter.toString();
124     }
125
126     /**
127      * Creates a copied instance of this localized exception.<p>
128      *
129      * @param container the message container
130      * @param cause the root cause
131      *
132      * @return a copied instance of this localized exception
133      */

134     public CmsException createException(CmsMessageContainer container, Throwable JavaDoc cause) {
135
136         return new CmsException(container, cause);
137     }
138
139     /**
140      * @see org.opencms.main.I_CmsThrowable#getLocalizedMessage()
141      */

142     public String JavaDoc getLocalizedMessage() {
143
144         if (m_message == null) {
145             return super.getLocalizedMessage();
146         }
147         return m_message.key();
148     }
149
150     /**
151      * @see org.opencms.main.I_CmsThrowable#getLocalizedMessage(Locale)
152      */

153     public String JavaDoc getLocalizedMessage(Locale JavaDoc locale) {
154
155         return m_message.key(locale);
156     }
157
158     /**
159      * @see java.lang.Throwable#getMessage()
160      */

161     public String JavaDoc getMessage() {
162
163         return getLocalizedMessage();
164     }
165
166     /**
167      * @see org.opencms.main.I_CmsThrowable#getMessageContainer()
168      */

169     public CmsMessageContainer getMessageContainer() {
170
171         return m_message;
172     }
173 }
Popular Tags