View Javadoc

1   /*
2    * $Id: InvocationSessionStore.java 454565 2006-10-10 00:02:56Z jmitchell $
3    *
4    * Copyright 2006 The Apache Software Foundation.
5    *
6    * Licensed under the Apache License, Version 2.0 (the "License");
7    * you may not use this file except in compliance with the License.
8    * You may obtain a copy of the License at
9    *
10   *      http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  package org.apache.struts2.util;
19  
20  import java.io.Serializable;
21  import java.util.HashMap;
22  import java.util.Map;
23  
24  import com.opensymphony.xwork2.ActionContext;
25  import com.opensymphony.xwork2.ActionInvocation;
26  import com.opensymphony.xwork2.util.ValueStack;
27  
28  
29  /***
30   * InvocationSessionStore
31   *
32   */
33  public class InvocationSessionStore {
34  
35      private static final String INVOCATION_MAP_KEY = "org.apache.struts2.util.InvocationSessionStore.invocationMap";
36  
37  
38      private InvocationSessionStore() {
39      }
40  
41  
42      /***
43       * Checks the Map in the Session for the key and the token. If the
44       * ActionInvocation is saved in the Session, the ValueStack from the
45       * ActionProxy associated with the ActionInvocation is set into the
46       * ActionContext and the ActionInvocation is returned.
47       *
48       * @param key the name the DefaultActionInvocation and ActionContext were saved as
49       * @return the DefaultActionInvocation saved using the key, or null if none was found
50       */
51      public static ActionInvocation loadInvocation(String key, String token) {
52          InvocationContext invocationContext = (InvocationContext) getInvocationMap().get(key);
53  
54          if ((invocationContext == null) || !invocationContext.token.equals(token)) {
55              return null;
56          }
57  
58          ValueStack stack = invocationContext.invocation.getStack();
59          ActionContext.getContext().setValueStack(stack);
60  
61          return invocationContext.invocation;
62      }
63  
64      /***
65       * Stores the DefaultActionInvocation and ActionContext into the Session using the provided key for loading later using
66       * {@link #loadInvocation}
67       *
68       * @param key
69       * @param invocation
70       */
71      public static void storeInvocation(String key, String token, ActionInvocation invocation) {
72          InvocationContext invocationContext = new InvocationContext(invocation, token);
73          Map invocationMap = getInvocationMap();
74          invocationMap.put(key, invocationContext);
75          setInvocationMap(invocationMap);
76      }
77  
78      static void setInvocationMap(Map invocationMap) {
79          Map session = ActionContext.getContext().getSession();
80  
81          if (session == null) {
82              throw new IllegalStateException("Unable to access the session.");
83          }
84  
85          session.put(INVOCATION_MAP_KEY, invocationMap);
86      }
87  
88      static Map getInvocationMap() {
89          Map session = ActionContext.getContext().getSession();
90  
91          if (session == null) {
92              throw new IllegalStateException("Unable to access the session.");
93          }
94  
95          Map invocationMap = (Map) session.get(INVOCATION_MAP_KEY);
96  
97          if (invocationMap == null) {
98              invocationMap = new HashMap();
99              setInvocationMap(invocationMap);
100         }
101 
102         return invocationMap;
103     }
104 
105 
106     private static class InvocationContext implements Serializable {
107     	
108 		private static final long serialVersionUID = -286697666275777888L;
109 		
110 		ActionInvocation invocation;
111         String token;
112 
113         public InvocationContext(ActionInvocation invocation, String token) {
114             this.invocation = invocation;
115             this.token = token;
116         }
117     }
118 }