michael@0: /* michael@0: * ==================================================================== michael@0: * michael@0: * Licensed to the Apache Software Foundation (ASF) under one or more michael@0: * contributor license agreements. See the NOTICE file distributed with michael@0: * this work for additional information regarding copyright ownership. michael@0: * The ASF licenses this file to You under the Apache License, Version 2.0 michael@0: * (the "License"); you may not use this file except in compliance with michael@0: * the License. You may obtain a copy of the License at michael@0: * michael@0: * http://www.apache.org/licenses/LICENSE-2.0 michael@0: * michael@0: * Unless required by applicable law or agreed to in writing, software michael@0: * distributed under the License is distributed on an "AS IS" BASIS, michael@0: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. michael@0: * See the License for the specific language governing permissions and michael@0: * limitations under the License. michael@0: * ==================================================================== michael@0: * michael@0: * This software consists of voluntary contributions made by many michael@0: * individuals on behalf of the Apache Software Foundation. For more michael@0: * information on the Apache Software Foundation, please see michael@0: * . michael@0: * michael@0: */ michael@0: michael@0: package ch.boye.httpclientandroidlib.impl.conn; michael@0: michael@0: import java.io.IOException; michael@0: michael@0: import ch.boye.httpclientandroidlib.HttpHost; michael@0: import ch.boye.httpclientandroidlib.params.HttpParams; michael@0: import ch.boye.httpclientandroidlib.protocol.HttpContext; michael@0: import ch.boye.httpclientandroidlib.conn.routing.HttpRoute; michael@0: import ch.boye.httpclientandroidlib.conn.ClientConnectionManager; michael@0: import ch.boye.httpclientandroidlib.conn.OperatedClientConnection; michael@0: michael@0: /** michael@0: * Abstract adapter from pool {@link AbstractPoolEntry entries} to michael@0: * {@link ch.boye.httpclientandroidlib.conn.ManagedClientConnection managed} michael@0: * client connections. michael@0: * The connection in the pool entry is used to initialize the base class. michael@0: * In addition, methods to establish a route are delegated to the michael@0: * pool entry. {@link #shutdown shutdown} and {@link #close close} michael@0: * will clear the tracked route in the pool entry and call the michael@0: * respective method of the wrapped connection. michael@0: * michael@0: * @since 4.0 michael@0: */ michael@0: public abstract class AbstractPooledConnAdapter extends AbstractClientConnAdapter { michael@0: michael@0: /** The wrapped pool entry. */ michael@0: protected volatile AbstractPoolEntry poolEntry; michael@0: michael@0: /** michael@0: * Creates a new connection adapter. michael@0: * michael@0: * @param manager the connection manager michael@0: * @param entry the pool entry for the connection being wrapped michael@0: */ michael@0: protected AbstractPooledConnAdapter(ClientConnectionManager manager, michael@0: AbstractPoolEntry entry) { michael@0: super(manager, entry.connection); michael@0: this.poolEntry = entry; michael@0: } michael@0: michael@0: /** michael@0: * Obtains the pool entry. michael@0: * michael@0: * @return the pool entry, or null if detached michael@0: */ michael@0: protected AbstractPoolEntry getPoolEntry() { michael@0: return this.poolEntry; michael@0: } michael@0: michael@0: /** michael@0: * Asserts that there is a valid pool entry. michael@0: * michael@0: * @throws ConnectionShutdownException if there is no pool entry michael@0: * or connection has been aborted michael@0: * michael@0: * @see #assertValid(OperatedClientConnection) michael@0: */ michael@0: protected void assertValid(final AbstractPoolEntry entry) { michael@0: if (isReleased() || entry == null) { michael@0: throw new ConnectionShutdownException(); michael@0: } michael@0: } michael@0: michael@0: /** michael@0: * @deprecated use {@link #assertValid(AbstractPoolEntry)} michael@0: */ michael@0: @Deprecated michael@0: protected final void assertAttached() { michael@0: if (poolEntry == null) { michael@0: throw new ConnectionShutdownException(); michael@0: } michael@0: } michael@0: michael@0: /** michael@0: * Detaches this adapter from the wrapped connection. michael@0: * This adapter becomes useless. michael@0: */ michael@0: @Override michael@0: protected synchronized void detach() { michael@0: poolEntry = null; michael@0: super.detach(); michael@0: } michael@0: michael@0: public HttpRoute getRoute() { michael@0: AbstractPoolEntry entry = getPoolEntry(); michael@0: assertValid(entry); michael@0: return (entry.tracker == null) ? null : entry.tracker.toRoute(); michael@0: } michael@0: michael@0: public void open(HttpRoute route, michael@0: HttpContext context, HttpParams params) michael@0: throws IOException { michael@0: AbstractPoolEntry entry = getPoolEntry(); michael@0: assertValid(entry); michael@0: entry.open(route, context, params); michael@0: } michael@0: michael@0: public void tunnelTarget(boolean secure, HttpParams params) michael@0: throws IOException { michael@0: AbstractPoolEntry entry = getPoolEntry(); michael@0: assertValid(entry); michael@0: entry.tunnelTarget(secure, params); michael@0: } michael@0: michael@0: public void tunnelProxy(HttpHost next, boolean secure, HttpParams params) michael@0: throws IOException { michael@0: AbstractPoolEntry entry = getPoolEntry(); michael@0: assertValid(entry); michael@0: entry.tunnelProxy(next, secure, params); michael@0: } michael@0: michael@0: public void layerProtocol(HttpContext context, HttpParams params) michael@0: throws IOException { michael@0: AbstractPoolEntry entry = getPoolEntry(); michael@0: assertValid(entry); michael@0: entry.layerProtocol(context, params); michael@0: } michael@0: michael@0: public void close() throws IOException { michael@0: AbstractPoolEntry entry = getPoolEntry(); michael@0: if (entry != null) michael@0: entry.shutdownEntry(); michael@0: michael@0: OperatedClientConnection conn = getWrappedConnection(); michael@0: if (conn != null) { michael@0: conn.close(); michael@0: } michael@0: } michael@0: michael@0: public void shutdown() throws IOException { michael@0: AbstractPoolEntry entry = getPoolEntry(); michael@0: if (entry != null) michael@0: entry.shutdownEntry(); michael@0: michael@0: OperatedClientConnection conn = getWrappedConnection(); michael@0: if (conn != null) { michael@0: conn.shutdown(); michael@0: } michael@0: } michael@0: michael@0: public Object getState() { michael@0: AbstractPoolEntry entry = getPoolEntry(); michael@0: assertValid(entry); michael@0: return entry.getState(); michael@0: } michael@0: michael@0: public void setState(final Object state) { michael@0: AbstractPoolEntry entry = getPoolEntry(); michael@0: assertValid(entry); michael@0: entry.setState(state); michael@0: } michael@0: michael@0: }