1 package za.co.csir.icomtek.workflow.handlers; 2 3 import java.util.HashMap ; 4 import java.util.Vector ; 5 import java.util.Iterator ; 6 import java.util.Enumeration ; 7 import java.util.ArrayList ; 8 import java.util.StringTokenizer ; 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 21 public abstract class BaseHandler { 22 23 private static HashMap cachedActionObjects = new HashMap (); 24 private static final String INVOKE = "invoke"; 25 26 public static Object [] populateParameterValues (Vector params, 27 WorkflowContext ctx, 28 ArrayList requestAttributes) 29 throws Exception 30 { 31 Object [] retVals = new Object [params.size()]; 32 int i = 0; 33 for (Iterator 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 getValFromProperty (WorkflowContext ctx, 50 Property prop, 51 ArrayList requestAttributes) 52 throws Exception 53 { 54 String propVal = prop.getProp(); 55 System.err.println("<< resolving property: "+ propVal); 56 Object actionVal = getValFromAction(ctx, prop.getAction(), 57 requestAttributes); 58 StringTokenizer st = new StringTokenizer (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 resolveProperty (String prop, Object obj) 73 throws Exception 74 { 75 System.err.println("<< resolving property: "+prop); 76 Object actionVal = null; 77 int idx2 = prop.indexOf("()"); 78 if (idx2 > 0) { 79 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 getValFromParam (WorkflowContext ctx, 90 Parameter param, 91 ArrayList requestAttributes) 92 throws Exception 93 { 94 Object 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 getValFromAction (WorkflowContext ctx, 111 Action action, 112 ArrayList requestAttributes) 113 throws Exception 114 { 115 Object [] params = new Object [action.getParameterElementCount()]; 116 int i = 0; 117 for (Enumeration 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 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 executeAction (WorkflowContext ctx, 138 Action action, 139 Object [] params) throws Exception 140 { 141 String className = action.getClazz(); 142 String methodCall = action.getMethod(); 143 Object retVal = null; 144 if (className.indexOf(".") > 0) { 145 Object actionObj = cachedActionObjects.get(className); 147 if (actionObj == null) { 148 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 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 requestAttributes) { 175 for (Iterator it = requestAttributes.iterator(); it.hasNext(); ) { 176 String attrName = (String )it.next(); 177 ctx.removeRequestAttribute(attrName); 178 } 179 } 180 } 181 | Popular Tags |