1 18 19 20 package org.apache.struts.config; 21 22 23 import java.io.Serializable ; 24 import java.lang.reflect.Array ; 25 import org.apache.commons.beanutils.ConvertUtils; 26 import org.apache.commons.logging.Log; 27 import org.apache.commons.logging.LogFactory; 28 29 30 38 39 public class FormPropertyConfig implements Serializable { 40 41 44 private static final Log log = 45 LogFactory.getLog(FormPropertyConfig.class); 46 47 48 50 51 54 public FormPropertyConfig() { 55 56 super(); 57 58 } 59 60 61 68 public FormPropertyConfig(String name, String type, String initial) { 69 70 this(name, type, initial, 0); 71 72 } 73 74 75 84 public FormPropertyConfig(String name, String type, 85 String initial, int size) { 86 87 super(); 88 setName(name); 89 setType(type); 90 setInitial(initial); 91 setSize(size); 92 93 } 94 95 96 98 99 102 protected boolean configured = false; 103 104 105 107 108 111 protected String initial = null; 112 113 public String getInitial() { 114 return (this.initial); 115 } 116 117 public void setInitial(String initial) { 118 if (configured) { 119 throw new IllegalStateException ("Configuration is frozen"); 120 } 121 this.initial = initial; 122 } 123 124 125 128 protected String name = null; 129 130 public String getName() { 131 return (this.name); 132 } 133 134 public void setName(String name) { 135 if (configured) { 136 throw new IllegalStateException ("Configuration is frozen"); 137 } 138 this.name = name; 139 } 140 141 142 149 protected int size = 0; 150 151 public int getSize() { 152 return (this.size); 153 } 154 155 public void setSize(int size) { 156 if (configured) { 157 throw new IllegalStateException ("Configuration is frozen"); 158 } 159 if (size < 0) { 160 throw new IllegalArgumentException ("size < 0"); 161 } 162 this.size = size; 163 } 164 165 166 167 172 protected String type = null; 173 174 public String getType() { 175 return (this.type); 176 } 177 178 public void setType(String type) { 179 if (configured) { 180 throw new IllegalStateException ("Configuration is frozen"); 181 } 182 this.type = type; 183 } 184 185 186 191 public Class getTypeClass() { 192 193 String baseType = getType(); 195 boolean indexed = false; 196 if (baseType.endsWith("[]")) { 197 baseType = baseType.substring(0, baseType.length() - 2); 198 indexed = true; 199 } 200 201 Class baseClass = null; 203 if ("boolean".equals(baseType)) { 204 baseClass = Boolean.TYPE; 205 } else if ("byte".equals(baseType)) { 206 baseClass = Byte.TYPE; 207 } else if ("char".equals(baseType)) { 208 baseClass = Character.TYPE; 209 } else if ("double".equals(baseType)) { 210 baseClass = Double.TYPE; 211 } else if ("float".equals(baseType)) { 212 baseClass = Float.TYPE; 213 } else if ("int".equals(baseType)) { 214 baseClass = Integer.TYPE; 215 } else if ("long".equals(baseType)) { 216 baseClass = Long.TYPE; 217 } else if ("short".equals(baseType)) { 218 baseClass = Short.TYPE; 219 } else { 220 ClassLoader classLoader = 221 Thread.currentThread().getContextClassLoader(); 222 if (classLoader == null) { 223 classLoader = this.getClass().getClassLoader(); 224 } 225 try { 226 baseClass = classLoader.loadClass(baseType); 227 } catch (Throwable t) { 228 baseClass = null; 229 } 230 } 231 232 if (indexed) { 234 return (Array.newInstance(baseClass, 0).getClass()); 235 } else { 236 return (baseClass); 237 } 238 239 } 240 241 242 243 245 246 277 public Object initial() { 278 279 Object initialValue = null; 280 try { 281 Class clazz = getTypeClass(); 282 if (clazz.isArray()) { 283 if (initial != null) { 284 initialValue = 285 ConvertUtils.convert(initial, clazz); 286 } else { 287 initialValue = 288 Array.newInstance(clazz.getComponentType(), size); 289 if (!(clazz.getComponentType().isPrimitive())) { 290 for (int i = 0; i < size; i++) { 291 try { 292 Array.set(initialValue, i, 293 clazz.getComponentType().newInstance()); 294 } catch (Throwable t) { 295 log.error("Unable to create instance of " + clazz.getName() + 296 " for property=" + name+ 297 ", type=" + type + 298 ", initial=" + initial + 299 ", size=" + size + "."); 300 } 302 } 303 } 304 } 305 } else { 306 if (initial != null) { 307 initialValue = ConvertUtils.convert(initial, clazz); 308 } else { 309 initialValue = clazz.newInstance(); 310 } 311 } 312 } catch (Throwable t) { 313 initialValue = null; 314 } 315 return (initialValue); 316 317 } 318 319 320 323 public void freeze() { 324 325 configured = true; 326 327 } 328 329 330 333 public String toString() { 334 335 StringBuffer sb = new StringBuffer ("FormPropertyConfig["); 336 sb.append("name="); 337 sb.append(this.name); 338 sb.append(",type="); 339 sb.append(this.type); 340 sb.append(",initial="); 341 sb.append(this.initial); 342 sb.append("]"); 343 return (sb.toString()); 344 345 } 346 347 348 } 349 | Popular Tags |