Added a convenient toString implementation for multimaps.
[jsvc.git] / src / dolda / jsvc / j2ee / Servlet.java
CommitLineData
78f5d120
FT
1package dolda.jsvc.j2ee;
2
3import dolda.jsvc.*;
104fa785
FT
4import java.lang.reflect.*;
5import java.util.*;
78f5d120
FT
6import java.io.*;
7import javax.servlet.http.*;
104fa785 8import javax.servlet.*;
78f5d120
FT
9
10public class Servlet extends HttpServlet {
11 private Responder root;
104fa785
FT
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 }
78f5d120 52
104fa785
FT
53 public void destroy() {
54 workers.interrupt();
55 if(root instanceof ContextResponder)
56 ((ContextResponder)root).destroy();
78f5d120
FT
57 }
58
59 public void service(HttpServletRequest req, HttpServletResponse resp) {
60 try {
61 req.setCharacterEncoding("UTF-8");
62 resp.setCharacterEncoding("UTF-8");
63 } catch(UnsupportedEncodingException e) {
64 throw(new Error(e));
65 }
104fa785 66 long mynum = reqs++;
78f5d120 67 Request rr = new J2eeRequest(getServletConfig(), req, resp);
104fa785
FT
68 RequestThread w = new RequestThread(root, rr, workers, "Worker thread " + mynum);
69 w.start();
70 try {
71 w.join();
72 } catch(InterruptedException e) {
73 w.interrupt();
74 return;
75 }
78f5d120
FT
76 }
77}