| 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 | } |
| 52 | |
| 53 | public void destroy() { |
| 54 | workers.interrupt(); |
| 55 | if(root instanceof ContextResponder) |
| 56 | ((ContextResponder)root).destroy(); |
| 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 | } |
| 66 | long mynum = reqs++; |
| 67 | Request rr = new J2eeRequest(getServletConfig(), req, resp); |
| 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 | } |
| 76 | } |
| 77 | } |