01: /*
02: * JFox - The most lightweight Java EE Application Server!
03: * more details please visit http://www.huihoo.org/jfox or http://www.jfox.org.cn.
04: *
05: * JFox is licenced and re-distributable under GNU LGPL.
06: */
07: package org.jfox.mvc;
08:
09: import java.io.Serializable;
10: import java.util.HashMap;
11: import java.util.Map;
12:
13: import org.jfox.ejb3.security.SecurityContext;
14:
15: /**
16: * Session 上下文,用æ?¥å˜æ”¾ Session æ•°æ?®
17: *
18: * @author <a href="mailto:jfox.young@gmail.com">Young Yang</a>
19: */
20: public class SessionContext implements Serializable {
21:
22: public static final String SECURITY_CONTEXT_SESSION_KEY = "__SECURITY_SUBJECT__";
23:
24: public static final String TOKEN_SESSION_KEY = "SESSION_TOKEN.";
25:
26: /**
27: * 使用Mapå˜å‚¨Sessionæ•°æ?®
28: */
29: private Map<String, Serializable> sessionMap = new HashMap<String, Serializable>();
30:
31: SessionContext() {
32: }
33:
34: public static SessionContext getCurrentThreadSessionContext() {
35: ActionContext actionContext = ActionContext
36: .getCurrentThreadInvocationContext();
37: if (actionContext != null) {
38: return actionContext.getSessionContext();
39: } else {
40: return null;
41: }
42: }
43:
44: /**
45: * 在Sessionä¸ç»‘定SecurityContext
46: * @param securityContext 认�完�之�生�的SecurityContext
47: */
48: public void bindSecurityContext(SecurityContext securityContext) {
49: this
50: .setAttribute(SECURITY_CONTEXT_SESSION_KEY,
51: securityContext);
52: }
53:
54: /**
55: * �得当�Session关�的SecurityContext
56: */
57: public SecurityContext getSecurityContext() {
58: if (!this .containsAttribute(SECURITY_CONTEXT_SESSION_KEY)) {
59: this .setAttribute(SECURITY_CONTEXT_SESSION_KEY,
60: new SecurityContext());
61: }
62:
63: return (SecurityContext) this
64: .getAttribute(SECURITY_CONTEXT_SESSION_KEY);
65: }
66:
67: public void setAttribute(String key, Serializable value) {
68: sessionMap.put(key, value);
69: }
70:
71: public Serializable getAttribute(String key) {
72: return sessionMap.get(key);
73: }
74:
75: public boolean containsAttribute(String key) {
76: return sessionMap.containsKey(key);
77: }
78:
79: public Serializable removeAttribute(String key) {
80: return sessionMap.remove(key);
81: }
82:
83: public String[] getAttributeNames() {
84: return sessionMap.keySet().toArray(
85: new String[sessionMap.size()]);
86: }
87:
88: /**
89: * 销� SessionContext
90: */
91: public void clearAttributes() {
92: sessionMap.clear();
93: }
94:
95: public static void main(String[] args) {
96:
97: }
98: }
|