1 16 17 package org.apache.struts.chain; 18 19 20 import org.apache.commons.beanutils.DynaBean; 21 import org.apache.commons.chain.Command; 22 import org.apache.commons.chain.Context; 23 import org.apache.commons.chain.web.WebContext; 24 import org.apache.struts.action.ActionForm; 25 import org.apache.struts.action.ActionMapping; 26 import org.apache.struts.action.ActionServlet; 27 import org.apache.struts.action.DynaActionForm; 28 import org.apache.struts.action.DynaActionFormClass; 29 import org.apache.struts.chain.Constants; 30 import org.apache.struts.chain.util.ClassUtils; 31 import org.apache.struts.config.ActionConfig; 32 import org.apache.struts.config.FormBeanConfig; 33 import org.apache.struts.config.ModuleConfig; 34 import org.apache.commons.logging.Log; 35 import org.apache.commons.logging.LogFactory; 36 37 38 44 45 public class CreateActionForm implements Command { 46 47 48 50 51 private String actionConfigKey = Constants.ACTION_CONFIG_KEY; 52 private String actionFormKey = Constants.ACTION_FORM_KEY; 53 private String actionServletKey = Constants.ACTION_SERVLET_KEY; 54 private String moduleConfigKey = Constants.MODULE_CONFIG_KEY; 55 56 private static final Log log = 57 LogFactory.getLog(CreateActionForm.class); 58 59 61 62 67 public String getActionConfigKey() { 68 69 return (this.actionConfigKey); 70 71 } 72 73 74 81 public void setActionConfigKey(String actionConfigKey) { 82 83 this.actionConfigKey = actionConfigKey; 84 85 } 86 87 88 93 public String getActionFormKey() { 94 95 return (this.actionFormKey); 96 97 } 98 99 100 107 public void setActionFormKey(String actionFormKey) { 108 109 this.actionFormKey = actionFormKey; 110 111 } 112 113 114 119 public String getActionServletKey() { 120 121 return (this.actionServletKey); 122 123 } 124 125 126 133 public void setActionServletKey(String actionServletKey) { 134 135 this.actionServletKey = actionServletKey; 136 137 } 138 139 144 public String getModuleConfigKey() { 145 146 return (this.moduleConfigKey); 147 148 } 149 150 151 158 public void setModuleConfigKey(String moduleConfigKey) { 159 160 this.moduleConfigKey = moduleConfigKey; 161 162 } 163 164 165 167 168 175 public boolean execute(Context context) throws Exception { 176 177 ActionConfig actionConfig = (ActionConfig) 179 context.get(getActionConfigKey()); 180 String name = actionConfig.getName(); 181 if (name == null) { 182 context.remove(getActionFormKey()); 183 return (false); 184 } 185 186 log.trace("Look up form-bean " + name); 187 188 FormBeanConfig formBeanConfig = 190 actionConfig.getModuleConfig().findFormBeanConfig(name); 191 if (formBeanConfig == null) { 192 log.warn("No FormBeanConfig found in module " 193 + actionConfig.getModuleConfig().getPrefix() 194 + " under name " + name); 195 context.remove(getActionFormKey()); 196 return (false); 197 } 198 199 WebContext wcontext = (WebContext) context; 201 ActionForm instance = null; 202 if ("session".equals(actionConfig.getScope())) { 203 instance = (ActionForm) 204 wcontext.getSessionScope().get(actionConfig.getAttribute()); 205 } 206 207 if (instance != null) { 209 log.trace("Found an instance in the session; test for reusability"); 210 if (formBeanConfig.getDynamic()) { 211 String className = 212 ((DynaBean) instance).getDynaClass().getName(); 213 if (className.equals(formBeanConfig.getName())) { 214 wcontext.put 215 (getActionFormKey(), instance); 216 222 log.debug("Using existing instance (dynamic)"); 223 return (false); 224 } 225 } else { 226 try { 227 Class configClass = 228 ClassUtils.getApplicationClass 229 (formBeanConfig.getType()); 230 if (configClass.isAssignableFrom(instance.getClass())) { 231 wcontext.put 232 (getActionFormKey(), instance); 233 239 log.debug("Using existing instance (non-dynamic)"); 240 return (false); 241 } 242 } catch (Exception e) { 243 log.debug("Error testing existing instance for reusability; just create a new instance", e); 244 } 245 } 246 } 247 248 log.trace("Make a new instance of: " + formBeanConfig); 249 if (formBeanConfig.getDynamic()) { 251 ModuleConfig moduleConfig = (ModuleConfig) 252 wcontext.get(getModuleConfigKey()); 253 DynaActionFormClass dynaClass = 254 DynaActionFormClass.createDynaActionFormClass(formBeanConfig); 255 instance = (ActionForm) dynaClass.newInstance(); 256 ((DynaActionForm) instance).initialize 257 ((ActionMapping) actionConfig); 258 } else { 259 instance = (ActionForm) 260 ClassUtils.getApplicationInstance(formBeanConfig.getType()); 261 } 262 263 ActionServlet servlet = (ActionServlet) 265 wcontext.get(getActionServletKey()); 266 instance.setServlet(servlet); 267 wcontext.put(getActionFormKey(), instance); 268 if ("session".equals(actionConfig.getScope())) { 269 wcontext.getSessionScope().put 270 (actionConfig.getAttribute(), instance); 271 } else if ("request".equals(actionConfig.getScope())) { 272 wcontext.getRequestScope().put 273 (actionConfig.getAttribute(), instance); 274 } 275 276 return (false); 277 278 } 279 280 281 } 282 | Popular Tags |