1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 package org.apache.struts2.util;
22
23 import java.io.Serializable;
24 import java.util.HashMap;
25 import java.util.Map;
26
27 import com.opensymphony.xwork2.ActionContext;
28 import com.opensymphony.xwork2.ActionInvocation;
29 import com.opensymphony.xwork2.util.ValueStack;
30
31
32 /***
33 * InvocationSessionStore
34 *
35 */
36 public class InvocationSessionStore {
37
38 private static final String INVOCATION_MAP_KEY = "org.apache.struts2.util.InvocationSessionStore.invocationMap";
39
40
41 private InvocationSessionStore() {
42 }
43
44
45 /***
46 * Checks the Map in the Session for the key and the token. If the
47 * ActionInvocation is saved in the Session, the ValueStack from the
48 * ActionProxy associated with the ActionInvocation is set into the
49 * ActionContext and the ActionInvocation is returned.
50 *
51 * @param key the name the DefaultActionInvocation and ActionContext were saved as
52 * @return the DefaultActionInvocation saved using the key, or null if none was found
53 */
54 public static ActionInvocation loadInvocation(String key, String token) {
55 InvocationContext invocationContext = (InvocationContext) getInvocationMap().get(key);
56
57 if ((invocationContext == null) || !invocationContext.token.equals(token)) {
58 return null;
59 }
60
61 ValueStack stack = invocationContext.invocation.getStack();
62 ActionContext.getContext().setValueStack(stack);
63
64 return invocationContext.invocation;
65 }
66
67 /***
68 * Stores the DefaultActionInvocation and ActionContext into the Session using the provided key for loading later using
69 * {@link #loadInvocation}
70 *
71 * @param key
72 * @param invocation
73 */
74 public static void storeInvocation(String key, String token, ActionInvocation invocation) {
75 InvocationContext invocationContext = new InvocationContext(invocation, token);
76 Map invocationMap = getInvocationMap();
77 invocationMap.put(key, invocationContext);
78 setInvocationMap(invocationMap);
79 }
80
81 static void setInvocationMap(Map invocationMap) {
82 Map session = ActionContext.getContext().getSession();
83
84 if (session == null) {
85 throw new IllegalStateException("Unable to access the session.");
86 }
87
88 session.put(INVOCATION_MAP_KEY, invocationMap);
89 }
90
91 static Map getInvocationMap() {
92 Map session = ActionContext.getContext().getSession();
93
94 if (session == null) {
95 throw new IllegalStateException("Unable to access the session.");
96 }
97
98 Map invocationMap = (Map) session.get(INVOCATION_MAP_KEY);
99
100 if (invocationMap == null) {
101 invocationMap = new HashMap();
102 setInvocationMap(invocationMap);
103 }
104
105 return invocationMap;
106 }
107
108
109 private static class InvocationContext implements Serializable {
110
111 private static final long serialVersionUID = -286697666275777888L;
112
113 ActionInvocation invocation;
114 String token;
115
116 public InvocationContext(ActionInvocation invocation, String token) {
117 this.invocation = invocation;
118 this.token = token;
119 }
120 }
121 }