1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
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 }