KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > za > co > csir > icomtek > workflow > handlers > ActionHandler


1 package za.co.csir.icomtek.workflow.handlers;
2
3 import java.util.Vector JavaDoc;
4 import java.util.ArrayList JavaDoc;
5 import java.util.Iterator JavaDoc;
6
7 import za.co.csir.icomtek.workflow.interfaces.WorkflowContext;
8 import za.co.csir.icomtek.workflow.model.Action;
9 import za.co.csir.icomtek.workflow.model.State;
10 import za.co.csir.icomtek.workflow.model.StateTransition;
11
12 /**
13  * This class is used to execute actions from the project workflow
14  *
15  * Actions consist of a clazz, method, name(optional), and any number of
16  * parameters elements. Parameter elements can contain parameters which are
17  * simple static values or nested actions.
18  * If the action class tag names a module then the method will be invoked on the
19  * module with all parameters being placed into the context. If the action class
20  * contains non module class then this class will try to instantiate the named
21  * class and invoke the method on it passing all parameters as method parameters
22  * to the call.
23  *
24  * The parameter blocks specify a value and a type or name. If the name is
25  * specified then it is assumed that the value will be passed to a module call
26  * as a parameter in the context. If the type is specified then it is assumed
27  * that the value will be passed as a method parameter and it will be converted
28  * to an object of that type.
29  * When specifying a hard-coded value the type field contains the java type.
30  * NOTE: always use the wrapper type for primatives
31  *
32  */

33 public class ActionHandler extends BaseHandler {
34
35     /**
36      * This method is used to fire off entry actions that have been specified
37      * in a state
38      *
39      * @param ctx is the context on who's behalf we are firing the actions
40      * @param state is the state containing entry actions that will be fired
41      */

42     public static void performActions (WorkflowContext ctx,
43                                        State state)
44         throws Exception JavaDoc
45     {
46         if (state.getEntryStateActions() != null &&
47             state.getEntryStateActions().getActionAsReference() != null) {
48             performActions(ctx, state.getEntryStateActions()
49                            .getActionAsReference());
50         }
51     }
52
53     /**
54      * This method is used to fire off exit actions that have been specified in
55      * a state transition
56      *
57      * @param ctx is the context on who's behalf we are firing the actions
58      * @param st is the state transtion containing exit actions that will be
59      * fired
60      */

61     public static void performActions (WorkflowContext ctx,
62                                        StateTransition st)
63         throws Exception JavaDoc
64     {
65         if (st.getExitStateActions() != null &&
66             st.getExitStateActions().getActionAsReference() != null) {
67             performActions(ctx, st.getExitStateActions()
68                            .getActionAsReference());
69         }
70     }
71
72     private static void performActions (WorkflowContext ctx,
73                                         Vector JavaDoc actions)
74         throws Exception JavaDoc
75     {
76         // Setup any workflow context for actions
77
try {
78             ctx.beginWorkflowAction();
79             for (Iterator JavaDoc it = actions.iterator(); it.hasNext(); ) {
80                 Action action = (Action)it.next();
81                 ArrayList JavaDoc reqAttVals = new ArrayList JavaDoc();
82                 try {
83                     Object JavaDoc [] paramVals = populateParameterValues(action.getParameterElementAsReference(), ctx, reqAttVals);
84                     executeAction(ctx, action, paramVals);
85                     clearRequestAttributes(ctx, reqAttVals);
86                 } catch (Exception JavaDoc e) {
87                     e.printStackTrace();
88                     // It is ok if an action fails
89
}
90             }
91         } finally {
92             ctx.endWorkflowAction();
93         }
94     }
95 }
96
Popular Tags