KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > opensymphony > workflow > util > WebWorkExecutor


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

5 package com.opensymphony.workflow.util;
6
7 import com.opensymphony.module.propertyset.PropertySet;
8
9 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 JavaDoc;
19
20 import java.util.*;
21
22
23 /**
24  * Executes a WebWork function and restores the old ActionContext when finished
25  * (but does not provide chaining support yet). The following conversion is done:
26  * <ul>
27  * <li>inputs -> ActionContext#parameters</li>
28  * <li>variables -> ActionContext#session</li>
29  * <li>args -> ActionContext#application</li>
30  * </ul>
31  * <p>
32  *
33  * <ul>
34  * <li><b>action.name</b> - the actionName to ask from the ActionFactory</li>
35  * </ul>
36  */

37 public class WebWorkExecutor implements FunctionProvider {
38     //~ Static fields/initializers /////////////////////////////////////////////
39

40     private static final Log log = LogFactory.getLog(WebWorkExecutor.class);
41
42     //~ Methods ////////////////////////////////////////////////////////////////
43

44     public void execute(Map transientVars, Map args, PropertySet ps) throws WorkflowException {
45         final WorkflowContext wfContext = (WorkflowContext) transientVars.get("context");
46
47         String JavaDoc actionName = (String JavaDoc) args.get("action.name");
48         GenericDispatcher gd = new GenericDispatcher(actionName);
49         gd.prepareContext();
50         ActionContext.setPrincipal(new Principal JavaDoc() {
51                 public String JavaDoc getName() {
52                     return wfContext.getCaller();
53                 }
54             });
55         ActionContext.setApplication(args);
56         ActionContext.setSession(ps.getProperties(""));
57         ActionContext.setLocale(Locale.getDefault());
58
59         Map params = new HashMap(transientVars);
60         params.putAll(args);
61         ActionContext.setParameters(Collections.unmodifiableMap(params));
62
63         try {
64             gd.executeAction();
65             gd.finish();
66             gd.finalizeContext();
67         } catch (Exception JavaDoc e) {
68             throw new WorkflowException("Could not execute action " + actionName, e);
69         }
70     }
71 }
72
Popular Tags