1 18 19 20 package org.apache.struts.config; 21 22 23 import java.io.Serializable ; 24 import java.util.HashMap ; 25 import org.apache.commons.beanutils.DynaBean; 26 import org.apache.commons.beanutils.MutableDynaClass; 27 import org.apache.struts.action.DynaActionForm; 28 import org.apache.struts.action.DynaActionFormClass; 29 import org.apache.struts.action.ActionServlet; 30 import org.apache.struts.action.ActionForm; 31 import org.apache.struts.validator.BeanValidatorForm; 32 33 34 42 43 public class FormBeanConfig implements Serializable { 44 45 46 48 49 52 protected boolean configured = false; 53 54 55 59 protected HashMap formProperties = new HashMap (); 60 61 62 65 protected String lock = ""; 66 67 68 70 71 74 protected transient DynaActionFormClass dynaActionFormClass; 75 76 81 public DynaActionFormClass getDynaActionFormClass() { 82 83 if (dynamic == false) { 84 throw new IllegalArgumentException ("ActionForm is not dynamic"); 85 } 86 synchronized (lock) { 87 if (dynaActionFormClass == null) { 88 dynaActionFormClass = new DynaActionFormClass(this); 89 } 90 } 91 return dynaActionFormClass; 92 } 93 94 95 99 protected boolean dynamic = false; 100 101 public boolean getDynamic() { 102 return (this.dynamic); 103 } 104 105 109 public void setDynamic(boolean dynamic) { 110 if (configured) { 111 throw new IllegalStateException ("Configuration is frozen"); 112 } 113 ; } 115 116 122 protected String name = null; 123 124 public String getName() { 125 return (this.name); 126 } 127 128 public void setName(String name) { 129 if (configured) { 130 throw new IllegalStateException ("Configuration is frozen"); 131 } 132 this.name = name; 133 } 134 135 136 140 protected String type = null; 141 142 public String getType() { 143 return (this.type); 144 } 145 146 public void setType(String type) { 147 if (configured) { 148 throw new IllegalStateException ("Configuration is frozen"); 149 } 150 this.type = type; 151 Class dynaBeanClass = DynaActionForm.class; 152 Class formBeanClass = formBeanClass(); 153 if (formBeanClass != null) { 154 if (dynaBeanClass.isAssignableFrom(formBeanClass)) { 155 this.dynamic = true; 156 } else { 157 this.dynamic = false; 158 } 159 } else { 160 this.dynamic = false; 161 } 162 } 163 164 167 protected boolean restricted = false; 168 169 174 public boolean isRestricted() { 175 return restricted; 176 } 177 178 183 public void setRestricted(boolean restricted) { 184 this.restricted = restricted; 185 } 186 187 188 190 191 203 public ActionForm createActionForm(ActionServlet servlet) 204 throws IllegalAccessException , InstantiationException { 205 206 Object obj = null; 207 208 if (getDynamic()) { 210 obj = getDynaActionFormClass().newInstance(); 211 } else { 212 obj = formBeanClass().newInstance(); 213 } 214 215 ActionForm form = null; 216 if (obj instanceof ActionForm) { 217 form = (ActionForm)obj; 218 } else { 219 form = new BeanValidatorForm(obj); 220 } 221 222 form.setServlet(servlet); 223 224 if (form instanceof DynaBean && 225 ((DynaBean)form).getDynaClass() instanceof MutableDynaClass) { 226 DynaBean dynaBean = (DynaBean)form; 227 MutableDynaClass dynaClass = (MutableDynaClass)dynaBean.getDynaClass(); 228 229 dynaClass.setRestricted(false); 231 FormPropertyConfig props[] = findFormPropertyConfigs(); 232 for (int i = 0; i < props.length; i++) { 233 dynaClass.add(props[i].getName(), props[i].getTypeClass()); 234 dynaBean.set(props[i].getName(), props[i].initial()); 235 } 236 dynaClass.setRestricted(isRestricted()); 237 238 } 239 240 return form; 241 242 } 243 244 245 254 public void addFormPropertyConfig(FormPropertyConfig config) { 255 256 if (configured) { 257 throw new IllegalStateException ("Configuration is frozen"); 258 } 259 if (formProperties.containsKey(config.getName())) { 260 throw new IllegalArgumentException ("Property " + 261 config.getName() + 262 " already defined"); 263 } 264 formProperties.put(config.getName(), config); 265 266 } 267 268 269 275 public FormPropertyConfig findFormPropertyConfig(String name) { 276 277 return ((FormPropertyConfig) formProperties.get(name)); 278 279 } 280 281 282 286 public FormPropertyConfig[] findFormPropertyConfigs() { 287 288 FormPropertyConfig results[] = 289 new FormPropertyConfig[formProperties.size()]; 290 return ((FormPropertyConfig[]) formProperties.values().toArray(results)); 291 292 } 293 294 295 298 public void freeze() { 299 300 configured = true; 301 302 FormPropertyConfig[] fpconfigs = findFormPropertyConfigs(); 303 for (int i = 0; i < fpconfigs.length; i++) { 304 fpconfigs[i].freeze(); 305 } 306 307 } 308 309 310 315 public void removeFormPropertyConfig(FormPropertyConfig config) { 316 317 if (configured) { 318 throw new IllegalStateException ("Configuration is frozen"); 319 } 320 formProperties.remove(config.getName()); 321 322 } 323 324 325 328 public String toString() { 329 330 StringBuffer sb = new StringBuffer ("FormBeanConfig["); 331 sb.append("name="); 332 sb.append(this.name); 333 sb.append(",type="); 334 sb.append(this.type); 335 sb.append("]"); 336 return (sb.toString()); 337 338 } 339 340 341 343 344 350 protected Class formBeanClass() { 351 352 ClassLoader classLoader = 353 Thread.currentThread().getContextClassLoader(); 354 if (classLoader == null) { 355 classLoader = this.getClass().getClassLoader(); 356 } 357 try { 358 return (classLoader.loadClass(getType())); 359 } catch (Exception e) { 360 return (null); 361 } 362 363 } 364 365 366 } 367 | Popular Tags |