From 104fa78539da892fb3687325bde4759461d5eac4 Mon Sep 17 00:00:00 2001 From: Fredrik Tolf Date: Mon, 12 Oct 2009 19:12:37 +0200 Subject: [PATCH] Initially working J2EE container. --- src/dolda/jsvc/ContextResponder.java | 5 +++ src/dolda/jsvc/RequestThread.java | 20 ++++++++++++ src/dolda/jsvc/j2ee/Archive.java | 2 +- src/dolda/jsvc/j2ee/J2eeRequest.java | 6 ---- src/dolda/jsvc/j2ee/Servlet.java | 59 ++++++++++++++++++++++++++++++++++-- 5 files changed, 82 insertions(+), 10 deletions(-) create mode 100644 src/dolda/jsvc/ContextResponder.java create mode 100644 src/dolda/jsvc/RequestThread.java diff --git a/src/dolda/jsvc/ContextResponder.java b/src/dolda/jsvc/ContextResponder.java new file mode 100644 index 0000000..1ff7b44 --- /dev/null +++ b/src/dolda/jsvc/ContextResponder.java @@ -0,0 +1,5 @@ +package dolda.jsvc; + +public interface ContextResponder extends Responder { + public void destroy(); +} diff --git a/src/dolda/jsvc/RequestThread.java b/src/dolda/jsvc/RequestThread.java new file mode 100644 index 0000000..c6b9373 --- /dev/null +++ b/src/dolda/jsvc/RequestThread.java @@ -0,0 +1,20 @@ +package dolda.jsvc; + +public class RequestThread extends Thread { + private Request req; + private Responder resp; + + public RequestThread(Responder resp, Request req, ThreadGroup th, String name) { + super(th, name); + this.resp = resp; + this.req = req; + } + + public void run() { + resp.respond(req); + } + + public static Request request() { + return(((RequestThread)Thread.currentThread()).req); + } +} diff --git a/src/dolda/jsvc/j2ee/Archive.java b/src/dolda/jsvc/j2ee/Archive.java index 6b01967..c43a156 100644 --- a/src/dolda/jsvc/j2ee/Archive.java +++ b/src/dolda/jsvc/j2ee/Archive.java @@ -119,7 +119,7 @@ public class Archive { } public void addjars(String[] jars) throws IOException { - jarprops(jars, "/jsvc.properties"); + jarprops(jars, "jsvc.properties"); ZipOutputStream zip = zip(); for(String jar : jars) { zip.putNextEntry(new ZipEntry("WEB-INF/lib/" + basename(jar))); diff --git a/src/dolda/jsvc/j2ee/J2eeRequest.java b/src/dolda/jsvc/j2ee/J2eeRequest.java index a5e7518..9676c46 100644 --- a/src/dolda/jsvc/j2ee/J2eeRequest.java +++ b/src/dolda/jsvc/j2ee/J2eeRequest.java @@ -20,12 +20,6 @@ public class J2eeRequest extends ResponseBuffer { this.cfg = cfg; this.req = req; this.resp = resp; - try { - req.setCharacterEncoding("UTF-8"); - resp.setCharacterEncoding("UTF-8"); - } catch(UnsupportedEncodingException e) { - throw(new AssertionError(e)); - } { String host = req.getHeader("Host"); if((host == null) || (host.length() < 1)) diff --git a/src/dolda/jsvc/j2ee/Servlet.java b/src/dolda/jsvc/j2ee/Servlet.java index d050c01..4d0d3bd 100644 --- a/src/dolda/jsvc/j2ee/Servlet.java +++ b/src/dolda/jsvc/j2ee/Servlet.java @@ -1,14 +1,59 @@ package dolda.jsvc.j2ee; import dolda.jsvc.*; +import java.lang.reflect.*; +import java.util.*; import java.io.*; import javax.servlet.http.*; +import javax.servlet.*; public class Servlet extends HttpServlet { private Responder root; + private ThreadGroup workers; + private long reqs = 0; + + public void init() throws ServletException { + workers = new ThreadGroup("JSvc worker threads") { + public void uncaughtException(Thread t, Throwable e) { + log("Worker thread terminated with an uncaught exception", e); + } + }; + Properties sprop = new Properties(); + try { + InputStream pi = Servlet.class.getClassLoader().getResourceAsStream("jsvc.properties"); + try { + sprop.load(pi); + } finally { + pi.close(); + } + } catch(IOException e) { + throw(new Error(e)); + } + String clnm = (String)sprop.get("jsvc.bootstrap"); + if(clnm == null) + throw(new ServletException("No JSvc bootstrapper specified")); + try { + Class rc = Class.forName(clnm); + Method cm = rc.getMethod("responder"); + Object resp = cm.invoke(null); + if(!(resp instanceof Responder)) + throw(new ServletException("JSvc bootstrapper did not return a responder")); + root = (Responder)resp; + } catch(ClassNotFoundException e) { + throw(new ServletException("Invalid JSvc bootstrapper specified", e)); + } catch(NoSuchMethodException e) { + throw(new ServletException("Invalid JSvc bootstrapper specified", e)); + } catch(IllegalAccessException e) { + throw(new ServletException("Invalid JSvc bootstrapper specified", e)); + } catch(InvocationTargetException e) { + throw(new ServletException("JSvc bootstrapper failed", e)); + } + } - public void init() { - + public void destroy() { + workers.interrupt(); + if(root instanceof ContextResponder) + ((ContextResponder)root).destroy(); } public void service(HttpServletRequest req, HttpServletResponse resp) { @@ -18,7 +63,15 @@ public class Servlet extends HttpServlet { } catch(UnsupportedEncodingException e) { throw(new Error(e)); } + long mynum = reqs++; Request rr = new J2eeRequest(getServletConfig(), req, resp); - root.respond(rr); + RequestThread w = new RequestThread(root, rr, workers, "Worker thread " + mynum); + w.start(); + try { + w.join(); + } catch(InterruptedException e) { + w.interrupt(); + return; + } } } -- 2.11.0