KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > ibatis > struts > ActionContext


1 package com.ibatis.struts;
2
3 import com.ibatis.struts.httpmap.*;
4
5 import javax.servlet.http.HttpServletRequest JavaDoc;
6 import javax.servlet.http.HttpServletResponse JavaDoc;
7 import java.util.Map JavaDoc;
8 import java.util.List JavaDoc;
9 import java.util.ArrayList JavaDoc;
10
11 /**
12  * The ActionContext class gives simplified, thread-safe access to
13  * the request and response, as well as form parameters, request
14  * attributes, session attributes, application attributes. Much
15  * of this can be accopmplished without using the Struts or even
16  * the Servlet API, therefore isolating your application from
17  * presentation framework details.
18  * <p/>
19  * This class also provides facilities for simpler message and error
20  * message handling. Although not as powerful as that provided by
21  * Struts, it is great for simple applications that don't require
22  * internationalization or the flexibility of resource bundles.
23  * <p/>
24  * <i>Note: A more complete error and message handling API will be implemented.</i>
25  * <p/>
26  * Date: Mar 9, 2004 9:57:39 PM
27  *
28  * @author Clinton Begin
29  */

30 public class ActionContext {
31
32   private static final ThreadLocal JavaDoc localContext = new ThreadLocal JavaDoc();
33
34   private HttpServletRequest JavaDoc request;
35   private HttpServletResponse JavaDoc response;
36
37   private Map JavaDoc cookieMap;
38   private Map JavaDoc parameterMap;
39   private Map JavaDoc requestMap;
40   private Map JavaDoc sessionMap;
41   private Map JavaDoc applicationMap;
42
43   private ActionContext() {
44   }
45
46   protected static void initialize(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response) {
47     ActionContext ctx = getActionContext();
48     ctx.request = request;
49     ctx.response = response;
50     ctx.cookieMap = null;
51     ctx.parameterMap = null;
52     ctx.requestMap = null;
53     ctx.sessionMap = null;
54     ctx.applicationMap = null;
55   }
56
57   public void setSimpleMessage(String JavaDoc message) {
58     getRequestMap().put("message", message);
59   }
60
61   public void addSimpleError(String JavaDoc message) {
62     List JavaDoc errors = (List JavaDoc) getRequestMap().get("errors");
63     if (errors == null) {
64       errors = new ArrayList JavaDoc();
65       getRequestMap().put("errors", errors);
66     }
67     errors.add(message);
68   }
69
70   public boolean isSimpleErrorsExist () {
71     List JavaDoc errors = (List JavaDoc) getRequestMap().get("errors");
72     return errors != null && errors.size() > 0;
73   }
74
75   public Map JavaDoc getCookieMap() {
76     if (cookieMap == null) {
77       cookieMap = new CookieMap(request);
78     }
79     return cookieMap;
80   }
81
82   public Map JavaDoc getParameterMap() {
83     if (parameterMap == null) {
84       parameterMap = new ParameterMap(request);
85     }
86     return parameterMap;
87   }
88
89   public Map JavaDoc getRequestMap() {
90     if (requestMap == null) {
91       requestMap = new RequestMap(request);
92     }
93     return requestMap;
94   }
95
96   public Map JavaDoc getSessionMap() {
97     if (sessionMap == null) {
98       sessionMap = new SessionMap(request);
99     }
100     return sessionMap;
101   }
102
103   public Map JavaDoc getApplicationMap() {
104     if (applicationMap == null) {
105       applicationMap = new ApplicationMap(request);
106     }
107     return applicationMap;
108   }
109
110   public HttpServletRequest JavaDoc getRequest() {
111     return request;
112   }
113
114   public HttpServletResponse JavaDoc getResponse() {
115     return response;
116   }
117
118   public static ActionContext getActionContext() {
119     ActionContext ctx = (ActionContext) localContext.get();
120     if (ctx == null) {
121       ctx = new ActionContext();
122       localContext.set(ctx);
123     }
124     return ctx;
125   }
126 }
127
Popular Tags