Home | History | Annotate | Download | only in conn
      1 /*
      2  * $HeadURL: http://svn.apache.org/repos/asf/httpcomponents/httpclient/trunk/module-client/src/main/java/org/apache/http/conn/BasicEofSensorWatcher.java $
      3  * $Revision $
      4  * $Date: 2008-06-27 12:49:20 -0700 (Fri, 27 Jun 2008) $
      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 
     31 package org.apache.http.conn;
     32 
     33 import java.io.InputStream;
     34 import java.io.IOException;
     35 
     36 
     37 /**
     38  * Basic implementation of {@link EofSensorWatcher EofSensorWatcher}.
     39  * The underlying connection is released on close or EOF.
     40  *
     41  * @author <a href="mailto:rolandw at apache.org">Roland Weber</a>
     42  *
     43  *
     44  * <!-- empty lines to avoid svn diff problems -->
     45  * @version $Revision: 672367 $
     46  *
     47  * @since 4.0
     48  */
     49 public class BasicEofSensorWatcher implements EofSensorWatcher {
     50 
     51 
     52     /** The connection to auto-release. */
     53     protected ManagedClientConnection managedConn;
     54 
     55     /** Whether to keep the connection alive. */
     56     protected boolean attemptReuse;
     57 
     58 
     59 
     60     /**
     61      * Creates a new watcher for auto-releasing a connection.
     62      *
     63      * @param conn      the connection to auto-release
     64      * @param reuse     whether the connection should be re-used
     65      */
     66     public BasicEofSensorWatcher(ManagedClientConnection conn,
     67                                  boolean reuse) {
     68         if (conn == null)
     69             throw new IllegalArgumentException
     70                 ("Connection may not be null.");
     71 
     72         managedConn = conn;
     73         attemptReuse = reuse;
     74     }
     75 
     76 
     77     // non-javadoc, see interface EofSensorWatcher
     78     public boolean eofDetected(InputStream wrapped)
     79         throws IOException {
     80 
     81         try {
     82             if (attemptReuse) {
     83                 // there may be some cleanup required, such as
     84                 // reading trailers after the response body:
     85                 wrapped.close();
     86                 managedConn.markReusable();
     87             }
     88         } finally {
     89             managedConn.releaseConnection();
     90         }
     91         return false;
     92     }
     93 
     94 
     95     // non-javadoc, see interface EofSensorWatcher
     96     public boolean streamClosed(InputStream wrapped)
     97         throws IOException {
     98 
     99         try {
    100             if (attemptReuse) {
    101                 // this assumes that closing the stream will
    102                 // consume the remainder of the response body:
    103                 wrapped.close();
    104                 managedConn.markReusable();
    105             }
    106         } finally {
    107             managedConn.releaseConnection();
    108         }
    109         return false;
    110     }
    111 
    112 
    113     // non-javadoc, see interface EofSensorWatcher
    114     public boolean streamAbort(InputStream wrapped)
    115         throws IOException {
    116 
    117         managedConn.abortConnection();
    118         return false;
    119     }
    120 
    121 } // class BasicEofSensorWatcher
    122