KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mortbay > util > MultiException


1 // ========================================================================
2
// $Id: MultiException.java,v 1.10 2004/05/09 20:32:49 gregwilkins Exp $
3
// Copyright 1999-2004 Mort Bay Consulting Pty. Ltd.
4
// ------------------------------------------------------------------------
5
// Licensed under the Apache License, Version 2.0 (the "License");
6
// you may not use this file except in compliance with the License.
7
// You may obtain a copy of the License at
8
// http://www.apache.org/licenses/LICENSE-2.0
9
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS IS" BASIS,
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
// See the License for the specific language governing permissions and
13
// limitations under the License.
14
// ========================================================================
15

16 package org.mortbay.util;
17 import java.io.PrintStream JavaDoc;
18 import java.io.PrintWriter JavaDoc;
19 import java.util.List JavaDoc;
20
21
22 /* ------------------------------------------------------------ */
23 /** Wraps multiple exceptions.
24  *
25  * Allows multiple exceptions to be thrown as a single exception.
26  *
27  * @version $Id: MultiException.java,v 1.10 2004/05/09 20:32:49 gregwilkins Exp $
28  * @author Greg Wilkins (gregw)
29  */

30 public class MultiException extends Exception JavaDoc
31 {
32     private Object JavaDoc nested;
33
34     /* ------------------------------------------------------------ */
35     public MultiException()
36     {
37         super("Multiple exceptions");
38     }
39
40     /* ------------------------------------------------------------ */
41     public void add(Exception JavaDoc e)
42     {
43         if (e instanceof MultiException)
44         {
45             MultiException me = (MultiException)e;
46             for (int i=0;i<LazyList.size(me.nested);i++)
47                 nested=LazyList.add(nested,LazyList.get(me.nested,i));
48         }
49         else
50             nested=LazyList.add(nested,e);
51     }
52
53     /* ------------------------------------------------------------ */
54     public int size()
55     {
56         return LazyList.size(nested);
57     }
58     
59     /* ------------------------------------------------------------ */
60     public List JavaDoc getExceptions()
61     {
62         return LazyList.getList(nested);
63     }
64     
65     /* ------------------------------------------------------------ */
66     public Exception JavaDoc getException(int i)
67     {
68         return (Exception JavaDoc) LazyList.get(nested,i);
69     }
70
71     /* ------------------------------------------------------------ */
72     /** Throw a multiexception.
73      * If this multi exception is empty then no action is taken. If it
74      * contains a single exception that is thrown, otherwise the this
75      * multi exception is thrown.
76      * @exception Exception
77      */

78     public void ifExceptionThrow()
79         throws Exception JavaDoc
80     {
81         switch (LazyList.size(nested))
82         {
83           case 0:
84               break;
85           case 1:
86               throw (Exception JavaDoc)LazyList.get(nested,0);
87           default:
88               throw this;
89         }
90     }
91     
92     /* ------------------------------------------------------------ */
93     /** Throw a multiexception.
94      * If this multi exception is empty then no action is taken. If it
95      * contains a any exceptions then this
96      * multi exception is thrown.
97      */

98     public void ifExceptionThrowMulti()
99         throws MultiException
100     {
101         if (LazyList.size(nested)>0)
102             throw this;
103     }
104
105     /* ------------------------------------------------------------ */
106     public String JavaDoc toString()
107     {
108         if (LazyList.size(nested)>0)
109             return "org.mortbay.util.MultiException"+
110                 LazyList.getList(nested);
111         return "org.mortbay.util.MultiException[]";
112     }
113
114     /* ------------------------------------------------------------ */
115     public void printStackTrace()
116     {
117         super.printStackTrace();
118         for (int i=0;i<LazyList.size(nested);i++)
119             ((Throwable JavaDoc)LazyList.get(nested,i)).printStackTrace();
120     }
121    
122
123     /* ------------------------------------------------------------------------------- */
124     /**
125      * @see java.lang.Throwable#printStackTrace(java.io.PrintStream)
126      */

127     public void printStackTrace(PrintStream JavaDoc out)
128     {
129         super.printStackTrace(out);
130         for (int i=0;i<LazyList.size(nested);i++)
131             ((Throwable JavaDoc)LazyList.get(nested,i)).printStackTrace(out);
132     }
133
134     /* ------------------------------------------------------------------------------- */
135     /**
136      * @see java.lang.Throwable#printStackTrace(java.io.PrintWriter)
137      */

138     public void printStackTrace(PrintWriter JavaDoc out)
139     {
140         super.printStackTrace(out);
141         for (int i=0;i<LazyList.size(nested);i++)
142             ((Throwable JavaDoc)LazyList.get(nested,i)).printStackTrace(out);
143     }
144
145 }
146
Popular Tags