KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > infoglue > cms > util > workflow > WebWorkExecutor


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

5 package org.infoglue.cms.util.workflow;
6
7 import java.security.Principal JavaDoc;
8 import java.util.Collections JavaDoc;
9 import java.util.HashMap JavaDoc;
10 import java.util.Locale JavaDoc;
11 import java.util.Map JavaDoc;
12
13 import org.apache.commons.logging.Log;
14 import org.apache.commons.logging.LogFactory;
15
16 import webwork.action.ActionContext;
17 import webwork.dispatcher.GenericDispatcher;
18
19 import com.opensymphony.module.propertyset.PropertySet;
20 import com.opensymphony.workflow.FunctionProvider;
21 import com.opensymphony.workflow.WorkflowContext;
22 import com.opensymphony.workflow.WorkflowException;
23
24
25 /**
26  * Executes a WebWork function and restores the old ActionContext when finished
27  * (but does not provide chaining support yet). The following conversion is done:
28  * <ul>
29  * <li>inputs -> ActionContext#parameters</li>
30  * <li>variables -> ActionContext#session</li>
31  * <li>args -> ActionContext#application</li>
32  * </ul>
33  * <p>
34  *
35  * <ul>
36  * <li><b>action.name</b> - the actionName to ask from the ActionFactory</li>
37  * </ul>
38  */

39 public class WebWorkExecutor implements FunctionProvider
40 {
41     //~ Static fields/initializers /////////////////////////////////////////////
42

43     private static final Log log = LogFactory.getLog(WebWorkExecutor.class);
44
45     //~ Methods ////////////////////////////////////////////////////////////////
46

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