165b0f2e2cfe489515da86777d8430b85e4749c0
[jsvc.git] / src / dolda / jsvc / j2ee / Servlet.java
1 package dolda.jsvc.j2ee;
2
3 import dolda.jsvc.*;
4 import java.lang.reflect.*;
5 import java.util.*;
6 import java.io.*;
7 import javax.servlet.http.*;
8 import javax.servlet.*;
9
10 public class Servlet extends HttpServlet {
11     private Responder root;
12     private ThreadGroup workers;
13     private long reqs = 0;
14
15     public void init() throws ServletException {
16         workers = new ThreadGroup("JSvc worker threads") {
17                 public void uncaughtException(Thread t, Throwable e) {
18                     log("Worker thread terminated with an uncaught exception", e);
19                 }
20             };
21         Properties sprop = new Properties();
22         try {
23             InputStream pi = Servlet.class.getClassLoader().getResourceAsStream("jsvc.properties");
24             try {
25                 sprop.load(pi);
26             } finally {
27                 pi.close();
28             }
29         } catch(IOException e) {
30             throw(new Error(e));
31         }
32         String clnm = (String)sprop.get("jsvc.bootstrap");
33         if(clnm == null)
34             throw(new ServletException("No JSvc bootstrapper specified"));
35         try {
36             Class<?> rc = Class.forName(clnm);
37             Method cm = rc.getMethod("responder");
38             Object resp = cm.invoke(null);
39             if(!(resp instanceof Responder))
40                 throw(new ServletException("JSvc bootstrapper did not return a responder"));
41             root = (Responder)resp;
42         } catch(ClassNotFoundException e) {
43             throw(new ServletException("Invalid JSvc bootstrapper specified", e));
44         } catch(NoSuchMethodException e) {
45             throw(new ServletException("Invalid JSvc bootstrapper specified", e));
46         } catch(IllegalAccessException e) {
47             throw(new ServletException("Invalid JSvc bootstrapper specified", e));
48         } catch(InvocationTargetException e) {
49             throw(new ServletException("JSvc bootstrapper failed", e));
50         }
51         ServletContext ctx = getServletContext();
52         ctx.setAttribute("jsvc.starttime", System.currentTimeMillis());
53     }
54     
55     public void destroy() {
56         workers.interrupt();
57         if(root instanceof ContextResponder)
58             ((ContextResponder)root).destroy();
59     }
60     
61     public void service(HttpServletRequest req, HttpServletResponse resp) {
62         try {
63             req.setCharacterEncoding("UTF-8");
64             resp.setCharacterEncoding("UTF-8");
65         } catch(UnsupportedEncodingException e) {
66             throw(new Error(e));
67         }
68         long mynum = reqs++;
69         Request rr = new J2eeRequest(getServletConfig(), req, resp);
70         RequestThread w = new RequestThread(root, rr, workers, "Worker thread " + mynum);
71         w.start();
72         try {
73             w.join();
74         } catch(InterruptedException e) {
75             w.interrupt();
76             return;
77         }
78     }
79 }