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.tsccm; michael@0: michael@0: import java.io.IOException; michael@0: import java.lang.ref.Reference; michael@0: import java.lang.ref.ReferenceQueue; michael@0: import java.util.Set; michael@0: import java.util.HashSet; michael@0: import java.util.Iterator; michael@0: import java.util.concurrent.TimeUnit; michael@0: import java.util.concurrent.locks.Lock; michael@0: import java.util.concurrent.locks.ReentrantLock; michael@0: michael@0: import ch.boye.httpclientandroidlib.annotation.GuardedBy; michael@0: michael@0: import ch.boye.httpclientandroidlib.androidextra.HttpClientAndroidLog; michael@0: /* LogFactory removed by HttpClient for Android script. */ michael@0: import ch.boye.httpclientandroidlib.conn.ConnectionPoolTimeoutException; michael@0: import ch.boye.httpclientandroidlib.conn.OperatedClientConnection; michael@0: import ch.boye.httpclientandroidlib.conn.routing.HttpRoute; michael@0: import ch.boye.httpclientandroidlib.impl.conn.IdleConnectionHandler; michael@0: michael@0: /** michael@0: * An abstract connection pool. michael@0: * It is used by the {@link ThreadSafeClientConnManager}. michael@0: * The abstract pool includes a {@link #poolLock}, which is used to michael@0: * synchronize access to the internal pool datastructures. michael@0: * Don't use synchronized for that purpose! michael@0: * michael@0: * @since 4.0 michael@0: */ michael@0: michael@0: @Deprecated michael@0: public abstract class AbstractConnPool implements RefQueueHandler { michael@0: michael@0: public HttpClientAndroidLog log; michael@0: michael@0: /** michael@0: * The global lock for this pool. michael@0: */ michael@0: protected final Lock poolLock; michael@0: michael@0: /** References to issued connections */ michael@0: @GuardedBy("poolLock") michael@0: protected Set leasedConnections; michael@0: michael@0: /** The current total number of connections. */ michael@0: @GuardedBy("poolLock") michael@0: protected int numConnections; michael@0: michael@0: /** Indicates whether this pool is shut down. */ michael@0: protected volatile boolean isShutDown; michael@0: michael@0: protected Set issuedConnections; michael@0: michael@0: protected ReferenceQueue refQueue; michael@0: michael@0: protected IdleConnectionHandler idleConnHandler; michael@0: michael@0: /** michael@0: * Creates a new connection pool. michael@0: */ michael@0: protected AbstractConnPool() { michael@0: super(); michael@0: this.log = new HttpClientAndroidLog(getClass()); michael@0: this.leasedConnections = new HashSet(); michael@0: this.idleConnHandler = new IdleConnectionHandler(); michael@0: this.poolLock = new ReentrantLock(); michael@0: } michael@0: michael@0: public void enableConnectionGC() michael@0: throws IllegalStateException { michael@0: } michael@0: michael@0: /** michael@0: * Obtains a pool entry with a connection within the given timeout. michael@0: * michael@0: * @param route the route for which to get the connection michael@0: * @param timeout the timeout, 0 or negative for no timeout michael@0: * @param tunit the unit for the timeout, michael@0: * may be null only if there is no timeout michael@0: * michael@0: * @return pool entry holding a connection for the route michael@0: * michael@0: * @throws ConnectionPoolTimeoutException michael@0: * if the timeout expired michael@0: * @throws InterruptedException michael@0: * if the calling thread was interrupted michael@0: */ michael@0: public final michael@0: BasicPoolEntry getEntry( michael@0: HttpRoute route, michael@0: Object state, michael@0: long timeout, michael@0: TimeUnit tunit) michael@0: throws ConnectionPoolTimeoutException, InterruptedException { michael@0: return requestPoolEntry(route, state).getPoolEntry(timeout, tunit); michael@0: } michael@0: michael@0: /** michael@0: * Returns a new {@link PoolEntryRequest}, from which a {@link BasicPoolEntry} michael@0: * can be obtained, or the request can be aborted. michael@0: */ michael@0: public abstract PoolEntryRequest requestPoolEntry(HttpRoute route, Object state); michael@0: michael@0: michael@0: /** michael@0: * Returns an entry into the pool. michael@0: * The connection of the entry is expected to be in a suitable state, michael@0: * either open and re-usable, or closed. The pool will not make any michael@0: * attempt to determine whether it can be re-used or not. michael@0: * michael@0: * @param entry the entry for the connection to release michael@0: * @param reusable true if the entry is deemed michael@0: * reusable, false otherwise. michael@0: * @param validDuration The duration that the entry should remain free and reusable. michael@0: * @param timeUnit The unit of time the duration is measured in. michael@0: */ michael@0: public abstract void freeEntry(BasicPoolEntry entry, boolean reusable, long validDuration, TimeUnit timeUnit) michael@0: ; michael@0: michael@0: public void handleReference(Reference ref) { michael@0: } michael@0: michael@0: protected abstract void handleLostEntry(HttpRoute route); michael@0: michael@0: /** michael@0: * Closes idle connections. michael@0: * michael@0: * @param idletime the time the connections should have been idle michael@0: * in order to be closed now michael@0: * @param tunit the unit for the idletime michael@0: */ michael@0: public void closeIdleConnections(long idletime, TimeUnit tunit) { michael@0: michael@0: // idletime can be 0 or negative, no problem there michael@0: if (tunit == null) { michael@0: throw new IllegalArgumentException("Time unit must not be null."); michael@0: } michael@0: michael@0: poolLock.lock(); michael@0: try { michael@0: idleConnHandler.closeIdleConnections(tunit.toMillis(idletime)); michael@0: } finally { michael@0: poolLock.unlock(); michael@0: } michael@0: } michael@0: michael@0: public void closeExpiredConnections() { michael@0: poolLock.lock(); michael@0: try { michael@0: idleConnHandler.closeExpiredConnections(); michael@0: } finally { michael@0: poolLock.unlock(); michael@0: } michael@0: } michael@0: michael@0: michael@0: /** michael@0: * Deletes all entries for closed connections. michael@0: */ michael@0: public abstract void deleteClosedConnections(); michael@0: michael@0: /** michael@0: * Shuts down this pool and all associated resources. michael@0: * Overriding methods MUST call the implementation here! michael@0: */ michael@0: public void shutdown() { michael@0: michael@0: poolLock.lock(); michael@0: try { michael@0: michael@0: if (isShutDown) michael@0: return; michael@0: michael@0: // close all connections that are issued to an application michael@0: Iterator iter = leasedConnections.iterator(); michael@0: while (iter.hasNext()) { michael@0: BasicPoolEntry entry = iter.next(); michael@0: iter.remove(); michael@0: closeConnection(entry.getConnection()); michael@0: } michael@0: idleConnHandler.removeAll(); michael@0: michael@0: isShutDown = true; michael@0: michael@0: } finally { michael@0: poolLock.unlock(); michael@0: } michael@0: } michael@0: michael@0: michael@0: /** michael@0: * Closes a connection from this pool. michael@0: * michael@0: * @param conn the connection to close, or null michael@0: */ michael@0: protected void closeConnection(final OperatedClientConnection conn) { michael@0: if (conn != null) { michael@0: try { michael@0: conn.close(); michael@0: } catch (IOException ex) { michael@0: log.debug("I/O error closing connection", ex); michael@0: } michael@0: } michael@0: } michael@0: michael@0: } // class AbstractConnPool michael@0: