ExceptionUtil xref

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: 480424 $
    4    * $Date: 2006-11-29 06:56:49 +0100 (Wed, 29 Nov 2006) $
    5    *
    6    * ====================================================================
    7    *
    8    *  Licensed to the Apache Software Foundation (ASF) under one or more
    9    *  contributor license agreements.  See the NOTICE file distributed with
    10   *  this work for additional information regarding copyright ownership.
    11   *  The ASF licenses this file to You under the Apache License, Version 2.0
    12   *  (the "License"); you may not use this file except in compliance with
    13   *  the License.  You may obtain a copy of the License at
    14   *
    15   *      http://www.apache.org/licenses/LICENSE-2.0
    16   *
    17   *  Unless required by applicable law or agreed to in writing, software
    18   *  distributed under the License is distributed on an "AS IS" BASIS,
    19   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    20   *  See the License for the specific language governing permissions and
    21   *  limitations under the License.
    22   * ====================================================================
    23   *
    24   * This software consists of voluntary contributions made by many
    25   * individuals on behalf of the Apache Software Foundation.  For more
    26   * information on the Apache Software Foundation, please see
    27   * <http://www.apache.org/>.
    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 }