KickJava   Java API By Example, From Geeks To Geeks.

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


1 package za.co.csir.icomtek.workflow.handlers;
2
3 import java.util.HashMap JavaDoc;
4 import java.util.Vector JavaDoc;
5 import java.util.Iterator JavaDoc;
6 import java.util.Enumeration JavaDoc;
7 import java.util.ArrayList JavaDoc;
8 import java.util.StringTokenizer JavaDoc;
9
10 import org.apache.commons.beanutils.ConvertUtils;
11 import org.apache.commons.beanutils.MethodUtils;
12 import org.apache.commons.beanutils.PropertyUtils;
13
14 import za.co.csir.icomtek.workflow.factories.ModuleInvokerFactory;
15 import za.co.csir.icomtek.workflow.interfaces.WorkflowContext;
16 import za.co.csir.icomtek.workflow.model.*;
17
18 /**
19  * This is the base class with utilities for all the handler classes
20  */

21 public abstract class BaseHandler {
22
23     private static HashMap JavaDoc cachedActionObjects = new HashMap JavaDoc();
24     private static final String JavaDoc INVOKE = "invoke";
25
26     public static Object JavaDoc [] populateParameterValues (Vector JavaDoc params,
27                                                      WorkflowContext ctx,
28                                                      ArrayList JavaDoc requestAttributes)
29         throws Exception JavaDoc
30     {
31         Object JavaDoc [] retVals = new Object JavaDoc[params.size()];
32         int i = 0;
33         for (Iterator JavaDoc it = params.iterator(); it.hasNext(); i++) {
34             ParameterElement paramElement = (ParameterElement)it.next();
35             if (paramElement.getParameter() != null) {
36                 Parameter param = paramElement.getParameter();
37                 retVals[i] = getValFromParam(ctx, param, requestAttributes);
38             } else if (paramElement.getAction() != null ) {
39                 Action action = paramElement.getAction();
40                 retVals[i] = getValFromAction(ctx, action, requestAttributes);
41             } else if (paramElement.getProperty() != null ) {
42                 Property prop = paramElement.getProperty();
43                 retVals[i] = getValFromProperty(ctx, prop, requestAttributes);
44             }
45         }
46         return retVals;
47     }
48
49     private static Object JavaDoc getValFromProperty (WorkflowContext ctx,
50                                               Property prop,
51                                               ArrayList JavaDoc requestAttributes)
52         throws Exception JavaDoc
53     {
54         String JavaDoc propVal = prop.getProp();
55         System.err.println("<< resolving property: "+ propVal);
56         Object JavaDoc actionVal = getValFromAction(ctx, prop.getAction(),
57                                             requestAttributes);
58         // Now get properties off the return object
59
StringTokenizer JavaDoc st = new StringTokenizer JavaDoc(propVal, ".");
60         while (st.hasMoreTokens()) {
61             actionVal = resolveProperty(st.nextToken(), actionVal);
62         }
63         if (prop.getName() != null) {
64             ctx.setRequestAttribute(prop.getName(), actionVal);
65             requestAttributes.add(prop.getName());
66             System.err.println("<< setting property: " + actionVal
67                                + " with name: " + prop.getName());
68         }
69         return actionVal;
70     }
71
72     private static Object JavaDoc resolveProperty (String JavaDoc prop, Object JavaDoc obj)
73         throws Exception JavaDoc
74     {
75         System.err.println("<< resolving property: "+prop);
76         Object JavaDoc actionVal = null;
77         int idx2 = prop.indexOf("()");
78         if (idx2 > 0) {
79             // Get the value the old fashion way
80
actionVal = MethodUtils.invokeMethod(obj, prop.substring(0, idx2),
81                                                  null);
82         } else {
83             actionVal = PropertyUtils.getProperty(obj, prop);
84         }
85         System.err.println("<< returing property val: "+actionVal);
86         return actionVal;
87     }
88
89     private static Object JavaDoc getValFromParam (WorkflowContext ctx,
90                                            Parameter param,
91                                            ArrayList JavaDoc requestAttributes)
92         throws Exception JavaDoc
93     {
94         Object JavaDoc retVal = null;
95         System.err.println("<<resolving param name: "+param.getParameterTypeChoice().getName());
96         System.err.println("<<resolving param type: "+param.getParameterTypeChoice().getType());
97         System.err.println("<<resolving param val: "+param.getValue());
98         if (param.getParameterTypeChoice().getName() != null) {
99             ctx.setRequestAttribute(param.getParameterTypeChoice().getName(),
100                                     param.getValue());
101             requestAttributes.add(param.getParameterTypeChoice().getName());
102             retVal = param.getValue();
103         } else {
104             retVal = ConvertUtils.convert(param.getValue(),
105                Class.forName(param.getParameterTypeChoice().getType()));
106         }
107         return retVal;
108     }
109
110     private static Object JavaDoc getValFromAction (WorkflowContext ctx,
111                                             Action action,
112                                             ArrayList JavaDoc requestAttributes)
113         throws Exception JavaDoc
114     {
115         Object JavaDoc [] params = new Object JavaDoc [action.getParameterElementCount()];
116         int i = 0;
117         for (Enumeration JavaDoc e = action.enumerateParameterElement();
118              e.hasMoreElements() ; i++) {
119             ParameterElement paramEle = (ParameterElement)e.nextElement();
120             if (paramEle.getParameter() != null) {
121                 params[i] = getValFromParam(ctx, paramEle.getParameter(),
122                                             requestAttributes);
123             } else if (paramEle.getAction() != null) {
124                 params[i] = getValFromAction(ctx, paramEle.getAction(),
125                                              requestAttributes);
126             }
127         }
128         Object JavaDoc retVal = executeAction(ctx, action, null);
129         if (action.getName() != null) {
130             System.err.println("<< setting: "+retVal+" in request as: |"+action.getName()+"|");
131             ctx.setRequestAttribute(action.getName(), retVal);
132             requestAttributes.add(action.getName());
133         }
134         return retVal;
135     }
136
137     public static Object JavaDoc executeAction (WorkflowContext ctx,
138                                         Action action,
139                                         Object JavaDoc [] params) throws Exception JavaDoc
140     {
141         String JavaDoc className = action.getClazz();
142         String JavaDoc methodCall = action.getMethod();
143         Object JavaDoc retVal = null;
144         if (className.indexOf(".") > 0) {
145             // invoke as if on an object
146
Object JavaDoc actionObj = cachedActionObjects.get(className);
147             if (actionObj == null) {
148                 // Otherwise instantiate the class
149
actionObj = Class.forName(className).newInstance();
150                 cachedActionObjects.put(className, actionObj);
151             }
152             retVal = MethodUtils.invokeMethod(actionObj, methodCall, params);
153             System.err.println("<< invoked class: "+className
154                                +" with method: "+methodCall);
155             System.err.println("<< got back value: "+retVal);
156         } else {
157             // invoke on module with params in the context
158
try {
159                 ctx.beginWorkflowAction();
160                 retVal = ModuleInvokerFactory.instance()
161                     .getWorkflowModuleInvoker()
162                     .invokeOpOnModule(className, methodCall, ctx);
163                 System.err.println("<< invoked module: "+className
164                                    +" with method: "+methodCall);
165                 System.err.println("<< got back value: "+retVal);
166             } finally {
167                 ctx.endWorkflowAction();
168             }
169         }
170         return retVal;
171     }
172
173     public static void clearRequestAttributes (WorkflowContext ctx,
174                                                ArrayList JavaDoc requestAttributes) {
175         for (Iterator JavaDoc it = requestAttributes.iterator(); it.hasNext(); ) {
176             String JavaDoc attrName = (String JavaDoc)it.next();
177             ctx.removeRequestAttribute(attrName);
178         }
179     }
180 }
181
Popular Tags