1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/mobile/android/thirdparty/ch/boye/httpclientandroidlib/impl/conn/tsccm/AbstractConnPool.java Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,234 @@ 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.tsccm; 1.31 + 1.32 +import java.io.IOException; 1.33 +import java.lang.ref.Reference; 1.34 +import java.lang.ref.ReferenceQueue; 1.35 +import java.util.Set; 1.36 +import java.util.HashSet; 1.37 +import java.util.Iterator; 1.38 +import java.util.concurrent.TimeUnit; 1.39 +import java.util.concurrent.locks.Lock; 1.40 +import java.util.concurrent.locks.ReentrantLock; 1.41 + 1.42 +import ch.boye.httpclientandroidlib.annotation.GuardedBy; 1.43 + 1.44 +import ch.boye.httpclientandroidlib.androidextra.HttpClientAndroidLog; 1.45 +/* LogFactory removed by HttpClient for Android script. */ 1.46 +import ch.boye.httpclientandroidlib.conn.ConnectionPoolTimeoutException; 1.47 +import ch.boye.httpclientandroidlib.conn.OperatedClientConnection; 1.48 +import ch.boye.httpclientandroidlib.conn.routing.HttpRoute; 1.49 +import ch.boye.httpclientandroidlib.impl.conn.IdleConnectionHandler; 1.50 + 1.51 +/** 1.52 + * An abstract connection pool. 1.53 + * It is used by the {@link ThreadSafeClientConnManager}. 1.54 + * The abstract pool includes a {@link #poolLock}, which is used to 1.55 + * synchronize access to the internal pool datastructures. 1.56 + * Don't use <code>synchronized</code> for that purpose! 1.57 + * 1.58 + * @since 4.0 1.59 + */ 1.60 + 1.61 +@Deprecated 1.62 +public abstract class AbstractConnPool implements RefQueueHandler { 1.63 + 1.64 + public HttpClientAndroidLog log; 1.65 + 1.66 + /** 1.67 + * The global lock for this pool. 1.68 + */ 1.69 + protected final Lock poolLock; 1.70 + 1.71 + /** References to issued connections */ 1.72 + @GuardedBy("poolLock") 1.73 + protected Set<BasicPoolEntry> leasedConnections; 1.74 + 1.75 + /** The current total number of connections. */ 1.76 + @GuardedBy("poolLock") 1.77 + protected int numConnections; 1.78 + 1.79 + /** Indicates whether this pool is shut down. */ 1.80 + protected volatile boolean isShutDown; 1.81 + 1.82 + protected Set<BasicPoolEntryRef> issuedConnections; 1.83 + 1.84 + protected ReferenceQueue<Object> refQueue; 1.85 + 1.86 + protected IdleConnectionHandler idleConnHandler; 1.87 + 1.88 + /** 1.89 + * Creates a new connection pool. 1.90 + */ 1.91 + protected AbstractConnPool() { 1.92 + super(); 1.93 + this.log = new HttpClientAndroidLog(getClass()); 1.94 + this.leasedConnections = new HashSet<BasicPoolEntry>(); 1.95 + this.idleConnHandler = new IdleConnectionHandler(); 1.96 + this.poolLock = new ReentrantLock(); 1.97 + } 1.98 + 1.99 + public void enableConnectionGC() 1.100 + throws IllegalStateException { 1.101 + } 1.102 + 1.103 + /** 1.104 + * Obtains a pool entry with a connection within the given timeout. 1.105 + * 1.106 + * @param route the route for which to get the connection 1.107 + * @param timeout the timeout, 0 or negative for no timeout 1.108 + * @param tunit the unit for the <code>timeout</code>, 1.109 + * may be <code>null</code> only if there is no timeout 1.110 + * 1.111 + * @return pool entry holding a connection for the route 1.112 + * 1.113 + * @throws ConnectionPoolTimeoutException 1.114 + * if the timeout expired 1.115 + * @throws InterruptedException 1.116 + * if the calling thread was interrupted 1.117 + */ 1.118 + public final 1.119 + BasicPoolEntry getEntry( 1.120 + HttpRoute route, 1.121 + Object state, 1.122 + long timeout, 1.123 + TimeUnit tunit) 1.124 + throws ConnectionPoolTimeoutException, InterruptedException { 1.125 + return requestPoolEntry(route, state).getPoolEntry(timeout, tunit); 1.126 + } 1.127 + 1.128 + /** 1.129 + * Returns a new {@link PoolEntryRequest}, from which a {@link BasicPoolEntry} 1.130 + * can be obtained, or the request can be aborted. 1.131 + */ 1.132 + public abstract PoolEntryRequest requestPoolEntry(HttpRoute route, Object state); 1.133 + 1.134 + 1.135 + /** 1.136 + * Returns an entry into the pool. 1.137 + * The connection of the entry is expected to be in a suitable state, 1.138 + * either open and re-usable, or closed. The pool will not make any 1.139 + * attempt to determine whether it can be re-used or not. 1.140 + * 1.141 + * @param entry the entry for the connection to release 1.142 + * @param reusable <code>true</code> if the entry is deemed 1.143 + * reusable, <code>false</code> otherwise. 1.144 + * @param validDuration The duration that the entry should remain free and reusable. 1.145 + * @param timeUnit The unit of time the duration is measured in. 1.146 + */ 1.147 + public abstract void freeEntry(BasicPoolEntry entry, boolean reusable, long validDuration, TimeUnit timeUnit) 1.148 + ; 1.149 + 1.150 + public void handleReference(Reference<?> ref) { 1.151 + } 1.152 + 1.153 + protected abstract void handleLostEntry(HttpRoute route); 1.154 + 1.155 + /** 1.156 + * Closes idle connections. 1.157 + * 1.158 + * @param idletime the time the connections should have been idle 1.159 + * in order to be closed now 1.160 + * @param tunit the unit for the <code>idletime</code> 1.161 + */ 1.162 + public void closeIdleConnections(long idletime, TimeUnit tunit) { 1.163 + 1.164 + // idletime can be 0 or negative, no problem there 1.165 + if (tunit == null) { 1.166 + throw new IllegalArgumentException("Time unit must not be null."); 1.167 + } 1.168 + 1.169 + poolLock.lock(); 1.170 + try { 1.171 + idleConnHandler.closeIdleConnections(tunit.toMillis(idletime)); 1.172 + } finally { 1.173 + poolLock.unlock(); 1.174 + } 1.175 + } 1.176 + 1.177 + public void closeExpiredConnections() { 1.178 + poolLock.lock(); 1.179 + try { 1.180 + idleConnHandler.closeExpiredConnections(); 1.181 + } finally { 1.182 + poolLock.unlock(); 1.183 + } 1.184 + } 1.185 + 1.186 + 1.187 + /** 1.188 + * Deletes all entries for closed connections. 1.189 + */ 1.190 + public abstract void deleteClosedConnections(); 1.191 + 1.192 + /** 1.193 + * Shuts down this pool and all associated resources. 1.194 + * Overriding methods MUST call the implementation here! 1.195 + */ 1.196 + public void shutdown() { 1.197 + 1.198 + poolLock.lock(); 1.199 + try { 1.200 + 1.201 + if (isShutDown) 1.202 + return; 1.203 + 1.204 + // close all connections that are issued to an application 1.205 + Iterator<BasicPoolEntry> iter = leasedConnections.iterator(); 1.206 + while (iter.hasNext()) { 1.207 + BasicPoolEntry entry = iter.next(); 1.208 + iter.remove(); 1.209 + closeConnection(entry.getConnection()); 1.210 + } 1.211 + idleConnHandler.removeAll(); 1.212 + 1.213 + isShutDown = true; 1.214 + 1.215 + } finally { 1.216 + poolLock.unlock(); 1.217 + } 1.218 + } 1.219 + 1.220 + 1.221 + /** 1.222 + * Closes a connection from this pool. 1.223 + * 1.224 + * @param conn the connection to close, or <code>null</code> 1.225 + */ 1.226 + protected void closeConnection(final OperatedClientConnection conn) { 1.227 + if (conn != null) { 1.228 + try { 1.229 + conn.close(); 1.230 + } catch (IOException ex) { 1.231 + log.debug("I/O error closing connection", ex); 1.232 + } 1.233 + } 1.234 + } 1.235 + 1.236 +} // class AbstractConnPool 1.237 +