1 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 46 public class ExceptionUtil { 47 48 49 private static final Log LOG = LogFactory.getLog(ExceptionUtil.class); 50 51 52 static private final Method INIT_CAUSE_METHOD = getInitCauseMethod(); 53 54 55 static private final Class SOCKET_TIMEOUT_CLASS = SocketTimeoutExceptionClass(); 56 57 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 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 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 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 } 123 | Popular Tags |