KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > httpclient > server > SimpleHttpServer


1 /*
2  * $Header: /home/cvs/jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/server/SimpleHttpServer.java,v 1.1.2.5 2004/02/22 18:21:18 olegk Exp $
3  * $Revision: 1.1.2.5 $
4  * $Date: 2004/02/22 18:21:18 $
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  * [Additional notices, if required by prior licensing conditions]
29  *
30  */

31
32 package org.apache.commons.httpclient.server;
33
34 import java.io.IOException JavaDoc;
35 import java.net.ServerSocket JavaDoc;
36 import java.net.Socket JavaDoc;
37 import java.net.SocketException JavaDoc;
38 import java.util.HashSet JavaDoc;
39 import java.util.Iterator JavaDoc;
40 import java.util.Set JavaDoc;
41
42 import org.apache.commons.httpclient.HttpStatus;
43 import org.apache.commons.logging.Log;
44 import org.apache.commons.logging.LogFactory;
45
46 /**
47  * A simple, but extensible HTTP server, mostly for testing purposes.
48  *
49  * @author Christian Kohlschuetter
50  */

51 public class SimpleHttpServer implements Runnable JavaDoc {
52     private static final Log LOG = LogFactory.getLog(SimpleHttpServer.class);
53     
54     private ServerSocket JavaDoc server = null;
55     private Thread JavaDoc t;
56     private ThreadGroup JavaDoc tg;
57     private boolean stopped = false;
58
59     private Set JavaDoc connections = new HashSet JavaDoc();
60
61     private HttpRequestHandler requestHandler = null;
62
63     /**
64      * Creates a new HTTP server instance, using an arbitrary free TCP port
65      *
66      * @throws IOException if anything goes wrong during initialization
67      */

68     public SimpleHttpServer() throws IOException JavaDoc {
69         this(0);
70     }
71
72     /**
73      * Creates a new HTTP server instance, using the specified TCP port
74      *
75      * @param port Desired TCP port
76      * @throws IOException if anything goes wrong during initialization
77      */

78     public SimpleHttpServer(int port) throws IOException JavaDoc {
79         server = new ServerSocket JavaDoc(port);
80         if(LOG.isInfoEnabled()) {
81             LOG.info("New SimpleHttpServer on port " + getLocalPort());
82         }
83         tg = new ThreadGroup JavaDoc("SimpleHttpServer group");
84         t = new Thread JavaDoc(tg, this, "SimpleHttpServer connection handler");
85         t.setDaemon(true);
86         t.start();
87     }
88
89     /**
90      * Returns the TCP port that this HTTP server instance is bound to.
91      *
92      * @return TCP port, or -1 if not running
93      */

94     public int getLocalPort() {
95         return server.getLocalPort();
96     }
97     
98     /**
99      * Returns the IP address that this HTTP server instance is bound to.
100      * @return String representation of the IP address or <code>null</code> if not running
101      */

102     public String JavaDoc getLocalAddress() {
103         return server.getInetAddress().getHostAddress();
104     }
105
106     /**
107      * Checks if this HTTP server instance is running.
108      *
109      * @return true/false
110      */

111     public boolean isRunning() {
112         if(t == null) {
113             return false;
114         }
115         return t.isAlive();
116     }
117
118     /**
119      * Stops this HTTP server instance.
120      */

121     public void destroy() {
122         if (stopped) {
123             return;
124         }
125
126         stopped = true;
127         if(LOG.isInfoEnabled()) {
128             LOG.info("Stopping SimpleHttpServer on port " + getLocalPort());
129         }
130
131         tg.interrupt();
132
133         if (server != null) {
134             try {
135                 server.close();
136             } catch(IOException JavaDoc e) {
137                 
138             }
139         }
140
141         for (Iterator JavaDoc it = connections.iterator(); it.hasNext();) {
142             SimpleHttpServerConnection conn =
143                 (SimpleHttpServerConnection) it.next();
144             conn.destroy();
145         }
146     }
147
148     /**
149      * Returns the currently used HttpRequestHandler by this SimpleHttpServer
150      *
151      * @return The used HttpRequestHandler, or null.
152      */

153     public HttpRequestHandler getRequestHandler() {
154         return requestHandler;
155     }
156
157     /**
158      * Sets the HttpRequestHandler to be used for this SimpleHttpServer.
159      *
160      * @param rh Request handler to be used, or null to disable.
161      */

162     public void setRequestHandler(HttpRequestHandler rh) {
163         requestHandler = rh;
164     }
165
166     public void removeConnection(SimpleHttpServerConnection conn) {
167         connections.remove(conn);
168     }
169
170     public void processRequest(SimpleHttpServerConnection conn)
171         throws IOException JavaDoc {
172
173         boolean complete = false;
174         if (requestHandler != null) {
175             complete = requestHandler.processRequest(conn);
176         }
177         if (!complete) {
178             conn.connectionClose();
179             ErrorResponse.getInstance().getResponse(HttpStatus.SC_SERVICE_UNAVAILABLE).processRequest(conn);
180         }
181     }
182
183     public void run() {
184         try {
185             while (!Thread.interrupted()) {
186                 Socket JavaDoc socket = server.accept();
187                 try {
188
189                     SimpleHttpServerConnection conn =
190                         new SimpleHttpServerConnection(this, socket);
191
192                     connections.add(conn);
193
194                     Thread JavaDoc t =
195                         new Thread JavaDoc(tg, conn, "SimpleHttpServer connection");
196                     t.setDaemon(true);
197                     t.start();
198                 } catch (IOException JavaDoc e) {
199                     LOG.error("SimpleHttpServer error", e);
200                     throw new RuntimeException JavaDoc(e.getMessage());
201                 }
202                 Thread.sleep(100);
203             }
204         } catch (InterruptedException JavaDoc accept) {
205         } catch (SocketException JavaDoc e) {
206             if (!stopped) {
207                 LOG.error("SimpleHttpServer error", e);
208                 throw new RuntimeException JavaDoc(e.getMessage());
209             }
210         } catch (IOException JavaDoc e) {
211             LOG.error("SimpleHttpServer error", e);
212             throw new RuntimeException JavaDoc(e.getMessage());
213         } finally {
214             destroy();
215         }
216     }
217 }
218
Popular Tags