01: /*
02: * Created on Aug 1, 2005
03: */
04: package uk.org.ponder.servletutil;
05:
06: import javax.servlet.ServletContext;
07: import javax.servlet.http.HttpServletRequest;
08: import javax.servlet.http.HttpServletResponse;
09:
10: /**
11: * @author Antranig Basman (antranig@caret.cam.ac.uk)
12: *
13: */
14: public class ServletState {
15: // Inclusion of request and response as thread-local state is a potentially
16: // dubious design - this was primarily motivated by being able to abstract
17: // the action of issuing a redirect, which for JSF is performed on ExternalContext,
18: // for plain servlets on HSResponse, neither of which should be something known
19: // to issuing code. This should be protected by appropriately restricting
20: // knowledge level of this package. This is potentially doubly dubious since
21: // HSR has STATE. In general do not use this state if at all possible,
22: // preferentially
23: public ServletContext context;
24: public HttpServletRequest request;
25: public HttpServletResponse response;
26:
27: public void clear() {
28: context = null;
29: request = null;
30: response = null;
31: }
32:
33: private static ThreadLocal contextstash = new ThreadLocal() {
34: public Object initialValue() {
35: return new ServletState();
36: }
37: };
38:
39: // public static void setServletContext(ServletContext toset) {
40: // contextstash.set(toset);
41: // }
42: public static void setServletState(ServletContext context,
43: HttpServletRequest request, HttpServletResponse response) {
44: ServletState state = getServletState();
45: state.context = context;
46: state.request = request;
47: state.response = response;
48: }
49:
50: public static ServletState getServletState() {
51: return (ServletState) contextstash.get();
52: }
53:
54: }
|