KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > error > ChainedThrowableSupport


1 /*
2  * Enhydra Java Application Server Project
3  *
4  * The contents of this file are subject to the Enhydra Public License
5  * Version 1.1 (the "License"); you may not use this file except in
6  * compliance with the License. You may obtain a copy of the License on
7  * the Enhydra web site ( http://www.enhydra.org/ ).
8  *
9  * Software distributed under the License is distributed on an "AS IS"
10  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
11  * the License for the specific terms governing rights and limitations
12  * under the License.
13  *
14  * The Initial Developer of the Enhydra Application Server is Lutris
15  * Technologies, Inc. The Enhydra Application Server and portions created
16  * by Lutris Technologies, Inc. are Copyright Lutris Technologies, Inc.
17  * All Rights Reserved.
18  *
19  * Contributor(s):
20  *
21  * $Id: ChainedThrowableSupport.java,v 1.2 2005/01/26 08:29:24 jkjome Exp $
22  */

23
24 package org.enhydra.error;
25
26 import java.io.PrintStream JavaDoc;
27 import java.io.PrintWriter JavaDoc;
28 import java.lang.reflect.InvocationTargetException JavaDoc;
29 import java.lang.reflect.Method JavaDoc;
30
31 /**
32  * Static methods used to implement the Chained* throwables.
33  */

