1 7 package com.inversoft.verge.mvc.controller.actionflow; 8 9 10 import java.util.Collections ; 11 import java.util.HashMap ; 12 import java.util.Map ; 13 14 import com.inversoft.beans.BeanException; 15 import com.inversoft.verge.mvc.controller.BeanHandle; 16 import com.inversoft.verge.mvc.controller.actionflow.config.Namespace; 17 import com.inversoft.verge.mvc.controller.actionflow.config.Node; 18 import com.inversoft.verge.repository.Repository; 19 import com.inversoft.verge.repository.RepositoryException; 20 21 22 31 public class ActionHandlerNodeExecutor implements NodeExecutor { 32 33 36 public static final String EXCEPTION_HANDLE_METHOD = "exception"; 37 38 41 public static final Class [] HANDLE_PARAMS = new Class [] {ActionFlowAction.class}; 42 43 48 static final boolean CACHING = true; 49 50 53 static Map cache = new HashMap (); 54 55 56 60 61 72 private static BeanHandle getBeanHandle(String handleName, Class beanClass) 73 throws BeanException { 74 75 if (!CACHING) { 77 return new BeanHandle(handleName, beanClass, HANDLE_PARAMS); 78 } 79 80 Map propMap = null; 82 synchronized (cache) { 83 propMap = (Map ) cache.get(beanClass); 84 if (propMap == null) { 85 propMap = Collections.synchronizedMap(new HashMap ()); 86 cache.put(beanClass, propMap); 87 } 88 } 89 90 BeanHandle bh = null; 92 synchronized (propMap) { 93 bh = (BeanHandle) propMap.get(handleName); 94 if (bh == null) { 95 bh = new BeanHandle(handleName, beanClass, HANDLE_PARAMS); 96 propMap.put(handleName, bh); 97 } 98 } 99 100 return bh; 101 } 102 103 104 107 public ActionHandlerNodeExecutor() { 108 } 109 110 111 133 public Object execute(Namespace namespace, Node config, ActionFlowAction action, 134 Map extraParams) 135 throws Exception { 136 assert (config != null) : "config == null"; 137 assert (action != null) : "action == null"; 138 assert (action.getAction() != null) : "action.getAction() == null"; 139 140 Object handler = findActionHandler(config, action); 141 142 return callHandleMethod(handler, action); 143 } 144 145 149 Object findActionHandler(Node config, ActionFlowAction action) 150 throws NodeExecutorException 151 { 152 if (config.getClassName() != null) { 153 try { 154 Class klass = Class.forName(config.getClassName()); 155 return klass.newInstance(); 156 } catch (Exception e) { 157 throw new NodeExecutorException(e); 158 } 159 } 160 161 try { 162 return Repository.getInstance().lookupItem(action.getHttpServletRequest(), 163 config.getRepositoryId()); 164 } catch (RepositoryException re) { 165 throw new NodeExecutorException(re); 166 } 167 } 168 169 174 Object callHandleMethod(Object handler, ActionFlowAction action) 175 throws Exception { 176 Object actionObj = action.getAction(); 177 String methodName = null; 178 Object [] params = new Object [] {action}; 179 180 181 if (actionObj instanceof Exception ) { 182 methodName = EXCEPTION_HANDLE_METHOD; 183 } else if (actionObj instanceof String ) { 184 methodName = action.getAction().toString(); 185 } else { 186 assert (false) : "action is of type not supported by ActionHandlers"; 187 } 188 189 try { 190 BeanHandle bh = ActionHandlerNodeExecutor.getBeanHandle(methodName, 191 handler.getClass()); 192 return bh.invokeHandle(handler, params); 193 } catch (BeanException be) { 194 Throwable t = be.getCause(); 195 if (t != null) { 196 if (t instanceof Exception ) { 197 throw (Exception ) t; 198 } else { 199 throw new Error ("Unhandlable direct sub-class of Throwable", t); 200 } 201 } else { 202 throw new NodeExecutorException("Error calling handle method", be); 203 } 204 } 205 } 206 } | Popular Tags |