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.*;
10:
11: import org.apache.commons.logging.Log;
12: import org.apache.commons.logging.LogFactory;
13:
14: import webwork.action.ActionContext;
15:
16: import webwork.dispatcher.GenericDispatcher;
17:
18: import java.security.Principal;
19:
20: import java.util.*;
21:
22: /**
23: * Executes a WebWork function and restores the old ActionContext when finished
24: * (but does not provide chaining support yet). The following conversion is done:
25: * <ul>
26: * <li>inputs -> ActionContext#parameters</li>
27: * <li>variables -> ActionContext#session</li>
28: * <li>args -> ActionContext#application</li>
29: * </ul>
30: * <p>
31: *
32: * <ul>
33: * <li><b>action.name</b> - the actionName to ask from the ActionFactory</li>
34: * </ul>
35: */
36: public class WebWorkExecutor implements FunctionProvider {
37: //~ Static fields/initializers /////////////////////////////////////////////
38:
39: private static final Log log = LogFactory
40: .getLog(WebWorkExecutor.class);
41:
42: //~ Methods ////////////////////////////////////////////////////////////////
43:
44: public void execute(Map transientVars, Map args, PropertySet ps)
45: throws WorkflowException {
46: final WorkflowContext wfContext = (WorkflowContext) transientVars
47: .get("context");
48:
49: String actionName = (String) args.get("action.name");
50: GenericDispatcher gd = new GenericDispatcher(actionName);
51: gd.prepareContext();
52: ActionContext.setPrincipal(new Principal() {
53: public String getName() {
54: return wfContext.getCaller();
55: }
56: });
57: ActionContext.setApplication(args);
58: ActionContext.setSession(ps.getProperties(""));
59: ActionContext.setLocale(Locale.getDefault());
60:
61: Map params = new HashMap(transientVars);
62: params.putAll(args);
63: ActionContext
64: .setParameters(Collections.unmodifiableMap(params));
65:
66: try {
67: gd.executeAction();
68: gd.finish();
69: gd.finalizeContext();
70: } catch (Exception e) {
71: throw new WorkflowException("Could not execute action "
72: + actionName, e);
73: }
74: }
75: }
|