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  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     @Override
137     public String toString()
138     {
139         if (LazyList.size(nested)>0)
140             return MultiException.class.getSimpleName()+
141                 LazyList.getList(nested);
142         return MultiException.class.getSimpleName()+"[]";
143     }
144 
145     /* ------------------------------------------------------------ */
146     @Override
147     public void printStackTrace()
148     {
149         super.printStackTrace();
150         for (int i=0;i<LazyList.size(nested);i++)
151             ((Throwable)LazyList.get(nested,i)).printStackTrace();
152     }
153    
154 
155     /* ------------------------------------------------------------------------------- */
156     /**
157      * @see java.lang.Throwable#printStackTrace(java.io.PrintStream)
158      */
159     @Override
160     public void printStackTrace(PrintStream out)
161     {
162         super.printStackTrace(out);
163         for (int i=0;i<LazyList.size(nested);i++)
164             ((Throwable)LazyList.get(nested,i)).printStackTrace(out);
165     }
166 
167     /* ------------------------------------------------------------------------------- */
168     /**
169      * @see java.lang.Throwable#printStackTrace(java.io.PrintWriter)
170      */
171     @Override
172     public void printStackTrace(PrintWriter out)
173     {
174         super.printStackTrace(out);
175         for (int i=0;i<LazyList.size(nested);i++)
176             ((Throwable)LazyList.get(nested,i)).printStackTrace(out);
177     }
178 
179 }