KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > opensymphony > webwork > util > InvocationSessionStore


1 /*
2  * Copyright (c) 2002-2003 by OpenSymphony
3  * All rights reserved.
4  */

5 package com.opensymphony.webwork.util;
6
7 import com.opensymphony.xwork.ActionContext;
8 import com.opensymphony.xwork.ActionInvocation;
9 import com.opensymphony.xwork.util.OgnlValueStack;
10
11 import java.io.Serializable JavaDoc;
12 import java.util.HashMap JavaDoc;
13 import java.util.Map JavaDoc;
14
15
16 /**
17  * InvocationSessionStore
18  *
19  * @author Jason Carreira
20  * Created Apr 12, 2003 9:53:19 PM
21  */

22 public class InvocationSessionStore {
23     //~ Static fields/initializers /////////////////////////////////////////////
24

25     private static final String JavaDoc INVOCATION_MAP_KEY = "com.opensymphony.webwork.util.InvocationSessionStore.invocationMap";
26
27     //~ Constructors ///////////////////////////////////////////////////////////
28

29     private InvocationSessionStore() {
30     }
31
32     //~ Methods ////////////////////////////////////////////////////////////////
33

34     /**
35      * Checks the Map in the Session for the key and the token. If the
36      * ActionInvocation is saved in the Session, the ValueStack from the
37      * ActionProxy associated with the ActionInvocation is set into the
38      * ActionContext and the ActionInvocation is returned.
39      *
40      * @param key the name the DefaultActionInvocation and ActionContext were saved as
41      * @return the DefaultActionInvocation saved using the key, or null if none was found
42      */

43     public static ActionInvocation loadInvocation(String JavaDoc key, String JavaDoc token) {
44         InvocationContext invocationContext = (InvocationContext) getInvocationMap().get(key);
45
46         if ((invocationContext == null) || !invocationContext.token.equals(token)) {
47             return null;
48         }
49
50         OgnlValueStack stack = invocationContext.invocation.getStack();
51         ActionContext.getContext().setValueStack(stack);
52
53         return invocationContext.invocation;
54     }
55
56     /**
57      * Stores the DefaultActionInvocation and ActionContext into the Session using the provided key for loading later using
58      * {@link #loadInvocation}
59      *
60      * @param key
61      * @param invocation
62      */

63     public static void storeInvocation(String JavaDoc key, String JavaDoc token, ActionInvocation invocation) {
64         InvocationContext invocationContext = new InvocationContext(invocation, token);
65         Map JavaDoc invocationMap = getInvocationMap();
66         invocationMap.put(key, invocationContext);
67         setInvocationMap(invocationMap);
68     }
69
70     static void setInvocationMap(Map JavaDoc invocationMap) {
71         Map JavaDoc session = ActionContext.getContext().getSession();
72
73         if (session == null) {
74             throw new IllegalStateException JavaDoc("Unable to access the session.");
75         }
76
77         session.put(INVOCATION_MAP_KEY, invocationMap);
78     }
79
80     static Map JavaDoc getInvocationMap() {
81         Map JavaDoc session = ActionContext.getContext().getSession();
82
83         if (session == null) {
84             throw new IllegalStateException JavaDoc("Unable to access the session.");
85         }
86
87         Map JavaDoc invocationMap = (Map JavaDoc) session.get(INVOCATION_MAP_KEY);
88
89         if (invocationMap == null) {
90             invocationMap = new HashMap JavaDoc();
91             setInvocationMap(invocationMap);
92         }
93
94         return invocationMap;
95     }
96
97     //~ Inner Classes //////////////////////////////////////////////////////////
98

99     private static class InvocationContext implements Serializable JavaDoc {
100         ActionInvocation invocation;
101         String JavaDoc token;
102
103         public InvocationContext(ActionInvocation invocation, String JavaDoc token) {
104             this.invocation = invocation;
105             this.token = token;
106         }
107     }
108 }
109
Popular Tags