KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > struts > config > FormPropertyConfig


1 /*
2  * $Id: FormPropertyConfig.java 54929 2004-10-16 16:38:42Z germuska $
3  *
4  * Copyright 1999-2004 The Apache Software Foundation.
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */

18
19
20 package org.apache.struts.config;
21
22
23 import java.io.Serializable JavaDoc;
24 import java.lang.reflect.Array JavaDoc;
25 import org.apache.commons.beanutils.ConvertUtils;
26 import org.apache.commons.logging.Log;
27 import org.apache.commons.logging.LogFactory;
28
29
30 /**
31  * <p>A JavaBean representing the configuration information of a
32  * <code>&lt;form-property&gt;</code> element in a Struts
33  * configuration file.<p>
34  *
35  * @version $Rev: 54929 $ $Date: 2004-10-16 17:38:42 +0100 (Sat, 16 Oct 2004) $
36  * @since Struts 1.1
37  */

38
39 public class FormPropertyConfig implements Serializable JavaDoc {
40
41     /**
42      * The logging instance
43      */

44     private static final Log log =
45         LogFactory.getLog(FormPropertyConfig.class);
46
47
48     // ----------------------------------------------------------- Constructors
49

50
51     /**
52      * Standard no-arguments constructor for dynamic instantiation.
53      */

54     public FormPropertyConfig() {
55
56         super();
57
58     }
59
60
61     /**
62      * Constructor that preconfigures the relevant properties.
63      *
64      * @param name Name of this property
65      * @param type Fully qualified class name of this property
66      * @param initial Initial value of this property (if any)
67      */

68     public FormPropertyConfig(String JavaDoc name, String JavaDoc type, String JavaDoc initial) {
69
70         this(name, type, initial, 0);
71
72     }
73
74
75     /**
76      * Constructor that preconfigures the relevant properties.
77      *
78      * @param name Name of this property
79      * @param type Fully qualified class name of this property
80      * @param initial Initial value of this property (if any)
81      * @param size Size of the array to be created if this property is an
82      * array with no defined initial value
83      */

84     public FormPropertyConfig(String JavaDoc name, String JavaDoc type,
85                               String JavaDoc initial, int size) {
86
87         super();
88         setName(name);
89         setType(type);
90         setInitial(initial);
91         setSize(size);
92
93     }
94
95
96     // ----------------------------------------------------- Instance Variables
97

98
99     /**
100      * Has this component been completely configured?
101      */

102     protected boolean configured = false;
103
104
105     // ------------------------------------------------------------- Properties
106

107
108     /**
109      * String representation of the initial value for this property.
110      */

111     protected String JavaDoc initial = null;
112
113     public String JavaDoc getInitial() {
114         return (this.initial);
115     }
116
117     public void setInitial(String JavaDoc initial) {
118         if (configured) {
119             throw new IllegalStateException JavaDoc("Configuration is frozen");
120         }
121         this.initial = initial;
122     }
123
124
125     /**
126      * The JavaBean property name of the property described by this element.
127      */

128     protected String JavaDoc name = null;
129
130     public String JavaDoc getName() {
131         return (this.name);
132     }
133
134     public void setName(String JavaDoc name) {
135         if (configured) {
136             throw new IllegalStateException JavaDoc("Configuration is frozen");
137         }
138         this.name = name;
139     }
140
141
142     /**
143      * <p>The size of the array to be created if this property is an array
144      * type and there is no specified <code>initial</code> value. This
145      * value must be non-negative.</p>
146      *
147      * @since Struts 1.1
148      */

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 JavaDoc("Configuration is frozen");
158         }
159         if (size < 0) {
160             throw new IllegalArgumentException JavaDoc("size < 0");
161         }
162         this.size = size;
163     }
164         
165
166
167     /**
168      * The fully qualified Java class name of the implementation class
169      * of this bean property, optionally followed by <code>[]</code> to
170      * indicate that the property is indexed.
171      */

172     protected String JavaDoc type = null;
173
174     public String JavaDoc getType() {
175         return (this.type);
176     }
177
178     public void setType(String JavaDoc type) {
179         if (configured) {
180             throw new IllegalStateException JavaDoc("Configuration is frozen");
181         }
182         this.type = type;
183     }
184
185
186     /**
187      * Return a Class corresponds to the value specified for the
188      * <code>type</code> property, taking into account the trailing "[]"
189      * for arrays (as well as the ability to specify primitive Java types).
190      */

191     public Class JavaDoc getTypeClass() {
192
193         // Identify the base class (in case an array was specified)
194
String JavaDoc baseType = getType();
195         boolean indexed = false;
196         if (baseType.endsWith("[]")) {
197             baseType = baseType.substring(0, baseType.length() - 2);
198             indexed = true;
199         }
200
201         // Construct an appropriate Class instance for the base class
202
Class JavaDoc 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 JavaDoc 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 JavaDoc t) {
228                 baseClass = null;
229             }
230         }
231
232         // Return the base class or an array appropriately
233
if (indexed) {
234             return (Array.newInstance(baseClass, 0).getClass());
235         } else {
236             return (baseClass);
237         }
238
239     }
240
241
242
243     // --------------------------------------------------------- Public Methods
244

245
246     /**
247      * <p>Return an object representing the initial value of this property.
248      * This is calculated according to the following algorithm:</p>
249      * <ul>
250      * <li>If the value you have specified for the <code>type</code>
251      * property represents an array (i.e. it ends with "[]"):
252      * <ul>
253      * <li>If you have specified a value for the <code>initial</code>
254      * property, <code>ConvertUtils.convert</code> will be
255      * called to convert it into an instance of the specified
256      * array type.</li>
257      * <li>If you have not specified a value for the <code>initial</code>
258      * property, an array of the length specified by the
259      * <code>size</code> property will be created. Each element
260      * of the array will be instantiated via the zero-args constructor
261      * on the specified class (if any). Otherwise, <code>null</code>
262      * will be returned.</li>
263      * </ul></li>
264      * <li>If the value you have specified for the <code>type</code>
265      * property does not represent an array:
266      * <ul>
267      * <li>If you have specified a value for the <code>initial</code>
268      * property, <code>ConvertUtils.convert</code>
269      * will be called to convert it into an object instance.</li>
270      * <li>If you have not specified a value for the <code>initial</code>
271      * attribute, Struts will instantiate an instance via the
272      * zero-args constructor on the specified class (if any).
273      * Otherwise, <code>null</code> will be returned.</li>
274      * </ul></li>
275      * </ul>
276      */

277     public Object JavaDoc initial() {
278
279         Object JavaDoc initialValue = null;
280         try {
281             Class JavaDoc 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 JavaDoc 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                                 //FIXME: Should we just dump the entire application/module ?
301
}
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 JavaDoc t) {
313             initialValue = null;
314         }
315         return (initialValue);
316
317     }
318
319
320     /**
321      * Freeze the configuration of this component.
322      */

323     public void freeze() {
324
325         configured = true;
326
327     }
328
329
330     /**
331      * Return a String representation of this object.
332      */

333     public String JavaDoc toString() {
334
335         StringBuffer JavaDoc sb = new StringBuffer JavaDoc("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