From 4b8346e1aad3c58196f8d4dadf9da6726f66f5f4 Mon Sep 17 00:00:00 2001 From: Fredrik Tolf Date: Thu, 15 Oct 2009 05:33:56 +0200 Subject: [PATCH] Made the server context a more useful concept. --- src/dolda/jsvc/ContextParam.java | 12 ++---------- src/dolda/jsvc/Request.java | 1 + src/dolda/jsvc/RequestThread.java | 4 ---- src/dolda/jsvc/RequestWrap.java | 1 + src/dolda/jsvc/ServerContext.java | 1 - src/dolda/jsvc/ThreadContext.java | 16 +++++++++++++++- src/dolda/jsvc/j2ee/J2eeContext.java | 14 ++++---------- src/dolda/jsvc/j2ee/J2eeRequest.java | 15 ++++++++++----- src/dolda/jsvc/j2ee/Servlet.java | 6 ++---- src/dolda/jsvc/util/Restarts.java | 5 ++--- 10 files changed, 37 insertions(+), 38 deletions(-) diff --git a/src/dolda/jsvc/ContextParam.java b/src/dolda/jsvc/ContextParam.java index f146dc3..ebdd9a7 100644 --- a/src/dolda/jsvc/ContextParam.java +++ b/src/dolda/jsvc/ContextParam.java @@ -22,7 +22,7 @@ public class ContextParam { Thread th = Thread.currentThread(); if(perthr.containsKey(th)) return(perthr.get(th)); - ThreadContext ctx = getctx(); + ThreadContext ctx = ThreadContext.current(); if(perctx.containsKey(ctx)) return(perctx.get(ctx)); if(!bound) @@ -31,18 +31,10 @@ public class ContextParam { } public synchronized T ctxset(T val) { - ThreadContext ctx = getctx(); + ThreadContext ctx = ThreadContext.current(); return(perctx.put(ctx, val)); } - private static ThreadContext getctx() { - for(ThreadGroup tg = Thread.currentThread().getThreadGroup(); tg != null; tg = tg.getParent()) { - if(tg instanceof ThreadContext) - return((ThreadContext)tg); - } - return(null); - } - public static Responder let(final Responder next, Object... params) { final Map values = new HashMap(); if((params.length % 2) != 0) diff --git a/src/dolda/jsvc/Request.java b/src/dolda/jsvc/Request.java index 8de008a..c0210e6 100644 --- a/src/dolda/jsvc/Request.java +++ b/src/dolda/jsvc/Request.java @@ -8,6 +8,7 @@ import java.util.Map; public interface Request { /* Input */ public URL url(); + public URL rooturl(); public String method(); public String path(); public InputStream input(); diff --git a/src/dolda/jsvc/RequestThread.java b/src/dolda/jsvc/RequestThread.java index 51f9d86..7940748 100644 --- a/src/dolda/jsvc/RequestThread.java +++ b/src/dolda/jsvc/RequestThread.java @@ -19,10 +19,6 @@ public class RequestThread extends Thread { } } - public static ServerContext context() { - return(((RequestThread)Thread.currentThread()).req.ctx()); - } - public static Request request() { return(((RequestThread)Thread.currentThread()).req); } diff --git a/src/dolda/jsvc/RequestWrap.java b/src/dolda/jsvc/RequestWrap.java index 1314c5d..d024622 100644 --- a/src/dolda/jsvc/RequestWrap.java +++ b/src/dolda/jsvc/RequestWrap.java @@ -13,6 +13,7 @@ public class RequestWrap implements Request { } public URL url() {return(bk.url());} + public URL rooturl() {return(bk.rooturl());} public String method() {return(bk.method());} public String path() {return(bk.path());} public InputStream input() {return(bk.input());} diff --git a/src/dolda/jsvc/ServerContext.java b/src/dolda/jsvc/ServerContext.java index c2d539b..a47a714 100644 --- a/src/dolda/jsvc/ServerContext.java +++ b/src/dolda/jsvc/ServerContext.java @@ -1,6 +1,5 @@ package dolda.jsvc; public interface ServerContext { - public String rootpath(); public long starttime(); } diff --git a/src/dolda/jsvc/ThreadContext.java b/src/dolda/jsvc/ThreadContext.java index 78a5d4e..2d9931e 100644 --- a/src/dolda/jsvc/ThreadContext.java +++ b/src/dolda/jsvc/ThreadContext.java @@ -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); + } } diff --git a/src/dolda/jsvc/j2ee/J2eeContext.java b/src/dolda/jsvc/j2ee/J2eeContext.java index 696e32b..5409b8c 100644 --- a/src/dolda/jsvc/j2ee/J2eeContext.java +++ b/src/dolda/jsvc/j2ee/J2eeContext.java @@ -7,20 +7,14 @@ import javax.servlet.http.*; public class J2eeContext implements ServerContext { private ServletConfig cfg; - private HttpServletRequest req; - private HttpServletResponse resp; + private long ctime; - J2eeContext(ServletConfig cfg, HttpServletRequest req, HttpServletResponse resp) { + J2eeContext(ServletConfig cfg) { this.cfg = cfg; - this.req = req; - this.resp = resp; - } - - public String rootpath() { - return("/" + Misc.stripslashes(req.getContextPath(), true, true)); + this.ctime = System.currentTimeMillis(); } public long starttime() { - return((Long)cfg.getServletContext().getAttribute("jsvc.starttime")); + return(ctime); } } diff --git a/src/dolda/jsvc/j2ee/J2eeRequest.java b/src/dolda/jsvc/j2ee/J2eeRequest.java index a014507..574355f 100644 --- a/src/dolda/jsvc/j2ee/J2eeRequest.java +++ b/src/dolda/jsvc/j2ee/J2eeRequest.java @@ -13,7 +13,7 @@ public class J2eeRequest extends ResponseBuffer { private HttpServletRequest req; private HttpServletResponse resp; private String method, path; - private URL url; + private URL url, context; private MultiMap params = null; private Map props = new HashMap(); @@ -60,6 +60,7 @@ public class J2eeRequest extends ResponseBuffer { q = ""; try { url = new URL(scheme, host, port, req.getContextPath() + req.getServletPath() + pi + q); + context = new URL(scheme, host, port, req.getContextPath()); } catch(MalformedURLException e) { throw(new Error(e)); } @@ -74,10 +75,6 @@ public class J2eeRequest extends ResponseBuffer { return(props); } - public ServerContext ctx() { - return(new J2eeContext(cfg, req, resp)); - } - public SocketAddress remoteaddr() { try { return(new InetSocketAddress(InetAddress.getByName(req.getRemoteAddr()), req.getRemotePort())); @@ -102,6 +99,14 @@ public class J2eeRequest extends ResponseBuffer { return(url); } + public URL rooturl() { + return(context); + } + + public ServerContext ctx() { + return(ThreadContext.current().server()); + } + public String method() { return(method); } diff --git a/src/dolda/jsvc/j2ee/Servlet.java b/src/dolda/jsvc/j2ee/Servlet.java index 4372652..14ace40 100644 --- a/src/dolda/jsvc/j2ee/Servlet.java +++ b/src/dolda/jsvc/j2ee/Servlet.java @@ -10,7 +10,7 @@ import javax.servlet.*; public class Servlet extends HttpServlet { private ThreadContext tg; - public void init() throws ServletException { + public void init(ServletConfig cfg) throws ServletException { Properties sprop = new Properties(); try { InputStream pi = Servlet.class.getClassLoader().getResourceAsStream("jsvc.properties"); @@ -31,9 +31,7 @@ public class Servlet extends HttpServlet { } catch(ClassNotFoundException e) { throw(new ServletException("Invalid JSvc bootstrapper specified", e)); } - tg = new ThreadContext(null, "JSvc service", bc); - ServletContext ctx = getServletContext(); - ctx.setAttribute("jsvc.starttime", System.currentTimeMillis()); + tg = new ThreadContext(null, "JSvc service", new J2eeContext(cfg), bc); } public void destroy() { diff --git a/src/dolda/jsvc/util/Restarts.java b/src/dolda/jsvc/util/Restarts.java index d4aab70..06ab682 100644 --- a/src/dolda/jsvc/util/Restarts.java +++ b/src/dolda/jsvc/util/Restarts.java @@ -34,11 +34,10 @@ public class Restarts { public void respond(Request req) { req.status(303); URL url; - String rel = req.ctx().rootpath() + "/" + Misc.stripslashes(path, true, false); try { - url = new URL(req.url(), rel); + url = new URL(req.rooturl(), Misc.stripslashes(path, true, false)); } catch(MalformedURLException e) { - throw(new RuntimeException("Bad relative URL: + " + rel, e)); + throw(new RuntimeException("Bad relative URL: + " + path, e)); } req.outheaders().put("Location", url.toString()); } -- 2.11.0