FileDocCategorySizeDatePackage
AbstractClientConnAdapter.javaAPI DocAndroid 1.5 API12981Wed May 06 22:41:10 BST 2009org.apache.http.impl.conn

AbstractClientConnAdapter

public abstract class AbstractClientConnAdapter extends Object implements ManagedClientConnection
Abstract adapter from {@link OperatedClientConnection operated} to {@link ManagedClientConnection managed} client connections. Read and write methods are delegated to the wrapped connection. Operations affecting the connection state have to be implemented by derived classes. Operations for querying the connection state are delegated to the wrapped connection if there is one, or return a default value if there is none.
This adapter tracks the checkpoints for reusable communication states, as indicated by {@link #markReusable markReusable} and queried by {@link #isMarkedReusable isMarkedReusable}. All send and receive operations will automatically clear the mark.
Connection release calls are delegated to the connection manager, if there is one. {@link #abortConnection abortConnection} will clear the reusability mark first. The connection manager is expected to tolerate multiple calls to the release method.
author
Roland Weber
version
$Revision: 672969 $ $Date: 2008-06-30 18:09:50 -0700 (Mon, 30 Jun 2008) $
since
4.0

Fields Summary
private final Thread
executionThread
Thread that requested this connection.
private volatile ClientConnectionManager
connManager
The connection manager, if any. This attribute MUST NOT be final, so the adapter can be detached from the connection manager without keeping a hard reference there.
private volatile OperatedClientConnection
wrappedConnection
The wrapped connection.
private volatile boolean
markedReusable
The reusability marker.
private volatile boolean
aborted
True if the connection has been aborted.
private volatile long
duration
The duration this is valid for while idle (in ms).
Constructors Summary
protected AbstractClientConnAdapter(ClientConnectionManager mgr, OperatedClientConnection conn)
Creates a new connection adapter. The adapter is initially not {@link #isMarkedReusable marked} as reusable.

param
mgr the connection manager, or null
param
conn the connection to wrap, or null

        super();
        executionThread = Thread.currentThread();
        connManager = mgr;
        wrappedConnection = conn;
        markedReusable = false;
        aborted = false;
        duration = Long.MAX_VALUE;
    
Methods Summary
public voidabortConnection()

        if (aborted) {
            return;
        }
        aborted = true;
        unmarkReusable();
        try {
            shutdown();
        } catch (IOException ignore) {
        }
        // Usually #abortConnection() is expected to be called from 
        // a helper thread in order to unblock the main execution thread 
        // blocked in an I/O operation. It may be unsafe to call 
        // #releaseConnection() from the helper thread, so we have to rely
        // on an IOException thrown by the closed socket on the main thread 
        // to trigger the release of the connection back to the 
        // connection manager.
        // 
        // However, if this method is called from the main execution thread 
        // it should be safe to release the connection immediately. Besides, 
        // this also helps ensure the connection gets released back to the 
        // manager if #abortConnection() is called from the main execution 
        // thread while there is no blocking I/O operation.
        if (executionThread.equals(Thread.currentThread())) {
            releaseConnection();
        }
    
protected final voidassertNotAborted()
Asserts that the connection has not been aborted.

throws
InterruptedIOException if the connection has been aborted

        if (aborted) {
            throw new InterruptedIOException("Connection has been shut down.");
        }
    
protected final voidassertValid(org.apache.http.conn.OperatedClientConnection wrappedConn)
Asserts that there is a wrapped connection to delegate to.

throws
IllegalStateException if there is no wrapped connection or connection has been aborted

        if (wrappedConn == null) {
            throw new IllegalStateException("No wrapped connection.");
        }
    
protected voiddetach()
Detaches this adapter from the wrapped connection. This adapter becomes useless.

        wrappedConnection = null;
        connManager = null; // base class attribute
        duration = Long.MAX_VALUE;
    
public voidflush()


        assertNotAborted();
        OperatedClientConnection conn = getWrappedConnection();
        assertValid(conn);

        conn.flush();
    
public java.net.InetAddressgetLocalAddress()

        OperatedClientConnection conn = getWrappedConnection();
        assertValid(conn);
        return conn.getLocalAddress();
    
public intgetLocalPort()

        OperatedClientConnection conn = getWrappedConnection();
        assertValid(conn);
        return conn.getLocalPort();
    
protected org.apache.http.conn.ClientConnectionManagergetManager()

        return connManager;
    
public org.apache.http.HttpConnectionMetricsgetMetrics()

        OperatedClientConnection conn = getWrappedConnection();
        assertValid(conn);
        return conn.getMetrics();
    
public java.net.InetAddressgetRemoteAddress()

        OperatedClientConnection conn = getWrappedConnection();
        assertValid(conn);
        return conn.getRemoteAddress();
    
public intgetRemotePort()

        OperatedClientConnection conn = getWrappedConnection();
        assertValid(conn);
        return conn.getRemotePort();
    
public javax.net.ssl.SSLSessiongetSSLSession()

        OperatedClientConnection conn = getWrappedConnection();
        assertValid(conn);
        if (!isOpen())
            return null;

        SSLSession result = null;
        Socket    sock    = conn.getSocket();
        if (sock instanceof SSLSocket) {
            result = ((SSLSocket)sock).getSession();
        }
        return result;
    
public intgetSocketTimeout()

        OperatedClientConnection conn = getWrappedConnection();
        assertValid(conn);
        return conn.getSocketTimeout();
    
protected org.apache.http.conn.OperatedClientConnectiongetWrappedConnection()

        return wrappedConnection;
    
public booleanisMarkedReusable()

        return markedReusable;
    
public booleanisOpen()

        OperatedClientConnection conn = getWrappedConnection();
        if (conn == null)
            return false;

        return conn.isOpen();
    
public booleanisResponseAvailable(int timeout)


        assertNotAborted();
        OperatedClientConnection conn = getWrappedConnection();
        assertValid(conn);

        return conn.isResponseAvailable(timeout);
    
public booleanisSecure()

        OperatedClientConnection conn = getWrappedConnection();
        assertValid(conn);
        return conn.isSecure();
    
public booleanisStale()

        if (aborted)
            return true;
        OperatedClientConnection conn = getWrappedConnection();
        if (conn == null)
            return true;

        return conn.isStale();
    
public voidmarkReusable()

        markedReusable = true;
    
public voidreceiveResponseEntity(org.apache.http.HttpResponse response)


        assertNotAborted();
        OperatedClientConnection conn = getWrappedConnection();
        assertValid(conn);

        unmarkReusable();
        conn.receiveResponseEntity(response);
    
public org.apache.http.HttpResponsereceiveResponseHeader()


        assertNotAborted();
        OperatedClientConnection conn = getWrappedConnection();
        assertValid(conn);

        unmarkReusable();
        return conn.receiveResponseHeader();
    
public voidreleaseConnection()

        if (connManager != null) {
            connManager.releaseConnection(this, duration, TimeUnit.MILLISECONDS);
        }
    
public voidsendRequestEntity(org.apache.http.HttpEntityEnclosingRequest request)


        assertNotAborted();
        OperatedClientConnection conn = getWrappedConnection();
        assertValid(conn);

        unmarkReusable();
        conn.sendRequestEntity(request);
    
public voidsendRequestHeader(org.apache.http.HttpRequest request)


        assertNotAborted();
        OperatedClientConnection conn = getWrappedConnection();
        assertValid(conn);
        
        unmarkReusable();
        conn.sendRequestHeader(request);
    
public voidsetIdleDuration(long duration, java.util.concurrent.TimeUnit unit)

        if(duration > 0) {
            this.duration = unit.toMillis(duration);
        } else {
            this.duration = -1;
        }
    
public voidsetSocketTimeout(int timeout)

        OperatedClientConnection conn = getWrappedConnection();
        assertValid(conn);
        conn.setSocketTimeout(timeout);
    
public voidunmarkReusable()

        markedReusable = false;