View Javadoc

1   /*
2    * $Id: InvocationSessionStore.java 471756 2006-11-06 15:01:43Z husted $
3    *
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *  http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
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 }