/*
* $HeadURL: http://svn.apache.org/repos/asf/httpcomponents/httpclient/trunk/module-client/src/main/java/org/apache/http/impl/conn/tsccm/ThreadSafeClientConnManager.java $
* $Revision: 673450 $
* $Date: 2008-07-02 10:35:05 -0700 (Wed, 02 Jul 2008) $
*
* ====================================================================
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation. For more
* information on the Apache Software Foundation, please see
*
* This class is derived from MultiThreadedHttpConnectionManager
* in HttpClient 3. See there for original authors.
*
null
*
* @return the connection operator to use
*/
protected ClientConnectionOperator
createConnectionOperator(SchemeRegistry schreg) {
return new DefaultClientConnectionOperator(schreg);
}
// non-javadoc, see interface ClientConnectionManager
public SchemeRegistry getSchemeRegistry() {
return this.schemeRegistry;
}
public ClientConnectionRequest requestConnection(
final HttpRoute route,
final Object state) {
final PoolEntryRequest poolRequest = connectionPool.requestPoolEntry(
route, state);
return new ClientConnectionRequest() {
public void abortRequest() {
poolRequest.abortRequest();
}
public ManagedClientConnection getConnection(
long timeout, TimeUnit tunit) throws InterruptedException,
ConnectionPoolTimeoutException {
if (route == null) {
throw new IllegalArgumentException("Route may not be null.");
}
if (log.isDebugEnabled()) {
log.debug("ThreadSafeClientConnManager.getConnection: "
+ route + ", timeout = " + timeout);
}
BasicPoolEntry entry = poolRequest.getPoolEntry(timeout, tunit);
// BEGIN android-changed
// When using a recycled Socket, we need to re-tag it with any
// updated statistics options.
try {
final Socket socket = entry.getConnection().getSocket();
if (socket != null) {
TrafficStats.tagSocket(socket);
}
} catch (IOException iox) {
log.debug("Problem tagging socket.", iox);
}
// END android-changed
return new BasicPooledConnAdapter(ThreadSafeClientConnManager.this, entry);
}
};
}
// non-javadoc, see interface ClientConnectionManager
public void releaseConnection(ManagedClientConnection conn, long validDuration, TimeUnit timeUnit) {
if (!(conn instanceof BasicPooledConnAdapter)) {
throw new IllegalArgumentException
("Connection class mismatch, " +
"connection not obtained from this manager.");
}
BasicPooledConnAdapter hca = (BasicPooledConnAdapter) conn;
if ((hca.getPoolEntry() != null) && (hca.getManager() != this)) {
throw new IllegalArgumentException
("Connection not obtained from this manager.");
}
try {
// BEGIN android-changed
// When recycling a Socket, we un-tag it to avoid collecting
// statistics from future users.
final BasicPoolEntry entry = (BasicPoolEntry) hca.getPoolEntry();
final Socket socket = entry.getConnection().getSocket();
if (socket != null) {
TrafficStats.untagSocket(socket);
}
// END android-changed
// make sure that the response has been read completely
if (hca.isOpen() && !hca.isMarkedReusable()) {
if (log.isDebugEnabled()) {
log.debug
("Released connection open but not marked reusable.");
}
// In MTHCM, there would be a call to
// SimpleHttpConnectionManager.finishLastResponse(conn);
// Consuming the response is handled outside in 4.0.
// make sure this connection will not be re-used
// Shut down rather than close, we might have gotten here
// because of a shutdown trigger.
// Shutdown of the adapter also clears the tracked route.
hca.shutdown();
}
} catch (IOException iox) {
//@@@ log as warning? let pass?
if (log.isDebugEnabled())
log.debug("Exception shutting down released connection.",
iox);
} finally {
BasicPoolEntry entry = (BasicPoolEntry) hca.getPoolEntry();
boolean reusable = hca.isMarkedReusable();
hca.detach();
if (entry != null) {
connectionPool.freeEntry(entry, reusable, validDuration, timeUnit);
}
}
}
// non-javadoc, see interface ClientConnectionManager
public void shutdown() {
connectionPool.shutdown();
}
/**
* Gets the total number of pooled connections for the given route.
* This is the total number of connections that have been created and
* are still in use by this connection manager for the route.
* This value will not exceed the maximum number of connections per host.
*
* @param route the route in question
*
* @return the total number of pooled connections for that route
*/
public int getConnectionsInPool(HttpRoute route) {
return ((ConnPoolByRoute)connectionPool).getConnectionsInPool(
route);
}
/**
* Gets the total number of pooled connections. This is the total number of
* connections that have been created and are still in use by this connection
* manager. This value will not exceed the maximum number of connections
* in total.
*
* @return the total number of pooled connections
*/
public int getConnectionsInPool() {
synchronized (connectionPool) {
return connectionPool.numConnections; //@@@
}
}
// non-javadoc, see interface ClientConnectionManager
public void closeIdleConnections(long idleTimeout, TimeUnit tunit) {
// combine these two in a single call?
connectionPool.closeIdleConnections(idleTimeout, tunit);
connectionPool.deleteClosedConnections();
}
public void closeExpiredConnections() {
connectionPool.closeExpiredConnections();
connectionPool.deleteClosedConnections();
}
} // class ThreadSafeClientConnManager