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: 155418 $
4    * $Date: 2005-02-26 08:01:52 -0500 (Sat, 26 Feb 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  import org.apache.commons.logging.Log;
39  import org.apache.commons.logging.LogFactory;
40  
41  /***
42   * A REALLY simple connection manager.
43   * 
44   * @author Oleg Kalnichevski
45   */
46  public class SimpleConnManager {
47      
48      private static final Log LOG = LogFactory.getLog(SimpleConnManager.class);
49      
50      private Map connsets = new HashMap();
51      
52      public SimpleConnManager() {
53          super();
54      }
55  
56      public synchronized SimpleHttpServerConnection openConnection(final SimpleHost host)
57          throws IOException {
58          if (host == null) {
59              throw new IllegalArgumentException("Host may not be null");
60          }
61          SimpleHttpServerConnection conn = null;
62          SimpleConnList connlist = (SimpleConnList)this.connsets.get(host);
63          if (connlist != null) {
64              conn = connlist.removeFirst();
65              if (conn != null && !conn.isOpen()) {
66                  conn = null;
67              }
68          }
69          if (conn == null) {
70              Socket socket = new Socket(host.getHostName(), host.getPort());
71              conn = new SimpleHttpServerConnection(socket);
72          }
73          return conn;
74      }
75      
76      public synchronized void releaseConnection(final SimpleHost host, 
77              final SimpleHttpServerConnection conn) throws IOException {
78          if (host == null) {
79              throw new IllegalArgumentException("Host may not be null");
80          }
81          if (conn == null) {
82              return;
83          }
84          if (!conn.isKeepAlive()) {
85              conn.close();
86          }
87          if (conn.isOpen()) {
88              SimpleConnList connlist = (SimpleConnList)this.connsets.get(host);
89              if (connlist == null) {
90                  connlist = new SimpleConnList();
91                  this.connsets.put(host, connlist);
92              }
93              connlist.addConnection(conn);
94          }
95      }
96  
97      public synchronized void shutdown() {
98          for (Iterator i = this.connsets.values().iterator(); i.hasNext();) {
99              SimpleConnList connlist = (SimpleConnList) i.next();
100             connlist.shutdown();
101         }
102         this.connsets.clear();
103     }
104 
105 }