commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ol...@apache.org
Subject cvs commit: jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/util ExceptionUtil.java
Date Tue, 19 Oct 2004 18:09:46 GMT
olegk       2004/10/19 11:09:46

  Modified:    httpclient/src/java/org/apache/commons/httpclient
                        HttpConnection.java HttpMethodBase.java
                        MultiThreadedHttpConnectionManager.java
               httpclient/src/java/org/apache/commons/httpclient/util
                        ExceptionUtil.java
  Log:
  PR #31607 (Catch SocketTimeoutException not InterruptedIOException)
  
  Contributed by Oleg Kalnichevski
  Reviewed by Michael Becke and .t.may <bipponobungus at hotmail.com>
  
  Revision  Changes    Path
  1.102     +17 -6     jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/HttpConnection.java
  
  Index: HttpConnection.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/HttpConnection.java,v
  retrieving revision 1.101
  retrieving revision 1.102
  diff -u -r1.101 -r1.102
  --- HttpConnection.java	6 Oct 2004 17:32:03 -0000	1.101
  +++ HttpConnection.java	19 Oct 2004 18:09:45 -0000	1.102
  @@ -46,6 +46,7 @@
   import org.apache.commons.httpclient.protocol.ProtocolSocketFactory;
   import org.apache.commons.httpclient.protocol.SecureProtocolSocketFactory;
   import org.apache.commons.httpclient.util.EncodingUtil;
  +import org.apache.commons.httpclient.util.ExceptionUtil;
   import org.apache.commons.logging.Log;
   import org.apache.commons.logging.LogFactory;
   
  @@ -424,8 +425,10 @@
        * <code>false</code> otherwise.
        * 
        * @see #isStale()
  +     * 
  +     * @since 3.0
        */
  -    public boolean closeIfStale() {
  +    public boolean closeIfStale() throws IOException {
           if (used && isOpen && isStale()) {
               LOG.debug("Connection is stale, closing...");
               close();
  @@ -485,10 +488,12 @@
        * {@link BufferedInputStream}, so although data might be read, what is visible
        * to clients of the connection will not change with this call.</p.
        *
  +     * @throws IOException if the stale connection test is interrupted.
  +     * 
        * @return <tt>true</tt> if the connection is already closed, or a read
would
        * fail.
        */
  -    protected boolean isStale() {
  +    protected boolean isStale() throws IOException {
           boolean isStale = true;
           if (isOpen) {
               // the connection is open, but now we have to see if we can read it
  @@ -512,6 +517,9 @@
                       }
                   }
               } catch (InterruptedIOException e) {
  +                if (!ExceptionUtil.isSocketTimeoutException(e)) {
  +                    throw e;
  +                }
                   // aha - the connection is NOT stale - continue on!
               } catch (IOException e) {
                   // oops - the connection is stale, the read or soTimeout failed.
  @@ -889,6 +897,9 @@
                       LOG.debug("Input data not available");
                   }
               } catch (InterruptedIOException e) {
  +                if (!ExceptionUtil.isSocketTimeoutException(e)) {
  +                    throw e;
  +                }
                   if (LOG.isDebugEnabled()) {
                       LOG.debug("Input data not available after " + timeout + " ms");
                   }
  
  
  
  1.219     +8 -4      jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/HttpMethodBase.java
  
  Index: HttpMethodBase.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/HttpMethodBase.java,v
  retrieving revision 1.218
  retrieving revision 1.219
  diff -u -r1.218 -r1.219
  --- HttpMethodBase.java	7 Oct 2004 16:14:15 -0000	1.218
  +++ HttpMethodBase.java	19 Oct 2004 18:09:45 -0000	1.219
  @@ -43,6 +43,7 @@
   import org.apache.commons.httpclient.params.HttpMethodParams;
   import org.apache.commons.httpclient.protocol.Protocol;
   import org.apache.commons.httpclient.util.EncodingUtil;
  +import org.apache.commons.httpclient.util.ExceptionUtil;
   import org.apache.commons.logging.Log;
   import org.apache.commons.logging.LogFactory;
   
  @@ -1941,6 +1942,9 @@
                           return;
                       }
                   } catch (InterruptedIOException e) {
  +                    if (!ExceptionUtil.isSocketTimeoutException(e)) {
  +                        throw e;
  +                    }
                       // Most probably Expect header is not recongnized
                       // Remove the header to signal the method 
                       // that it's okay to go ahead with sending data
  
  
  
  1.46      +4 -4      jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/MultiThreadedHttpConnectionManager.java
  
  Index: MultiThreadedHttpConnectionManager.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/MultiThreadedHttpConnectionManager.java,v
  retrieving revision 1.45
  retrieving revision 1.46
  diff -u -r1.45 -r1.46
  --- MultiThreadedHttpConnectionManager.java	6 Oct 2004 17:32:04 -0000	1.45
  +++ MultiThreadedHttpConnectionManager.java	19 Oct 2004 18:09:45 -0000	1.46
  @@ -1274,7 +1274,7 @@
               }
           }
   
  -        public boolean closeIfStale() {
  +        public boolean closeIfStale() throws IOException {
               if (hasConnection()) {
                   return wrappedConnection.closeIfStale();
               } else {
  
  
  
  1.5       +42 -6     jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/util/ExceptionUtil.java
  
  Index: ExceptionUtil.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/util/ExceptionUtil.java,v
  retrieving revision 1.4
  retrieving revision 1.5
  diff -u -r1.4 -r1.5
  --- ExceptionUtil.java	13 May 2004 04:01:22 -0000	1.4
  +++ ExceptionUtil.java	19 Oct 2004 18:09:46 -0000	1.5
  @@ -28,6 +28,7 @@
    */
   package org.apache.commons.httpclient.util;
   
  +import java.io.InterruptedIOException;
   import java.lang.reflect.Method;
   
   import org.apache.commons.logging.Log;
  @@ -47,8 +48,11 @@
       private static final Log LOG = LogFactory.getLog(ExceptionUtil.class);
   
       /** A reference to Throwable's initCause method, or null if it's not there in this
JVM */
  -    static private final Method initCause = getInitCauseMethod();
  +    static private final Method INIT_CAUSE_METHOD = getInitCauseMethod();
   
  +    /** A reference to SocketTimeoutExceptionClass class, or null if it's not there in
this JVM */
  +    static private final Class SOCKET_TIMEOUT_CLASS = SocketTimeoutExceptionClass();
  +    
       /**
        * Returns a <code>Method<code> allowing access to
        * {@link Throwable.initCause(Throwable) initCause} method of {@link Throwable},
  @@ -67,6 +71,20 @@
           }
       }
       
  +    /**
  +     * Returns <code>SocketTimeoutExceptionClass<code> or <code>null</code>
if the class
  +     * does not exist.
  +     * 
  +     * @return <code>SocketTimeoutExceptionClass<code>, or <code>null</code>
if unavailable.
  +     */ 
  +    static private Class SocketTimeoutExceptionClass() {
  +        try {
  +            return Class.forName("java.net.SocketTimeoutException");
  +        } catch (ClassNotFoundException e) {
  +            return null;
  +        }
  +    }
  +    
       /** 
        * If we're running on JDK 1.4 or later, initialize the cause for the given throwable.
        * 
  @@ -74,12 +92,30 @@
        * @param  cause     The cause of the throwable.
        */
       public static void initCause(Throwable throwable, Throwable cause) {
  -        if (initCause != null) {
  +        if (INIT_CAUSE_METHOD != null) {
               try {
  -                initCause.invoke(throwable, new Object[] { cause });
  +                INIT_CAUSE_METHOD.invoke(throwable, new Object[] { cause });
               } catch (Exception e) {
                   LOG.warn("Exception invoking Throwable.initCause", e);
               }
  +        }
  +    }
  +
  +    /** 
  +     * If SocketTimeoutExceptionClass is defined, returns <tt>true</tt> only
if the 
  +     * exception is an instance of SocketTimeoutExceptionClass. If 
  +     * SocketTimeoutExceptionClass is undefined, always returns <tt>true</tt>.

  +     * 
  +     * @param  e an instance of InterruptedIOException class.
  +     * 
  +     * @return <tt>true</tt> if the exception signals socket timeout, <tt>false</tt>
  +     *  otherwise.   
  +     */
  +    public static boolean isSocketTimeoutException(final InterruptedIOException e) {
  +        if (SOCKET_TIMEOUT_CLASS != null) {
  +            return SOCKET_TIMEOUT_CLASS.isInstance(e);
  +        } else {
  +            return true;
           }
       }
   }
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org


Mime
View raw message