Made the server context a more useful concept.
[jsvc.git] / src / dolda / jsvc / ThreadContext.java
index 78a5d4e..2d9931e 100644 (file)
@@ -7,10 +7,12 @@ public class ThreadContext extends ThreadGroup {
     private Logger logger = Logger.getLogger("dolda.jsvc.context");
     private ThreadGroup workers;
     private long reqs = 0;
+    private final ServerContext ctx;
     public final Responder root;
     
-    public ThreadContext(ThreadGroup parent, String name, Class<?> bootclass) {
+    public ThreadContext(ThreadGroup parent, String name, ServerContext ctx, Class<?> bootclass) {
        super((parent == null)?(Thread.currentThread().getThreadGroup()):parent, name);
+       this.ctx = ctx;
        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);
@@ -23,6 +25,10 @@ public class ThreadContext extends ThreadGroup {
        logger.log(Level.SEVERE, "Service thread " + t.toString() + " terminated with an uncaught exception", e);
     }
     
+    public ServerContext server() {
+       return(ctx);
+    }
+    
     public void shutdown() {
        if(root instanceof ContextResponder)
            ((ContextResponder)root).destroy();
@@ -88,4 +94,12 @@ public class ThreadContext extends ThreadGroup {
        }
        return(res[0]);
     }
+
+    public static ThreadContext current() {
+       for(ThreadGroup tg = Thread.currentThread().getThreadGroup(); tg != null; tg = tg.getParent()) {
+           if(tg instanceof ThreadContext)
+               return((ThreadContext)tg);
+       }
+       return(null);
+    }
 }