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.io.InterruptedIOException;
34
35 import org.apache.commons.logging.Log;
36 import org.apache.commons.logging.LogFactory;
37
38 /***
39 * Simple HTTP connection thread.
40 *
41 * @author Christian Kohlschuetter
42 * @author Oleg Kalnichevski
43 */
44 public class SimpleConnectionThread extends Thread {
45
46 private static final Log LOG = LogFactory.getLog(SimpleConnectionThread.class);
47
48 public static final String DEFAULT_CONTENT_CHARSET = "ISO-8859-1";
49
50 private SimpleHttpServerConnection conn = null;
51 private SimpleConnSet connpool = null;
52 private HttpRequestHandler handler = null;
53 transient boolean stopped;
54
55 public SimpleConnectionThread(
56 final ThreadGroup tg,
57 final String name,
58 final SimpleHttpServerConnection conn,
59 final SimpleConnSet connpool,
60 final HttpRequestHandler handler)
61 throws IOException {
62 super(tg, name);
63 if (conn == null) {
64 throw new IllegalArgumentException("Connection may not be null");
65 }
66 if (connpool == null) {
67 throw new IllegalArgumentException("Connection pool not be null");
68 }
69 if (handler == null) {
70 throw new IllegalArgumentException("Request handler may not be null");
71 }
72 this.conn = conn;
73 this.connpool = connpool;
74 this.handler = handler;
75 this.stopped = false;
76 }
77
78 public synchronized void destroy() {
79 if (this.stopped) {
80 return;
81 }
82 this.stopped = true;
83 if (conn != null) {
84 conn.close();
85 conn = null;
86 }
87 interrupt();
88 }
89
90 public void run() {
91 try {
92 do {
93 this.conn.setKeepAlive(false);
94 SimpleRequest request = this.conn.readRequest();
95 if (request != null) {
96 this.handler.processRequest(this.conn, request);
97 }
98 } while (this.conn.isKeepAlive());
99 } catch (InterruptedIOException e) {
100 } catch (IOException e) {
101 if (!this.stopped && !isInterrupted() && LOG.isWarnEnabled()) {
102 LOG.warn("[" + getName() + "] I/O error: " + e.getMessage());
103 }
104 } finally {
105 destroy();
106 this.connpool.removeConnection(this.conn);
107 }
108 }
109
110 }
111