001: /*
002: * File : $Source $
003: * Date : $Date $
004: * Version: $Revision $
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, 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.CmsMessageContainer;
035:
036: import java.util.ArrayList;
037: import java.util.Collections;
038: import java.util.Iterator;
039: import java.util.List;
040: import java.util.Locale;
041:
042: /**
043: * A multi exception is a container for several exception messages that may be caused by an internal operation.<p>
044: *
045: * This is provided so that the user can see a full picuture of all the issues that have been caused in an operation,
046: * rather then only one (usually the first) issue.
047: *
048: * @author Alexander Kandzior
049: *
050: * @version $Revision: 1.5 $
051: *
052: * @since 2.0.0
053: */
054: public class CmsMultiException extends CmsException {
055:
056: /** Serial version UID required for safe serialization. */
057: private static final long serialVersionUID = 1197300254684159700L;
058:
059: /** The list of internal exceptions. */
060: protected List m_exceptions;
061:
062: /** Indicates if the message has been set as individual message. */
063: protected boolean m_individualMessage;
064:
065: /**
066: * Creates a new multi exception, a container for several exception messages.<p>
067: */
068: public CmsMultiException() {
069:
070: this (Messages.get().container(Messages.ERR_MULTI_EXCEPTION_1,
071: new Integer(0)));
072: }
073:
074: /**
075: * Creates a new multi exception using the given base message.<p>
076: *
077: * @param message the basic message to use
078: */
079: public CmsMultiException(CmsMessageContainer message) {
080:
081: super (message);
082: m_exceptions = new ArrayList();
083: setMessage(message);
084: }
085:
086: /**
087: * Creates a new multi exception for the given list of <code>{@link CmsException}</code> instances.<p>
088: *
089: * @param exceptions a list of <code>{@link CmsException}</code> instances
090: */
091: public CmsMultiException(List exceptions) {
092:
093: this ();
094: setExceptions(exceptions);
095: }
096:
097: /**
098: * Adds an Exception to the list of Exceptions kept in this multi Exception.<p>
099: *
100: * @param exception the Exception to add
101: */
102: public void addException(CmsException exception) {
103:
104: m_exceptions.add(exception);
105: updateMessage();
106: }
107:
108: /**
109: * Adds all Exceptions in the given List to the list of Exceptions kept in this multi Exception.<p>
110: *
111: * @param exceptions the Exceptions to add
112: */
113: public void addExceptions(List exceptions) {
114:
115: m_exceptions.addAll(exceptions);
116: updateMessage();
117: }
118:
119: /**
120: * @see org.opencms.main.CmsException#createException(org.opencms.i18n.CmsMessageContainer, java.lang.Throwable)
121: */
122: public CmsException createException(CmsMessageContainer container,
123: Throwable cause) {
124:
125: if (cause instanceof CmsMultiException) {
126: CmsMultiException multiException = (CmsMultiException) cause;
127: return new CmsMultiException(multiException.getExceptions());
128: }
129: // not a multi exception, use standard handling
130: return super .createException(container, cause);
131: }
132:
133: /**
134: * Returns the (unmodifiable) List of exceptions that are tored in this multi exception.<p>
135: *
136: * @return the (unmodifiable) List of exceptions that are tored in this multi exception
137: */
138: public List getExceptions() {
139:
140: return Collections.unmodifiableList(m_exceptions);
141: }
142:
143: /**
144: * Returns a localized message composed of all contained exceptions.<p>
145: *
146: * @see java.lang.Throwable#getLocalizedMessage()
147: */
148: public String getLocalizedMessage() {
149:
150: if (m_exceptions.isEmpty()) {
151: return null;
152: }
153: StringBuffer result = new StringBuffer(128);
154: Iterator it = m_exceptions.iterator();
155: while (it.hasNext()) {
156: CmsException ex = (CmsException) it.next();
157: result.append(ex.getLocalizedMessage());
158: if (it.hasNext()) {
159: result.append('\n');
160: }
161: }
162: return result.toString();
163: }
164:
165: /**
166: * Returns a localized message for the given locale composed of all contained exceptions.<p>
167: *
168: * @see org.opencms.main.I_CmsThrowable#getLocalizedMessage(java.util.Locale)
169: */
170: public String getLocalizedMessage(Locale locale) {
171:
172: if (m_exceptions.isEmpty()) {
173: return null;
174: }
175: StringBuffer result = new StringBuffer(128);
176: Iterator it = m_exceptions.iterator();
177: while (it.hasNext()) {
178: CmsException ex = (CmsException) it.next();
179: result.append(ex.getLocalizedMessage(locale));
180: if (it.hasNext()) {
181: result.append('\n');
182: }
183: }
184: return result.toString();
185: }
186:
187: /**
188: * Returns the individual message (if set) or an empty String.<p>
189: *
190: * @param locale the locale for the message to generate
191: * @return the individual message (if set) or an empty String
192: */
193: public String getMessage(Locale locale) {
194:
195: if (hasIndividualMessage()) {
196: return m_message.key(locale);
197: }
198: return "";
199: }
200:
201: /**
202: * Returns <code>true</code> if this multi exceptions contains at last one individual Exception.<p>
203: *
204: * @return <code>true</code> if this multi exceptions contains at last one individual Exception
205: */
206: public boolean hasExceptions() {
207:
208: return !m_exceptions.isEmpty();
209: }
210:
211: /**
212: * Returns <code>true</code> if this multi message has an individual base message set.<p>
213: *
214: * @return <code>true</code> if this multi message has an individual base message set
215: *
216: * @see #setMessage(CmsMessageContainer)
217: */
218: public boolean hasIndividualMessage() {
219:
220: return m_individualMessage;
221: }
222:
223: /**
224: * Sets an individual message for the multi exception base message.<p>
225: *
226: * If no individual message has been set, a default message using the key
227: * <code>{@link org.opencms.main.Messages#ERR_MULTI_EXCEPTION_1}</code> will be used.<p>
228: *
229: * If <code>null</code> is given as parameter, any individual message that
230: * has been set is reset to the default message.<p>
231: *
232: * @param message the message to set
233: */
234: public void setMessage(CmsMessageContainer message) {
235:
236: if ((message != null)
237: && (message.getKey() != Messages.ERR_MULTI_EXCEPTION_1)) {
238: m_individualMessage = true;
239: m_message = message;
240: } else {
241: // if message is null, reset and use default message again
242: m_individualMessage = false;
243: updateMessage();
244: }
245: }
246:
247: /**
248: * Updates the internal list of stored exceptions.<p>
249: *
250: * @param exceptions the exceptions to use (will replace the current exception list)
251: */
252: protected void setExceptions(List exceptions) {
253:
254: m_exceptions = new ArrayList(exceptions);
255: updateMessage();
256: }
257:
258: /**
259: * Updates the intenal message for the Exception.<p>
260: */
261: protected void updateMessage() {
262:
263: if (!hasIndividualMessage()) {
264: m_message = Messages.get().container(
265: Messages.ERR_MULTI_EXCEPTION_1,
266: new Integer(m_exceptions.size()));
267: }
268: }
269: }
|