KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > oreilly > servlet > DaemonHttpServlet


1 // Copyright (C) 1998-2001 by Jason Hunter <jhunter_AT_acm_DOT_org>.
2
// All rights reserved. Use of this class is limited.
3
// Please see the LICENSE for more information.
4

5 package com.oreilly.servlet;
6
7 import java.io.*;
8 import java.net.*;
9 import java.util.*;
10 import javax.servlet.*;
11 import javax.servlet.http.*;
12
13 /**
14  * A superclass for HTTP servlets that wish to accept raw socket
15  * connections. DaemonHttpServlet
16  * starts listening for client requests in its <tt>init()</tt> method
17  * and stops listening in its <tt>destroy()</tt> method. In between,
18  * for every connection it receives, it calls the abstract
19  * <tt>handleClient(Socket client)</tt> method. This method should
20  * be implemented by the servlet subclassing DaemonHttpServlet.
21  * The port on which the servlet is to listen is determined by the
22  * <tt>getSocketPort()</tt> method.
23  *
24  * @see com.oreilly.servlet.RemoteDaemonHttpServlet
25  *
26  * @author <b>Jason Hunter</b>, Copyright &#169; 1998
27  * @version 1.0, 98/09/18
28  */

29 public abstract class DaemonHttpServlet extends HttpServlet {
30
31   /**
32    * The default listening port (1313)
33    */

34   protected int DEFAULT_PORT = 1313;
35   private Thread JavaDoc daemonThread;
36
37   /**
38    * Begins a thread listening for socket connections. Subclasses
39    * that override this method must be sure to first call
40    * <tt>super.init(config)</tt>.
41    *
42    * @param config the servlet config
43    * @exception ServletException if a servlet exception occurs
44    */

45   public void init(ServletConfig config) throws ServletException {
46     super.init(config);
47
48     try {
49       daemonThread = new Daemon(this);
50       daemonThread.start();
51     }
52     catch (Exception JavaDoc e) {
53       log("Problem starting socket server daemon thread" +
54           e.getClass().getName() + ": " + e.getMessage());
55     }
56   }
57
58   /**
59    * Returns the socket port on which the servlet will listen.
60    * A servlet can change the port in three ways: by using the
61    * <tt>socketPort</tt> init parameter, by setting the <tt>DEFAULT_PORT</tt>
62    * variable before calling <tt>super.init()</tt>, or by overriding this
63    * method's implementation.
64    *
65    * @return the port number on which to listen
66    */

67   protected int getSocketPort() {
68     try { return Integer.parseInt(getInitParameter("socketPort")); }
69     catch (NumberFormatException JavaDoc e) { return DEFAULT_PORT; }
70   }
71
72   /**
73    * Handles a new socket connection. Subclasses must define this method.
74    *
75    * @param client the client socket
76    */

77   abstract public void handleClient(Socket client);
78
79   /**
80    * Halts the thread listening for socket connections. Subclasses
81    * that override this method must be sure to first call
82    * <tt>super.destroy()</tt>.
83    */

84   public void destroy() {
85     try {
86       daemonThread.stop();
87       daemonThread = null;
88     }
89     catch (Exception JavaDoc e) {
90       log("Problem stopping server socket daemon thread: " +
91           e.getClass().getName() + ": " + e.getMessage());
92     }
93   }
94 }
95
96 // This work is broken into a helper class so that subclasses of
97
// DaemonHttpServlet can define their own run() method without problems.
98

99 class Daemon extends Thread JavaDoc {
100
101   private ServerSocket serverSocket;
102   private DaemonHttpServlet servlet;
103
104   public Daemon(DaemonHttpServlet servlet) {
105     this.servlet = servlet;
106   }
107
108   public void run() {
109     try {
110       // Create a server socket to accept connections
111
serverSocket = new ServerSocket(servlet.getSocketPort());
112     }
113     catch (Exception JavaDoc e) {
114       servlet.log("Problem establishing server socket: " +
115                   e.getClass().getName() + ": " + e.getMessage());
116       return;
117     }
118
119     try {
120       while (true) {
121         // As each connection comes in, call the servlet's handleClient().
122
// Note this method is blocking. It's the servlet's responsibility
123
// to spawn a handler thread for long-running connections.
124
try {
125           servlet.handleClient(serverSocket.accept());
126         }
127         catch (IOException ioe) {
128           servlet.log("Problem accepting client's socket connection: " +
129                       ioe.getClass().getName() + ": " + ioe.getMessage());
130         }
131       }
132     }
133     catch (ThreadDeath JavaDoc e) {
134       // When the thread is killed, close the server socket
135
try {
136         serverSocket.close();
137       }
138       catch (IOException ioe) {
139         servlet.log("Problem closing server socket: " +
140                     ioe.getClass().getName() + ": " + ioe.getMessage());
141       }
142     }
143   }
144 }
145
Popular Tags