3 import dolda.jsvc.util.Misc;
4 import java.util.logging.*;
5 import java.lang.reflect.*;
8 public class ThreadContext extends ThreadGroup {
9 private Logger logger = Logger.getLogger("dolda.jsvc.context");
10 private ThreadGroup workers;
11 private long reqs = 0;
12 private final ServerContext ctx;
13 public final Responder root;
14 private int timelimit = 0;
15 private boolean forcelimit = false;
17 public ThreadContext(ThreadGroup parent, String name, ServerContext ctx, Class<?> bootclass) {
18 super((parent == null)?(Thread.currentThread().getThreadGroup()):parent, name);
20 workers = new ThreadGroup(this, "Worker threads") {
21 public void uncaughtException(Thread t, Throwable e) {
22 logger.log(Level.SEVERE, "Worker thread terminated with an uncaught exception", e);
27 tl = Integer.parseInt(ctx.sysconfig("jsvc.timelimit", "0"));
28 if((tl > 0) && ((timelimit == 0) || (tl < timelimit)))
30 tl = Integer.parseInt(ctx.libconfig("jsvc.timelimit", "0"));
31 if((tl > 0) && ((timelimit == 0) || (tl < timelimit)))
33 forcelimit |= Misc.boolval(ctx.sysconfig("jsvc.forcelimit", "0"));
34 forcelimit |= Misc.boolval(ctx.libconfig("jsvc.forcelimit", "0"));
36 root = bootstrap(bootclass);
39 (new WatchDog()).start();
42 private class WatchDog extends Thread {
43 private Map<RequestThread, State> state = new WeakHashMap<RequestThread, State>();
46 String st = "running";
51 super(ThreadContext.this, "Worker watchdog");
55 @SuppressWarnings("deprecation")
56 private long ckthread(long now, RequestThread rt) {
57 State st = state.get(rt);
62 if(st.st == "running") {
63 if(now - rt.stime() > timelimit) {
65 st.st = "interrupted";
69 return(timelimit - (now - rt.stime()));
71 } else if((st.st == "interrupted") || (st.st == "killed")) {
73 logger.log(Level.WARNING, "Thread " + rt + " refused to die; killing again");
74 if(now - st.lastkill > 5000) {
82 return(5000 - (now - st.lastkill));
91 long next = timelimit;
92 long now = System.currentTimeMillis();
93 Thread[] w = new Thread[workers.activeCount() + 5];
94 int num = workers.enumerate(w);
95 for(int i = 0; i < num; i++) {
96 if(w[i] instanceof RequestThread){
97 RequestThread rt = (RequestThread)w[i];
99 long n = ckthread(now, rt);
107 } catch(InterruptedException e) {
112 public void uncaughtException(Thread t, Throwable e) {
113 logger.log(Level.SEVERE, "Service thread " + t.toString() + " terminated with an uncaught exception", e);
116 public ServerContext server() {
120 public void shutdown() {
121 if(root instanceof ContextResponder)
122 ((ContextResponder)root).destroy();
126 long now = System.currentTimeMillis();
127 if(now - last > 10000) {
131 Thread[] th = new Thread[1];
132 if(enumerate(th) < 1)
136 } catch(InterruptedException e) {
137 logger.log(Level.WARNING, "Interrupted while trying to shut down all service threads. Some may remain.", e);
142 public RequestThread respond(Request req) {
143 return(ctx.worker(root, req, workers, "Worker thread " + reqs++));
146 private Responder bootstrap(final Class<?> bootclass) {
147 final Throwable[] err = new Throwable[1];
148 final Responder[] res = new Responder[1];
149 Thread boot = new Thread(this, "JSvc boot thread") {
152 Method cm = bootclass.getMethod("responder");
153 Object resp = cm.invoke(null);
154 if(!(resp instanceof Responder))
155 throw(new ClassCastException("JSvc bootstrapper did not return a responder"));
156 res[0] = (Responder)resp;
157 } catch(NoSuchMethodException e) {
158 logger.log(Level.SEVERE, "Invalid JSvc bootstrapper specified", e);
160 } catch(IllegalAccessException e) {
161 logger.log(Level.SEVERE, "Invalid JSvc bootstrapper specified", e);
163 } catch(InvocationTargetException e) {
164 logger.log(Level.SEVERE, "JSvc bootstrapper failed", e);
172 } catch(InterruptedException e) {
173 logger.log(Level.WARNING, "Interrupted during bootstrapping", e);
175 Thread.currentThread().interrupt();
179 throw(new RuntimeException(err[0]));
183 logger.log(Level.SEVERE, "No responder returned in spite of no error having happened.");
184 throw(new NullPointerException("No responder returned in spite of no error having happened."));
189 public static ThreadContext current() {
190 for(ThreadGroup tg = Thread.currentThread().getThreadGroup(); tg != null; tg = tg.getParent()) {
191 if(tg instanceof ThreadContext)
192 return((ThreadContext)tg);
197 public static class CreateException extends Exception {
198 public CreateException(String message) {
202 public CreateException(String message, Throwable cause) {
203 super(message, cause);
207 public static ThreadContext create(ServerContext ctx, ClassLoader cl) throws CreateException {
208 String nm = "JSvc Service";
209 if(ctx.name() != null)
210 nm = "JSvc Service for " + ctx.name();
212 String clnm = ctx.libconfig("jsvc.bootstrap", null);
214 throw(new CreateException("No JSvc bootstrapper specified"));
217 bc = cl.loadClass(clnm);
218 } catch(ClassNotFoundException e) {
219 throw(new CreateException("Invalid JSvc bootstrapper specified", e));
221 return(new ThreadContext(null, nm, ctx, bc));