1069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project/*
2069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * $HeadURL: http://svn.apache.org/repos/asf/httpcomponents/httpclient/trunk/module-client/src/main/java/org/apache/http/impl/conn/IdleConnectionHandler.java $
3069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * $Revision: 673450 $
4069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * $Date: 2008-07-02 10:35:05 -0700 (Wed, 02 Jul 2008) $
5069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project *
6069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * ====================================================================
7069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project *
8069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project *  Licensed to the Apache Software Foundation (ASF) under one or more
9069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project *  contributor license agreements.  See the NOTICE file distributed with
10069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project *  this work for additional information regarding copyright ownership.
11069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project *  The ASF licenses this file to You under the Apache License, Version 2.0
12069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project *  (the "License"); you may not use this file except in compliance with
13069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project *  the License.  You may obtain a copy of the License at
14069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project *
15069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project *      http://www.apache.org/licenses/LICENSE-2.0
16069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project *
17069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project *  Unless required by applicable law or agreed to in writing, software
18069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project *  distributed under the License is distributed on an "AS IS" BASIS,
19069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project *  See the License for the specific language governing permissions and
21069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project *  limitations under the License.
22069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * ====================================================================
23069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project *
24069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * This software consists of voluntary contributions made by many
25069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * individuals on behalf of the Apache Software Foundation.  For more
26069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * information on the Apache Software Foundation, please see
27069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * <http://www.apache.org/>.
28069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project *
29069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project */
30069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Projectpackage org.apache.http.impl.conn;
31069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
32069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Projectimport java.io.IOException;
33069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Projectimport java.util.HashMap;
34069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Projectimport java.util.Iterator;
35069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Projectimport java.util.Map;
36069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Projectimport java.util.concurrent.TimeUnit;
37069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
38069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Projectimport org.apache.commons.logging.Log;
39069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Projectimport org.apache.commons.logging.LogFactory;
40069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Projectimport org.apache.http.HttpConnection;
41069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
42069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
43069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project/**
44069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * A helper class for connection managers to track idle connections.
45069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project *
46069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * <p>This class is not synchronized.</p>
47069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project *
48069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * @see org.apache.http.conn.ClientConnectionManager#closeIdleConnections
49069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project *
50069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project * @since 4.0
51d42abb2fd917184764daf22f5f299e848b8701d7Narayan Kamath *
52d42abb2fd917184764daf22f5f299e848b8701d7Narayan Kamath * @deprecated Please use {@link java.net.URL#openConnection} instead.
53d42abb2fd917184764daf22f5f299e848b8701d7Narayan Kamath *     Please visit <a href="http://android-developers.blogspot.com/2011/09/androids-http-clients.html">this webpage</a>
54d42abb2fd917184764daf22f5f299e848b8701d7Narayan Kamath *     for further details.
55069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project */
56d42abb2fd917184764daf22f5f299e848b8701d7Narayan Kamath@Deprecated
57069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Projectpublic class IdleConnectionHandler {
58069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
59069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    private final Log log = LogFactory.getLog(getClass());
60069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
61069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    /** Holds connections and the time they were added. */
62069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    private final Map<HttpConnection,TimeValues> connectionToTimes;
63069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
64069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
65069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    public IdleConnectionHandler() {
66069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        super();
67069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        connectionToTimes = new HashMap<HttpConnection,TimeValues>();
68069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    }
69069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
70069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    /**
71069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * Registers the given connection with this handler.  The connection will be held until
72069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * {@link #remove} or {@link #closeIdleConnections} is called.
73069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     *
74069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * @param connection the connection to add
75069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     *
76069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * @see #remove
77069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     */
78069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    public void add(HttpConnection connection, long validDuration, TimeUnit unit) {
79069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
80069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        Long timeAdded = Long.valueOf(System.currentTimeMillis());
81069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
82069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        if (log.isDebugEnabled()) {
83069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project            log.debug("Adding connection at: " + timeAdded);
84069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        }
85069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
86069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        connectionToTimes.put(connection, new TimeValues(timeAdded, validDuration, unit));
87069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    }
88069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
89069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    /**
90069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * Removes the given connection from the list of connections to be closed when idle.
91069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * This will return true if the connection is still valid, and false
92069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * if the connection should be considered expired and not used.
93069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     *
94069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * @param connection
95069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * @return True if the connection is still valid.
96069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     */
97069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    public boolean remove(HttpConnection connection) {
98069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        TimeValues times = connectionToTimes.remove(connection);
99069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        if(times == null) {
100069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project            log.warn("Removing a connection that never existed!");
101069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project            return true;
102069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        } else {
103069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project            return System.currentTimeMillis() <= times.timeExpires;
104069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        }
105069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    }
106069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
107069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    /**
108069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * Removes all connections referenced by this handler.
109069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     */
110069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    public void removeAll() {
111069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        this.connectionToTimes.clear();
112069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    }
113069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
114069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    /**
115069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * Closes connections that have been idle for at least the given amount of time.
116069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     *
117069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     * @param idleTime the minimum idle time, in milliseconds, for connections to be closed
118069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project     */
119069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    //@@@ add TimeUnit argument here?
120069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    public void closeIdleConnections(long idleTime) {
121069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
122069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        // the latest time for which connections will be closed
123069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        long idleTimeout = System.currentTimeMillis() - idleTime;
124069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
125069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        if (log.isDebugEnabled()) {
126069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project            log.debug("Checking for connections, idleTimeout: "  + idleTimeout);
127069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        }
128069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
129069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        Iterator<HttpConnection> connectionIter =
130069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project            connectionToTimes.keySet().iterator();
131069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
132069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        while (connectionIter.hasNext()) {
133069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project            HttpConnection conn = connectionIter.next();
134069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project            TimeValues times = connectionToTimes.get(conn);
135069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project            Long connectionTime = times.timeAdded;
136069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project            if (connectionTime.longValue() <= idleTimeout) {
137069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project                if (log.isDebugEnabled()) {
138069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project                    log.debug("Closing connection, connection time: "  + connectionTime);
139069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project                }
140069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project                connectionIter.remove();
141069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project                try {
142069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project                    conn.close();
143069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project                } catch (IOException ex) {
144069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project                    log.debug("I/O error closing connection", ex);
145069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project                }
146069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project            }
147069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        }
148069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    }
149069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
150069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
151069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    public void closeExpiredConnections() {
152069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        long now = System.currentTimeMillis();
153069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        if (log.isDebugEnabled()) {
154069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project            log.debug("Checking for expired connections, now: "  + now);
155069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        }
156069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
157069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        Iterator<HttpConnection> connectionIter =
158069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project            connectionToTimes.keySet().iterator();
159069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
160069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        while (connectionIter.hasNext()) {
161069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project            HttpConnection conn = connectionIter.next();
162069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project            TimeValues times = connectionToTimes.get(conn);
163069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project            if(times.timeExpires <= now) {
164069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project                if (log.isDebugEnabled()) {
165069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project                    log.debug("Closing connection, expired @: "  + times.timeExpires);
166069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project                }
167069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project                connectionIter.remove();
168069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project                try {
169069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project                    conn.close();
170069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project                } catch (IOException ex) {
171069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project                    log.debug("I/O error closing connection", ex);
172069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project                }
173069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project            }
174069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        }
175069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    }
176069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
177069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    private static class TimeValues {
178069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        private final long timeAdded;
179069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        private final long timeExpires;
180069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project
181069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        /**
182069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project         * @param now The current time in milliseconds
183069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project         * @param validDuration The duration this connection is valid for
184069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project         * @param validUnit The unit of time the duration is specified in.
185069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project         */
186069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        TimeValues(long now, long validDuration, TimeUnit validUnit) {
187069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project            this.timeAdded = now;
188069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project            if(validDuration > 0) {
189069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project                this.timeExpires = now + validUnit.toMillis(validDuration);
190069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project            } else {
191069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project                this.timeExpires = Long.MAX_VALUE;
192069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project            }
193069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project        }
194069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project    }
195069490a5ca2fd1988d29daf45d892f47ad665115The Android Open Source Project}
196