001: /*
002: * JFox - The most lightweight Java EE Application Server!
003: * more details please visit http://www.huihoo.org/jfox or http://www.jfox.org.cn.
004: *
005: * JFox is licenced and re-distributable under GNU LGPL.
006: */
007: package org.jfox.mvc;
008:
009: import java.lang.reflect.Method;
010: import java.util.Collection;
011: import java.util.HashMap;
012: import java.util.Map;
013: import javax.servlet.ServletConfig;
014: import javax.servlet.ServletContext;
015: import javax.servlet.http.HttpServletRequest;
016:
017: /**
018: * 调用上下文,å°?装调用过程ä¸çš„所有资æº?
019: *
020: * @author <a href="mailto:jfox.young@gmail.com">Young Yang</a>
021: */
022: public class ActionContext {
023:
024: private String actionName;
025:
026: /**
027: * ActionMethodå??称
028: */
029: private String actionMethodName;
030:
031: /**
032: * 执行的Action方法
033: */
034: private Method actionMethod = null;
035:
036: /**
037: * 表å?• å’Œ QueryString æ??交的数æ?®
038: */
039: private Map<String, String[]> parameterMap = new HashMap<String, String[]>();
040:
041: /**
042: * ä¸Šä¼ çš„æ–‡ä»¶ fieldname=>FileUploaded
043: */
044: private Map<String, FileUploaded> fileUploadedMap = new HashMap<String, FileUploaded>();
045:
046: private PageContext pageContext;
047: private SessionContext sessionContext;
048: private Invocation invocation;
049:
050: private ServletConfig servletConfig;
051:
052: private HttpServletRequest request = null;
053:
054: private Map<String, Object> attributes = new HashMap<String, Object>();
055:
056: public static final String SESSION_KEY = "__SESSION_KEY__";
057:
058: /**
059: * 使用 ThreadLocal 将 InvocationContext 和当�线程进行关�
060: */
061: static transient ThreadLocal<ActionContext> threadInvocationContext = new ThreadLocal<ActionContext>();
062:
063: public ActionContext(ServletConfig servletConfig,
064: HttpServletRequest request,
065: Map<String, String[]> parameterMap,
066: Map<String, FileUploaded> fileUploadedMap,
067: String actionName, String actionMethodName) {
068: this .servletConfig = servletConfig;
069: this .request = request;
070: this .parameterMap.putAll(parameterMap);
071: this .fileUploadedMap.putAll(fileUploadedMap);
072: this .actionName = actionName;
073: this .actionMethodName = actionMethodName;
074: this .pageContext = new PageContext();
075: }
076:
077: /**
078: * 得到当�线程绑定的 InvocationContext
079: */
080: public static ActionContext getCurrentThreadInvocationContext() {
081: return threadInvocationContext.get();
082: }
083:
084: public void initInvocationContext() {
085: this .sessionContext = initSessionContext();
086: threadInvocationContext.set(this );
087: }
088:
089: public void disassociateThreadInvocationContext() {
090: threadInvocationContext.remove();
091: }
092:
093: /**
094: * 使用 request �始化 session context,
095: * �始化完毕之�,将 session context 关�到当�线程
096: */
097: private SessionContext initSessionContext() {
098: if (request == null) {
099: return null;
100: }
101: SessionContext sessionContext = (SessionContext) request
102: .getSession().getAttribute(SESSION_KEY);
103: if (sessionContext == null) {
104: sessionContext = new SessionContext();
105: request.getSession().setAttribute(SESSION_KEY,
106: sessionContext);
107: }
108: return sessionContext;
109: }
110:
111: HttpServletRequest getServletRequest() {
112: return request;
113: }
114:
115: /**
116: * 销� Session
117: */
118: public void destroySessionContext() {
119: getSessionContext().clearAttributes();
120: request.getSession().removeAttribute(SESSION_KEY);
121: }
122:
123: public String getRequestURI() {
124: return getServletRequest().getRequestURI();
125: }
126:
127: public ServletConfig getServletConfig() {
128: return servletConfig;
129: }
130:
131: public ServletContext getServletContext() {
132: return servletConfig.getServletContext();
133: }
134:
135: void setActionMethod(Method actionMethod) {
136: this .actionMethod = actionMethod;
137: }
138:
139: public Method getActionMethod() {
140: return actionMethod;
141: }
142:
143: public String getActionMethodName() {
144: return actionMethodName;
145: }
146:
147: public String getFullActionMethodName() {
148: return getActionName() + "." + getActionMethodName();
149: }
150:
151: public String getActionName() {
152: return actionName;
153: }
154:
155: public boolean isPost() {
156: return "POST".equals(getServletRequest().getMethod()
157: .toUpperCase());
158: }
159:
160: public SessionContext getSessionContext() {
161: return sessionContext;
162: }
163:
164: void setInvocation(Invocation invocation) {
165: this .invocation = invocation;
166: }
167:
168: public Invocation getInvocation() {
169: return invocation;
170: }
171:
172: public PageContext getPageContext() {
173: return pageContext;
174: }
175:
176: public String getParameterValue(String key) {
177: return parameterMap.get(key)[0];
178: }
179:
180: public String[] getParameterValues(String key) {
181: return parameterMap.get(key);
182: }
183:
184: public Map<String, String[]> getParameterMap() {
185: return parameterMap;
186: }
187:
188: public Collection<FileUploaded> getFilesUploaded() {
189: return fileUploadedMap.values();
190: }
191:
192: public FileUploaded getFileUploaded(String fieldname) {
193: return fileUploadedMap.get(fieldname);
194: }
195:
196: public String getRemoteAddress() {
197: return getServletRequest().getRemoteAddr();
198: }
199:
200: public void setAttribute(String key, Object value) {
201: attributes.put(key, value);
202: }
203:
204: public Object getAttribute(String key) {
205: return attributes.get(key);
206: }
207:
208: public boolean constainsKey(String key) {
209: return attributes.containsKey(key);
210: }
211:
212: public boolean removeAttribute(String key) {
213: return attributes.remove(key) != null;
214: }
215:
216: public static void main(String[] args) {
217:
218: }
219: }
|