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