KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > axis > transport > http > SimpleAxisServer


1 /*
2  * The Apache Software License, Version 1.1
3  *
4  *
5  * Copyright (c) 2001-2003 The Apache Software Foundation. All rights
6  * reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright
13  * notice, this list of conditions and the following disclaimer.
14  *
15  * 2. Redistributions in binary form must reproduce the above copyright
16  * notice, this list of conditions and the following disclaimer in
17  * the documentation and/or other materials provided with the
18  * distribution.
19  *
20  * 3. The end-user documentation included with the redistribution,
21  * if any, must include the following acknowledgment:
22  * "This product includes software developed by the
23  * Apache Software Foundation (http://www.apache.org/)."
24  * Alternately, this acknowledgment may appear in the software itself,
25  * if and wherever such third-party acknowledgments normally appear.
26  *
27  * 4. The names "Axis" and "Apache Software Foundation" must
28  * not be used to endorse or promote products derived from this
29  * software without prior written permission. For written
30  * permission, please contact apache@apache.org.
31  *
32  * 5. Products derived from this software may not be called "Apache",
33  * nor may "Apache" appear in their name, without prior written
34  * permission of the Apache Software Foundation.
35  *
36  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
37  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
38  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
39  * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
40  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
41  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
42  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
43  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
44  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
45  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
46  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
47  * SUCH DAMAGE.
48  * ====================================================================
49  *
50  * This software consists of voluntary contributions made by many
51  * individuals on behalf of the Apache Software Foundation. For more
52  * information on the Apache Software Foundation, please see
53  * <http://www.apache.org/>.
54  */

55
56 package org.jboss.axis.transport.http;
57
58 import org.jboss.axis.server.AxisServer;
59 import org.jboss.axis.session.Session;
60 import org.jboss.axis.session.SimpleSession;
61 import org.jboss.axis.utils.Messages;
62 import org.jboss.axis.utils.Options;
63 import org.jboss.logging.Logger;
64
65 import java.net.MalformedURLException JavaDoc;
66 import java.net.ServerSocket JavaDoc;
67 import java.net.Socket JavaDoc;
68 import java.util.Hashtable JavaDoc;
69
70 /**
71  * This is a simple implementation of an HTTP server for processing
72  * SOAP requests via Apache's xml-axis. This is not intended for production
73  * use. Its intended uses are for demos, debugging, and performance
74  * profiling.
75  *
76  * @author Sam Ruby (ruby@us.ibm.com)
77  * @author Rob Jellinghaus (robj@unrealities.com)
78  */

