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: package ch.boye.httpclientandroidlib.impl.conn; michael@0: michael@0: import java.io.IOException; michael@0: import java.util.HashMap; michael@0: import java.util.Map; michael@0: import java.util.Map.Entry; michael@0: import java.util.concurrent.TimeUnit; 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.HttpConnection; michael@0: michael@0: // Currently only used by AbstractConnPool michael@0: /** michael@0: * A helper class for connection managers to track idle connections. michael@0: * michael@0: *

This class is not synchronized.

michael@0: * michael@0: * @see ch.boye.httpclientandroidlib.conn.ClientConnectionManager#closeIdleConnections michael@0: * michael@0: * @since 4.0 michael@0: * michael@0: * @deprecated no longer used michael@0: */ michael@0: @Deprecated michael@0: public class IdleConnectionHandler { michael@0: michael@0: public HttpClientAndroidLog log = new HttpClientAndroidLog(getClass()); michael@0: michael@0: /** Holds connections and the time they were added. */ michael@0: private final Map connectionToTimes; michael@0: michael@0: michael@0: public IdleConnectionHandler() { michael@0: super(); michael@0: connectionToTimes = new HashMap(); michael@0: } michael@0: michael@0: /** michael@0: * Registers the given connection with this handler. The connection will be held until michael@0: * {@link #remove} or {@link #closeIdleConnections} is called. michael@0: * michael@0: * @param connection the connection to add michael@0: * michael@0: * @see #remove michael@0: */ michael@0: public void add(HttpConnection connection, long validDuration, TimeUnit unit) { michael@0: michael@0: long timeAdded = System.currentTimeMillis(); michael@0: michael@0: if (log.isDebugEnabled()) { michael@0: log.debug("Adding connection at: " + timeAdded); michael@0: } michael@0: michael@0: connectionToTimes.put(connection, new TimeValues(timeAdded, validDuration, unit)); michael@0: } michael@0: michael@0: /** michael@0: * Removes the given connection from the list of connections to be closed when idle. michael@0: * This will return true if the connection is still valid, and false michael@0: * if the connection should be considered expired and not used. michael@0: * michael@0: * @param connection michael@0: * @return True if the connection is still valid. michael@0: */ michael@0: public boolean remove(HttpConnection connection) { michael@0: TimeValues times = connectionToTimes.remove(connection); michael@0: if(times == null) { michael@0: log.warn("Removing a connection that never existed!"); michael@0: return true; michael@0: } else { michael@0: return System.currentTimeMillis() <= times.timeExpires; michael@0: } michael@0: } michael@0: michael@0: /** michael@0: * Removes all connections referenced by this handler. michael@0: */ michael@0: public void removeAll() { michael@0: this.connectionToTimes.clear(); michael@0: } michael@0: michael@0: /** michael@0: * Closes connections that have been idle for at least the given amount of time. michael@0: * michael@0: * @param idleTime the minimum idle time, in milliseconds, for connections to be closed michael@0: */ michael@0: public void closeIdleConnections(long idleTime) { michael@0: michael@0: // the latest time for which connections will be closed michael@0: long idleTimeout = System.currentTimeMillis() - idleTime; michael@0: michael@0: if (log.isDebugEnabled()) { michael@0: log.debug("Checking for connections, idle timeout: " + idleTimeout); michael@0: } michael@0: michael@0: for (Entry entry : connectionToTimes.entrySet()) { michael@0: HttpConnection conn = entry.getKey(); michael@0: TimeValues times = entry.getValue(); michael@0: long connectionTime = times.timeAdded; michael@0: if (connectionTime <= idleTimeout) { michael@0: if (log.isDebugEnabled()) { michael@0: log.debug("Closing idle connection, connection time: " + connectionTime); michael@0: } 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: michael@0: michael@0: public void closeExpiredConnections() { michael@0: long now = System.currentTimeMillis(); michael@0: if (log.isDebugEnabled()) { michael@0: log.debug("Checking for expired connections, now: " + now); michael@0: } michael@0: michael@0: for (Entry entry : connectionToTimes.entrySet()) { michael@0: HttpConnection conn = entry.getKey(); michael@0: TimeValues times = entry.getValue(); michael@0: if(times.timeExpires <= now) { michael@0: if (log.isDebugEnabled()) { michael@0: log.debug("Closing connection, expired @: " + times.timeExpires); michael@0: } 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: michael@0: private static class TimeValues { michael@0: private final long timeAdded; michael@0: private final long timeExpires; michael@0: michael@0: /** michael@0: * @param now The current time in milliseconds michael@0: * @param validDuration The duration this connection is valid for michael@0: * @param validUnit The unit of time the duration is specified in. michael@0: */ michael@0: TimeValues(long now, long validDuration, TimeUnit validUnit) { michael@0: this.timeAdded = now; michael@0: if(validDuration > 0) { michael@0: this.timeExpires = now + validUnit.toMillis(validDuration); michael@0: } else { michael@0: this.timeExpires = Long.MAX_VALUE; michael@0: } michael@0: } michael@0: } michael@0: }