01: /*
02: * Copyright (c) 2002-2003 by OpenSymphony
03: * All rights reserved.
04: */
05: package com.opensymphony.workflow.util;
06:
07: import com.opensymphony.module.propertyset.PropertySet;
08:
09: import com.opensymphony.workflow.FunctionProvider;
10: import com.opensymphony.workflow.WorkflowException;
11:
12: import com.opensymphony.xwork.ActionContext;
13: import com.opensymphony.xwork.ActionProxy;
14: import com.opensymphony.xwork.ActionProxyFactory;
15:
16: import java.util.Collections;
17: import java.util.HashMap;
18: import java.util.Locale;
19: import java.util.Map;
20:
21: /**
22: * Executes an XWork function.
23: * The following conversion is done:
24: * <ul>
25: * <li>inputs -> ActionContext#parameters</li>
26: * <li>variables -> ActionContext#session</li>
27: * <li>args -> ActionContext#application</li>
28: * </ul>
29: * <p>
30: *
31: * <ul>
32: * <li><b>action.name</b> - the actionName to ask from the ActionProxy</li>
33: * <li><b>namespace</b> - the namespace to ask from the ActionProxy</li>
34: * </ul>
35: */
36: public class XWorkExecutor implements FunctionProvider {
37: //~ Methods ////////////////////////////////////////////////////////////////
38:
39: public void execute(Map transientVars, Map args, PropertySet ps)
40: throws WorkflowException {
41: String actionName = (String) args.get("action.name");
42: String namespace = (String) args.get("namespace");
43:
44: Map extraContext = new HashMap();
45: extraContext.put(ActionContext.APPLICATION, args);
46: extraContext.put(ActionContext.SESSION, ps.getProperties(""));
47: extraContext.put(ActionContext.LOCALE, Locale.getDefault());
48:
49: Map params = new HashMap(transientVars);
50: params.putAll(args);
51: extraContext.put(ActionContext.PARAMETERS, Collections
52: .unmodifiableMap(params));
53:
54: try {
55: ActionProxy proxy = ActionProxyFactory.getFactory()
56: .createActionProxy(namespace, actionName,
57: extraContext, false);
58: proxy.execute();
59: } catch (Exception e) {
60: throw new WorkflowException("Could not execute action "
61: + actionName, e);
62: }
63: }
64: }
|