1   /*
2    * $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//httpclient/src/test/org/apache/commons/httpclient/server/SimpleConnectionThread.java,v 1.3 2004/11/13 22:38:27 mbecke 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.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      private static final String HTTP_ELEMENT_CHARSET = "US-ASCII";
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