KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > opensubsystems > core > error > OSSMultiException


1 /*
2  * Copyright (c) 2003 - 2007 OpenSubsystems s.r.o. Slovak Republic. All rights reserved.
3  *
4  * Project: OpenSubsystems
5  *
6  * $Id: OSSMultiException.java,v 1.7 2007/01/07 06:14:50 bastafidli Exp $
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; version 2 of the License.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20  */

21
22 package org.opensubsystems.core.error;
23
24 import java.io.PrintStream JavaDoc;
25 import java.io.PrintWriter JavaDoc;
26 import java.util.ArrayList JavaDoc;
27 import java.util.Iterator JavaDoc;
28 import java.util.List JavaDoc;
29
30 /**
31  * This class allows to throw multiple exceptions at once for example in
32  * scenario when we need to propagate one exception even though anothe
33  * exception has occured. This class is inspired by
34  * org.mortbay.util.MultiException from Jetty distribution.
35  *
36  * @version $Id: OSSMultiException.java,v 1.7 2007/01/07 06:14:50 bastafidli Exp $
37  * @author Miro Halas
38  * @code.reviewer Miro Halas
39  * @code.reviewed 1.5 2006/02/17 06:54:08 bastafidli
40  */

41 public class OSSMultiException extends OSSException
42 {
43    // Attributes ///////////////////////////////////////////////////////////////
44

45    /**
46     * Generated serial version id for this class.
47     */

48    private static final long serialVersionUID = -1993344389739792900L;
49
50    /**
51     * Excpetions which should be thrown together
52     */

53    protected List JavaDoc m_lstExceptions;
54
55    // Constructor //////////////////////////////////////////////////////////////
56

57    /**
58     * Create new exception
59     */

60    public OSSMultiException()
61    {
62       super();
63    }
64
65    /**
66     * Create new exception
67     *
68     * @param message - message to display
69     */

70    public OSSMultiException(String JavaDoc message)
71    {
72       super(message);
73    }
74
75    /**
76     * Create new exception
77     *
78     * @param message - message to display
79     * @param cause - cause for error
80     */

81    public OSSMultiException(String JavaDoc message, Throwable JavaDoc cause)
82    {
83       super(message, cause);
84       add(cause);
85    }
86
87    /**
88     * Create new exception
89     *
90     * @param cause - cause for error
91     */

92    public OSSMultiException(Throwable JavaDoc cause)
93    {
94       super(cause);
95       add(cause);
96    }
97    
98
99    /**
100     * Constructor.
101     *
102     * @param first - first exception which has occured
103     * @param second - second exception which has occured
104     */

105    public OSSMultiException(
106       Throwable JavaDoc first,
107       Throwable JavaDoc second
108    )
109    {
110       super("Multiple exceptions");
111       
112       m_lstExceptions = new ArrayList JavaDoc(2);
113       add(first);
114       add(second);
115    }
116
117    // Public methods ///////////////////////////////////////////////////////////
118

119    /**
120     * Add new exception which should be thrown together with others.
121     *
122     * @param thr - new exception
123     */

124    public void add(
125      Throwable JavaDoc thr
126    )
127    {
128       if (m_lstExceptions == null)
129       {
130          m_lstExceptions = new ArrayList JavaDoc();
131       }
132
133       if (thr instanceof OSSMultiException)
134       {
135          // Instead of adding the top level exception add the children directly
136
OSSMultiException multiExc = (OSSMultiException)thr;
137          m_lstExceptions.addAll(multiExc.m_lstExceptions);
138       }
139       else
140       {
141          m_lstExceptions.add(thr);
142       }
143    }
144
145    /**
146     * {@inheritDoc}
147     */

148    public String JavaDoc toString(
149    )
150    {
151       return this.getClass().getName() + ": " + m_lstExceptions.toString();
152    }
153
154    /**
155     * {@inheritDoc}
156     */

157    public void printStackTrace(
158    )
159    {
160       super.printStackTrace();
161       
162       for (Iterator JavaDoc exc = m_lstExceptions.iterator(); exc.hasNext();)
163       {
164          ((Throwable JavaDoc)exc.next()).printStackTrace();
165       }
166    }
167    
168    /**
169     * {@inheritDoc}
170     */

171    public void printStackTrace(
172       PrintStream JavaDoc stream
173    )
174    {
175       super.printStackTrace(stream);
176       
177       for (Iterator JavaDoc exc = m_lstExceptions.iterator(); exc.hasNext();)
178       {
179          ((Throwable JavaDoc)exc.next()).printStackTrace(stream);
180       }
181    }
182    
183    /**
184     * {@inheritDoc}
185     */

186    public void printStackTrace(
187       PrintWriter JavaDoc writer
188    )
189    {
190       super.printStackTrace(writer);
191       
192       for (Iterator JavaDoc exc = m_lstExceptions.iterator(); exc.hasNext();)
193       {
194          ((Throwable JavaDoc)exc.next()).printStackTrace(writer);
195       }
196    }
197
198    /**
199     * {@inheritDoc}
200     */

201    public synchronized Throwable JavaDoc initCause(
202       Throwable JavaDoc thr
203    )
204    {
205       if (thr != null)
206       {
207          add(thr);
208       }
209       return this;
210    }
211 }
212
Popular Tags