Replaced ContextResponder with a more generic Destroyable interface.
[jsvc.git] / src / dolda / jsvc / ThreadContext.java
index 27d90bf..36b0121 100644 (file)
@@ -72,7 +72,10 @@ public class ThreadContext extends ThreadGroup {
                if(st.st == "killed")
                    logger.log(Level.WARNING, "Thread " + rt + " refused to die; killing again");
                if(now - st.lastkill > 5000) {
-                   rt.stop();
+                   if(forcelimit)
+                       rt.stop();
+                   else
+                       rt.interrupt();
                    st.st = "killed";
                    st.lastkill = now;
                } else {
@@ -115,8 +118,8 @@ public class ThreadContext extends ThreadGroup {
     }
     
     public void shutdown() {
-       if(root instanceof ContextResponder)
-           ((ContextResponder)root).destroy();
+       if(root instanceof Destroyable)
+           ((Destroyable)root).destroy();
        try {
            long last = 0;
            while(true) {
@@ -137,7 +140,7 @@ public class ThreadContext extends ThreadGroup {
     }
     
     public RequestThread respond(Request req) {
-       return(new RequestThread(root, req, workers, "Worker thread " + reqs++));
+       return(ctx.worker(root, req, workers, "Worker thread " + reqs++));
     }
     
     private Responder bootstrap(final Class<?> bootclass) {
@@ -190,4 +193,31 @@ public class ThreadContext extends ThreadGroup {
        }
        return(null);
     }
+    
+    public static class CreateException extends Exception {
+       public CreateException(String message) {
+           super(message);
+       }
+
+       public CreateException(String message, Throwable cause) {
+           super(message, cause);
+       }
+    }
+
+    public static ThreadContext create(ServerContext ctx, ClassLoader cl) throws CreateException {
+       String nm = "JSvc Service";
+       if(ctx.name() != null)
+           nm = "JSvc Service for " + ctx.name();
+       
+       String clnm = ctx.libconfig("jsvc.bootstrap", null);
+       if(clnm == null)
+           throw(new CreateException("No JSvc bootstrapper specified"));
+       Class<?> bc;
+       try {
+           bc = cl.loadClass(clnm);
+       } catch(ClassNotFoundException e) {
+           throw(new CreateException("Invalid JSvc bootstrapper specified", e));
+       }
+       return(new ThreadContext(null, nm, ctx, bc));
+    }
 }