1   /*
2    * $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 $
3    * $Revision: 224451 $
4    * $Date: 2005-07-23 06:23:59 -0400 (Sat, 23 Jul 2005) $
5    *
6    * ====================================================================
7    *
8    *  Copyright 1999-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  
30  package org.apache.commons.httpclient.server;
31  
32  import java.io.IOException;
33  import java.net.Socket;
34  import java.util.HashMap;
35  import java.util.Iterator;
36  import java.util.Map;
37  
38  /***
39   * A REALLY simple connection manager.
40   * 
41   * @author Oleg Kalnichevski
42   */
43  public class SimpleConnManager {
44      
45      private Map connsets = new HashMap();
46      
47      public SimpleConnManager() {
48          super();
49      }
50  
51      public synchronized SimpleHttpServerConnection openConnection(final SimpleHost host)
52          throws IOException {
53          if (host == null) {
54              throw new IllegalArgumentException("Host may not be null");
55          }
56          SimpleHttpServerConnection conn = null;
57          SimpleConnList connlist = (SimpleConnList)this.connsets.get(host);
58          if (connlist != null) {
59              conn = connlist.removeFirst();
60              if (conn != null && !conn.isOpen()) {
61                  conn = null;
62              }
63          }
64          if (conn == null) {
65              Socket socket = new Socket(host.getHostName(), host.getPort());
66              conn = new SimpleHttpServerConnection(socket);
67          }
68          return conn;
69      }
70      
71      public synchronized void releaseConnection(final SimpleHost host, 
72              final SimpleHttpServerConnection conn) throws IOException {
73          if (host == null) {
74              throw new IllegalArgumentException("Host may not be null");
75          }
76          if (conn == null) {
77              return;
78          }
79          if (!conn.isKeepAlive()) {
80              conn.close();
81          }
82          if (conn.isOpen()) {
83              SimpleConnList connlist = (SimpleConnList)this.connsets.get(host);
84              if (connlist == null) {
85                  connlist = new SimpleConnList();
86                  this.connsets.put(host, connlist);
87              }
88              connlist.addConnection(conn);
89          }
90      }
91  
92      public synchronized void shutdown() {
93          for (Iterator i = this.connsets.values().iterator(); i.hasNext();) {
94              SimpleConnList connlist = (SimpleConnList) i.next();
95              connlist.shutdown();
96          }
97          this.connsets.clear();
98      }
99  
100 }