Improved the configuration structure.
[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 ThreadContext tg;
12
13     public void init(ServletConfig cfg) throws ServletException {
14         J2eeContext ctx = J2eeContext.create(cfg);
15         try {
16             InputStream pi = Servlet.class.getClassLoader().getResourceAsStream("jsvc.properties");
17             try {
18                 ctx.loadconfig(pi);
19             } finally {
20                 pi.close();
21             }
22         } catch(IOException e) {
23             throw(new Error(e));
24         }
25         String clnm = ctx.libconfig("jsvc.bootstrap", null);
26         if(clnm == null)
27             throw(new ServletException("No JSvc bootstrapper specified"));
28         Class<?> bc;
29         try {
30             bc = Class.forName(clnm);
31         } catch(ClassNotFoundException e) {
32             throw(new ServletException("Invalid JSvc bootstrapper specified", e));
33         }
34         String tgn;
35         if(ctx.name() != null)
36             tgn = "JSvc service for " + ctx.name();
37         else
38             tgn = "JSvc service";
39         tg = new ThreadContext(null, tgn, ctx, bc);
40     }
41     
42     public void destroy() {
43         tg.shutdown();
44     }
45     
46     public void service(HttpServletRequest req, HttpServletResponse resp) {
47         try {
48             req.setCharacterEncoding("UTF-8");
49             resp.setCharacterEncoding("UTF-8");
50         } catch(UnsupportedEncodingException e) {
51             throw(new Error(e));
52         }
53         Request rr = new J2eeRequest(getServletConfig(), req, resp);
54         RequestThread w = tg.respond(rr);
55         w.start();
56         try {
57             w.join();
58         } catch(InterruptedException e) {
59             w.interrupt();
60             return;
61         }
62     }
63 }