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 37 92 public abstract class DispatchAction extends Action { 93 94 95 97 98 101 protected Class clazz = this.getClass(); 102 103 104 107 protected static Log log = LogFactory.getLog(DispatchAction.class); 108 109 110 113 protected static MessageResources messages = 114 MessageResources.getMessageResources 115 ("org.apache.struts.actions.LocalStrings"); 116 117 118 124 protected HashMap methods = new HashMap (); 125 126 127 131 protected Class [] types = 132 { 133 ActionMapping.class, 134 ActionForm.class, 135 HttpServletRequest .class, 136 HttpServletResponse .class}; 137 138 139 140 142 143 157 public ActionForward execute(ActionMapping mapping, 158 ActionForm form, 159 HttpServletRequest request, 160 HttpServletResponse response) 161 throws Exception { 162 if (isCancelled(request)) { 163 ActionForward af = cancelled(mapping, form, request, response); 164 if (af != null) { 165 return af; 166 } 167 } 168 String parameter = mapping.getParameter(); 170 if (parameter == null) { 171 String message = 172 messages.getMessage("dispatch.handler", mapping.getPath()); 173 174 log.error(message); 175 176 throw new ServletException (message); 177 } 178 179 String name = getMethodName(mapping, form, request, response, parameter); 181 182 183 if ("execute".equals(name) || "perform".equals(name)){ 185 String message = 186 messages.getMessage("dispatch.recursive", mapping.getPath()); 187 188 log.error(message); 189 throw new ServletException (message); 190 } 191 192 193 return dispatchMethod(mapping, form, request, response, name); 195 196 } 197 198 199 200 201 207 protected ActionForward unspecified( 208 ActionMapping mapping, 209 ActionForm form, 210 HttpServletRequest request, 211 HttpServletResponse response) 212 throws Exception { 213 214 String message = 215 messages.getMessage( 216 "dispatch.parameter", 217 mapping.getPath(), 218 mapping.getParameter()); 219 220 log.error(message); 221 222 throw new ServletException (message); 223 } 224 225 231 protected ActionForward cancelled(ActionMapping mapping, 232 ActionForm form, 233 HttpServletRequest request, 234 HttpServletResponse response) 235 throws Exception { 236 237 return null; 238 } 239 240 242 243 247 protected ActionForward dispatchMethod(ActionMapping mapping, 248 ActionForm form, 249 HttpServletRequest request, 250 HttpServletResponse response, 251 String name) throws Exception { 252 253 if (name == null) { 256 return this.unspecified(mapping, form, request, response); 257 } 258 259 Method method = null; 261 try { 262 method = getMethod(name); 263 264 } catch(NoSuchMethodException e) { 265 String message = 266 messages.getMessage("dispatch.method", mapping.getPath(), name); 267 log.error(message, e); 268 throw e; 269 } 270 271 ActionForward forward = null; 272 try { 273 Object args[] = {mapping, form, request, response}; 274 forward = (ActionForward) method.invoke(this, args); 275 276 } catch(ClassCastException e) { 277 String message = 278 messages.getMessage("dispatch.return", mapping.getPath(), name); 279 log.error(message, e); 280 throw e; 281 282 } catch(IllegalAccessException e) { 283 String message = 284 messages.getMessage("dispatch.error", mapping.getPath(), name); 285 log.error(message, e); 286 throw e; 287 288 } catch(InvocationTargetException e) { 289 Throwable t = e.getTargetException(); 292 if (t instanceof Exception ) { 293 throw ((Exception ) t); 294 } else { 295 String message = 296 messages.getMessage("dispatch.error", mapping.getPath(), name); 297 log.error(message, e); 298 throw new ServletException (t); 299 } 300 } 301 302 return (forward); 304 } 305 306 307 316 protected Method getMethod(String name) 317 throws NoSuchMethodException { 318 319 synchronized(methods) { 320 Method method = (Method ) methods.get(name); 321 if (method == null) { 322 method = clazz.getMethod(name, types); 323 methods.put(name, method); 324 } 325 return (method); 326 } 327 328 } 329 330 342 protected String getMethodName(ActionMapping mapping, 343 ActionForm form, 344 HttpServletRequest request, 345 HttpServletResponse response, 346 String parameter) 347 throws Exception { 348 349 return request.getParameter(parameter); 352 } 353 354 } 355 | Popular Tags |