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