1 18 19 package org.apache.struts.actions; 20 21 import java.lang.reflect.InvocationTargetException ; 22 import java.lang.reflect.Method ; 23 import java.util.HashMap ; 24 25 import javax.servlet.ServletException ; 26 import javax.servlet.http.HttpServletRequest ; 27 import javax.servlet.http.HttpServletResponse ; 28 29 import org.apache.commons.logging.Log; 30 import org.apache.commons.logging.LogFactory; 31 import org.apache.struts.action.Action; 32 import org.apache.struts.action.ActionForm; 33 import org.apache.struts.action.ActionForward; 34 import org.apache.struts.action.ActionMapping; 35 import org.apache.struts.util.MessageResources; 36 import org.apache.struts.Globals; 37 38 88 public class ActionDispatcher { 89 90 91 93 96 public static final int DEFAULT_FLAVOR = 0; 97 98 101 public static final int MAPPING_FLAVOR = 1; 102 103 106 public static final int DISPATCH_FLAVOR = 2; 107 108 109 112 protected Action actionInstance; 113 114 117 protected int flavor; 118 119 122 protected Class clazz; 123 124 127 protected static Log log = LogFactory.getLog(ActionDispatcher.class); 128 129 130 133 protected static MessageResources messages = 134 MessageResources.getMessageResources 135 ("org.apache.struts.actions.LocalStrings"); 136 137 138 144 protected HashMap methods = new HashMap (); 145 146 150 protected Class [] types = { 151 ActionMapping.class, 152 ActionForm.class, 153 HttpServletRequest .class, 154 HttpServletResponse .class}; 155 156 158 public ActionDispatcher(Action actionInstance) { 159 this(actionInstance, DEFAULT_FLAVOR); 160 } 161 162 163 public ActionDispatcher(Action actionInstance, int flavor) { 164 165 this.actionInstance = actionInstance; 166 this.flavor = flavor; 167 168 clazz = actionInstance.getClass(); 169 170 } 171 172 173 175 176 189 public ActionForward execute(ActionMapping mapping, 190 ActionForm form, 191 HttpServletRequest request, 192 HttpServletResponse response) 193 throws Exception { 194 195 if (isCancelled(request)) { 197 ActionForward af = cancelled(mapping, form, request, response); 198 if (af != null) { 199 return af; 200 } 201 } 202 String parameter = getParameter(mapping, form, request, response); 204 205 String name = getMethodName(mapping, form, request, response, parameter); 207 208 209 if ("execute".equals(name) || "perform".equals(name)) { 211 String message = 212 messages.getMessage("dispatch.recursive", mapping.getPath()); 213 214 log.error(message); 215 throw new ServletException (message); 216 } 217 218 219 return dispatchMethod(mapping, form, request, response, name); 221 222 } 223 224 225 232 protected ActionForward unspecified(ActionMapping mapping, 233 ActionForm form, 234 HttpServletRequest request, 235 HttpServletResponse response) 236 throws Exception { 237 238 239 String name = "unspecified"; 241 Method method = null; 242 try { 243 method = getMethod(name); 244 245 } catch (NoSuchMethodException e) { 246 String message = messages.getMessage("dispatch.parameter", 247 mapping.getPath(), 248 mapping.getParameter()); 249 250 log.error(message); 251 252 throw new ServletException (message); 253 } 254 255 return dispatchMethod(mapping, form, request, response, name, method); 256 257 } 258 259 265 protected ActionForward cancelled(ActionMapping mapping, 266 ActionForm form, 267 HttpServletRequest request, 268 HttpServletResponse response) 269 throws Exception { 270 271 String name = "cancelled"; 273 Method method = null; 274 try { 275 method = getMethod(name); 276 277 } catch (NoSuchMethodException e) { 278 return null; 279 } 280 281 return dispatchMethod(mapping, form, request, response, name, method); 282 283 } 284 285 287 288 291 protected ActionForward dispatchMethod(ActionMapping mapping, 292 ActionForm form, 293 HttpServletRequest request, 294 HttpServletResponse response, 295 String name) throws Exception { 296 297 if (name == null) { 300 return this.unspecified(mapping, form, request, response); 301 } 302 303 Method method = null; 305 try { 306 method = getMethod(name); 307 308 } catch (NoSuchMethodException e) { 309 String message = 310 messages.getMessage("dispatch.method", mapping.getPath(), name); 311 log.error(message, e); 312 throw e; 313 } 314 315 return dispatchMethod(mapping, form, request, response, name, method); 316 317 } 318 319 322 protected ActionForward dispatchMethod(ActionMapping mapping, 323 ActionForm form, 324 HttpServletRequest request, 325 HttpServletResponse response, 326 String name, 327 Method method) throws Exception { 328 329 ActionForward forward = null; 330 try { 331 Object args[] = {mapping, form, request, response}; 332 forward = (ActionForward) method.invoke(actionInstance, args); 333 334 } catch (ClassCastException e) { 335 String message = 336 messages.getMessage("dispatch.return", mapping.getPath(), name); 337 log.error(message, e); 338 throw e; 339 340 } catch (IllegalAccessException e) { 341 String message = 342 messages.getMessage("dispatch.error", mapping.getPath(), name); 343 log.error(message, e); 344 throw e; 345 346 } catch (InvocationTargetException e) { 347 Throwable t = e.getTargetException(); 350 if (t instanceof Exception ) { 351 throw ((Exception ) t); 352 } else { 353 String message = 354 messages.getMessage("dispatch.error", mapping.getPath(), name); 355 log.error(message, e); 356 throw new ServletException (t); 357 } 358 } 359 360 return (forward); 362 } 363 364 365 373 protected Method getMethod(String name) 374 throws NoSuchMethodException { 375 376 synchronized (methods) { 377 Method method = (Method ) methods.get(name); 378 if (method == null) { 379 method = clazz.getMethod(name, types); 380 methods.put(name, method); 381 } 382 return (method); 383 } 384 385 } 386 387 397 protected String getParameter(ActionMapping mapping, 398 ActionForm form, 399 HttpServletRequest request, 400 HttpServletResponse response) 401 throws Exception { 402 403 String parameter = mapping.getParameter(); 404 if ("".equals(parameter)) { 405 parameter = null; 406 } 407 408 if ((parameter == null) && (flavor == DEFAULT_FLAVOR)) { 409 return "method"; 411 } 412 413 if ((parameter == null) && 414 ((flavor == MAPPING_FLAVOR || flavor == DISPATCH_FLAVOR))) { 415 String message = 416 messages.getMessage("dispatch.handler", mapping.getPath()); 417 418 log.error(message); 419 420 throw new ServletException (message); 421 } 422 423 return parameter; 424 425 } 426 427 437 protected String getMethodName(ActionMapping mapping, 438 ActionForm form, 439 HttpServletRequest request, 440 HttpServletResponse response, 441 String parameter) 442 throws Exception { 443 444 445 if (flavor == MAPPING_FLAVOR) { 447 return parameter; 448 } 449 450 return request.getParameter(parameter); 452 } 453 454 466 protected boolean isCancelled(HttpServletRequest request) { 467 468 return (request.getAttribute(Globals.CANCEL_KEY) != null); 469 470 } 471 472 } 473 474 | Popular Tags |