34 public final class ChainedThrowableSupport {
35     /** Name of SAXException class. */
36     private static final String JavaDoc SAX_EXCEPTION_CLASS = "org.xml.sax.SAXException";
37
38     /**
39      * Can't makes instances of this class.
40      */

41     private ChainedThrowableSupport() {
42     }
43
44     /**
45      * Generate the message to set for the exception message when an explict
46      * message is not available. The message is derived from the causing
47      * exception. Used to support constructor that take only a cause
48      * {@link Throwable Throwable} as an argument. This method is used to
49      * generate the message to has to the super constructor.
50      *
51      * @param cause The causing exception, which maybe null.
52      */

53     public static String JavaDoc makeMessage(Throwable JavaDoc cause) {
54         if (cause == null) {
55             return null;
56         } else {
57             String JavaDoc causeMsg = cause.getMessage();
58             if (causeMsg == null) {
59                 return cause.getClass().getName();
60             } else {
61                 return causeMsg;
62             }
63         }
64     }
65
66     /**
67      * Get the message associated with this exception.
68      * @param except The chained throwable generating the
69      * message.
70      * @param superMsg The result of super.getMessage(), so
71      * that the contained messages is available. Maybe null.
72      */

73     public static String JavaDoc getMessage(ChainedThrowable except,
74                                     String JavaDoc superMsg) {
75         if (superMsg != null) {
76             return superMsg;
77         }
78         Thread JavaDoc thread = Thread.currentThread();
79         String JavaDoc threadMsg = " " + thread + " (" + thread.getThreadGroup().getName() + ", " + thread.getName() + ")";
80         Throwable JavaDoc cause = except.getCause();
81         if (cause == null) {
82             return except.getClass().getName() + threadMsg;
83         } else {
84             String JavaDoc causeMsg = cause.getMessage();
85             if ((causeMsg == null) || (causeMsg.length() == 0)) {
86                 causeMsg = cause.getClass().getName();
87             }
88             return causeMsg + threadMsg;
89         }
90     }
91
92     /**
93      * Get the localhist message associated with this exception.
94      * @param except The chained throwable generating the message.
95      * @param superMsg The result of super.getLocalizedMessage(), so
96      * that the contained messages is available. Maybe null.
97      */

98     public static String JavaDoc getLocalizedMessage(ChainedThrowable except,
99                                              String JavaDoc superMsg) {
100         if (superMsg != null) {
101             return superMsg;
102         }
103         Throwable JavaDoc cause = except.getCause();
104         if (cause == null) {
105             return except.getClass().getName();
106         } else {
107             String JavaDoc causeMsg = cause.getLocalizedMessage();
108             if ((causeMsg == null) || (causeMsg.length() == 0)) {
109                 causeMsg = cause.getClass().getName();
110             }
111             return causeMsg;
112         }
113     }
114
115     /**
116      * Check if this is one of the many java.* classes that contained
117      * a chained throwable.
118      */

119     private static Throwable JavaDoc getJavaChain(Throwable JavaDoc cause) {
120         if (cause instanceof java.io.WriteAbortedException JavaDoc) {
121             return ((java.io.WriteAbortedException JavaDoc)cause).detail;
122         } else if (cause instanceof java.lang.ClassNotFoundException JavaDoc) {
123             return ((java.lang.ClassNotFoundException JavaDoc)cause).getException();
124         } else if (cause instanceof java.lang.ExceptionInInitializerError JavaDoc) {
125             return ((java.lang.ExceptionInInitializerError JavaDoc)cause).getException();
126         } else if (cause instanceof java.lang.reflect.InvocationTargetException JavaDoc) {
127             return ((java.lang.reflect.InvocationTargetException JavaDoc)cause).getTargetException();
128         } else if (cause instanceof java.rmi.RemoteException JavaDoc) {
129             return ((java.rmi.RemoteException JavaDoc)cause).detail;
130         } else if (cause instanceof java.rmi.activation.ActivationException JavaDoc) {
131             return ((java.rmi.activation.ActivationException JavaDoc)cause).detail;
132         } else if (cause instanceof java.rmi.server.ServerCloneException JavaDoc) {
133             return ((java.rmi.server.ServerCloneException JavaDoc)cause).detail;
134         } else if (cause instanceof java.security.PrivilegedActionException JavaDoc) {
135             return ((java.security.PrivilegedActionException JavaDoc)cause).getException();
136         } else if (cause instanceof java.sql.SQLException JavaDoc) {
137             return ((java.sql.SQLException JavaDoc)cause).getNextException();
138         } else {
139             return null;
140         }
141     }
142
143     /**
144      * Obtain a chain cause via an parameterless method accessed through
145      * reflection.
146      */

147     private static Throwable JavaDoc getChainWithAccessor(Throwable JavaDoc cause,
148                                                   String JavaDoc accessorName)
149             throws NoSuchMethodException JavaDoc, IllegalAccessException JavaDoc,
150                    InvocationTargetException JavaDoc {
151         Method JavaDoc accessor = cause.getClass().getMethod(accessorName, (Class JavaDoc[])null);
152         return (Throwable JavaDoc)accessor.invoke(cause, (Object JavaDoc[])null);
153     }
154
155     /**
156      * Check for AWT exception with a chain. These are treated as optional,
157      * despite being a java.* class, since a server-only JVM might not
158      * include them.
159      */

160     private static Throwable JavaDoc getAWTChain(Throwable JavaDoc cause)
161             throws NoSuchMethodException JavaDoc, IllegalAccessException JavaDoc,
162                    InvocationTargetException JavaDoc {
163         if (cause.getClass().getName().equals("java.awt.print.PrinterIOException")) {
164             return getChainWithAccessor(cause, "getIOException");
165         } else {
166             return null;
167         }
168     }
169
170     /**
171      * Check if an exception is a SAXException without actually referencing
172      * the class.
173      */

174     private static boolean isSAXException(Throwable JavaDoc except) {
175         return except.getClass().getName().equals(SAX_EXCEPTION_CLASS);
176     }
177
178     /**
179      * Check SAXException chain.
180      */

181     private static Throwable JavaDoc getSAXExceptionChain(Throwable JavaDoc cause)
182             throws NoSuchMethodException JavaDoc, IllegalAccessException JavaDoc,
183                    InvocationTargetException JavaDoc {
184         if (isSAXException(cause)) {
185             return getChainWithAccessor(cause, "getException");
186         } else {
187             return null;
188         }
189     }
190
191     /**
192      * Do work of printing the causes of a chained exception. This is
193      * recursive. This attempts to following causing exceptions that are
194      * chained in other types of exception objects. If only Sun would
195      * standardize a mechanism in throwable. For classes that are
196      * not part of the standard runtime we access them by reflection,
197      * as this class if often lower down in the class loader hierarchy.
198      */

199     private static void printChainedCauses(Throwable JavaDoc cause,
200                                            PrintWriter JavaDoc out) {
201         if (cause != null) {
202             out.println("*** Caused by:"); //FIXME: I18N
203

204             if (isSAXException(cause)) {
205                 // SAXException `hides' itself when printing the stack by
206
// overriding toString() to print the contained exception,
207
// we print something extra to keep this from being confusing
208
out.print(SAX_EXCEPTION_CLASS);
209                 out.print(": ");
210             }
211
212             cause.printStackTrace(out);
213             try {
214                 // If cause was is a ChainedThrowable, its chain has already
215
// been followed, otherwise, see what we can do.
216
if (!(cause instanceof ChainedThrowable)) {
217                     Throwable JavaDoc nextCause = getJavaChain(cause);
218                     if (nextCause == null) {
219                         nextCause = getAWTChain(cause);
220                     }
221                     if (nextCause == null) {
222                         nextCause = getSAXExceptionChain(cause);
223                     }
224                     if (nextCause != null) {
225                         printChainedCauses(nextCause, out);
226                     }
227                 }
228             } catch (Throwable JavaDoc except) {
229                 // Can't find one of the chained exceptions
230
out.println("WARNING: can't print rest of exception chain: "
231                             + except);
232             }
233         }
234     }
235
236     /**
237      * Prints stacktrace and cause stacktrace.
238      */

239     public static void printCauseTrace(ChainedThrowable except) {
240         PrintWriter JavaDoc pw = new PrintWriter JavaDoc(System.err);
241         printChainedCauses(except.getCause(), pw);
242         pw.flush();
243     }
244
245     /**
246      * Prints stacktrace and cause stacktrace.
247      */

248     public static void printCauseTrace(ChainedThrowable except,
249                                        PrintStream JavaDoc s) {
250         PrintWriter JavaDoc pw = new PrintWriter JavaDoc(s);
251         printChainedCauses(except.getCause(), pw);
252         pw.flush();
253     }
254
255     /**
256      * Prints stacktrace and cause stacktrace.
257      */

258     public static void printCauseTrace(ChainedThrowable except,
259                                        PrintWriter JavaDoc out) {
260         printChainedCauses(except.getCause(), out);
261         out.flush();
262     }
263
264
265 }
266
Popular Tags