001: /*
002: * $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/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 $
003: * $Revision: 480424 $
004: * $Date: 2006-11-29 06:56:49 +0100 (Wed, 29 Nov 2006) $
005: *
006: * ====================================================================
007: *
008: * Licensed to the Apache Software Foundation (ASF) under one or more
009: * contributor license agreements. See the NOTICE file distributed with
010: * this work for additional information regarding copyright ownership.
011: * The ASF licenses this file to You under the Apache License, Version 2.0
012: * (the "License"); you may not use this file except in compliance with
013: * the License. You may obtain a copy of the License at
014: *
015: * http://www.apache.org/licenses/LICENSE-2.0
016: *
017: * Unless required by applicable law or agreed to in writing, software
018: * distributed under the License is distributed on an "AS IS" BASIS,
019: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
020: * See the License for the specific language governing permissions and
021: * limitations under the License.
022: * ====================================================================
023: *
024: * This software consists of voluntary contributions made by many
025: * individuals on behalf of the Apache Software Foundation. For more
026: * information on the Apache Software Foundation, please see
027: * <http://www.apache.org/>.
028: *
029: */
030: package org.apache.commons.httpclient.util;
031:
032: import java.util.HashMap;
033: import java.util.Iterator;
034: import java.util.Map;
035:
036: import org.apache.commons.httpclient.HttpConnection;
037: import org.apache.commons.logging.Log;
038: import org.apache.commons.logging.LogFactory;
039:
040: /**
041: * A helper class for connection managers to track idle connections.
042: *
043: * <p>This class is not synchronized.</p>
044: *
045: * @see org.apache.commons.httpclient.HttpConnectionManager#closeIdleConnections(long)
046: *
047: * @since 3.0
048: */
049: public class IdleConnectionHandler {
050:
051: private static final Log LOG = LogFactory
052: .getLog(IdleConnectionHandler.class);
053:
054: /** Holds connections and the time they were added. */
055: private Map connectionToAdded = new HashMap();
056:
057: /**
058: *
059: */
060: public IdleConnectionHandler() {
061: super ();
062: }
063:
064: /**
065: * Registers the given connection with this handler. The connection will be held until
066: * {@link #remove(HttpConnection)} or {@link #closeIdleConnections(long)} is called.
067: *
068: * @param connection the connection to add
069: *
070: * @see #remove(HttpConnection)
071: */
072: public void add(HttpConnection connection) {
073:
074: Long timeAdded = new Long(System.currentTimeMillis());
075:
076: if (LOG.isDebugEnabled()) {
077: LOG.debug("Adding connection at: " + timeAdded);
078: }
079:
080: connectionToAdded.put(connection, timeAdded);
081: }
082:
083: /**
084: * Removes the given connection from the list of connections to be closed when idle.
085: * @param connection
086: */
087: public void remove(HttpConnection connection) {
088: connectionToAdded.remove(connection);
089: }
090:
091: /**
092: * Removes all connections referenced by this handler.
093: */
094: public void removeAll() {
095: this .connectionToAdded.clear();
096: }
097:
098: /**
099: * Closes connections that have been idle for at least the given amount of time.
100: *
101: * @param idleTime the minimum idle time, in milliseconds, for connections to be closed
102: */
103: public void closeIdleConnections(long idleTime) {
104:
105: // the latest time for which connections will be closed
106: long idleTimeout = System.currentTimeMillis() - idleTime;
107:
108: if (LOG.isDebugEnabled()) {
109: LOG.debug("Checking for connections, idleTimeout: "
110: + idleTimeout);
111: }
112:
113: Iterator connectionIter = connectionToAdded.keySet().iterator();
114:
115: while (connectionIter.hasNext()) {
116: HttpConnection conn = (HttpConnection) connectionIter
117: .next();
118: Long connectionTime = (Long) connectionToAdded.get(conn);
119: if (connectionTime.longValue() <= idleTimeout) {
120: if (LOG.isDebugEnabled()) {
121: LOG.debug("Closing connection, connection time: "
122: + connectionTime);
123: }
124: connectionIter.remove();
125: conn.close();
126: }
127: }
128: }
129: }
|