001: package com.ibatis.struts;
002:
003: import java.util.ArrayList;
004: import java.util.List;
005: import java.util.Map;
006:
007: import javax.servlet.http.HttpServletRequest;
008: import javax.servlet.http.HttpServletResponse;
009:
010: import com.ibatis.struts.httpmap.ApplicationMap;
011: import com.ibatis.struts.httpmap.CookieMap;
012: import com.ibatis.struts.httpmap.ParameterMap;
013: import com.ibatis.struts.httpmap.RequestMap;
014: import com.ibatis.struts.httpmap.SessionMap;
015:
016: /**
017: * The ActionContext class gives simplified, thread-safe access to the request
018: * and response, as well as form parameters, request attributes, session
019: * attributes, application attributes. Much of this can be accopmplished without
020: * using the Struts or even the Servlet API, therefore isolating your
021: * application from presentation framework details. <p/>This class also
022: * provides facilities for simpler message and error message handling. Although
023: * not as powerful as that provided by Struts, it is great for simple
024: * applications that don't require internationalization or the flexibility of
025: * resource bundles. <p/><i>Note: A more complete error and message handling
026: * API will be implemented. </i> <p/>Date: Mar 9, 2004 9:57:39 PM
027: *
028: * @author Clinton Begin
029: */
030: public class ActionContext {
031:
032: private static final ThreadLocal localContext = new ThreadLocal();
033:
034: private HttpServletRequest request;
035:
036: private HttpServletResponse response;
037:
038: private Map cookieMap;
039:
040: private Map parameterMap;
041:
042: private Map requestMap;
043:
044: private Map sessionMap;
045:
046: private Map applicationMap;
047:
048: private ActionContext() {
049: }
050:
051: protected static void initialize(HttpServletRequest request,
052: HttpServletResponse response) {
053: ActionContext ctx = getActionContext();
054: ctx.request = request;
055: ctx.response = response;
056: ctx.cookieMap = null;
057: ctx.parameterMap = null;
058: ctx.requestMap = null;
059: ctx.sessionMap = null;
060: ctx.applicationMap = null;
061: }
062:
063: public void setSimpleMessage(String message) {
064: getRequestMap().put("message", message);
065: }
066:
067: public void addSimpleError(String message) {
068: List errors = (List) getRequestMap().get("errors");
069: if (errors == null) {
070: errors = new ArrayList();
071: getRequestMap().put("errors", errors);
072: }
073: errors.add(message);
074: }
075:
076: public boolean isSimpleErrorsExist() {
077: List errors = (List) getRequestMap().get("errors");
078: return errors != null && errors.size() > 0;
079: }
080:
081: public Map getCookieMap() {
082: if (cookieMap == null) {
083: cookieMap = new CookieMap(request);
084: }
085: return cookieMap;
086: }
087:
088: public Map getParameterMap() {
089: if (parameterMap == null) {
090: parameterMap = new ParameterMap(request);
091: }
092: return parameterMap;
093: }
094:
095: public Map getRequestMap() {
096: if (requestMap == null) {
097: requestMap = new RequestMap(request);
098: }
099: return requestMap;
100: }
101:
102: public Map getSessionMap() {
103: if (sessionMap == null) {
104: sessionMap = new SessionMap(request);
105: }
106: return sessionMap;
107: }
108:
109: public Map getApplicationMap() {
110: if (applicationMap == null) {
111: applicationMap = new ApplicationMap(request);
112: }
113: return applicationMap;
114: }
115:
116: public HttpServletRequest getRequest() {
117: return request;
118: }
119:
120: public HttpServletResponse getResponse() {
121: return response;
122: }
123:
124: public static ActionContext getActionContext() {
125: ActionContext ctx = (ActionContext) localContext.get();
126: if (ctx == null) {
127: ctx = new ActionContext();
128: localContext.set(ctx);
129: }
130: return ctx;
131: }
132: }
|