001: /*
002: * $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//httpclient/src/test/org/apache/commons/httpclient/server/SimpleConnManager.java,v 1.2 2004/11/20 17:56:40 olegk 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:
031: package org.apache.commons.httpclient.server;
032:
033: import java.io.IOException;
034: import java.net.Socket;
035: import java.util.HashMap;
036: import java.util.Iterator;
037: import java.util.Map;
038:
039: /**
040: * A REALLY simple connection manager.
041: *
042: * @author Oleg Kalnichevski
043: */
044: public class SimpleConnManager {
045:
046: private Map connsets = new HashMap();
047:
048: public SimpleConnManager() {
049: super ();
050: }
051:
052: public synchronized SimpleHttpServerConnection openConnection(
053: final SimpleHost host) throws IOException {
054: if (host == null) {
055: throw new IllegalArgumentException("Host may not be null");
056: }
057: SimpleHttpServerConnection conn = null;
058: SimpleConnList connlist = (SimpleConnList) this .connsets
059: .get(host);
060: if (connlist != null) {
061: conn = connlist.removeFirst();
062: if (conn != null && !conn.isOpen()) {
063: conn = null;
064: }
065: }
066: if (conn == null) {
067: Socket socket = new Socket(host.getHostName(), host
068: .getPort());
069: conn = new SimpleHttpServerConnection(socket);
070: }
071: return conn;
072: }
073:
074: public synchronized void releaseConnection(final SimpleHost host,
075: final SimpleHttpServerConnection conn) throws IOException {
076: if (host == null) {
077: throw new IllegalArgumentException("Host may not be null");
078: }
079: if (conn == null) {
080: return;
081: }
082: if (!conn.isKeepAlive()) {
083: conn.close();
084: }
085: if (conn.isOpen()) {
086: SimpleConnList connlist = (SimpleConnList) this .connsets
087: .get(host);
088: if (connlist == null) {
089: connlist = new SimpleConnList();
090: this .connsets.put(host, connlist);
091: }
092: connlist.addConnection(conn);
093: }
094: }
095:
096: public synchronized void shutdown() {
097: for (Iterator i = this .connsets.values().iterator(); i
098: .hasNext();) {
099: SimpleConnList connlist = (SimpleConnList) i.next();
100: connlist.shutdown();
101: }
102: this.connsets.clear();
103: }
104:
105: }
|