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/impl/conn/AbstractPooledConnAdapter.java $
      3  * $Revision: 658775 $
      4  * $Date: 2008-05-21 10:30:45 -0700 (Wed, 21 May 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.impl.conn;
     32 
     33 
     34 import java.io.IOException;
     35 
     36 import org.apache.http.HttpHost;
     37 import org.apache.http.params.HttpParams;
     38 import org.apache.http.protocol.HttpContext;
     39 import org.apache.http.conn.routing.HttpRoute;
     40 import org.apache.http.conn.ClientConnectionManager;
     41 import org.apache.http.conn.OperatedClientConnection;
     42 
     43 
     44 
     45 /**
     46  * Abstract adapter from pool {@link AbstractPoolEntry entries} to
     47  * {@link org.apache.http.conn.ManagedClientConnection managed}
     48  * client connections.
     49  * The connection in the pool entry is used to initialize the base class.
     50  * In addition, methods to establish a route are delegated to the
     51  * pool entry. {@link #shutdown shutdown} and {@link #close close}
     52  * will clear the tracked route in the pool entry and call the
     53  * respective method of the wrapped connection.
     54  *
     55  * @author <a href="mailto:rolandw at apache.org">Roland Weber</a>
     56  *
     57  *
     58  * <!-- empty lines to avoid svn diff problems -->
     59  * @version   $Revision: 658775 $ $Date: 2008-05-21 10:30:45 -0700 (Wed, 21 May 2008) $
     60  *
     61  * @since 4.0
     62  */
     63 public abstract class AbstractPooledConnAdapter extends AbstractClientConnAdapter {
     64 
     65     /** The wrapped pool entry. */
     66     protected volatile AbstractPoolEntry poolEntry;
     67 
     68 
     69     /**
     70      * Creates a new connection adapter.
     71      *
     72      * @param manager   the connection manager
     73      * @param entry     the pool entry for the connection being wrapped
     74      */
     75     protected AbstractPooledConnAdapter(ClientConnectionManager manager,
     76                                         AbstractPoolEntry entry) {
     77         super(manager, entry.connection);
     78         this.poolEntry = entry;
     79     }
     80 
     81 
     82     /**
     83      * Asserts that this adapter is still attached.
     84      *
     85      * @throws IllegalStateException
     86      *      if it is {@link #detach detach}ed
     87      */
     88     protected final void assertAttached() {
     89         if (poolEntry == null) {
     90             throw new IllegalStateException("Adapter is detached.");
     91         }
     92     }
     93 
     94     /**
     95      * Detaches this adapter from the wrapped connection.
     96      * This adapter becomes useless.
     97      */
     98     @Override
     99     protected void detach() {
    100         super.detach();
    101         poolEntry = null;
    102     }
    103 
    104 
    105     // non-javadoc, see interface ManagedHttpConnection
    106     public HttpRoute getRoute() {
    107 
    108         assertAttached();
    109         return (poolEntry.tracker == null) ?
    110             null : poolEntry.tracker.toRoute();
    111     }
    112 
    113     // non-javadoc, see interface ManagedHttpConnection
    114     public void open(HttpRoute route,
    115                      HttpContext context, HttpParams params)
    116         throws IOException {
    117 
    118         assertAttached();
    119         poolEntry.open(route, context, params);
    120     }
    121 
    122 
    123     // non-javadoc, see interface ManagedHttpConnection
    124     public void tunnelTarget(boolean secure, HttpParams params)
    125         throws IOException {
    126 
    127         assertAttached();
    128         poolEntry.tunnelTarget(secure, params);
    129     }
    130 
    131 
    132     // non-javadoc, see interface ManagedHttpConnection
    133     public void tunnelProxy(HttpHost next, boolean secure, HttpParams params)
    134         throws IOException {
    135 
    136         assertAttached();
    137         poolEntry.tunnelProxy(next, secure, params);
    138     }
    139 
    140 
    141     // non-javadoc, see interface ManagedHttpConnection
    142     public void layerProtocol(HttpContext context, HttpParams params)
    143         throws IOException {
    144 
    145         assertAttached();
    146         poolEntry.layerProtocol(context, params);
    147     }
    148 
    149 
    150 
    151     // non-javadoc, see interface HttpConnection
    152     public void close() throws IOException {
    153         if (poolEntry != null)
    154             poolEntry.shutdownEntry();
    155 
    156         OperatedClientConnection conn = getWrappedConnection();
    157         if (conn != null) {
    158             conn.close();
    159         }
    160     }
    161 
    162     // non-javadoc, see interface HttpConnection
    163     public void shutdown() throws IOException {
    164         if (poolEntry != null)
    165             poolEntry.shutdownEntry();
    166 
    167         OperatedClientConnection conn = getWrappedConnection();
    168         if (conn != null) {
    169             conn.shutdown();
    170         }
    171     }
    172 
    173 
    174     // non-javadoc, see interface ManagedClientConnection
    175     public Object getState() {
    176         assertAttached();
    177         return poolEntry.getState();
    178     }
    179 
    180 
    181     // non-javadoc, see interface ManagedClientConnection
    182     public void setState(final Object state) {
    183         assertAttached();
    184         poolEntry.setState(state);
    185     }
    186 
    187 
    188 } // class AbstractPooledConnAdapter
    189