Added a way to get the time of start of the server context.
[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 }
7114c38b
FT
51 ServletContext ctx = getServletContext();
52 ctx.setAttribute("jsvc.starttime", System.currentTimeMillis());
104fa785 53 }
78f5d120 54
104fa785
FT
55 public void destroy() {
56 workers.interrupt();
57 if(root instanceof ContextResponder)
58 ((ContextResponder)root).destroy();
78f5d120
FT
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 }
104fa785 68 long mynum = reqs++;
78f5d120 69 Request rr = new J2eeRequest(getServletConfig(), req, resp);
104fa785
FT
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 }
78f5d120
FT
78 }
79}