1   
2   
3   
4   
5   
6   
7   
8   
9   
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  package org.apache.commons.httpclient.util;
31  
32  import java.io.InterruptedIOException;
33  import java.lang.reflect.Method;
34  
35  import org.apache.commons.logging.Log;
36  import org.apache.commons.logging.LogFactory;
37  
38  /**
39   * The home for utility methods that handle various exception-related tasks.
40   * 
41   * @author <a href="mailto:oleg@ural.ru">Oleg Kalnichevski</a>
42   * @author <a href="mailto:laura@lwerner.org">Laura Werner</a>
43   * 
44   * @since 3.0
45   */
46  public class ExceptionUtil {
47  
48      /** Log object for this class. */
49      private static final Log LOG = LogFactory.getLog(ExceptionUtil.class);
50  
51      /** A reference to Throwable's initCause method, or null if it's not there in this JVM */
52      static private final Method INIT_CAUSE_METHOD = getInitCauseMethod();
53  
54      /** A reference to SocketTimeoutExceptionClass class, or null if it's not there in this JVM */
55      static private final Class SOCKET_TIMEOUT_CLASS = SocketTimeoutExceptionClass();
56      
57      /**
58       * Returns a <code>Method<code> allowing access to
59       * {@link Throwable.initCause(Throwable) initCause} method of {@link Throwable},
60       * or <code>null</code> if the method
61       * does not exist.
62       * 
63       * @return A <code>Method<code> for <code>Throwable.initCause</code>, or
64       * <code>null</code> if unavailable.
65       */ 
66      static private Method getInitCauseMethod() {
67          try {
68              Class[] paramsClasses = new Class[] { Throwable.class };
69              return Throwable.class.getMethod("initCause", paramsClasses);
70          } catch (NoSuchMethodException e) {
71              return null;
72          }
73      }
74      
75      /**
76       * Returns <code>SocketTimeoutExceptionClass<code> or <code>null</code> if the class
77       * does not exist.
78       * 
79       * @return <code>SocketTimeoutExceptionClass<code>, or <code>null</code> if unavailable.
80       */ 
81      static private Class SocketTimeoutExceptionClass() {
82          try {
83              return Class.forName("java.net.SocketTimeoutException");
84          } catch (ClassNotFoundException e) {
85              return null;
86          }
87      }
88      
89      /** 
90       * If we're running on JDK 1.4 or later, initialize the cause for the given throwable.
91       * 
92       * @param  throwable The throwable.
93       * @param  cause     The cause of the throwable.
94       */
95      public static void initCause(Throwable throwable, Throwable cause) {
96          if (INIT_CAUSE_METHOD != null) {
97              try {
98                  INIT_CAUSE_METHOD.invoke(throwable, new Object[] { cause });
99              } catch (Exception e) {
100                 LOG.warn("Exception invoking Throwable.initCause", e);
101             }
102         }
103     }
104 
105     /** 
106      * If SocketTimeoutExceptionClass is defined, returns <tt>true</tt> only if the 
107      * exception is an instance of SocketTimeoutExceptionClass. If 
108      * SocketTimeoutExceptionClass is undefined, always returns <tt>true</tt>. 
109      * 
110      * @param  e an instance of InterruptedIOException class.
111      * 
112      * @return <tt>true</tt> if the exception signals socket timeout, <tt>false</tt>
113      *  otherwise.   
114      */
115     public static boolean isSocketTimeoutException(final InterruptedIOException e) {
116         if (SOCKET_TIMEOUT_CLASS != null) {
117             return SOCKET_TIMEOUT_CLASS.isInstance(e);
118         } else {
119             return true;
120         }
121     }
122 }