001: /*
002: * File : $Source: /usr/local/cvs/opencms/src/org/opencms/main/CmsException.java,v $
003: * Date : $Date: 2008-02-27 12:05:39 $
004: * Version: $Revision: 1.39 $
005: *
006: * This library is part of OpenCms -
007: * the Open Source Content Management System
008: *
009: * Copyright (c) 2002 - 2008 Alkacon Software GmbH (http://www.alkacon.com)
010: *
011: * This library is free software; you can redistribute it and/or
012: * modify it under the terms of the GNU Lesser General Public
013: * License as published by the Free Software Foundation; either
014: * version 2.1 of the License, or (at your option) any later version.
015: *
016: * This library is distributed in the hope that it will be useful,
017: * but WITHOUT ANY WARRANTY; without even the implied warranty of
018: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
019: * Lesser General Public License for more details.
020: *
021: * For further information about Alkacon Software GmbH, please see the
022: * company website: http://www.alkacon.com
023: *
024: * For further information about OpenCms, please see the
025: * project website: http://www.opencms.org
026: *
027: * You should have received a copy of the GNU Lesser General Public
028: * License along with this library; if not, write to the Free Software
029: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
030: */
031:
032: package org.opencms.main;
033:
034: import org.opencms.i18n.CmsEncoder;
035: import org.opencms.i18n.CmsMessageContainer;
036: import org.opencms.util.CmsStringUtil;
037:
038: import java.io.PrintWriter;
039: import java.io.StringWriter;
040: import java.util.Iterator;
041: import java.util.Locale;
042:
043: /**
044: * Master exception type for all exceptions caused in OpenCms.<p>
045: *
046: * @author Alexander Kandzior
047: * @author Michael Emmerich
048: * @author Michael Moossen
049: * @author Jan Baudisch
050: *
051: * @version $Revision: 1.39 $
052: *
053: * @since 6.0.0
054: */
055: public class CmsException extends Exception implements I_CmsThrowable {
056:
057: /** Serial version UID required for safe serialization. */
058: private static final long serialVersionUID = -1372556209321406104L;
059:
060: /** The container for the localized message. */
061: protected CmsMessageContainer m_message;
062:
063: /**
064: * Creates a new localized Exception.<p>
065: *
066: * @param message the localized message container to use
067: */
068: public CmsException(CmsMessageContainer message) {
069:
070: super (message.getKey());
071: m_message = message;
072: }
073:
074: /**
075: * Creates a new localized Exception that also containes a root cause.<p>
076: *
077: * @param message the localized message container to use
078: * @param cause the Exception root cause
079: */
080: public CmsException(CmsMessageContainer message, Throwable cause) {
081:
082: super (message.getKey(), cause);
083: m_message = message;
084: }
085:
086: /**
087: * Returns the HTML formatted error stack of a Throwable.<p>
088: *
089: * The error stack is used by the common error screen
090: * that is displayed if an error occurs.<p>
091: *
092: * @param t the throwable to get the errorstack from
093: * @return the formatted value of the errorstack parameter
094: */
095: public static String getFormattedErrorstack(Throwable t) {
096:
097: String stacktrace = CmsException.getStackTraceAsString(t);
098: if (CmsStringUtil.isEmpty(stacktrace)) {
099: return "";
100: } else {
101: stacktrace = CmsStringUtil.escapeJavaScript(stacktrace);
102: stacktrace = CmsEncoder.escapeXml(stacktrace);
103: StringBuffer result = new StringBuffer(256);
104: result
105: .append("<html><body style='background-color: Window; overflow: scroll;'><pre>");
106: result.append(stacktrace);
107: result.append("</pre></body></html>");
108: return result.toString();
109: }
110: }
111:
112: /**
113: * Returns the stack trace (including the message) of an exception as a String.<p>
114: *
115: * If the exception is a CmsException,
116: * also writes the root cause to the String.<p>
117: *
118: * @param e the exception to get the stack trace from
119: * @return the stack trace of an exception as a String
120: */
121: public static String getStackTraceAsString(Throwable e) {
122:
123: StringWriter stringWriter = new StringWriter();
124: PrintWriter printWriter = new PrintWriter(stringWriter);
125: e.printStackTrace(printWriter);
126: if (e instanceof CmsMultiException) {
127: CmsMultiException me = (CmsMultiException) e;
128: Iterator it = me.getExceptions().iterator();
129: while (it.hasNext()) {
130: Throwable t = (Throwable) it.next();
131: t.printStackTrace(printWriter);
132: }
133: }
134: return stringWriter.toString();
135: }
136:
137: /**
138: * Creates a copied instance of this localized exception.<p>
139: *
140: * @param container the message container
141: * @param cause the root cause
142: *
143: * @return a copied instance of this localized exception
144: */
145: public CmsException createException(CmsMessageContainer container,
146: Throwable cause) {
147:
148: return new CmsException(container, cause);
149: }
150:
151: /**
152: * @see org.opencms.main.I_CmsThrowable#getLocalizedMessage()
153: */
154: public String getLocalizedMessage() {
155:
156: if (m_message == null) {
157: return super .getLocalizedMessage();
158: }
159: return m_message.key();
160: }
161:
162: /**
163: * @see org.opencms.main.I_CmsThrowable#getLocalizedMessage(Locale)
164: */
165: public String getLocalizedMessage(Locale locale) {
166:
167: return m_message.key(locale);
168: }
169:
170: /**
171: * @see java.lang.Throwable#getMessage()
172: */
173: public String getMessage() {
174:
175: return getLocalizedMessage();
176: }
177:
178: /**
179: * @see org.opencms.main.I_CmsThrowable#getMessageContainer()
180: */
181: public CmsMessageContainer getMessageContainer() {
182:
183: return m_message;
184: }
185: }
|