KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > opencms > jsp > util > CmsErrorBean


1 /*
2  * File : $Source: /usr/local/cvs/opencms/src/org/opencms/jsp/util/CmsErrorBean.java,v $
3  * Date : $Date: 2006/03/27 14:52:59 $
4  * Version: $Revision: 1.8 $
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.jsp.util;
33
34 import org.opencms.file.CmsObject;
35 import org.opencms.i18n.CmsMessages;
36 import org.opencms.main.CmsException;
37 import org.opencms.main.CmsLog;
38 import org.opencms.main.CmsMultiException;
39 import org.opencms.main.I_CmsThrowable;
40 import org.opencms.main.OpenCms;
41 import org.opencms.util.CmsMacroResolver;
42 import org.opencms.util.CmsStringUtil;
43
44 import java.util.Locale JavaDoc;
45 import java.util.Properties JavaDoc;
46
47 /**
48  * Class to display the error dialog.<p>
49  *
50  * @author Jan Baudisch
51  *
52  * @version $Revision: 1.8 $
53  *
54  * @since 6.0.0
55  */

56 public class CmsErrorBean {
57
58     /** Name of the property file containing HTML fragments for setup wizard and error dialog.<p> */
59     public static final String JavaDoc ERRORPAGE = "org/opencms/jsp/util/errorpage.properties";
60
61     /** The html for the buttons. */
62     private String JavaDoc m_buttons;
63
64     /** The current CmsObject.<p> */
65     private CmsObject m_cms;
66
67     /** The optional error message. */
68     private String JavaDoc m_errorMessage;
69
70     /** The html code for the hidden parameters. */
71     private String JavaDoc m_hiddenParams;
72
73     /** The locale for the errorpage. */
74     private Locale JavaDoc m_locale;
75
76     /** Messages container. */
77     private CmsMessages m_messages;
78
79     /** The html code for the buttons. */
80     private String JavaDoc m_paramAction;
81
82     /** The exception that was caught.<p> */
83     private Throwable JavaDoc m_throwable;
84
85     /** The title for the error page. */
86     private String JavaDoc m_title;
87
88     /**
89      * Constructs a new error bean.<p>
90      *
91      * @param cms the current CmsObject
92      * @param throwable the exception that was caught
93      */

94     public CmsErrorBean(CmsObject cms, Throwable JavaDoc throwable) {
95
96         m_cms = cms;
97         m_locale = cms.getRequestContext().getLocale();
98         m_throwable = throwable;
99         m_messages = Messages.get().getBundle(m_locale);
100     }
101
102     /**
103      * Returns the html code for the buttons, when the errorpage is included from outside the workplace.<p>
104      *
105      * @return the default html for the buttons
106      */

107     public String JavaDoc getDefaultButtonsHtml() {
108
109         StringBuffer JavaDoc result = new StringBuffer JavaDoc();
110         String JavaDoc closeLabel = m_messages.key(Messages.GUI_CLOSE_0, new Object JavaDoc[] {});
111         String JavaDoc detailsLabel = m_messages.key(Messages.GUI_DETAILS_0, new Object JavaDoc[] {});
112         result.append("<div class=\"dialogbuttons\" unselectable=\"on\">");
113         result.append("<input name=\"close\" type=\"button\" value=\"").append(closeLabel).append(
114             "\" onclick=\"closeDialog();\" class=\"dialogbutton\">");
115         result.append("<input name=\"details\" type=\"button\" value=\"").append(detailsLabel).append(
116             "\" class=\"dialogbutton\" onclick=\"toggleElement('errordetails');\">");
117         result.append("</div>");
118         return result.toString();
119     }
120
121     /**
122      * Returns the error message to be displayed.<p>
123      *
124      * @return the error message to be displayed
125      */

126     public String JavaDoc getErrorMessage() {
127
128         StringBuffer JavaDoc result = new StringBuffer JavaDoc(512);
129
130         String JavaDoc reason = m_messages.key(Messages.GUI_REASON_0, new Object JavaDoc[] {});
131
132         if (CmsStringUtil.isNotEmpty(m_errorMessage)) {
133             result.append(m_errorMessage);
134             result.append("\n").append(reason).append(": ");
135         }
136
137         // if a localized message is already set as a parameter, append it.
138
result.append(getMessage(m_throwable));
139         // recursively append all error reasons to the message
140
for (Throwable JavaDoc cause = m_throwable.getCause(); cause != null; cause = cause.getCause()) {
141             result.append("\n").append(reason).append(": ");
142             result.append(getMessage(cause));
143         }
144         return result.toString();
145     }
146
147     /**
148      * Returns the localized Message, if the argument is a CmsException, or
149      * the message otherwise.<p>
150      *
151      * @param t the Throwable to get the message from
152      *
153      * @return returns the localized Message, if the argument is a CmsException, or
154      * the message otherwise
155      */

156     public String JavaDoc getMessage(Throwable JavaDoc t) {
157
158         if (t instanceof I_CmsThrowable && ((I_CmsThrowable)t).getMessageContainer() != null) {
159             StringBuffer JavaDoc result = new StringBuffer JavaDoc(256);
160             if (m_throwable instanceof CmsMultiException) {
161                 CmsMultiException exc = (CmsMultiException)m_throwable;
162                 String JavaDoc message = exc.getMessage(m_locale);
163                 if (CmsStringUtil.isNotEmpty(message)) {
164                     result.append(message);
165                     result.append('\n');
166                 }
167
168             }
169
170             I_CmsThrowable cmsThrowable = (I_CmsThrowable)t;
171             result.append(cmsThrowable.getLocalizedMessage(m_locale));
172             return result.toString();
173         } else {
174             String JavaDoc message = t.getMessage();
175             if (CmsStringUtil.isEmptyOrWhitespaceOnly(message)) {
176                 // no error message found (e.g. for NPE), provide default message text
177
message = m_messages.key(Messages.GUI_ERROR_UNKNOWN_0);
178             }
179             return message;
180         }
181     }
182
183     /**
184      * Sets the buttons.<p>
185      *
186      * @param buttons the buttons to set
187      */

188     public void setButtons(String JavaDoc buttons) {
189
190         m_buttons = buttons;
191     }
192
193     /**
194      * Sets the error message which can be displayed if no exception is there.<p>
195      *
196      * @param errorMessage the error message to set
197      */

198     public void setErrorMessage(String JavaDoc errorMessage) {
199
200         m_errorMessage = errorMessage;
201     }
202
203     /**
204      * Sets the hiddenParams.<p>
205      *
206      * @param hiddenParams the hiddenParams to set
207      */

208     public void setHiddenParams(String JavaDoc hiddenParams) {
209
210         m_hiddenParams = hiddenParams;
211     }
212
213     /**
214      * Sets the action parameter.<p>
215      *
216      * @param paramAction the action parameter to set
217      */

218     public void setParamAction(String JavaDoc paramAction) {
219
220         m_paramAction = paramAction;
221     }
222
223     /**
224      * Sets the title of the error page.<p>
225      *
226      * @param title of the error page
227      */

228     public void setTitle(String JavaDoc title) {
229
230         m_title = title;
231     }
232
233     /**
234      * Returns the html code for a errorpage.<p>
235      *
236      * @return the html for the errorpage
237      */

238     public String JavaDoc toHtml() {
239
240         CmsMacroResolver resolver = new CmsMacroResolver();
241         if (CmsStringUtil.isEmpty(m_title)) {
242             m_title = m_messages.key(Messages.GUI_ERROR_0, new Object JavaDoc[] {});
243         }
244         resolver.addMacro("title", m_title);
245         resolver.addMacro("label_error", m_messages.key(Messages.GUI_ERROR_0, new Object JavaDoc[] {}));
246         resolver.addMacro("errorstack", CmsException.getFormattedErrorstack(m_throwable));
247         resolver.addMacro("message", CmsStringUtil.escapeHtml(getErrorMessage()));
248         resolver.addMacro("styleuri", OpenCms.getLinkManager().substituteLink(
249             m_cms,
250             "/system/workplace/commons/style/workplace.css"));
251         if (CmsStringUtil.isEmpty(m_buttons)) {
252             resolver.addMacro("buttons", getDefaultButtonsHtml());
253         } else {
254             resolver.addMacro("buttons", m_buttons);
255             resolver.addMacro("paramaction", m_paramAction);
256         }
257
258         if (CmsStringUtil.isNotEmpty(m_hiddenParams)) {
259             resolver.addMacro("hiddenparams", m_hiddenParams);
260         }
261         resolver.addMacro("erroricon", OpenCms.getLinkManager().substituteLink(
262             m_cms,
263             "/system/workplace/resources/commons/error.png"));
264         Properties JavaDoc errorpage = new Properties JavaDoc();
265         try {
266             errorpage.load(CmsErrorBean.class.getClassLoader().getResourceAsStream(ERRORPAGE));
267         } catch (Throwable JavaDoc th) {
268             CmsLog.INIT.error(org.opencms.main.Messages.get().getBundle().key(
269                 org.opencms.main.Messages.INIT_ERR_LOAD_HTML_PROPERTY_FILE_1,
270                 ERRORPAGE), th);
271         }
272         return resolver.resolveMacros(errorpage.getProperty("ERRORPAGE"));
273     }
274 }
Popular Tags