View Javadoc

1   /*
2    * $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//httpclient/src/java/org/apache/commons/httpclient/util/ExceptionUtil.java,v 1.5 2004/10/19 18:09:46 olegk Exp $
3    * $Revision: 155418 $
4    * $Date: 2005-02-26 08:01:52 -0500 (Sat, 26 Feb 2005) $
5    *
6    * ====================================================================
7    *
8    *  Copyright 1999-2004 The Apache Software Foundation
9    *
10   *  Licensed under the Apache License, Version 2.0 (the "License");
11   *  you may not use this file except in compliance with the License.
12   *  You may obtain a copy of the License at
13   *
14   *      http://www.apache.org/licenses/LICENSE-2.0
15   *
16   *  Unless required by applicable law or agreed to in writing, software
17   *  distributed under the License is distributed on an "AS IS" BASIS,
18   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19   *  See the License for the specific language governing permissions and
20   *  limitations under the License.
21   * ====================================================================
22   *
23   * This software consists of voluntary contributions made by many
24   * individuals on behalf of the Apache Software Foundation.  For more
25   * information on the Apache Software Foundation, please see
26   * <http://www.apache.org/>.
27   *
28   */
29  package org.apache.commons.httpclient.util;
30  
31  import java.io.InterruptedIOException;
32  import java.lang.reflect.Method;
33  
34  import org.apache.commons.logging.Log;
35  import org.apache.commons.logging.LogFactory;
36  
37  /***
38   * The home for utility methods that handle various exception-related tasks.
39   * 
40   * @author <a href="mailto:oleg@ural.ru">Oleg Kalnichevski</a>
41   * @author <a href="mailto:laura@lwerner.org">Laura Werner</a>
42   * 
43   * @since 3.0
44   */
45  public class ExceptionUtil {
46  
47      /*** Log object for this class. */
48      private static final Log LOG = LogFactory.getLog(ExceptionUtil.class);
49  
50      /*** A reference to Throwable's initCause method, or null if it's not there in this JVM */
51      static private final Method INIT_CAUSE_METHOD = getInitCauseMethod();
52  
53      /*** A reference to SocketTimeoutExceptionClass class, or null if it's not there in this JVM */
54      static private final Class SOCKET_TIMEOUT_CLASS = SocketTimeoutExceptionClass();
55      
56      /***
57       * Returns a <code>Method<code> allowing access to
58       * {@link Throwable.initCause(Throwable) initCause} method of {@link Throwable},
59       * or <code>null</code> if the method
60       * does not exist.
61       * 
62       * @return A <code>Method<code> for <code>Throwable.initCause</code>, or
63       * <code>null</code> if unavailable.
64       */ 
65      static private Method getInitCauseMethod() {
66          try {
67              Class[] paramsClasses = new Class[] { Throwable.class };
68              return Throwable.class.getMethod("initCause", paramsClasses);
69          } catch (NoSuchMethodException e) {
70              return null;
71          }
72      }
73      
74      /***
75       * Returns <code>SocketTimeoutExceptionClass<code> or <code>null</code> if the class
76       * does not exist.
77       * 
78       * @return <code>SocketTimeoutExceptionClass<code>, or <code>null</code> if unavailable.
79       */ 
80      static private Class SocketTimeoutExceptionClass() {
81          try {
82              return Class.forName("java.net.SocketTimeoutException");
83          } catch (ClassNotFoundException e) {
84              return null;
85          }
86      }
87      
88      /*** 
89       * If we're running on JDK 1.4 or later, initialize the cause for the given throwable.
90       * 
91       * @param  throwable The throwable.
92       * @param  cause     The cause of the throwable.
93       */
94      public static void initCause(Throwable throwable, Throwable cause) {
95          if (INIT_CAUSE_METHOD != null) {
96              try {
97                  INIT_CAUSE_METHOD.invoke(throwable, new Object[] { cause });
98              } catch (Exception e) {
99                  LOG.warn("Exception invoking Throwable.initCause", e);
100             }
101         }
102     }
103 
104     /*** 
105      * If SocketTimeoutExceptionClass is defined, returns <tt>true</tt> only if the 
106      * exception is an instance of SocketTimeoutExceptionClass. If 
107      * SocketTimeoutExceptionClass is undefined, always returns <tt>true</tt>. 
108      * 
109      * @param  e an instance of InterruptedIOException class.
110      * 
111      * @return <tt>true</tt> if the exception signals socket timeout, <tt>false</tt>
112      *  otherwise.   
113      */
114     public static boolean isSocketTimeoutException(final InterruptedIOException e) {
115         if (SOCKET_TIMEOUT_CLASS != null) {
116             return SOCKET_TIMEOUT_CLASS.isInstance(e);
117         } else {
118             return true;
119         }
120     }
121 }