001: /*
002: * $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 $
003: * $Revision: 480424 $
004: * $Date: 2006-11-29 06:56:49 +0100 (Wed, 29 Nov 2006) $
005: *
006: * ====================================================================
007: *
008: * Licensed to the Apache Software Foundation (ASF) under one or more
009: * contributor license agreements. See the NOTICE file distributed with
010: * this work for additional information regarding copyright ownership.
011: * The ASF licenses this file to You under the Apache License, Version 2.0
012: * (the "License"); you may not use this file except in compliance with
013: * the License. You may obtain a copy of the License at
014: *
015: * http://www.apache.org/licenses/LICENSE-2.0
016: *
017: * Unless required by applicable law or agreed to in writing, software
018: * distributed under the License is distributed on an "AS IS" BASIS,
019: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
020: * See the License for the specific language governing permissions and
021: * limitations under the License.
022: * ====================================================================
023: *
024: * This software consists of voluntary contributions made by many
025: * individuals on behalf of the Apache Software Foundation. For more
026: * information on the Apache Software Foundation, please see
027: * <http://www.apache.org/>.
028: *
029: */
030: package org.apache.commons.httpclient.util;
031:
032: import java.io.InterruptedIOException;
033: import java.lang.reflect.Method;
034:
035: import org.apache.commons.logging.Log;
036: import org.apache.commons.logging.LogFactory;
037:
038: /**
039: * The home for utility methods that handle various exception-related tasks.
040: *
041: * @author <a href="mailto:oleg@ural.ru">Oleg Kalnichevski</a>
042: * @author <a href="mailto:laura@lwerner.org">Laura Werner</a>
043: *
044: * @since 3.0
045: */
046: public class ExceptionUtil {
047:
048: /** Log object for this class. */
049: private static final Log LOG = LogFactory
050: .getLog(ExceptionUtil.class);
051:
052: /** A reference to Throwable's initCause method, or null if it's not there in this JVM */
053: static private final Method INIT_CAUSE_METHOD = getInitCauseMethod();
054:
055: /** A reference to SocketTimeoutExceptionClass class, or null if it's not there in this JVM */
056: static private final Class SOCKET_TIMEOUT_CLASS = SocketTimeoutExceptionClass();
057:
058: /**
059: * Returns a <code>Method<code> allowing access to
060: * {@link Throwable.initCause(Throwable) initCause} method of {@link Throwable},
061: * or <code>null</code> if the method
062: * does not exist.
063: *
064: * @return A <code>Method<code> for <code>Throwable.initCause</code>, or
065: * <code>null</code> if unavailable.
066: */
067: static private Method getInitCauseMethod() {
068: try {
069: Class[] paramsClasses = new Class[] { Throwable.class };
070: return Throwable.class
071: .getMethod("initCause", paramsClasses);
072: } catch (NoSuchMethodException e) {
073: return null;
074: }
075: }
076:
077: /**
078: * Returns <code>SocketTimeoutExceptionClass<code> or <code>null</code> if the class
079: * does not exist.
080: *
081: * @return <code>SocketTimeoutExceptionClass<code>, or <code>null</code> if unavailable.
082: */
083: static private Class SocketTimeoutExceptionClass() {
084: try {
085: return Class.forName("java.net.SocketTimeoutException");
086: } catch (ClassNotFoundException e) {
087: return null;
088: }
089: }
090:
091: /**
092: * If we're running on JDK 1.4 or later, initialize the cause for the given throwable.
093: *
094: * @param throwable The throwable.
095: * @param cause The cause of the throwable.
096: */
097: public static void initCause(Throwable throwable, Throwable cause) {
098: if (INIT_CAUSE_METHOD != null) {
099: try {
100: INIT_CAUSE_METHOD.invoke(throwable,
101: new Object[] { cause });
102: } catch (Exception e) {
103: LOG.warn("Exception invoking Throwable.initCause", e);
104: }
105: }
106: }
107:
108: /**
109: * If SocketTimeoutExceptionClass is defined, returns <tt>true</tt> only if the
110: * exception is an instance of SocketTimeoutExceptionClass. If
111: * SocketTimeoutExceptionClass is undefined, always returns <tt>true</tt>.
112: *
113: * @param e an instance of InterruptedIOException class.
114: *
115: * @return <tt>true</tt> if the exception signals socket timeout, <tt>false</tt>
116: * otherwise.
117: */
118: public static boolean isSocketTimeoutException(
119: final InterruptedIOException e) {
120: if (SOCKET_TIMEOUT_CLASS != null) {
121: return SOCKET_TIMEOUT_CLASS.isInstance(e);
122: } else {
123: return true;
124: }
125: }
126: }
|