1 // ========================================================================
2 // Copyright (c) 1999-2009 Mort Bay Consulting Pty. Ltd.
3 // ------------------------------------------------------------------------
4 // All rights reserved. This program and the accompanying materials
5 // are made available under the terms of the Eclipse Public License v1.0
6 // and Apache License v2.0 which accompanies this distribution.
7 // The Eclipse Public License is available at
8 // http://www.eclipse.org/legal/epl-v10.html
9 // The Apache License v2.0 is available at
10 // http://www.opensource.org/licenses/apache2.0.php
11 // You may elect to redistribute this code under either of these licenses.
12 // ========================================================================
13
14 package org.eclipse.jetty.util;
15 import java.io.PrintStream;
16 import java.io.PrintWriter;
17 import java.util.List;
18
19
20 /* ------------------------------------------------------------ */
21 /** Wraps multiple exceptions.
22 *
23 * Allows multiple exceptions to be thrown as a single exception.
24 *
25 *
26 */
27 public class MultiException extends Exception
28 {
29 private Object nested;
30
31 /* ------------------------------------------------------------ */
32 public MultiException()
33 {
34 super("Multiple exceptions");
35 }
36
37 /* ------------------------------------------------------------ */
38 public void add(Throwable e)
39 {
40 if (e instanceof MultiException)
41 {
42 MultiException me = (MultiException)e;
43 for (int i=0;i<LazyList.size(me.nested);i++)
44 nested=LazyList.add(nested,LazyList.get(me.nested,i));
45 }
46 else
47 nested=LazyList.add(nested,e);
48 }
49
50 /* ------------------------------------------------------------ */
51 public int size()
52 {
53 return LazyList.size(nested);
54 }
55
56 /* ------------------------------------------------------------ */
57 public List getThrowables()
58 {
59 return LazyList.getList(nested);
60 }
61
62 /* ------------------------------------------------------------ */
63 public Throwable getThrowable(int i)
64 {
65 return (Throwable) LazyList.get(nested,i);
66 }
67
68 /* ------------------------------------------------------------ */
69 /** Throw a multiexception.
70 * If this multi exception is empty then no action is taken. If it
71 * contains a single exception that is thrown, otherwise the this
72 * multi exception is thrown.
73 * @exception Exception
74 */
75 public void ifExceptionThrow()
76 throws Exception
77 {
78 switch (LazyList.size(nested))
79 {
80 case 0:
81 break;
82 case 1:
83 Throwable th=(Throwable)LazyList.get(nested,0);
84 if (th instanceof Error)
85 throw (Error)th;
86 if (th instanceof Exception)
87 throw (Exception)th;
88 default:
89 throw this;
90 }
91 }
92
93 /* ------------------------------------------------------------ */
94 /** Throw a Runtime exception.
95 * If this multi exception is empty then no action is taken. If it
96 * contains a single error or runtime exception that is thrown, otherwise the this
97 * multi exception is thrown, wrapped in a runtime exception.
98 * @exception Error If this exception contains exactly 1 {@link Error}
99 * @exception RuntimeException If this exception contains 1 {@link Throwable} but it is not an error,
100 * or it contains more than 1 {@link Throwable} of any type.
101 */
102 public void ifExceptionThrowRuntime()
103 throws Error
104 {
105 switch (LazyList.size(nested))
106 {
107 case 0:
108 break;
109 case 1:
110 Throwable th=(Throwable)LazyList.get(nested,0);
111 if (th instanceof Error)
112 throw (Error)th;
113 else if (th instanceof RuntimeException)
114 throw (RuntimeException)th;
115 else
116 throw new RuntimeException(th);
117 default:
118 throw new RuntimeException(this);
119 }
120 }
121
122 /* ------------------------------------------------------------ */
123 /** Throw a multiexception.
124 * If this multi exception is empty then no action is taken. If it
125 * contains a any exceptions then this
126 * multi exception is thrown.
127 */
128 public void ifExceptionThrowMulti()
129 throws MultiException
130 {
131 if (LazyList.size(nested)>0)
132 throw this;
133 }
134
135 /* ------------------------------------------------------------ */
136 public String toString()
137 {
138 if (LazyList.size(nested)>0)
139 return MultiException.class.getSimpleName()+
140 LazyList.getList(nested);
141 return MultiException.class.getSimpleName()+"[]";
142 }
143
144 /* ------------------------------------------------------------ */
145 public void printStackTrace()
146 {
147 super.printStackTrace();
148 for (int i=0;i<LazyList.size(nested);i++)
149 ((Throwable)LazyList.get(nested,i)).printStackTrace();
150 }
151
152
153 /* ------------------------------------------------------------------------------- */
154 /**
155 * @see java.lang.Throwable#printStackTrace(java.io.PrintStream)
156 */
157 public void printStackTrace(PrintStream out)
158 {
159 super.printStackTrace(out);
160 for (int i=0;i<LazyList.size(nested);i++)
161 ((Throwable)LazyList.get(nested,i)).printStackTrace(out);
162 }
163
164 /* ------------------------------------------------------------------------------- */
165 /**
166 * @see java.lang.Throwable#printStackTrace(java.io.PrintWriter)
167 */
168 public void printStackTrace(PrintWriter out)
169 {
170 super.printStackTrace(out);
171 for (int i=0;i<LazyList.size(nested);i++)
172 ((Throwable)LazyList.get(nested,i)).printStackTrace(out);
173 }
174
175 }