79 public class SimpleAxisServer implements Runnable JavaDoc
80 {
81    private static Logger log = Logger.getLogger(SimpleAxisServer.class.getName());
82
83    // session state.
84
// This table maps session keys (random numbers) to SimpleAxisSession objects.
85
//
86
// There is NO CLEANUP of this table at present, and if clients are not
87
// passing cookies, then a new session will be created for *every* request.
88
// This is the biggest impediment to any kind of real SimpleAxisServer use.
89
// So, if this becomes objectionable, we will implement some simpleminded
90
// cleanup (perhaps just a cap on max # of sessions, and some kind of LRU
91
// cleanup policy).
92
private Hashtable JavaDoc sessions = new Hashtable JavaDoc();
93
94    // Are we doing threads?
95
private static boolean doThreads = true;
96
97    // Are we doing sessions?
98
// Set this to false if you don't want any session overhead.
99
private static boolean doSessions = true;
100
101    protected boolean isSessionUsed()
102    {
103       return doSessions;
104    }
105
106    public void setDoThreads(boolean value)
107    {
108       doThreads = value;
109    }
110
111    public boolean getDoThreads()
112    {
113       return doThreads;
114    }
115
116    protected Session createSession(String JavaDoc cooky)
117    {
118       // is there a session already?
119
Session session = null;
120       if (sessions.containsKey(cooky))
121       {
122          session = (Session)sessions.get(cooky);
123       }
124       else
125       {
126          // no session for this cooky, bummer
127
session = new SimpleSession();
128
129          // ADD CLEANUP LOGIC HERE if needed
130
sessions.put(cooky, session);
131       }
132       return session;
133    }
134
135    // What is our current session index?
136
// This is a monotonically increasing, non-thread-safe integer
137
// (thread safety not considered crucial here)
138
public static int sessionIndex = 0;
139
140    // Axis server (shared between instances)
141
private static AxisServer myAxisServer = null;
142
143    protected static synchronized AxisServer getAxisServer()
144    {
145       if (myAxisServer == null)
146       {
147          myAxisServer = new AxisServer();
148       }
149       return myAxisServer;
150    }
151
152    // are we stopped?
153
// latch to true if stop() is called
154
private boolean stopped = false;
155
156    /**
157     * Accept requests from a given TCP port and send them through the
158     * Axis engine for processing.
159     */

160    public void run()
161    {
162       log.info(Messages.getMessage("start00", "SimpleAxisServer",
163               new Integer JavaDoc(getServerSocket().getLocalPort()).toString()));
164
165       // Accept and process requests from the socket
166
while (!stopped)
167       {
168          Socket JavaDoc socket = null;
169          try
170          {
171             socket = serverSocket.accept();
172          }
173          catch (java.io.InterruptedIOException JavaDoc iie)
174          {
175          }
176          catch (Exception JavaDoc e)
177          {
178             log.debug(Messages.getMessage("exception00"), e);
179             break;
180          }
181          if (socket != null)
182          {
183             SimpleAxisWorker worker = new SimpleAxisWorker(this, socket);
184             if (doThreads)
185             {
186                Thread JavaDoc thread = new Thread JavaDoc(worker);
187                thread.setDaemon(true);
188                thread.start();
189             }
190             else
191             {
192                worker.run();
193             }
194          }
195       }
196       log.info(Messages.getMessage("quit00", "SimpleAxisServer"));
197    }
198
199    // per thread socket information
200
private ServerSocket JavaDoc serverSocket;
201
202    /**
203     * Obtain the serverSocket that that SimpleAxisServer is listening on.
204     */

205    public ServerSocket JavaDoc getServerSocket()
206    {
207       return serverSocket;
208    }
209
210    /**
211     * Set the serverSocket this server should listen on.
212     * (note : changing this will not affect a running server, but if you
213     * stop() and then start() the server, the new socket will be used).
214     */

215    public void setServerSocket(ServerSocket JavaDoc serverSocket)
216    {
217       this.serverSocket = serverSocket;
218    }
219
220    /**
221     * Start this server.
222     * <p/>
223     * Spawns a worker thread to listen for HTTP requests.
224     *
225     * @param daemon a boolean indicating if the thread should be a daemon.
226     */

227    public void start(boolean daemon) throws Exception JavaDoc
228    {
229       if (doThreads)
230       {
231          Thread JavaDoc thread = new Thread JavaDoc(this);
232          thread.setDaemon(daemon);
233          thread.start();
234       }
235       else
236       {
237          run();
238       }
239    }
240
241    /**
242     * Start this server as a NON-daemon.
243     */

244    public void start() throws Exception JavaDoc
245    {
246       start(false);
247    }
248
249    /**
250     * Stop this server.
251     * <p/>
252     * This will interrupt any pending accept().
253     */

254    public void stop() throws Exception JavaDoc
255    {
256       /*
257        * Close the server socket cleanly, but avoid fresh accepts while
258        * the socket is closing.
259        */

260       stopped = true;
261       try
262       {
263          serverSocket.close();
264       }
265       catch (Exception JavaDoc e)
266       {
267          log.info(Messages.getMessage("exception00"), e);
268       }
269
270       log.info(Messages.getMessage("quit00", "SimpleAxisServer"));
271
272       // Kill the JVM, which will interrupt pending accepts even on linux.
273
System.exit(0);
274    }
275
276    /**
277     * Server process.
278     */

279    public static void main(String JavaDoc args[])
280    {
281
282       SimpleAxisServer sas = new SimpleAxisServer();
283
284       Options opts = null;
285       try
286       {
287          opts = new Options(args);
288       }
289       catch (MalformedURLException JavaDoc e)
290       {
291          log.error(Messages.getMessage("malformedURLException00"), e);
292          return;
293       }
294
295       try
296       {
297          doThreads = (opts.isFlagSet('t') > 0);
298
299          int port = opts.getPort();
300          ServerSocket JavaDoc ss = null;
301          // Try five times
302
for (int i = 0; i < 5; i++)
303          {
304             try
305             {
306                ss = new ServerSocket JavaDoc(port);
307                break;
308             }
309             catch (java.net.BindException JavaDoc be)
310             {
311                log.debug(Messages.getMessage("exception00"), be);
312                if (i < 4)
313                {
314                   // At 3 second intervals.
315
Thread.sleep(3000);
316                }
317                else
318                {
319                   throw new Exception JavaDoc(Messages.getMessage("unableToStartServer00",
320                           Integer.toString(port)));
321                }
322             }
323          }
324          sas.setServerSocket(ss);
325          sas.start();
326       }
327       catch (Exception JavaDoc e)
328       {
329          log.error(Messages.getMessage("exception00"), e);
330          return;
331       }
332
333    }
334 }
335
Popular Tags