1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.struts.action;
19
20 import org.apache.commons.beanutils.DynaBean;
21 import org.apache.commons.beanutils.DynaClass;
22 import org.apache.commons.beanutils.DynaProperty;
23 import org.apache.struts.config.FormBeanConfig;
24 import org.apache.struts.config.FormPropertyConfig;
25 import org.apache.struts.util.RequestUtils;
26
27 import java.io.Serializable;
28
29 import java.util.HashMap;
30
31 /***
32 * <p>Implementation of <code>DynaClass</code> for <code>DynaActionForm</code>
33 * classes that allow developers to define ActionForms without having to
34 * individually code all of the classes. <strong>NOTE</strong> - This class is
35 * only used in the internal implementation of dynamic action form beans.
36 * Application developers never need to consult this documentation.</p>
37 *
38 * @version $Rev: 421119 $ $Date: 2006-01-17 07:26:20 -0500 (Tue, 17 Jan 2006)
39 * $
40 * @since Struts 1.1
41 */
42 public class DynaActionFormClass implements DynaClass, Serializable {
43
44
45 /***
46 * <p>The <code>DynaActionForm</code> implementation <code>Class</code>
47 * which we will use to create new bean instances.</p>
48 */
49 protected transient Class beanClass = null;
50
51 /***
52 * <p>The form bean configuration information for this class.</p>
53 */
54 protected FormBeanConfig config = null;
55
56 /***
57 * <p>The "dynamic class name" for this <code>DynaClass</code>.</p>
58 */
59 protected String name = null;
60
61 /***
62 * <p>The set of dynamic properties that are part of this DynaClass.</p>
63 */
64 protected DynaProperty[] properties = null;
65
66 /***
67 * <p>The set of dynamic properties that are part of this
68 * <code>DynaClass</code>, keyed by the property name. Individual
69 * descriptor instances will be the same instances as those in the
70 * <code>properties</code> list.
71 */
72 protected HashMap propertiesMap = new HashMap();
73
74
75
76 /***
77 * <p>Construct a new <code>DynaActionFormClass</code> for the specified
78 * form bean configuration. This constructor is private;
79 * <code>DynaActionFormClass</code> instances will be created as needed
80 * via calls to the static <code>createDynaActionFormClass()</code>
81 * method.</p>
82 *
83 * @param config The FormBeanConfig instance describing the properties of
84 * the bean to be created
85 * @throws IllegalArgumentException if the bean implementation class
86 * specified in the configuration is not
87 * DynaActionForm (or a subclass of
88 * DynaActionForm)
89 */
90 public DynaActionFormClass(FormBeanConfig config) {
91 introspect(config);
92 }
93
94
95
96 /***
97 * <p>Return the name of this <code>DynaClass</code> (analogous to the
98 * <code>getName()</code> method of <code>java.lang.Class</code>, which
99 * allows the same <code>DynaClass</code> implementation class to support
100 * different dynamic classes, with different sets of properties.
101 *
102 * @return The name of this <code>DynaClass</code>.
103 */
104 public String getName() {
105 return (this.name);
106 }
107
108 /***
109 * <p>Return a property descriptor for the specified property, if it
110 * exists; otherwise, return <code>null</code>.</p>
111 *
112 * @param name Name of the dynamic property for which a descriptor is
113 * requested
114 * @return A property descriptor for the specified property.
115 * @throws IllegalArgumentException if no property name is specified
116 */
117 public DynaProperty getDynaProperty(String name) {
118 if (name == null) {
119 throw new IllegalArgumentException("No property name specified");
120 }
121
122 return ((DynaProperty) propertiesMap.get(name));
123 }
124
125 /***
126 * <p>Return an array of <code>DynaProperty</code>s for the properties
127 * currently defined in this <code>DynaClass</code>. If no properties are
128 * defined, a zero-length array will be returned.</p>
129 *
130 * @return An array of property instances for this class.
131 */
132 public DynaProperty[] getDynaProperties() {
133 return (properties);
134
135
136
137
138 }
139
140 /***
141 * <p>Instantiate and return a new {@link DynaActionForm} instance,
142 * associated with this <code>DynaActionFormClass</code>. The properties
143 * of the returned {@link DynaActionForm} will have been initialized to
144 * the default values specified in the form bean configuration
145 * information.</p>
146 *
147 * @return A new {@link DynaActionForm} instance.
148 * @throws IllegalAccessException if the Class or the appropriate
149 * constructor is not accessible
150 * @throws InstantiationException if this Class represents an abstract
151 * class, an array class, a primitive type,
152 * or void; or if instantiation fails for
153 * some other reason
154 */
155 public DynaBean newInstance()
156 throws IllegalAccessException, InstantiationException {
157 DynaActionForm dynaBean = (DynaActionForm) getBeanClass().newInstance();
158
159 dynaBean.setDynaActionFormClass(this);
160
161 FormPropertyConfig[] props = config.findFormPropertyConfigs();
162
163 for (int i = 0; i < props.length; i++) {
164 dynaBean.set(props[i].getName(), props[i].initial());
165 }
166
167 return (dynaBean);
168 }
169
170
171
172 /***
173 * <p>Render a <code>String</code> representation of this object.</p>
174 *
175 * @return The string representation of this instance.
176 */
177 public String toString() {
178 StringBuffer sb = new StringBuffer("DynaActionFormBean[name=");
179
180 sb.append(name);
181
182 DynaProperty[] props = getDynaProperties();
183
184 if (props == null) {
185 props = new DynaProperty[0];
186 }
187
188 for (int i = 0; i < props.length; i++) {
189 sb.append(',');
190 sb.append(props[i].getName());
191 sb.append('/');
192 sb.append(props[i].getType());
193 }
194
195 sb.append("]");
196
197 return (sb.toString());
198 }
199
200
201
202 /***
203 * Return the <code>DynaActionFormClass</code> instance for the specified
204 * form bean configuration instance.
205 *
206 * @param config The config for which the class should be created.
207 * @return The instance for the specified form bean config.
208 */
209 public static DynaActionFormClass createDynaActionFormClass(
210 FormBeanConfig config) {
211 return config.getDynaActionFormClass();
212 }
213
214
215
216 /***
217 * <p>Return the implementation class we are using to construct new
218 * instances, re-introspecting our {@link FormBeanConfig} if necessary
219 * (that is, after being deserialized, since <code>beanClass</code> is
220 * marked transient).</p>
221 *
222 * @return The implementation class used to construct new instances.
223 */
224 protected Class getBeanClass() {
225 if (beanClass == null) {
226 introspect(config);
227 }
228
229 return (beanClass);
230 }
231
232 /***
233 * <p>Introspect our form bean configuration to identify the supported
234 * properties.</p>
235 *
236 * @param config The FormBeanConfig instance describing the properties of
237 * the bean to be created
238 * @throws IllegalArgumentException if the bean implementation class
239 * specified in the configuration is not
240 * DynaActionForm (or a subclass of
241 * DynaActionForm)
242 */
243 protected void introspect(FormBeanConfig config) {
244 this.config = config;
245
246
247 try {
248 beanClass = RequestUtils.applicationClass(config.getType());
249 } catch (Throwable t) {
250 throw new IllegalArgumentException(
251 "Cannot instantiate ActionFormBean class '" + config.getType()
252 + "': " + t);
253 }
254
255 if (!DynaActionForm.class.isAssignableFrom(beanClass)) {
256 throw new IllegalArgumentException("Class '" + config.getType()
257 + "' is not a subclass of "
258 + "'org.apache.struts.action.DynaActionForm'");
259 }
260
261
262 this.name = config.getName();
263
264
265 FormPropertyConfig[] descriptors = config.findFormPropertyConfigs();
266
267 if (descriptors == null) {
268 descriptors = new FormPropertyConfig[0];
269 }
270
271
272 properties = new DynaProperty[descriptors.length];
273
274 for (int i = 0; i < descriptors.length; i++) {
275 properties[i] =
276 new DynaProperty(descriptors[i].getName(),
277 descriptors[i].getTypeClass());
278 propertiesMap.put(properties[i].getName(), properties[i]);
279 }
280 }
281
282
283 }