1   
2   
3   
4   
5   
6   
7   
8   
9   
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  package org.apache.commons.httpclient.util;
31  
32  import java.util.HashMap;
33  import java.util.Iterator;
34  import java.util.Map;
35  
36  import org.apache.commons.httpclient.HttpConnection;
37  import org.apache.commons.logging.Log;
38  import org.apache.commons.logging.LogFactory;
39  
40  /**
41   * A helper class for connection managers to track idle connections.
42   * 
43   * <p>This class is not synchronized.</p>
44   * 
45   * @see org.apache.commons.httpclient.HttpConnectionManager#closeIdleConnections(long)
46   * 
47   * @since 3.0
48   */
49  public class IdleConnectionHandler {
50      
51      private static final Log LOG = LogFactory.getLog(IdleConnectionHandler.class);
52      
53      /** Holds connections and the time they were added. */
54      private Map connectionToAdded = new HashMap();
55      
56      /**
57       * 
58       */
59      public IdleConnectionHandler() {
60          super();
61      }
62      
63      /**
64       * Registers the given connection with this handler.  The connection will be held until 
65       * {@link #remove(HttpConnection)} or {@link #closeIdleConnections(long)} is called.
66       * 
67       * @param connection the connection to add
68       * 
69       * @see #remove(HttpConnection)
70       */
71      public void add(HttpConnection connection) {
72          
73          Long timeAdded = new Long(System.currentTimeMillis());
74          
75          if (LOG.isDebugEnabled()) {
76              LOG.debug("Adding connection at: " + timeAdded);
77          }
78          
79          connectionToAdded.put(connection, timeAdded);
80      }
81      
82      /**
83       * Removes the given connection from the list of connections to be closed when idle.
84       * @param connection
85       */
86      public void remove(HttpConnection connection) {
87          connectionToAdded.remove(connection);
88      }
89  
90      /**
91       * Removes all connections referenced by this handler.
92       */
93      public void removeAll() {
94          this.connectionToAdded.clear();
95      }
96      
97      /**
98       * Closes connections that have been idle for at least the given amount of time.
99       * 
100      * @param idleTime the minimum idle time, in milliseconds, for connections to be closed
101      */
102     public void closeIdleConnections(long idleTime) {
103         
104         
105         long idleTimeout = System.currentTimeMillis() - idleTime;
106 
107         if (LOG.isDebugEnabled()) {
108             LOG.debug("Checking for connections, idleTimeout: "  + idleTimeout);
109         }
110         
111         Iterator connectionIter = connectionToAdded.keySet().iterator();
112         
113         while (connectionIter.hasNext()) {
114             HttpConnection conn = (HttpConnection) connectionIter.next();
115             Long connectionTime = (Long) connectionToAdded.get(conn);
116             if (connectionTime.longValue() <= idleTimeout) {
117                 if (LOG.isDebugEnabled()) {
118                     LOG.debug("Closing connection, connection time: "  + connectionTime);
119                 }
120                 connectionIter.remove();
121                 conn.close();
122             }
123         }
124     }
125 }