KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > quartz > utils > ExceptionHelper


1 /*
2  * Copyright 2004-2006 OpenSymphony
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5  * use this file except in compliance with the License. You may obtain a copy
6  * of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13  * License for the specific language governing permissions and limitations
14  * under the License.
15  */

16 package org.quartz.utils;
17
18 import java.lang.reflect.Method JavaDoc;
19
20 import org.apache.commons.logging.Log;
21 import org.apache.commons.logging.LogFactory;
22
23 /**
24  * <p>
25  * Helper class for handling exception nesting, which is only supported in JDKs
26  * which are version 1.4 or later.
27  * </p>
28  *
29  * Sample usage:
30  * <pre>
31  * try {
32  * // Validate arguments
33  * } catch (Exception e) {
34  * Exception myException = new IllegalArgumentException("Doh!");
35  * ExceptionHelper.setCause(myException, e);
36  * throw myException;
37  * }
38  * </pre>
39  */

40 public class ExceptionHelper {
41     private static Boolean JavaDoc supportsNestedThrowable = null;
42     
43     private ExceptionHelper() {
44     }
45     
46     /**
47      * Set the given <code>Throwable<code>'s cause if this JDK supports
48      * the <code>Throwable#initCause(Throwable)</code> method.
49      */

50     public static Throwable JavaDoc setCause(Throwable JavaDoc exception, Throwable JavaDoc cause) {
51         if (exception != null) {
52             if (supportsNestedThrowable()) {
53                 try {
54                     Method JavaDoc initCauseMethod =
55                         exception.getClass().getMethod("initCause", new Class JavaDoc[] {Throwable JavaDoc.class});
56                     initCauseMethod.invoke(exception, new Object JavaDoc[] {cause});
57                 } catch (Exception JavaDoc e) {
58                     getLog().warn(
59                         "Unable to invoke initCause() method on class: " +
60                         exception.getClass().getName(), e);
61                 }
62             }
63         }
64         return exception;
65     }
66     
67     /**
68      * Get the underlying cause <code>Throwable</code> of the given exception
69      * if this JDK supports the <code>Throwable#getCause()</code> method.
70      */

71     public static Throwable JavaDoc getCause(Throwable JavaDoc exception) {
72         if (supportsNestedThrowable()) {
73             try {
74                 Method JavaDoc getCauseMethod =
75                     exception.getClass().getMethod("getCause", (Class JavaDoc[])null);
76                 return (Throwable JavaDoc)getCauseMethod.invoke(exception, (Object JavaDoc[])null);
77             } catch (Exception JavaDoc e) {
78                 getLog().warn(
79                     "Unable to invoke getCause() method on class: " +
80                     exception.getClass().getName(), e);
81             }
82         }
83         
84         return null;
85     }
86     
87     /**
88      * Get whether the Throwable hierarchy for this JDK supports
89      * initCause()/getCause().
90      */

91     public static synchronized boolean supportsNestedThrowable() {
92         if (supportsNestedThrowable == null) {
93             try {
94                 Throwable JavaDoc.class.getMethod("initCause", new Class JavaDoc[] {Throwable JavaDoc.class});
95                 Throwable JavaDoc.class.getMethod("getCause", (Class JavaDoc[])null);
96                 supportsNestedThrowable = Boolean.TRUE;
97                 getLog().debug("Detected JDK support for nested exceptions.");
98             } catch (NoSuchMethodException JavaDoc e) {
99                 supportsNestedThrowable = Boolean.FALSE;
100                 getLog().debug("Nested exceptions are not supported by this JDK.");
101             }
102         }
103         
104         return supportsNestedThrowable.booleanValue();
105     }
106     
107     private static Log getLog() {
108         return LogFactory.getLog(ExceptionHelper.class);
109     }
110 }
111
Popular Tags