KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > geronimo > interop > util > ExceptionUtil


1 /**
2  *
3  * Copyright 2004-2005 The Apache Software Foundation
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  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  *
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */

18 package org.apache.geronimo.interop.util;
19
20 import java.io.BufferedReader JavaDoc;
21 import java.io.StringReader JavaDoc;
22 import java.util.ArrayList JavaDoc;
23 import java.util.Iterator JavaDoc;
24 import java.util.List JavaDoc;
25
26 import org.apache.geronimo.interop.CheckedException;
27 import org.apache.geronimo.interop.SystemException;
28
29
30 public abstract class ExceptionUtil {
31     public static List JavaDoc addException(List JavaDoc exceptions, Throwable JavaDoc ex) {
32         if (exceptions == null) {
33             exceptions = new ArrayList JavaDoc(1);
34         }
35         exceptions.add(ex);
36         return exceptions;
37     }
38
39     public static void checkExceptions(List JavaDoc exceptions) {
40         if (exceptions != null) {
41             int n = exceptions.size();
42             if (n == 1) {
43                 Throwable JavaDoc ex = (Throwable JavaDoc) exceptions.get(0);
44                 if (ex instanceof Error JavaDoc) {
45                     throw (Error JavaDoc) ex;
46                 }
47                 if (ex instanceof RuntimeException JavaDoc) {
48                     throw (RuntimeException JavaDoc) ex;
49                 }
50                 throw new SystemException(ex);
51             } else {
52                 StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
53                 for (Iterator JavaDoc i = exceptions.iterator(); i.hasNext();) {
54                     Exception JavaDoc ex = (Exception JavaDoc) i.next();
55                     if (buffer.length() > 0) {
56                         buffer.append("\n______________________________________________________________\n\n");
57                     }
58                     buffer.append(ExceptionUtil.getStackTrace(ex));
59                 }
60                 throw new SystemException(buffer.toString());
61             }
62         }
63     }
64
65     public static String JavaDoc getDivider() {
66         return "\n ______________________________________________________________\n";
67     }
68
69     /**
70      * * Construct a detail message for an exception which doesn't take a
71      * * cause parameter in its constructor.
72      */

73     public static String JavaDoc causedBy(Throwable JavaDoc ex) {
74         return "\nCaused by: " + getStackTrace(ex) + getDivider();
75     }
76
77     public static String JavaDoc causedBy(String JavaDoc stackTrace)
78     {
79         return "\nCaused by: " + getTraceLines(stackTrace);
80     }
81
82     public static Throwable JavaDoc getCause(Throwable JavaDoc ex)
83     {
84         for (;;)
85         {
86             if (ex instanceof SystemException)
87             {
88                 SystemException se = (SystemException)ex;
89                 if (se.getCause() != null && se.getMessage() == null)
90                 {
91                     ex = se.getCause();
92                 }
93                 else
94                 {
95                     break;
96                 }
97             }
98             else
99             {
100                 break;
101             }
102         }
103         return ex;
104     }
105
106     public static String JavaDoc getCauseChain(Throwable JavaDoc ex) {
107         String JavaDoc stackTrace = getStackTrace(ex);
108         return getCauseChain(stackTrace);
109     }
110
111     public static String JavaDoc getCauseChain(String JavaDoc stackTrace) {
112         try {
113             BufferedReader JavaDoc input = new BufferedReader JavaDoc(new StringReader JavaDoc(stackTrace));
114             StringBuffer JavaDoc output = new StringBuffer JavaDoc(100);
115             String JavaDoc line;
116             while ((line = input.readLine()) != null) {
117                 line = line.trim();
118                 if (!line.startsWith("at ") && !line.startsWith("... ")) {
119                     output.append(line);
120                     output.append('\n');
121                 }
122             }
123             return output.toString();
124         } catch (Exception JavaDoc ex2) {
125             ex2.printStackTrace();
126             return stackTrace;
127         }
128     }
129
130     public static String JavaDoc getStackTrace(Throwable JavaDoc ex) {
131         java.io.StringWriter JavaDoc sw = new java.io.StringWriter JavaDoc();
132         java.io.PrintWriter JavaDoc pw = new java.io.PrintWriter JavaDoc(sw);
133         ex.printStackTrace(pw);
134         return sw.toString().trim();
135     }
136
137     public static String JavaDoc getTraceLines(String JavaDoc stackTrace)
138     {
139         try
140         {
141             BufferedReader JavaDoc input = new BufferedReader JavaDoc(new StringReader JavaDoc(stackTrace));
142             StringBuffer JavaDoc output = new StringBuffer JavaDoc(100);
143             String JavaDoc line;
144             boolean first = true;
145             while ((line = input.readLine()) != null)
146             {
147                 line = line.trim();
148                 if (line.length() != 0)
149                 {
150                     if (! first)
151                     {
152                         output.append("| ");
153                     }
154                     first = false;
155                     output.append(line);
156                     output.append('\n');
157                 }
158             }
159             return output.toString();
160         }
161         catch (Exception JavaDoc ex2)
162         {
163             ex2.printStackTrace();
164             return stackTrace;
165         }
166     }
167
168     public static String JavaDoc getCurrentStackTrace() {
169         return StringUtil.removePrefix(getStackTrace(new Exception JavaDoc()), "java.lang.Exception:");
170     }
171
172     public static String JavaDoc indentLines(String JavaDoc lines) {
173         return " " + StringUtil.replace(lines.trim(), "\n", "\n ");
174     }
175
176     public static boolean isApplicationException(Throwable JavaDoc ex) {
177         return !isSystemException(ex);
178     }
179
180     public static boolean isSystemException(Throwable JavaDoc ex) {
181         Class JavaDoc exClass = ex.getClass();
182         return Error JavaDoc.class.isAssignableFrom(exClass)
183                || RuntimeException JavaDoc.class.isAssignableFrom(exClass);
184     }
185
186     public static boolean isUserException(Class JavaDoc exClass) {
187         if (RuntimeException JavaDoc.class.isAssignableFrom(exClass)
188             || Error JavaDoc.class.isAssignableFrom(exClass)) {
189             return false;
190         }
191         return true;
192     }
193
194     public static RuntimeException JavaDoc getRuntimeException(Exception JavaDoc ex) {
195         if (ex instanceof RuntimeException JavaDoc) {
196             return (RuntimeException JavaDoc) ex;
197         } else {
198             return new SystemException(ex);
199         }
200     }
201
202     public static RuntimeException JavaDoc rethrow(Throwable JavaDoc ex) {
203         if (ex instanceof Error JavaDoc) {
204             throw (Error JavaDoc) ex;
205         } else if (ex instanceof RuntimeException JavaDoc) {
206             return (RuntimeException JavaDoc) ex;
207         } else {
208             return new CheckedException(ex);
209         }
210     }
211 }
212
Popular Tags