mobile/android/thirdparty/ch/boye/httpclientandroidlib/impl/conn/IdleConnectionHandler.java

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/mobile/android/thirdparty/ch/boye/httpclientandroidlib/impl/conn/IdleConnectionHandler.java	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,180 @@
     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 +package ch.boye.httpclientandroidlib.impl.conn;
    1.30 +
    1.31 +import java.io.IOException;
    1.32 +import java.util.HashMap;
    1.33 +import java.util.Map;
    1.34 +import java.util.Map.Entry;
    1.35 +import java.util.concurrent.TimeUnit;
    1.36 +
    1.37 +import ch.boye.httpclientandroidlib.androidextra.HttpClientAndroidLog;
    1.38 +/* LogFactory removed by HttpClient for Android script. */
    1.39 +import ch.boye.httpclientandroidlib.HttpConnection;
    1.40 +
    1.41 +// Currently only used by AbstractConnPool
    1.42 +/**
    1.43 + * A helper class for connection managers to track idle connections.
    1.44 + *
    1.45 + * <p>This class is not synchronized.</p>
    1.46 + *
    1.47 + * @see ch.boye.httpclientandroidlib.conn.ClientConnectionManager#closeIdleConnections
    1.48 + *
    1.49 + * @since 4.0
    1.50 + *
    1.51 + * @deprecated no longer used
    1.52 + */
    1.53 +@Deprecated
    1.54 +public class IdleConnectionHandler {
    1.55 +
    1.56 +    public HttpClientAndroidLog log = new HttpClientAndroidLog(getClass());
    1.57 +
    1.58 +    /** Holds connections and the time they were added. */
    1.59 +    private final Map<HttpConnection,TimeValues> connectionToTimes;
    1.60 +
    1.61 +
    1.62 +    public IdleConnectionHandler() {
    1.63 +        super();
    1.64 +        connectionToTimes = new HashMap<HttpConnection,TimeValues>();
    1.65 +    }
    1.66 +
    1.67 +    /**
    1.68 +     * Registers the given connection with this handler.  The connection will be held until
    1.69 +     * {@link #remove} or {@link #closeIdleConnections} is called.
    1.70 +     *
    1.71 +     * @param connection the connection to add
    1.72 +     *
    1.73 +     * @see #remove
    1.74 +     */
    1.75 +    public void add(HttpConnection connection, long validDuration, TimeUnit unit) {
    1.76 +
    1.77 +        long timeAdded = System.currentTimeMillis();
    1.78 +
    1.79 +        if (log.isDebugEnabled()) {
    1.80 +            log.debug("Adding connection at: " + timeAdded);
    1.81 +        }
    1.82 +
    1.83 +        connectionToTimes.put(connection, new TimeValues(timeAdded, validDuration, unit));
    1.84 +    }
    1.85 +
    1.86 +    /**
    1.87 +     * Removes the given connection from the list of connections to be closed when idle.
    1.88 +     * This will return true if the connection is still valid, and false
    1.89 +     * if the connection should be considered expired and not used.
    1.90 +     *
    1.91 +     * @param connection
    1.92 +     * @return True if the connection is still valid.
    1.93 +     */
    1.94 +    public boolean remove(HttpConnection connection) {
    1.95 +        TimeValues times = connectionToTimes.remove(connection);
    1.96 +        if(times == null) {
    1.97 +            log.warn("Removing a connection that never existed!");
    1.98 +            return true;
    1.99 +        } else {
   1.100 +            return System.currentTimeMillis() <= times.timeExpires;
   1.101 +        }
   1.102 +    }
   1.103 +
   1.104 +    /**
   1.105 +     * Removes all connections referenced by this handler.
   1.106 +     */
   1.107 +    public void removeAll() {
   1.108 +        this.connectionToTimes.clear();
   1.109 +    }
   1.110 +
   1.111 +    /**
   1.112 +     * Closes connections that have been idle for at least the given amount of time.
   1.113 +     *
   1.114 +     * @param idleTime the minimum idle time, in milliseconds, for connections to be closed
   1.115 +     */
   1.116 +    public void closeIdleConnections(long idleTime) {
   1.117 +
   1.118 +        // the latest time for which connections will be closed
   1.119 +        long idleTimeout = System.currentTimeMillis() - idleTime;
   1.120 +
   1.121 +        if (log.isDebugEnabled()) {
   1.122 +            log.debug("Checking for connections, idle timeout: "  + idleTimeout);
   1.123 +        }
   1.124 +
   1.125 +        for (Entry<HttpConnection, TimeValues> entry : connectionToTimes.entrySet()) {
   1.126 +            HttpConnection conn = entry.getKey();
   1.127 +            TimeValues times = entry.getValue();
   1.128 +            long connectionTime = times.timeAdded;
   1.129 +            if (connectionTime <= idleTimeout) {
   1.130 +                if (log.isDebugEnabled()) {
   1.131 +                    log.debug("Closing idle connection, connection time: "  + connectionTime);
   1.132 +                }
   1.133 +                try {
   1.134 +                    conn.close();
   1.135 +                } catch (IOException ex) {
   1.136 +                    log.debug("I/O error closing connection", ex);
   1.137 +                }
   1.138 +            }
   1.139 +        }
   1.140 +    }
   1.141 +
   1.142 +
   1.143 +    public void closeExpiredConnections() {
   1.144 +        long now = System.currentTimeMillis();
   1.145 +        if (log.isDebugEnabled()) {
   1.146 +            log.debug("Checking for expired connections, now: "  + now);
   1.147 +        }
   1.148 +
   1.149 +        for (Entry<HttpConnection, TimeValues> entry : connectionToTimes.entrySet()) {
   1.150 +            HttpConnection conn = entry.getKey();
   1.151 +            TimeValues times = entry.getValue();
   1.152 +            if(times.timeExpires <= now) {
   1.153 +                if (log.isDebugEnabled()) {
   1.154 +                    log.debug("Closing connection, expired @: "  + times.timeExpires);
   1.155 +                }
   1.156 +                try {
   1.157 +                    conn.close();
   1.158 +                } catch (IOException ex) {
   1.159 +                    log.debug("I/O error closing connection", ex);
   1.160 +                }
   1.161 +            }
   1.162 +        }
   1.163 +    }
   1.164 +
   1.165 +    private static class TimeValues {
   1.166 +        private final long timeAdded;
   1.167 +        private final long timeExpires;
   1.168 +
   1.169 +        /**
   1.170 +         * @param now The current time in milliseconds
   1.171 +         * @param validDuration The duration this connection is valid for
   1.172 +         * @param validUnit The unit of time the duration is specified in.
   1.173 +         */
   1.174 +        TimeValues(long now, long validDuration, TimeUnit validUnit) {
   1.175 +            this.timeAdded = now;
   1.176 +            if(validDuration > 0) {
   1.177 +                this.timeExpires = now + validUnit.toMillis(validDuration);
   1.178 +            } else {
   1.179 +                this.timeExpires = Long.MAX_VALUE;
   1.180 +            }
   1.181 +        }
   1.182 +    }
   1.183 +}

mercurial