1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 package org.apache.struts.config;
22
23 import java.lang.reflect.Array;
24 import java.lang.reflect.InvocationTargetException;
25
26 import org.apache.commons.beanutils.ConvertUtils;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
29
30
31
32
33
34
35
36
37
38 public class FormPropertyConfig extends BaseConfig {
39 private static final long serialVersionUID = 8436264202472421426L;
40
41
42
43
44 private transient final Logger log =
45 LoggerFactory.getLogger(FormPropertyConfig.class);
46
47
48
49
50
51
52
53 protected String initial = null;
54
55
56
57
58 protected String name = null;
59
60
61
62
63
64
65
66
67
68
69 protected String reset = null;
70
71
72
73
74
75
76
77
78 protected int size = 0;
79
80
81
82
83
84
85 protected String type = null;
86
87
88
89
90
91
92 public FormPropertyConfig() {
93 super();
94 }
95
96
97
98
99
100
101
102
103 public FormPropertyConfig(String name, String type, String initial) {
104 this(name, type, initial, 0);
105 }
106
107
108
109
110
111
112
113
114
115
116 public FormPropertyConfig(String name, String type, String initial,
117 String reset) {
118 this(name, type, initial, reset, 0);
119 }
120
121
122
123
124
125
126
127
128
129
130 public FormPropertyConfig(String name, String type, String initial, int size) {
131 this(name, type, initial, null, size);
132 }
133
134
135
136
137
138
139
140
141
142
143
144
145 public FormPropertyConfig(String name, String type, String initial,
146 String reset, int size) {
147 super();
148 setName(name);
149 setType(type);
150 setInitial(initial);
151 setReset(reset);
152 setSize(size);
153 }
154
155 public String getInitial() {
156 return (this.initial);
157 }
158
159 public void setInitial(String initial) {
160 if (configured) {
161 throw new IllegalStateException("Configuration is frozen");
162 }
163
164 this.initial = initial;
165 }
166
167 public String getName() {
168 return (this.name);
169 }
170
171 public void setName(String name) {
172 if (configured) {
173 throw new IllegalStateException("Configuration is frozen");
174 }
175
176 this.name = name;
177 }
178
179 public String getReset() {
180 return (this.reset);
181 }
182
183 public void setReset(String reset) {
184 if (configured) {
185 throw new IllegalStateException("Configuration is frozen");
186 }
187
188 this.reset = reset;
189 }
190
191 public int getSize() {
192 return (this.size);
193 }
194
195 public void setSize(int size) {
196 if (configured) {
197 throw new IllegalStateException("Configuration is frozen");
198 }
199
200 if (size < 0) {
201 throw new IllegalArgumentException("size < 0");
202 }
203
204 this.size = size;
205 }
206
207 public String getType() {
208 return (this.type);
209 }
210
211 public void setType(String type) {
212 if (configured) {
213 throw new IllegalStateException("Configuration is frozen");
214 }
215
216 this.type = type;
217 }
218
219
220
221
222
223
224 public Class<?> getTypeClass() {
225
226 String baseType = getType();
227 boolean indexed = false;
228
229 if (baseType.endsWith("[]")) {
230 baseType = baseType.substring(0, baseType.length() - 2);
231 indexed = true;
232 }
233
234
235 Class<?> baseClass = null;
236
237 if ("boolean".equals(baseType)) {
238 baseClass = Boolean.TYPE;
239 } else if ("byte".equals(baseType)) {
240 baseClass = Byte.TYPE;
241 } else if ("char".equals(baseType)) {
242 baseClass = Character.TYPE;
243 } else if ("double".equals(baseType)) {
244 baseClass = Double.TYPE;
245 } else if ("float".equals(baseType)) {
246 baseClass = Float.TYPE;
247 } else if ("int".equals(baseType)) {
248 baseClass = Integer.TYPE;
249 } else if ("long".equals(baseType)) {
250 baseClass = Long.TYPE;
251 } else if ("short".equals(baseType)) {
252 baseClass = Short.TYPE;
253 } else {
254 ClassLoader classLoader =
255 Thread.currentThread().getContextClassLoader();
256
257 if (classLoader == null) {
258 classLoader = this.getClass().getClassLoader();
259 }
260
261 try {
262 baseClass = classLoader.loadClass(baseType);
263 } catch (ClassNotFoundException ex) {
264 log.error("Class '{}' not found for property '{}'",
265 baseType, name);
266 baseClass = null;
267 }
268 }
269
270
271 if (indexed) {
272 return (Array.newInstance(baseClass, 0).getClass());
273 } else {
274 return (baseClass);
275 }
276 }
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321 public Object initial() {
322 Object initialValue = null;
323
324 try {
325 Class<?> clazz = getTypeClass();
326
327 if (clazz.isArray()) {
328 if (initial != null) {
329 initialValue = ConvertUtils.convert(initial, clazz);
330 } else {
331 initialValue =
332 Array.newInstance(clazz.getComponentType(), size);
333
334 if (!(clazz.getComponentType().isPrimitive())) {
335 for (int i = 0; i < size; i++) {
336 try {
337 Array.set(initialValue, i,
338 clazz.getComponentType().getDeclaredConstructor().newInstance());
339 } catch (Throwable t) {
340 log.error("Unable to create instance of {} for property={}, "
341 + "type={}, initial={}, size={}.",
342 clazz.getName(), name, type, initial, size);
343
344
345 }
346 }
347 }
348 }
349 } else {
350 if (initial != null) {
351 initialValue = ConvertUtils.convert(initial, clazz);
352 } else {
353 initialValue = clazz.getDeclaredConstructor().newInstance();
354 }
355 }
356 } catch (Throwable t) {
357 initialValue = null;
358 }
359
360 return (initialValue);
361 }
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382 public void inheritFrom(FormPropertyConfig config)
383 throws IllegalAccessException, InvocationTargetException,
384 InstantiationException, ClassNotFoundException {
385 if (configured) {
386 throw new IllegalStateException("Configuration is frozen");
387 }
388
389 if (getInitial() == null) {
390 setInitial(config.getInitial());
391 }
392
393 if (getName() == null) {
394 setName(config.getName());
395 }
396
397 if (getSize() == 0) {
398 setSize(config.getSize());
399 }
400
401 if (getType() == null) {
402 setType(config.getType());
403 }
404
405 inheritProperties(config);
406 }
407
408
409
410
411 public String toString() {
412 StringBuilder sb = new StringBuilder("FormPropertyConfig[");
413
414 sb.append("name=");
415 sb.append(this.name);
416 sb.append(",type=");
417 sb.append(this.type);
418 sb.append(",initial=");
419 sb.append(this.initial);
420 sb.append(",reset=");
421 sb.append(this.reset);
422 sb.append("]");
423
424 return (sb.toString());
425 }
426 }