001: /*
002: * Copyright (c) 2002-2003 by OpenSymphony
003: * All rights reserved.
004: */
005: package com.opensymphony.webwork.util;
006:
007: import com.opensymphony.xwork.ActionContext;
008: import com.opensymphony.xwork.ActionInvocation;
009: import com.opensymphony.xwork.util.OgnlValueStack;
010:
011: import java.io.Serializable;
012: import java.util.HashMap;
013: import java.util.Map;
014:
015: /**
016: * InvocationSessionStore
017: *
018: * @author Jason Carreira
019: * Created Apr 12, 2003 9:53:19 PM
020: */
021: public class InvocationSessionStore {
022:
023: private static final String INVOCATION_MAP_KEY = "com.opensymphony.webwork.util.InvocationSessionStore.invocationMap";
024:
025: private InvocationSessionStore() {
026: }
027:
028: /**
029: * Checks the Map in the Session for the key and the token. If the
030: * ActionInvocation is saved in the Session, the ValueStack from the
031: * ActionProxy associated with the ActionInvocation is set into the
032: * ActionContext and the ActionInvocation is returned.
033: *
034: * @param key the name the DefaultActionInvocation and ActionContext were saved as
035: * @return the DefaultActionInvocation saved using the key, or null if none was found
036: */
037: public static ActionInvocation loadInvocation(String key,
038: String token) {
039: InvocationContext invocationContext = (InvocationContext) getInvocationMap()
040: .get(key);
041:
042: if ((invocationContext == null)
043: || !invocationContext.token.equals(token)) {
044: return null;
045: }
046:
047: OgnlValueStack stack = invocationContext.invocation.getStack();
048: ActionContext.getContext().setValueStack(stack);
049:
050: return invocationContext.invocation;
051: }
052:
053: /**
054: * Stores the DefaultActionInvocation and ActionContext into the Session using the provided key for loading later using
055: * {@link #loadInvocation}
056: *
057: * @param key
058: * @param invocation
059: */
060: public static void storeInvocation(String key, String token,
061: ActionInvocation invocation) {
062: InvocationContext invocationContext = new InvocationContext(
063: invocation, token);
064: Map invocationMap = getInvocationMap();
065: invocationMap.put(key, invocationContext);
066: setInvocationMap(invocationMap);
067: }
068:
069: static void setInvocationMap(Map invocationMap) {
070: Map session = ActionContext.getContext().getSession();
071:
072: if (session == null) {
073: throw new IllegalStateException(
074: "Unable to access the session.");
075: }
076:
077: session.put(INVOCATION_MAP_KEY, invocationMap);
078: }
079:
080: static Map getInvocationMap() {
081: Map session = ActionContext.getContext().getSession();
082:
083: if (session == null) {
084: throw new IllegalStateException(
085: "Unable to access the session.");
086: }
087:
088: Map invocationMap = (Map) session.get(INVOCATION_MAP_KEY);
089:
090: if (invocationMap == null) {
091: invocationMap = new HashMap();
092: setInvocationMap(invocationMap);
093: }
094:
095: return invocationMap;
096: }
097:
098: private static class InvocationContext implements Serializable {
099: ActionInvocation invocation;
100: String token;
101:
102: public InvocationContext(ActionInvocation invocation,
103: String token) {
104: this.invocation = invocation;
105: this.token = token;
106: }
107: }
108: }
|