KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > struts > action > DynaActionFormClass


1 /*
2  * $Id: DynaActionFormClass.java 54929 2004-10-16 16:38:42Z germuska $
3  *
4  * Copyright 2000-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.action;
21
22
23 import java.io.Serializable JavaDoc;
24 import java.util.HashMap JavaDoc;
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 /**
36  * <p>Implementation of <code>DynaClass</code> for
37  * <code>DynaActionForm</code> classes that allow developers to define
38  * ActionForms without having to individually code all of the classes.
39  * <strong>NOTE</strong> - This class is only used in the internal
40  * implementation of dynamic action form beans. Application developers
41  * never need to consult this documentation.</p>
42  *
43  * @version $Rev: 54929 $ $Date: 2004-10-16 17:38:42 +0100 (Sat, 16 Oct 2004) $
44  * @since Struts 1.1
45  */

46
47 public class DynaActionFormClass implements DynaClass, Serializable JavaDoc {
48
49
50     // ----------------------------------------------------------- Constructors
51

52
53     /**
54      * <p>Construct a new <code>DynaActionFormClass</code> for the specified
55      * form bean configuration. This constructor is private;
56      * <code>DynaActionFormClass</code> instances will be created as needed via
57      * calls to the static <code>createDynaActionFormClass()</code> method.</p>
58      *
59      * @param config The FormBeanConfig instance describing the properties
60      * of the bean to be created
61      *
62      * @exception IllegalArgumentException if the bean implementation class
63      * specified in the configuration is not DynaActionForm (or a subclass
64      * of DynaActionForm)
65      */

66     public DynaActionFormClass(FormBeanConfig config) {
67
68         introspect(config);
69
70     }
71
72
73     // ----------------------------------------------------- Instance Variables
74

75
76     /**
77      * <p>The <code>DynaActionForm</code> implementation <code>Class</code>
78      * which we will use to create new bean instances.</p>
79      */

80     protected transient Class JavaDoc beanClass = null;
81
82
83     /**
84      * <p>The form bean configuration information for this class.</p>
85      */

86     protected FormBeanConfig config = null;
87
88
89     /**
90      * <p>The "dynamic class name" for this <code>DynaClass</code>.</p>
91      */

92     protected String JavaDoc name = null;
93
94
95     /**
96      * <p>The set of dynamic properties that are part of this DynaClass.</p>
97      */

98     protected DynaProperty properties[] = null;
99
100
101     /**
102      * <p>The set of dynamic properties that are part of this
103      * <code>DynaClass</code>, keyed by the property name. Individual
104      * descriptor instances will be the same instances as those in the
105      * <code>properties</code> list.
106      */

107     protected HashMap JavaDoc propertiesMap = new HashMap JavaDoc();
108
109
110     // ------------------------------------------------------ DynaClass Methods
111

112
113     /**
114      * <p>Return the name of this <code>DynaClass</code> (analogous to the
115      * <code>getName()</code> method of <code>java.lang.Class</code), which
116      * allows the same <code>DynaClass</code> implementation class to support
117      * different dynamic classes, with different sets of properties.
118      */

119     public String JavaDoc getName() {
120
121         return (this.name);
122
123     }
124
125
126     /**
127      * <p>Return a property descriptor for the specified property, if it exists;
128      * otherwise, return <code>null</code>.</p>
129      *
130      * @param name Name of the dynamic property for which a descriptor
131      * is requested
132      *
133      * @exception IllegalArgumentException if no property name is specified
134      */

135     public DynaProperty getDynaProperty(String JavaDoc name) {
136
137         if (name == null) {
138             throw new IllegalArgumentException JavaDoc
139                 ("No property name specified");
140         }
141         return ((DynaProperty) propertiesMap.get(name));
142
143     }
144
145
146     /**
147      * <p>Return an array of <code>DynaProperty</code>s for the properties
148      * currently defined in this <code>DynaClass</code>. If no properties are
149      * defined, a zero-length array will be returned.</p>
150      */

151     public DynaProperty[] getDynaProperties() {
152
153         return (properties);
154         // :FIXME: Should we really be implementing
155
// getBeanInfo instead, which returns property descriptors
156
// and a bunch of other stuff?
157

158     }
159
160
161     /**
162      * <p>Instantiate and return a new {@link DynaActionForm} instance,
163      * associated with this <code>DynaActionFormClass</code>. The
164      * properties of the returned {@link DynaActionForm} will have been
165      * initialized to the default values specified in the form bean
166      * configuration information.</p>
167      *
168      * @exception IllegalAccessException if the Class or the appropriate
169      * constructor is not accessible
170      * @exception InstantiationException if this Class represents an abstract
171      * class, an array class, a primitive type, or void; or if instantiation
172      * fails for some other reason
173      */

174     public DynaBean newInstance()
175         throws IllegalAccessException JavaDoc, InstantiationException JavaDoc {
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     // --------------------------------------------------------- Public Methods
190

191
192     /**
193      * <p>Render a <code>String</code> representation of this object.</p>
194      */

195     public String JavaDoc toString() {
196
197         StringBuffer JavaDoc sb = new StringBuffer JavaDoc("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     // --------------------------------------------------------- Static Methods
216

217
218     /**
219      * @deprecated No longer need to Clear our cache of <code>DynaActionFormClass</code> instances.
220      */

221     public static void clear() {
222     }
223
224
225     /**
226      * Return the <code>DynaActionFormClass</code> instance for the specified form bean
227      * configuration instance.
228      */

229     public static DynaActionFormClass
230         createDynaActionFormClass(FormBeanConfig config) {
231
232         return config.getDynaActionFormClass();
233
234     }
235
236
237     // ------------------------------------------------------ Protected Methods
238

239
240     /**
241      * <p>Return the implementation class we are using to construct new
242      * instances, re-introspecting our {@link FormBeanConfig} if necessary
243      * (that is, after being deserialized, since <code>beanClass</code> is
244      * marked transient).</p>
245      */

246     protected Class JavaDoc getBeanClass() {
247
248         if (beanClass == null) {
249             introspect(config);
250         }
251         return (beanClass);
252
253     }
254
255
256     /**
257      * <p>Introspect our form bean configuration to identify the supported
258      * properties.</p>
259      *
260      * @param config The FormBeanConfig instance describing the properties
261      * of the bean to be created
262      *
263      * @exception IllegalArgumentException if the bean implementation class
264      * specified in the configuration is not DynaActionForm (or a subclass
265      * of DynaActionForm)
266      */

267     protected void introspect(FormBeanConfig config) {
268
269         this.config = config;
270
271         // Validate the ActionFormBean implementation class
272
try {
273             beanClass = RequestUtils.applicationClass(config.getType());
274         } catch (Throwable JavaDoc t) {
275             throw new IllegalArgumentException JavaDoc
276                 ("Cannot instantiate ActionFormBean class '" +
277                  config.getType() + "': " + t);
278         }
279         if (!DynaActionForm.class.isAssignableFrom(beanClass)) {
280             throw new IllegalArgumentException JavaDoc
281                 ("Class '" + config.getType() + "' is not a subclass of " +
282                  "'org.apache.struts.action.DynaActionForm'");
283         }
284
285         // Set the name we will know ourselves by from the form bean name
286
this.name = config.getName();
287
288         // Look up the property descriptors for this bean class
289
FormPropertyConfig descriptors[] = config.findFormPropertyConfigs();
290         if (descriptors == null) {
291             descriptors = new FormPropertyConfig[0];
292         }
293
294         // Create corresponding dynamic property definitions
295
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