KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > lutris > util > ChainedThrowableUtil


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: ChainedThrowableUtil.java,v 1.2 2005/03/24 10:51:25 slobodan Exp $
22  *
23  * formatted with JxBeauty (c) johann.langhofer@nextra.at
24  */

25
26
27 package com.lutris.util;
28
29 import java.io.PrintStream JavaDoc;
30 import java.io.PrintWriter JavaDoc;
31
32
33 /**
34  * Utilities used to implement the Chained* throwables.
35  */

36 // compliance with WEBDOCWF begin
37
// WebDocWf extension
38
// The following line has been changed:
39
public class ChainedThrowableUtil {
40
41   // before : class ChainedThrowableUtil {
42
// end of WebDocWf extension
43
// original line
44
// class ChainedThrowableUtil {
45
// compliance with WEBDOCWF end
46
/**
47    * Can't makes instances.
48    */

49   private ChainedThrowableUtil () {
50   }
51
52   /**
53    * Generate the message to set for the exception message.
54    * When not explicit message is supplied.
55    */

56   // compliance with WEBDOCWF begin
57
// WebDocWf extension
58
// The following line has been changed:
59
public static String JavaDoc makeMessage (Throwable JavaDoc cause) {
60     // before: protected static String makeMessage(Throwable cause) {
61
// end of WebDocWf extension
62
// original line
63
//protected static String makeMessage(Throwable cause) {
64
// compliance with WEBDOCWF end
65
String JavaDoc causeMsg = cause.getMessage();
66     if (causeMsg == null) {
67       return cause.getClass().getName();
68     }
69     else {
70       return causeMsg;
71     }
72   }
73
74   /**
75    * Get the message associated with this exception.
76    */

77   // compliance with WEBDOCWF begin
78
// WebDocWf extension
79
// The following line has been changed:
80
public static String JavaDoc getMessage (ChainedThrowable except, String JavaDoc superMsg) {
81     //before: protected static String getMessage(ChainedThrowable except, String superMsg) {
82
// end of WebDocWf extension
83
// original line
84
// protected static String getMessage(ChainedThrowable except,
85
// String superMsg) {
86
// compliance with WEBDOCWF end
87
Throwable JavaDoc cause = except.getCause();
88     if (cause == null) {
89       return superMsg;
90     }
91     else {
92       String JavaDoc causeMsg = cause.getMessage();
93       if ((causeMsg == null) || (causeMsg.length() == 0)) {
94         causeMsg = cause.getClass().getName();
95       }
96       return superMsg + ": " + causeMsg;
97     }
98   }
99
100   /**
101    * Do work of printing the causes of a chained exception. This is
102    * recursive. This attempts to following causing exceptions that are
103    * chained in other types of exception objects. If only Sun would
104    * standardize a mechanism in throwable instead of letting it accumulate
105    * in an ad-hoc manner.
106    */

107   private static void printChainedCauses (Throwable JavaDoc cause, PrintWriter JavaDoc out) {
108     if (cause != null) {
109       out.println("*** Caused by:");
110       cause.printStackTrace(out);
111       if (cause instanceof ChainedThrowable) {
112       // If cause was is a ChainedThrowable, its chain has already
113
// been followed, otherwise, see what we can do.
114
}
115       else if (cause instanceof java.awt.print.PrinterIOException JavaDoc) {
116         printChainedCauses(((java.awt.print.PrinterIOException JavaDoc)cause).getIOException(),
117             out);
118       }
119       else if (cause instanceof java.io.WriteAbortedException JavaDoc) {
120         printChainedCauses(((java.io.WriteAbortedException JavaDoc)cause).detail, out);
121       }
122       else if (cause instanceof java.lang.ClassNotFoundException JavaDoc) {
123         printChainedCauses(((java.lang.ClassNotFoundException JavaDoc)cause).getException(),
124             out);
125       }
126       else if (cause instanceof java.lang.ExceptionInInitializerError JavaDoc) {
127         printChainedCauses(((java.lang.ExceptionInInitializerError JavaDoc)cause).getException(),
128             out);
129       }
130       else if (cause instanceof java.lang.reflect.InvocationTargetException JavaDoc) {
131         printChainedCauses(((java.lang.reflect.InvocationTargetException JavaDoc)cause).getTargetException(),
132             out);
133       }
134       else if (cause instanceof java.rmi.RemoteException JavaDoc) {
135         printChainedCauses(((java.rmi.RemoteException JavaDoc)cause).detail, out);
136       }
137       else if (cause instanceof java.rmi.activation.ActivationException JavaDoc) {
138         printChainedCauses(((java.rmi.activation.ActivationException JavaDoc)cause).detail,
139             out);
140       }
141       else if (cause instanceof java.rmi.server.ServerCloneException JavaDoc) {
142         printChainedCauses(((java.rmi.server.ServerCloneException JavaDoc)cause).detail,
143             out);
144       }
145       else if (cause instanceof java.security.PrivilegedActionException JavaDoc) {
146         printChainedCauses(((java.security.PrivilegedActionException JavaDoc)cause).getException(),
147             out);
148       }
149       else if (cause instanceof java.sql.SQLException JavaDoc) {
150         printChainedCauses(((java.sql.SQLException JavaDoc)cause).getNextException(),
151             out);
152       }
153       else if (cause instanceof org.xml.sax.SAXException JavaDoc) {
154         printChainedCauses(((org.xml.sax.SAXException JavaDoc)cause).getException(),
155             out);
156       }
157     }
158   }
159
160   /**
161    * Prints stacktrace and cause stacktrace.
162    */

163   // compliance with WEBDOCWF begin
164
// WebDocWf extension
165
// The following line has been changed:
166
public static void printCauseTrace (ChainedThrowable except) {
167     //before: protected static void printCauseTrace(ChainedThrowable except) {
168
// end of WebDocWf extension
169
// original line
170
// protected static void printCauseTrace(ChainedThrowable except) {
171
// compliance with WEBDOCWF end
172
PrintWriter JavaDoc pw = new PrintWriter JavaDoc(System.err);
173     printChainedCauses(except.getCause(), pw);
174     pw.flush();
175   }
176
177   /**
178    * Prints stacktrace and cause stacktrace.
179    */

180   // compliance with WEBDOCWF begin
181
// WebDocWf extension
182
// The following line has been changed:
183
public static void printCauseTrace (ChainedThrowable except, PrintStream JavaDoc s) {
184     //before: protected static void printCauseTrace(ChainedThrowable except, PrintStream s) {
185
// end of WebDocWf extension
186
// original line
187
// protected static void printCauseTrace(ChainedThrowable except,
188
//PrintStream s) {
189
// compliance with WEBDOCWF end
190
PrintWriter JavaDoc pw = new PrintWriter JavaDoc(s);
191     printChainedCauses(except.getCause(), pw);
192     pw.flush();
193   }
194
195   /**
196    * Prints stacktrace and cause stacktrace.
197    */

198   public static void printCauseTrace (ChainedThrowable except, PrintWriter JavaDoc out) {
199     printChainedCauses(except.getCause(), out);
200     out.flush();
201   }
202 }
203
204
205
206
Popular Tags