1 18 19 20 package org.apache.struts.action; 21 22 23 import java.io.Serializable ; 24 import java.util.HashMap ; 25 26 import org.apache.commons.beanutils.DynaBean; 27 import org.apache.commons.beanutils.DynaClass; 28 import org.apache.commons.beanutils.DynaProperty; 29 import org.apache.struts.config.FormBeanConfig; 30 import org.apache.struts.config.FormPropertyConfig; 31 import org.apache.struts.config.ModuleConfig; 32 import org.apache.struts.util.RequestUtils; 33 34 35 46 47 public class DynaActionFormClass implements DynaClass, Serializable { 48 49 50 52 53 66 public DynaActionFormClass(FormBeanConfig config) { 67 68 introspect(config); 69 70 } 71 72 73 75 76 80 protected transient Class beanClass = null; 81 82 83 86 protected FormBeanConfig config = null; 87 88 89 92 protected String name = null; 93 94 95 98 protected DynaProperty properties[] = null; 99 100 101 107 protected HashMap propertiesMap = new HashMap (); 108 109 110 112 113 119 public String getName() { 120 121 return (this.name); 122 123 } 124 125 126 135 public DynaProperty getDynaProperty(String name) { 136 137 if (name == null) { 138 throw new IllegalArgumentException 139 ("No property name specified"); 140 } 141 return ((DynaProperty) propertiesMap.get(name)); 142 143 } 144 145 146 151 public DynaProperty[] getDynaProperties() { 152 153 return (properties); 154 158 } 159 160 161 174 public DynaBean newInstance() 175 throws IllegalAccessException , InstantiationException { 176 177 DynaActionForm dynaBean = 178 (DynaActionForm) getBeanClass().newInstance(); 179 dynaBean.setDynaActionFormClass(this); 180 FormPropertyConfig props[] = config.findFormPropertyConfigs(); 181 for (int i = 0; i < props.length; i++) { 182 dynaBean.set(props[i].getName(), props[i].initial()); 183 } 184 return (dynaBean); 185 186 } 187 188 189 191 192 195 public String toString() { 196 197 StringBuffer sb = new StringBuffer ("DynaActionFormBean[name="); 198 sb.append(name); 199 DynaProperty props[] = getDynaProperties(); 200 if (props == null) { 201 props = new DynaProperty[0]; 202 } 203 for (int i = 0; i < props.length; i++) { 204 sb.append(','); 205 sb.append(props[i].getName()); 206 sb.append('/'); 207 sb.append(props[i].getType()); 208 } 209 sb.append("]"); 210 return (sb.toString()); 211 212 } 213 214 215 217 218 221 public static void clear() { 222 } 223 224 225 229 public static DynaActionFormClass 230 createDynaActionFormClass(FormBeanConfig config) { 231 232 return config.getDynaActionFormClass(); 233 234 } 235 236 237 239 240 246 protected Class getBeanClass() { 247 248 if (beanClass == null) { 249 introspect(config); 250 } 251 return (beanClass); 252 253 } 254 255 256 267 protected void introspect(FormBeanConfig config) { 268 269 this.config = config; 270 271 try { 273 beanClass = RequestUtils.applicationClass(config.getType()); 274 } catch (Throwable t) { 275 throw new IllegalArgumentException 276 ("Cannot instantiate ActionFormBean class '" + 277 config.getType() + "': " + t); 278 } 279 if (!DynaActionForm.class.isAssignableFrom(beanClass)) { 280 throw new IllegalArgumentException 281 ("Class '" + config.getType() + "' is not a subclass of " + 282 "'org.apache.struts.action.DynaActionForm'"); 283 } 284 285 this.name = config.getName(); 287 288 FormPropertyConfig descriptors[] = config.findFormPropertyConfigs(); 290 if (descriptors == null) { 291 descriptors = new FormPropertyConfig[0]; 292 } 293 294 properties = new DynaProperty[descriptors.length]; 296 for (int i = 0; i < descriptors.length; i++) { 297 properties[i] = 298 new DynaProperty(descriptors[i].getName(), 299 descriptors[i].getTypeClass()); 300 propertiesMap.put(properties[i].getName(), 301 properties[i]); 302 } 303 304 } 305 306 307 } 308 | Popular Tags |