KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > oddjob > framework > WrapDynaBean


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

16
17
18 package org.oddjob.framework;
19
20 import org.apache.commons.beanutils.ConversionException;
21 import org.apache.commons.beanutils.DynaBean;
22 import org.apache.commons.beanutils.DynaClass;
23 import org.apache.commons.beanutils.DynaProperty;
24 import org.apache.commons.beanutils.PropertyUtils;
25
26 /**
27  * <p>Implementation of <code>DynaBean</code> that wraps a standard JavaBean
28  * instance, so that DynaBean APIs can be used to access its properties.</p>
29  *
30  * <p>
31  * The most common use cases for this class involve wrapping an existing java bean.
32  * (This makes it different from the typical use cases for other <code>DynaBean</code>'s.)
33  * For example:
34  * </p>
35  * <code><pre>
36  * Object aJavaBean = ...;
37  * ...
38  * DynaBean db = new WrapDynaBean(aJavaBean);
39  * ...
40  * </pre></code>
41  *
42  * <p><strong>IMPLEMENTATION NOTE</strong> - This implementation does not
43  * support the <code>contains()</code> and <code>remove()</code> methods.</p>
44  *
45  * <p>
46  * Based on the BeanUtils version but provide a Serializable DynaClass.
47  *
48  * @author Rob Gorodon, based on the original from BeanUtils.
49  */

