KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * File : $Source $
3  * Date : $Date $
4  * Version: $Revision $
5  *
6  * This library is part of OpenCms -
7  * the Open Source Content Mananagement System
8  *
9  * Copyright (C) 2002 - 2005 Alkacon Software (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, 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.CmsMessageContainer;
35
36 import java.util.ArrayList JavaDoc;
37 import java.util.Collections JavaDoc;
38 import java.util.Iterator JavaDoc;
39 import java.util.List JavaDoc;
40 import java.util.Locale JavaDoc;
41
42 /**
43  * A multi exception is a container for several exception messages that may be caused by an internal operation.<p>
44  *
45  * This is provided so that the user can see a full picuture of all the issues that have been caused in an operation,
46  * rather then only one (usually the first) issue.
47  *
48  * @author Alexander Kandzior
49  *
50  * @version $Revision: 1.2 $
51  *
52  * @since 2.0.0
53  */

54 public class CmsMultiException extends CmsException {
55
56     /** Serial version UID required for safe serialization. */
57     private static final long serialVersionUID = 1197300254684159700L;
58
59     /** The list of internal exceptions. */
60     protected List JavaDoc m_exceptions;
61
62     /** Indicates if the message has been set as individual message. */
63     protected boolean m_individualMessage;
64
65     /**
66      * Creates a new multi exception, a container for several exception messages.<p>
67      */

68     public CmsMultiException() {
69
70         this(Messages.get().container(Messages.ERR_MULTI_EXCEPTION_1, new Integer JavaDoc(0)));
71     }
72
73     /**
74      * Creates a new multi exception using the given base message.<p>
75      *
76      * @param message the basic message to use
77      */

78     public CmsMultiException(CmsMessageContainer message) {
79
80         super(message);
81         m_exceptions = new ArrayList JavaDoc();
82         setMessage(message);
83     }
84
85     /**
86      * Creates a new multi exception for the given list of <code>{@link CmsException}</code> instances.<p>
87      *
88      * @param exceptions a list of <code>{@link CmsException}</code> instances
89      */

90     public CmsMultiException(List JavaDoc exceptions) {
91
92         this();
93         setExceptions(exceptions);
94     }
95
96     /**
97      * Adds an Exception to the list of Exceptions kept in this multi Exception.<p>
98      *
99      * @param exception the Exception to add
100      */

101     public void addException(CmsException exception) {
102
103         m_exceptions.add(exception);
104         updateMessage();
105     }
106
107     /**
108      * Adds all Exceptions in the given List to the list of Exceptions kept in this multi Exception.<p>
109      *
110      * @param exceptions the Exceptions to add
111      */

112     public void addExceptions(List JavaDoc exceptions) {
113
114         m_exceptions.addAll(exceptions);
115         updateMessage();
116     }
117
118     /**
119      * @see org.opencms.main.CmsException#createException(org.opencms.i18n.CmsMessageContainer, java.lang.Throwable)
120      */

121     public CmsException createException(CmsMessageContainer container, Throwable JavaDoc cause) {
122
123         if (cause instanceof CmsMultiException) {
124             CmsMultiException multiException = (CmsMultiException)cause;
125             return new CmsMultiException(multiException.getExceptions());
126         }
127         // not a multi exception, use standard handling
128
return super.createException(container, cause);
129     }
130
131     /**
132      * Returns the (unmodifiable) List of exceptions that are tored in this multi exception.<p>
133      *
134      * @return the (unmodifiable) List of exceptions that are tored in this multi exception
135      */

136     public List JavaDoc getExceptions() {
137
138         return Collections.unmodifiableList(m_exceptions);
139     }
140
141     /**
142      * Returns a localized message composed of all contained exceptions.<p>
143      *
144      * @see java.lang.Throwable#getLocalizedMessage()
145      */

146     public String JavaDoc getLocalizedMessage() {
147
148         if (m_exceptions.isEmpty()) {
149             return null;
150         }
151         StringBuffer JavaDoc result = new StringBuffer JavaDoc(128);
152         Iterator JavaDoc it = m_exceptions.iterator();
153         while (it.hasNext()) {
154             CmsException ex = (CmsException)it.next();
155             result.append(ex.getLocalizedMessage());
156             if (it.hasNext()) {
157                 result.append('\n');
158             }
159         }
160         return result.toString();
161     }
162
163     /**
164      * Returns a localized message for the given locale composed of all contained exceptions.<p>
165      *
166      * @see org.opencms.main.I_CmsThrowable#getLocalizedMessage(java.util.Locale)
167      */

168     public String JavaDoc getLocalizedMessage(Locale JavaDoc locale) {
169
170         if (m_exceptions.isEmpty()) {
171             return null;
172         }
173         StringBuffer JavaDoc result = new StringBuffer JavaDoc(128);
174         Iterator JavaDoc it = m_exceptions.iterator();
175         while (it.hasNext()) {
176             CmsException ex = (CmsException)it.next();
177             result.append(ex.getLocalizedMessage(locale));
178             if (it.hasNext()) {
179                 result.append('\n');
180             }
181         }
182         return result.toString();
183     }
184     
185     /**
186      * Returns the individual message (if set) or an empty String.<p>
187      *
188      * @param locale the locale for the message to generate
189      * @return the individual message (if set) or an empty String
190      */

191     public String JavaDoc getMessage(Locale JavaDoc locale) {
192
193         if (hasIndividualMessage()) {
194             return m_message.key(locale);
195         }
196         return "";
197     }
198
199     /**
200      * Returns <code>true</code> if this multi exceptions contains at last one individual Exception.<p>
201      *
202      * @return <code>true</code> if this multi exceptions contains at last one individual Exception
203      */

204     public boolean hasExceptions() {
205
206         return !m_exceptions.isEmpty();
207     }
208
209     /**
210      * Returns <code>true</code> if this multi message has an individual base message set.<p>
211      *
212      * @return <code>true</code> if this multi message has an individual base message set
213      *
214      * @see #setMessage(CmsMessageContainer)
215      */

216     public boolean hasIndividualMessage() {
217
218         return m_individualMessage;
219     }
220
221     /**
222      * Sets an individual message for the multi exception base message.<p>
223      *
224      * If no individual message has been set, a default message using the key
225      * <code>{@link Messages#ERR_MULTI_EXCEPTION_1}</code> will be used.<p>
226      *
227      * If <code>null</code> is given as parameter, any individual message that
228      * has been set is reset to the default message.<p>
229      *
230      * @param message the message to set
231      */

232     public void setMessage(CmsMessageContainer message) {
233
234         if ((message != null) && (message.getKey() != Messages.ERR_MULTI_EXCEPTION_1)) {
235             m_individualMessage = true;
236             m_message = message;
237         } else {
238             // if message is null, reset and use default message again
239
m_individualMessage = false;
240             updateMessage();
241         }
242     }
243
244     /**
245      * Updates the internal list of stored exceptions.<p>
246      *
247      * @param exceptions the exceptions to use (will replace the current exception list)
248      */

249     protected void setExceptions(List JavaDoc exceptions) {
250
251         m_exceptions = new ArrayList JavaDoc(exceptions);
252         updateMessage();
253     }
254
255     /**
256      * Updates the intenal message for the Exception.<p>
257      */

258     protected void updateMessage() {
259
260         if (!hasIndividualMessage()) {
261             m_message = Messages.get().container(Messages.ERR_MULTI_EXCEPTION_1, new Integer JavaDoc(m_exceptions.size()));
262         }
263     }
264 }
Popular Tags