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