1. /*
  2. * $Header: /home/cvs/jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/util/IdleConnectionHandler.java,v 1.2 2004/05/13 02:40:36 mbecke Exp $
  3. * $Revision: 1.2 $
  4. * $Date: 2004/05/13 02:40:36 $
  5. *
  6. * ====================================================================
  7. *
  8. * Copyright 2004 The Apache Software Foundation
  9. *
  10. * Licensed under the Apache License, Version 2.0 (the "License");
  11. * you may not use this file except in compliance with the License.
  12. * You may obtain a copy of the License at
  13. *
  14. * http://www.apache.org/licenses/LICENSE-2.0
  15. *
  16. * Unless required by applicable law or agreed to in writing, software
  17. * distributed under the License is distributed on an "AS IS" BASIS,
  18. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  19. * See the License for the specific language governing permissions and
  20. * limitations under the License.
  21. * ====================================================================
  22. *
  23. * This software consists of voluntary contributions made by many
  24. * individuals on behalf of the Apache Software Foundation. For more
  25. * information on the Apache Software Foundation, please see
  26. * <http://www.apache.org/>.
  27. *
  28. */
  29. package org.apache.commons.httpclient.util;
  30. import java.util.HashMap;
  31. import java.util.Iterator;
  32. import java.util.Map;
  33. import org.apache.commons.httpclient.HttpConnection;
  34. import org.apache.commons.logging.Log;
  35. import org.apache.commons.logging.LogFactory;
  36. /**
  37. * A helper class for connection managers to track idle connections.
  38. *
  39. * <p>This class is not synchronized.</p>
  40. *
  41. * @see org.apache.commons.httpclient.HttpConnectionManager#closeIdleConnections(long)
  42. *
  43. * @since 3.0
  44. */
  45. public class IdleConnectionHandler {
  46. private static final Log LOG = LogFactory.getLog(IdleConnectionHandler.class);
  47. /** Holds connections and the time they were added. */
  48. private Map connectionToAdded = new HashMap();
  49. /**
  50. *
  51. */
  52. public IdleConnectionHandler() {
  53. super();
  54. }
  55. /**
  56. * Registers the given connection with this handler. The connection will be held until
  57. * {@link #remove(HttpConnection)} or {@link #closeIdleConnections(long)} is called.
  58. *
  59. * @param connection the connection to add
  60. *
  61. * @see #remove(HttpConnection)
  62. */
  63. public void add(HttpConnection connection) {
  64. Long timeAdded = new Long(System.currentTimeMillis());
  65. if (LOG.isDebugEnabled()) {
  66. LOG.debug("Adding connection at: " + timeAdded);
  67. }
  68. connectionToAdded.put(connection, timeAdded);
  69. }
  70. /**
  71. * Removes the given connection from the list of connections to be closed when idle.
  72. * @param connection
  73. */
  74. public void remove(HttpConnection connection) {
  75. connectionToAdded.remove(connection);
  76. }
  77. /**
  78. * Removes all connections referenced by this handler.
  79. */
  80. public void removeAll() {
  81. this.connectionToAdded.clear();
  82. }
  83. /**
  84. * Closes connections that have been idle for at least the given amount of time.
  85. *
  86. * @param idleTime the minimum idle time, in milliseconds, for connections to be closed
  87. */
  88. public void closeIdleConnections(long idleTime) {
  89. // the latest time for which connections will be closed
  90. long idleTimeout = System.currentTimeMillis() - idleTime;
  91. if (LOG.isDebugEnabled()) {
  92. LOG.debug("Checking for connections, idleTimeout: " + idleTimeout);
  93. }
  94. Iterator connectionIter = connectionToAdded.keySet().iterator();
  95. while (connectionIter.hasNext()) {
  96. HttpConnection conn = (HttpConnection) connectionIter.next();
  97. Long connectionTime = (Long) connectionToAdded.get(conn);
  98. if (connectionTime.longValue() <= idleTimeout) {
  99. if (LOG.isDebugEnabled()) {
  100. LOG.debug("Closing connection, connection time: " + connectionTime);
  101. }
  102. connectionIter.remove();
  103. conn.close();
  104. }
  105. }
  106. }
  107. }