001: /*
002: * $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 $
003: * $Revision: 480424 $
004: * $Date: 2006-11-29 06:56:49 +0100 (Wed, 29 Nov 2006) $
005: *
006: * ====================================================================
007: *
008: * Licensed to the Apache Software Foundation (ASF) under one or more
009: * contributor license agreements. See the NOTICE file distributed with
010: * this work for additional information regarding copyright ownership.
011: * The ASF licenses this file to You under the Apache License, Version 2.0
012: * (the "License"); you may not use this file except in compliance with
013: * the License. You may obtain a copy of the License at
014: *
015: * http://www.apache.org/licenses/LICENSE-2.0
016: *
017: * Unless required by applicable law or agreed to in writing, software
018: * distributed under the License is distributed on an "AS IS" BASIS,
019: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
020: * See the License for the specific language governing permissions and
021: * limitations under the License.
022: * ====================================================================
023: *
024: * This software consists of voluntary contributions made by many
025: * individuals on behalf of the Apache Software Foundation. For more
026: * information on the Apache Software Foundation, please see
027: * <http://www.apache.org/>.
028: *
029: */
030:
031: package org.apache.commons.httpclient.server;
032:
033: import java.io.IOException;
034: import java.io.InterruptedIOException;
035:
036: import org.apache.commons.logging.Log;
037: import org.apache.commons.logging.LogFactory;
038:
039: /**
040: * Simple HTTP connection thread.
041: *
042: * @author Christian Kohlschuetter
043: * @author Oleg Kalnichevski
044: */
045: public class SimpleConnectionThread extends Thread {
046:
047: private static final Log LOG = LogFactory
048: .getLog(SimpleConnectionThread.class);
049:
050: public static final String DEFAULT_CONTENT_CHARSET = "ISO-8859-1";
051:
052: private SimpleHttpServerConnection conn = null;
053: private SimpleConnSet connpool = null;
054: private HttpRequestHandler handler = null;
055: transient boolean stopped;
056:
057: public SimpleConnectionThread(final ThreadGroup tg,
058: final String name, final SimpleHttpServerConnection conn,
059: final SimpleConnSet connpool,
060: final HttpRequestHandler handler) throws IOException {
061: super (tg, name);
062: if (conn == null) {
063: throw new IllegalArgumentException(
064: "Connection may not be null");
065: }
066: if (connpool == null) {
067: throw new IllegalArgumentException(
068: "Connection pool not be null");
069: }
070: if (handler == null) {
071: throw new IllegalArgumentException(
072: "Request handler may not be null");
073: }
074: this .conn = conn;
075: this .connpool = connpool;
076: this .handler = handler;
077: this .stopped = false;
078: }
079:
080: public synchronized void destroy() {
081: if (this .stopped) {
082: return;
083: }
084: this .stopped = true;
085: if (conn != null) {
086: conn.close();
087: conn = null;
088: }
089: interrupt();
090: }
091:
092: public void run() {
093: try {
094: do {
095: this .conn.setKeepAlive(false);
096: SimpleRequest request = this .conn.readRequest();
097: if (request != null) {
098: this .handler.processRequest(this .conn, request);
099: }
100: } while (this .conn.isKeepAlive());
101: } catch (InterruptedIOException e) {
102: } catch (IOException e) {
103: if (!this .stopped && !isInterrupted()
104: && LOG.isWarnEnabled()) {
105: LOG.warn("[" + getName() + "] I/O error: "
106: + e.getMessage());
107: }
108: } finally {
109: destroy();
110: this.connpool.removeConnection(this.conn);
111: }
112: }
113:
114: }
|