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 /***
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 }