--- /dev/null
+package dolda.jsvc;
+
+import java.util.logging.*;
+import java.lang.reflect.*;
+
+public class ThreadContext extends ThreadGroup {
+ private Logger logger = Logger.getLogger("dolda.jsvc.context");
+ private ThreadGroup workers;
+ private long reqs = 0;
+ public final Responder root;
+
+ public ThreadContext(ThreadGroup parent, String name, Class<?> bootclass) {
+ super((parent == null)?(Thread.currentThread().getThreadGroup()):parent, name);
+ workers = new ThreadGroup(this, "Worker threads") {
+ public void uncaughtException(Thread t, Throwable e) {
+ logger.log(Level.SEVERE, "Worker thread terminated with an uncaught exception", e);
+ }
+ };
+ root = bootstrap(bootclass);
+ }
+
+ public void uncaughtException(Thread t, Throwable e) {
+ logger.log(Level.SEVERE, "Service thread " + t.toString() + " terminated with an uncaught exception", e);
+ }
+
+ public void shutdown() {
+ interrupt();
+ if(root instanceof ContextResponder)
+ ((ContextResponder)root).destroy();
+ }
+
+ public RequestThread respond(Request req) {
+ return(new RequestThread(root, req, workers, "Worker thread " + reqs++));
+ }
+
+ private Responder bootstrap(final Class<?> bootclass) {
+ final Throwable[] err = new Throwable[1];
+ final Responder[] res = new Responder[1];
+ Thread boot = new Thread(this, "JSvc boot thread") {
+ public void run() {
+ try {
+ Method cm = bootclass.getMethod("responder");
+ Object resp = cm.invoke(null);
+ if(!(resp instanceof Responder))
+ throw(new ClassCastException("JSvc bootstrapper did not return a responder"));
+ res[0] = (Responder)resp;
+ } catch(NoSuchMethodException e) {
+ logger.log(Level.SEVERE, "Invalid JSvc bootstrapper specified", e);
+ err[0] = e;
+ } catch(IllegalAccessException e) {
+ logger.log(Level.SEVERE, "Invalid JSvc bootstrapper specified", e);
+ err[0] = e;
+ } catch(InvocationTargetException e) {
+ logger.log(Level.SEVERE, "JSvc bootstrapper failed", e);
+ err[0] = e;
+ }
+ }
+ };
+ boot.start();
+ try {
+ boot.join();
+ } catch(InterruptedException e) {
+ logger.log(Level.WARNING, "Interrupted during bootstrapping", e);
+ boot.interrupt();
+ Thread.currentThread().interrupt();
+ }
+ if(err[0] != null)
+ throw(new RuntimeException(err[0]));
+ if(res[0] == null) {
+ logger.log(Level.SEVERE, "No responder returned in spite of no error having happened.");
+ throw(new NullPointerException("No responder returned in spite of no error having happened."));
+ }
+ return(res[0]);
+ }
+}
import javax.servlet.*;
public class Servlet extends HttpServlet {
- private Responder root;
- private ThreadGroup workers;
- private long reqs = 0;
+ private ThreadContext tg;
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");
String clnm = (String)sprop.get("jsvc.bootstrap");
if(clnm == null)
throw(new ServletException("No JSvc bootstrapper specified"));
+ Class<?> bc;
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;
+ bc = Class.forName(clnm);
} 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));
}
+ tg = new ThreadContext(null, "JSvc service", bc);
ServletContext ctx = getServletContext();
ctx.setAttribute("jsvc.starttime", System.currentTimeMillis());
}
public void destroy() {
- workers.interrupt();
- if(root instanceof ContextResponder)
- ((ContextResponder)root).destroy();
+ tg.shutdown();
}
public void service(HttpServletRequest req, HttpServletResponse resp) {
} catch(UnsupportedEncodingException e) {
throw(new Error(e));
}
- long mynum = reqs++;
Request rr = new J2eeRequest(getServletConfig(), req, resp);
- RequestThread w = new RequestThread(root, rr, workers, "Worker thread " + mynum);
+ RequestThread w = tg.respond(rr);
w.start();
try {
w.join();