View Javadoc

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  @SuppressWarnings("serial")
28  public class MultiException extends Exception
29  {
30      private Object nested;
31  
32      /* ------------------------------------------------------------ */
33      public MultiException()
34      {
35          super("Multiple exceptions");
36      }
37  
38      /* ------------------------------------------------------------ */
39      public void add(Throwable e)
40      {
41          if (e instanceof MultiException)
42          {
43              MultiException me = (MultiException)e;
44              for (int i=0;i<LazyList.size(me.nested);i++)
45                  nested=LazyList.add(nested,LazyList.get(me.nested,i));
46          }
47          else
48              nested=LazyList.add(nested,e);
49      }
50  
51      /* ------------------------------------------------------------ */
52      public int size()
53      {
54          return LazyList.size(nested);
55      }
56      
57      /* ------------------------------------------------------------ */
58      public List<Throwable> getThrowables()
59      {
60          return LazyList.getList(nested);
61      }
62      
63      /* ------------------------------------------------------------ */
64      public Throwable getThrowable(int i)
65      {
66          return (Throwable) LazyList.get(nested,i);
67      }
68  
69      /* ------------------------------------------------------------ */
70      /** Throw a multiexception.
71       * If this multi exception is empty then no action is taken. If it
72       * contains a single exception that is thrown, otherwise the this
73       * multi exception is thrown. 
74       * @exception Exception 
75       */
76      public void ifExceptionThrow()
77          throws Exception
78      {
79          switch (LazyList.size(nested))
80          {
81            case 0:
82                break;
83            case 1:
84                Throwable th=(Throwable)LazyList.get(nested,0);
85                if (th instanceof Error)
86                    throw (Error)th;
87                if (th instanceof Exception)
88                    throw (Exception)th;
89            default:
90                throw this;
91          }
92      }
93      
94      /* ------------------------------------------------------------ */
95      /** Throw a Runtime exception.
96       * If this multi exception is empty then no action is taken. If it
97       * contains a single error or runtime exception that is thrown, otherwise the this
98       * multi exception is thrown, wrapped in a runtime exception. 
99       * @exception Error If this exception contains exactly 1 {@link Error} 
100      * @exception RuntimeException If this exception contains 1 {@link Throwable} but it is not an error,
101      *                             or it contains more than 1 {@link Throwable} of any type.
102      */
103     public void ifExceptionThrowRuntime()
104         throws Error
105     {
106         switch (LazyList.size(nested))
107         {
108           case 0:
109               break;
110           case 1:
111               Throwable th=(Throwable)LazyList.get(nested,0);
112               if (th instanceof Error)
113                   throw (Error)th;
114               else if (th instanceof RuntimeException)
115                   throw (RuntimeException)th;
116               else
117                   throw new RuntimeException(th);
118           default:
119               throw new RuntimeException(this);
120         }
121     }
122     
123     /* ------------------------------------------------------------ */
124     /** Throw a multiexception.
125      * If this multi exception is empty then no action is taken. If it
126      * contains a any exceptions then this
127      * multi exception is thrown. 
128      */
129     public void ifExceptionThrowMulti()
130         throws MultiException
131     {
132         if (LazyList.size(nested)>0)
133             throw this;
134     }
135 
136     /* ------------------------------------------------------------ */
137     @Override
138     public String toString()
139     {
140         if (LazyList.size(nested)>0)
141             return MultiException.class.getSimpleName()+
142                 LazyList.getList(nested);
143         return MultiException.class.getSimpleName()+"[]";
144     }
145 
146     /* ------------------------------------------------------------ */
147     @Override
148     public void printStackTrace()
149     {
150         super.printStackTrace();
151         for (int i=0;i<LazyList.size(nested);i++)
152             ((Throwable)LazyList.get(nested,i)).printStackTrace();
153     }
154    
155 
156     /* ------------------------------------------------------------------------------- */
157     /**
158      * @see java.lang.Throwable#printStackTrace(java.io.PrintStream)
159      */
160     @Override
161     public void printStackTrace(PrintStream out)
162     {
163         super.printStackTrace(out);
164         for (int i=0;i<LazyList.size(nested);i++)
165             ((Throwable)LazyList.get(nested,i)).printStackTrace(out);
166     }
167 
168     /* ------------------------------------------------------------------------------- */
169     /**
170      * @see java.lang.Throwable#printStackTrace(java.io.PrintWriter)
171      */
172     @Override
173     public void printStackTrace(PrintWriter out)
174     {
175         super.printStackTrace(out);
176         for (int i=0;i<LazyList.size(nested);i++)
177             ((Throwable)LazyList.get(nested,i)).printStackTrace(out);
178     }
179 
180 }