Made thread management more predictable.
[jsvc.git] / src / dolda / jsvc / ThreadContext.java
CommitLineData
c9837b5e
FT
1package dolda.jsvc;
2
3import java.util.logging.*;
4import java.lang.reflect.*;
5
6public class ThreadContext extends ThreadGroup {
7 private Logger logger = Logger.getLogger("dolda.jsvc.context");
8 private ThreadGroup workers;
9 private long reqs = 0;
10 public final Responder root;
11
12 public ThreadContext(ThreadGroup parent, String name, Class<?> bootclass) {
13 super((parent == null)?(Thread.currentThread().getThreadGroup()):parent, name);
14 workers = new ThreadGroup(this, "Worker threads") {
15 public void uncaughtException(Thread t, Throwable e) {
16 logger.log(Level.SEVERE, "Worker thread terminated with an uncaught exception", e);
17 }
18 };
19 root = bootstrap(bootclass);
20 }
21
22 public void uncaughtException(Thread t, Throwable e) {
23 logger.log(Level.SEVERE, "Service thread " + t.toString() + " terminated with an uncaught exception", e);
24 }
25
26 public void shutdown() {
27 interrupt();
28 if(root instanceof ContextResponder)
29 ((ContextResponder)root).destroy();
30 }
31
32 public RequestThread respond(Request req) {
33 return(new RequestThread(root, req, workers, "Worker thread " + reqs++));
34 }
35
36 private Responder bootstrap(final Class<?> bootclass) {
37 final Throwable[] err = new Throwable[1];
38 final Responder[] res = new Responder[1];
39 Thread boot = new Thread(this, "JSvc boot thread") {
40 public void run() {
41 try {
42 Method cm = bootclass.getMethod("responder");
43 Object resp = cm.invoke(null);
44 if(!(resp instanceof Responder))
45 throw(new ClassCastException("JSvc bootstrapper did not return a responder"));
46 res[0] = (Responder)resp;
47 } catch(NoSuchMethodException e) {
48 logger.log(Level.SEVERE, "Invalid JSvc bootstrapper specified", e);
49 err[0] = e;
50 } catch(IllegalAccessException e) {
51 logger.log(Level.SEVERE, "Invalid JSvc bootstrapper specified", e);
52 err[0] = e;
53 } catch(InvocationTargetException e) {
54 logger.log(Level.SEVERE, "JSvc bootstrapper failed", e);
55 err[0] = e;
56 }
57 }
58 };
59 boot.start();
60 try {
61 boot.join();
62 } catch(InterruptedException e) {
63 logger.log(Level.WARNING, "Interrupted during bootstrapping", e);
64 boot.interrupt();
65 Thread.currentThread().interrupt();
66 }
67 if(err[0] != null)
68 throw(new RuntimeException(err[0]));
69 if(res[0] == null) {
70 logger.log(Level.SEVERE, "No responder returned in spite of no error having happened.");
71 throw(new NullPointerException("No responder returned in spite of no error having happened."));
72 }
73 return(res[0]);
74 }
75}