1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/mobile/android/thirdparty/ch/boye/httpclientandroidlib/impl/conn/AbstractPooledConnAdapter.java Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,179 @@ 1.4 +/* 1.5 + * ==================================================================== 1.6 + * 1.7 + * Licensed to the Apache Software Foundation (ASF) under one or more 1.8 + * contributor license agreements. See the NOTICE file distributed with 1.9 + * this work for additional information regarding copyright ownership. 1.10 + * The ASF licenses this file to You under the Apache License, Version 2.0 1.11 + * (the "License"); you may not use this file except in compliance with 1.12 + * the License. You may obtain a copy of the License at 1.13 + * 1.14 + * http://www.apache.org/licenses/LICENSE-2.0 1.15 + * 1.16 + * Unless required by applicable law or agreed to in writing, software 1.17 + * distributed under the License is distributed on an "AS IS" BASIS, 1.18 + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 1.19 + * See the License for the specific language governing permissions and 1.20 + * limitations under the License. 1.21 + * ==================================================================== 1.22 + * 1.23 + * This software consists of voluntary contributions made by many 1.24 + * individuals on behalf of the Apache Software Foundation. For more 1.25 + * information on the Apache Software Foundation, please see 1.26 + * <http://www.apache.org/>. 1.27 + * 1.28 + */ 1.29 + 1.30 +package ch.boye.httpclientandroidlib.impl.conn; 1.31 + 1.32 +import java.io.IOException; 1.33 + 1.34 +import ch.boye.httpclientandroidlib.HttpHost; 1.35 +import ch.boye.httpclientandroidlib.params.HttpParams; 1.36 +import ch.boye.httpclientandroidlib.protocol.HttpContext; 1.37 +import ch.boye.httpclientandroidlib.conn.routing.HttpRoute; 1.38 +import ch.boye.httpclientandroidlib.conn.ClientConnectionManager; 1.39 +import ch.boye.httpclientandroidlib.conn.OperatedClientConnection; 1.40 + 1.41 +/** 1.42 + * Abstract adapter from pool {@link AbstractPoolEntry entries} to 1.43 + * {@link ch.boye.httpclientandroidlib.conn.ManagedClientConnection managed} 1.44 + * client connections. 1.45 + * The connection in the pool entry is used to initialize the base class. 1.46 + * In addition, methods to establish a route are delegated to the 1.47 + * pool entry. {@link #shutdown shutdown} and {@link #close close} 1.48 + * will clear the tracked route in the pool entry and call the 1.49 + * respective method of the wrapped connection. 1.50 + * 1.51 + * @since 4.0 1.52 + */ 1.53 +public abstract class AbstractPooledConnAdapter extends AbstractClientConnAdapter { 1.54 + 1.55 + /** The wrapped pool entry. */ 1.56 + protected volatile AbstractPoolEntry poolEntry; 1.57 + 1.58 + /** 1.59 + * Creates a new connection adapter. 1.60 + * 1.61 + * @param manager the connection manager 1.62 + * @param entry the pool entry for the connection being wrapped 1.63 + */ 1.64 + protected AbstractPooledConnAdapter(ClientConnectionManager manager, 1.65 + AbstractPoolEntry entry) { 1.66 + super(manager, entry.connection); 1.67 + this.poolEntry = entry; 1.68 + } 1.69 + 1.70 + /** 1.71 + * Obtains the pool entry. 1.72 + * 1.73 + * @return the pool entry, or <code>null</code> if detached 1.74 + */ 1.75 + protected AbstractPoolEntry getPoolEntry() { 1.76 + return this.poolEntry; 1.77 + } 1.78 + 1.79 + /** 1.80 + * Asserts that there is a valid pool entry. 1.81 + * 1.82 + * @throws ConnectionShutdownException if there is no pool entry 1.83 + * or connection has been aborted 1.84 + * 1.85 + * @see #assertValid(OperatedClientConnection) 1.86 + */ 1.87 + protected void assertValid(final AbstractPoolEntry entry) { 1.88 + if (isReleased() || entry == null) { 1.89 + throw new ConnectionShutdownException(); 1.90 + } 1.91 + } 1.92 + 1.93 + /** 1.94 + * @deprecated use {@link #assertValid(AbstractPoolEntry)} 1.95 + */ 1.96 + @Deprecated 1.97 + protected final void assertAttached() { 1.98 + if (poolEntry == null) { 1.99 + throw new ConnectionShutdownException(); 1.100 + } 1.101 + } 1.102 + 1.103 + /** 1.104 + * Detaches this adapter from the wrapped connection. 1.105 + * This adapter becomes useless. 1.106 + */ 1.107 + @Override 1.108 + protected synchronized void detach() { 1.109 + poolEntry = null; 1.110 + super.detach(); 1.111 + } 1.112 + 1.113 + public HttpRoute getRoute() { 1.114 + AbstractPoolEntry entry = getPoolEntry(); 1.115 + assertValid(entry); 1.116 + return (entry.tracker == null) ? null : entry.tracker.toRoute(); 1.117 + } 1.118 + 1.119 + public void open(HttpRoute route, 1.120 + HttpContext context, HttpParams params) 1.121 + throws IOException { 1.122 + AbstractPoolEntry entry = getPoolEntry(); 1.123 + assertValid(entry); 1.124 + entry.open(route, context, params); 1.125 + } 1.126 + 1.127 + public void tunnelTarget(boolean secure, HttpParams params) 1.128 + throws IOException { 1.129 + AbstractPoolEntry entry = getPoolEntry(); 1.130 + assertValid(entry); 1.131 + entry.tunnelTarget(secure, params); 1.132 + } 1.133 + 1.134 + public void tunnelProxy(HttpHost next, boolean secure, HttpParams params) 1.135 + throws IOException { 1.136 + AbstractPoolEntry entry = getPoolEntry(); 1.137 + assertValid(entry); 1.138 + entry.tunnelProxy(next, secure, params); 1.139 + } 1.140 + 1.141 + public void layerProtocol(HttpContext context, HttpParams params) 1.142 + throws IOException { 1.143 + AbstractPoolEntry entry = getPoolEntry(); 1.144 + assertValid(entry); 1.145 + entry.layerProtocol(context, params); 1.146 + } 1.147 + 1.148 + public void close() throws IOException { 1.149 + AbstractPoolEntry entry = getPoolEntry(); 1.150 + if (entry != null) 1.151 + entry.shutdownEntry(); 1.152 + 1.153 + OperatedClientConnection conn = getWrappedConnection(); 1.154 + if (conn != null) { 1.155 + conn.close(); 1.156 + } 1.157 + } 1.158 + 1.159 + public void shutdown() throws IOException { 1.160 + AbstractPoolEntry entry = getPoolEntry(); 1.161 + if (entry != null) 1.162 + entry.shutdownEntry(); 1.163 + 1.164 + OperatedClientConnection conn = getWrappedConnection(); 1.165 + if (conn != null) { 1.166 + conn.shutdown(); 1.167 + } 1.168 + } 1.169 + 1.170 + public Object getState() { 1.171 + AbstractPoolEntry entry = getPoolEntry(); 1.172 + assertValid(entry); 1.173 + return entry.getState(); 1.174 + } 1.175 + 1.176 + public void setState(final Object state) { 1.177 + AbstractPoolEntry entry = getPoolEntry(); 1.178 + assertValid(entry); 1.179 + entry.setState(state); 1.180 + } 1.181 + 1.182 +}