Initially working J2EE container.
authorFredrik Tolf <fredrik@dolda2000.com>
Mon, 12 Oct 2009 17:12:37 +0000 (19:12 +0200)
committerFredrik Tolf <fredrik@dolda2000.com>
Mon, 12 Oct 2009 17:12:37 +0000 (19:12 +0200)
src/dolda/jsvc/ContextResponder.java [new file with mode: 0644]
src/dolda/jsvc/RequestThread.java [new file with mode: 0644]
src/dolda/jsvc/j2ee/Archive.java
src/dolda/jsvc/j2ee/J2eeRequest.java
src/dolda/jsvc/j2ee/Servlet.java

diff --git a/src/dolda/jsvc/ContextResponder.java b/src/dolda/jsvc/ContextResponder.java
new file mode 100644 (file)
index 0000000..1ff7b44
--- /dev/null
@@ -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 (file)
index 0000000..c6b9373
--- /dev/null
@@ -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);
+    }
+}
index 6b01967..c43a156 100644 (file)
@@ -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)));
index a5e7518..9676c46 100644 (file)
@@ -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))
index d050c01..4d0d3bd 100644 (file)
@@ -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;
+       }
     }
 }