50
51 public class WrapDynaBean implements DynaBean {
52
53
54     // ---------------------------------------------------------- Constructors
55

56
57     /**
58      * Construct a new <code>DynaBean</code> associated with the specified
59      * JavaBean instance.
60      *
61      * @param instance JavaBean instance to be wrapped
62      */

63     public WrapDynaBean(Object JavaDoc instance) {
64
65         super();
66         this.instance = instance;
67         this.dynaClass = WrapDynaClass.createDynaClass(instance.getClass());
68
69     }
70
71
72     // ---------------------------------------------------- Instance Variables
73

74
75     /**
76      * The <code>DynaClass</code> "base class" that this DynaBean
77      * is associated with.
78      */

79     protected WrapDynaClass dynaClass = null;
80
81
82     /**
83      * The JavaBean instance wrapped by this WrapDynaBean.
84      */

85     protected Object JavaDoc instance = null;
86
87
88     // ------------------------------------------------------ DynaBean Methods
89

90
91     /**
92      * Does the specified mapped property contain a value for the specified
93      * key value?
94      *
95      * @param name Name of the property to check
96      * @param key Name of the key to check
97      *
98      * @exception IllegalArgumentException if there is no property
99      * of the specified name
100      */

101     public boolean contains(String JavaDoc name, String JavaDoc key) {
102
103         throw new UnsupportedOperationException JavaDoc
104                 ("WrapDynaBean does not support contains()");
105
106     }
107
108
109     /**
110      * Return the value of a simple property with the specified name.
111      *
112      * @param name Name of the property whose value is to be retrieved
113      *
114      * @exception IllegalArgumentException if there is no property
115      * of the specified name
116      */

117     public Object JavaDoc get(String JavaDoc name) {
118
119         Object JavaDoc value = null;
120         try {
121             value = PropertyUtils.getSimpleProperty(instance, name);
122         } catch (Throwable JavaDoc t) {
123             throw new IllegalArgumentException JavaDoc
124                     ("Property '" + name + "' has no read method");
125         }
126         return (value);
127
128     }
129
130
131     /**
132      * Return the value of an indexed property with the specified name.
133      *
134      * @param name Name of the property whose value is to be retrieved
135      * @param index Index of the value to be retrieved
136      *
137      * @exception IllegalArgumentException if there is no property
138      * of the specified name
139      * @exception IllegalArgumentException if the specified property
140      * exists, but is not indexed
141      * @exception IndexOutOfBoundsException if the specified index
142      * is outside the range of the underlying property
143      * @exception NullPointerException if no array or List has been
144      * initialized for this property
145      */

146     public Object JavaDoc get(String JavaDoc name, int index) {
147
148         Object JavaDoc value = null;
149         try {
150             value = PropertyUtils.getIndexedProperty(instance, name, index);
151         } catch (IndexOutOfBoundsException JavaDoc e) {
152             throw e;
153         } catch (Throwable JavaDoc t) {
154             throw new IllegalArgumentException JavaDoc
155                     ("Property '" + name + "' has no indexed read method");
156         }
157         return (value);
158
159     }
160
161
162     /**
163      * Return the value of a mapped property with the specified name,
164      * or <code>null</code> if there is no value for the specified key.
165      *
166      * @param name Name of the property whose value is to be retrieved
167      * @param key Key of the value to be retrieved
168      *
169      * @exception IllegalArgumentException if there is no property
170      * of the specified name
171      * @exception IllegalArgumentException if the specified property
172      * exists, but is not mapped
173      */

174     public Object JavaDoc get(String JavaDoc name, String JavaDoc key) {
175
176         Object JavaDoc value = null;
177         try {
178             value = PropertyUtils.getMappedProperty(instance, name, key);
179         } catch (Throwable JavaDoc t) {
180             throw new IllegalArgumentException JavaDoc
181                     ("Property '" + name + "' has no mapped read method");
182         }
183         return (value);
184
185     }
186
187
188     /**
189      * Return the <code>DynaClass</code> instance that describes the set of
190      * properties available for this DynaBean.
191      */

192     public DynaClass getDynaClass() {
193
194         return (this.dynaClass);
195
196     }
197
198
199     /**
200      * Remove any existing value for the specified key on the
201      * specified mapped property.
202      *
203      * @param name Name of the property for which a value is to
204      * be removed
205      * @param key Key of the value to be removed
206      *
207      * @exception IllegalArgumentException if there is no property
208      * of the specified name
209      */

210     public void remove(String JavaDoc name, String JavaDoc key) {
211
212
213         throw new UnsupportedOperationException JavaDoc
214                 ("WrapDynaBean does not support remove()");
215
216     }
217
218
219     /**
220      * Set the value of a simple property with the specified name.
221      *
222      * @param name Name of the property whose value is to be set
223      * @param value Value to which this property is to be set
224      *
225      * @exception ConversionException if the specified value cannot be
226      * converted to the type required for this property
227      * @exception IllegalArgumentException if there is no property
228      * of the specified name
229      * @exception NullPointerException if an attempt is made to set a
230      * primitive property to null
231      */

232     public void set(String JavaDoc name, Object JavaDoc value) {
233
234         try {
235             PropertyUtils.setSimpleProperty(instance, name, value);
236         } catch (Throwable JavaDoc t) {
237             throw new IllegalArgumentException JavaDoc
238                     ("Property '" + name + "' has no write method");
239         }
240
241     }
242
243
244     /**
245      * Set the value of an indexed property with the specified name.
246      *
247      * @param name Name of the property whose value is to be set
248      * @param index Index of the property to be set
249      * @param value Value to which this property is to be set
250      *
251      * @exception ConversionException if the specified value cannot be
252      * converted to the type required for this property
253      * @exception IllegalArgumentException if there is no property
254      * of the specified name
255      * @exception IllegalArgumentException if the specified property
256      * exists, but is not indexed
257      * @exception IndexOutOfBoundsException if the specified index
258      * is outside the range of the underlying property
259      */

260     public void set(String JavaDoc name, int index, Object JavaDoc value) {
261
262         try {
263             PropertyUtils.setIndexedProperty(instance, name, index, value);
264         } catch (IndexOutOfBoundsException JavaDoc e) {
265             throw e;
266         } catch (Throwable JavaDoc t) {
267             throw new IllegalArgumentException JavaDoc
268                     ("Property '" + name + "' has no indexed write method");
269         }
270
271     }
272
273
274     /**
275      * Set the value of a mapped property with the specified name.
276      *
277      * @param name Name of the property whose value is to be set
278      * @param key Key of the property to be set
279      * @param value Value to which this property is to be set
280      *
281      * @exception ConversionException if the specified value cannot be
282      * converted to the type required for this property
283      * @exception IllegalArgumentException if there is no property
284      * of the specified name
285      * @exception IllegalArgumentException if the specified property
286      * exists, but is not mapped
287      */

288     public void set(String JavaDoc name, String JavaDoc key, Object JavaDoc value) {
289
290         try {
291             PropertyUtils.setMappedProperty(instance, name, key, value);
292         } catch (Throwable JavaDoc t) {
293             throw new IllegalArgumentException JavaDoc
294                     ("Property '" + name + "' has no mapped write method");
295         }
296
297     }
298
299     /**
300      * Gets the bean instance wrapped by this DynaBean.
301      * For most common use cases,
302      * this object should already be known
303      * and this method safely be ignored.
304      * But some creators of frameworks using <code>DynaBean</code>'s may
305      * find this useful.
306      *
307      * @return the java bean Object wrapped by this <code>DynaBean</code>
308      */

309     public Object JavaDoc getInstance() {
310         return instance;
311     }
312
313
314     // ------------------------------------------------------ Protected Methods
315

316
317     /**
318      * Return the property descriptor for the specified property name.
319      *
320      * @param name Name of the property for which to retrieve the descriptor
321      *
322      * @exception IllegalArgumentException if this is not a valid property
323      * name for our DynaClass
324      */

325     protected DynaProperty getDynaProperty(String JavaDoc name) {
326
327         DynaProperty descriptor = getDynaClass().getDynaProperty(name);
328         if (descriptor == null) {
329             throw new IllegalArgumentException JavaDoc
330                     ("Invalid property name '" + name + "'");
331         }
332         return (descriptor);
333
334     }
335
336
337 }
338
Popular Tags