Dolda2000 GitWeb
/
jsvc.git
/ commitdiff
commit
grep
author
committer
pickaxe
?
search:
re
summary
|
shortlog
|
log
|
commit
| commitdiff |
tree
raw
|
patch
|
inline
| side by side (from parent 1:
d7bc1a7
)
Made the server context a more useful concept.
author
Fredrik Tolf
<fredrik@dolda2000.com>
Thu, 15 Oct 2009 03:33:56 +0000
(
05:33
+0200)
committer
Fredrik Tolf
<fredrik@dolda2000.com>
Thu, 15 Oct 2009 03:37:10 +0000
(
05:37
+0200)
src/dolda/jsvc/ContextParam.java
patch
|
blob
|
blame
|
history
src/dolda/jsvc/Request.java
patch
|
blob
|
blame
|
history
src/dolda/jsvc/RequestThread.java
patch
|
blob
|
blame
|
history
src/dolda/jsvc/RequestWrap.java
patch
|
blob
|
blame
|
history
src/dolda/jsvc/ServerContext.java
patch
|
blob
|
blame
|
history
src/dolda/jsvc/ThreadContext.java
patch
|
blob
|
blame
|
history
src/dolda/jsvc/j2ee/J2eeContext.java
patch
|
blob
|
blame
|
history
src/dolda/jsvc/j2ee/J2eeRequest.java
patch
|
blob
|
blame
|
history
src/dolda/jsvc/j2ee/Servlet.java
patch
|
blob
|
blame
|
history
src/dolda/jsvc/util/Restarts.java
patch
|
blob
|
blame
|
history
diff --git
a/src/dolda/jsvc/ContextParam.java
b/src/dolda/jsvc/ContextParam.java
index
f146dc3
..
ebdd9a7
100644
(file)
--- a/
src/dolda/jsvc/ContextParam.java
+++ b/
src/dolda/jsvc/ContextParam.java
@@
-22,7
+22,7
@@
public class ContextParam<T> {
Thread th = Thread.currentThread();
if(perthr.containsKey(th))
return(perthr.get(th));
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)
if(perctx.containsKey(ctx))
return(perctx.get(ctx));
if(!bound)
@@
-31,18
+31,10
@@
public class ContextParam<T> {
}
public synchronized T ctxset(T val) {
}
public synchronized T ctxset(T val) {
- ThreadContext ctx =
getctx
();
+ ThreadContext ctx =
ThreadContext.current
();
return(perctx.put(ctx, val));
}
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<ContextParam, Object> values = new HashMap<ContextParam, Object>();
if((params.length % 2) != 0)
public static Responder let(final Responder next, Object... params) {
final Map<ContextParam, Object> values = new HashMap<ContextParam, Object>();
if((params.length % 2) != 0)
diff --git
a/src/dolda/jsvc/Request.java
b/src/dolda/jsvc/Request.java
index
8de008a
..
c0210e6
100644
(file)
--- 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 interface Request {
/* Input */
public URL url();
+ public URL rooturl();
public String method();
public String path();
public InputStream input();
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
(file)
--- 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);
}
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
(file)
--- 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 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());}
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
(file)
--- a/
src/dolda/jsvc/ServerContext.java
+++ b/
src/dolda/jsvc/ServerContext.java
@@
-1,6
+1,5
@@
package dolda.jsvc;
public interface ServerContext {
package dolda.jsvc;
public interface ServerContext {
- public String rootpath();
public long starttime();
}
public long starttime();
}
diff --git
a/src/dolda/jsvc/ThreadContext.java
b/src/dolda/jsvc/ThreadContext.java
index
78a5d4e
..
2d9931e
100644
(file)
--- 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 Logger logger = Logger.getLogger("dolda.jsvc.context");
private ThreadGroup workers;
private long reqs = 0;
+ private final ServerContext ctx;
public final Responder root;
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);
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);
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);
}
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();
public void shutdown() {
if(root instanceof ContextResponder)
((ContextResponder)root).destroy();
@@
-88,4
+94,12
@@
public class ThreadContext extends ThreadGroup {
}
return(res[0]);
}
}
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
(file)
--- 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;
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.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() {
}
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
(file)
--- 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 HttpServletRequest req;
private HttpServletResponse resp;
private String method, path;
- private URL url;
+ private URL url
, context
;
private MultiMap<String, String> params = null;
private Map<Object, Object> props = new HashMap<Object, Object>();
private MultiMap<String, String> params = null;
private Map<Object, Object> props = new HashMap<Object, Object>();
@@
-60,6
+60,7
@@
public class J2eeRequest extends ResponseBuffer {
q = "";
try {
url = new URL(scheme, host, port, req.getContextPath() + req.getServletPath() + pi + q);
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));
}
} catch(MalformedURLException e) {
throw(new Error(e));
}
@@
-74,10
+75,6
@@
public class J2eeRequest extends ResponseBuffer {
return(props);
}
return(props);
}
- public ServerContext ctx() {
- return(new J2eeContext(cfg, req, resp));
- }
-
public SocketAddress remoteaddr() {
try {
return(new InetSocketAddress(InetAddress.getByName(req.getRemoteAddr()), req.getRemotePort()));
public SocketAddress remoteaddr() {
try {
return(new InetSocketAddress(InetAddress.getByName(req.getRemoteAddr()), req.getRemotePort()));
@@
-102,6
+99,14
@@
public class J2eeRequest extends ResponseBuffer {
return(url);
}
return(url);
}
+ public URL rooturl() {
+ return(context);
+ }
+
+ public ServerContext ctx() {
+ return(ThreadContext.current().server());
+ }
+
public String method() {
return(method);
}
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
(file)
--- 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 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");
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));
}
} 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() {
}
public void destroy() {
diff --git
a/src/dolda/jsvc/util/Restarts.java
b/src/dolda/jsvc/util/Restarts.java
index
d4aab70
..
06ab682
100644
(file)
--- 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;
public void respond(Request req) {
req.status(303);
URL url;
- String rel = req.ctx().rootpath() + "/" + Misc.stripslashes(path, true, false);
try {
try {
- url = new URL(req.
url(), rel
);
+ url = new URL(req.
rooturl(), Misc.stripslashes(path, true, false)
);
} catch(MalformedURLException e) {
} 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());
}
}
req.outheaders().put("Location", url.toString());
}