KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > hivemind > util > PropertyUtils


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

15 package org.apache.hivemind.util;
16
17 import java.beans.BeanInfo JavaDoc;
18 import java.beans.Introspector JavaDoc;
19 import java.util.HashMap JavaDoc;
20 import java.util.List JavaDoc;
21 import java.util.Map JavaDoc;
22
23 import org.apache.hivemind.ApplicationRuntimeException;
24 import org.apache.hivemind.HiveMind;
25
26 /**
27  * A collection of static methods used to perform property-level access on arbitrary objects.
28  *
29  * @author Howard Lewis Ship
30  */

31 public class PropertyUtils
32 {
33     private static final Map JavaDoc _classAdaptors = new HashMap JavaDoc();
34
35     // Prevent instantiation
36
private PropertyUtils()
37     {
38     }
39
40     /**
41      * Updates the property of the target object.
42      *
43      * @param target
44      * the object to update
45      * @param propertyName
46      * the name of the property to be updated
47      * @param value
48      * the value to be stored into the target object property
49      */

50     public static void write(Object JavaDoc target, String JavaDoc propertyName, Object JavaDoc value)
51     {
52         ClassAdaptor a = getAdaptor(target);
53
54         a.write(target, propertyName, value);
55     }
56
57     /**
58      * An improved version of {@link #write(Object, String, Object)} where the value starts as a
59      * string and is converted to the correct property type before being assigned.
60      *
61      * @since 1.1
62      */

63     public static void smartWrite(Object JavaDoc target, String JavaDoc propertyName, String JavaDoc value)
64     {
65         ClassAdaptor a = getAdaptor(target);
66
67         a.smartWrite(target, propertyName, value);
68     }
69
70     /**
71      * Initializes the properties of an object from a string. The string is a comma-seperated
72      * sequence of property names and values. Property names are seperated from values be an equals
73      * sign. Spaces before and after the property names are trimmed.
74      * For boolean properties, the equals sign and value may be omitted (a value of true is
75      * assumed), or the property name may be prefixed with an exclamation point to indicated false
76      * value. Example: <code>validate,maxLength=10,displayName=User Id</code>.
77      *
78      * @param target
79      * the object to be configured
80      * @param initializer
81      * the string encoding the properties and values to be configured in the target
82      * object
83      * @since 1.1
84      */

85
86     public static void configureProperties(Object JavaDoc target, String JavaDoc initializer)
87     {
88         ClassAdaptor a = getAdaptor(target);
89
90         a.configureProperties(target, initializer);
91     }
92
93     /**
94      * Returns true of the instance contains a writable property of the given type.
95      *
96      * @param target
97      * the object to inspect
98      * @param propertyName
99      * the name of the property to check
100      */

101
102     public static boolean isWritable(Object JavaDoc target, String JavaDoc propertyName)
103     {
104         return getAdaptor(target).isWritable(propertyName);
105     }
106
107     public static boolean isReadable(Object JavaDoc target, String JavaDoc propertyName)
108     {
109         return getAdaptor(target).isReadable(propertyName);
110     }
111
112     /**
113      * Updates the property of the target object.
114      *
115      * @param target
116      * the object to update
117      * @param propertyName
118      * the name of a property toread
119      */

120
121     public static Object JavaDoc read(Object JavaDoc target, String JavaDoc propertyName)
122     {
123         ClassAdaptor a = getAdaptor(target);
124
125         return a.read(target, propertyName);
126     }
127
128     /**
129      * Returns the type of the named property.
130      *
131      * @param target
132      * the object to examine
133      * @param propertyName
134      * the name of the property to check
135      */

136     public static Class JavaDoc getPropertyType(Object JavaDoc target, String JavaDoc propertyName)
137     {
138         ClassAdaptor a = getAdaptor(target);
139
140         return a.getPropertyType(target, propertyName);
141     }
142
143     /**
144      * Returns the {@link PropertyAdaptor} for the given target object and property name.
145      *
146      * @throws ApplicationRuntimeException
147      * if the property does not exist.
148      */

149     public static PropertyAdaptor getPropertyAdaptor(Object JavaDoc target, String JavaDoc propertyName)
150     {
151         ClassAdaptor a = getAdaptor(target);
152
153         return a.getPropertyAdaptor(target, propertyName);
154     }
155
156     /**
157      * Returns an unordered List of the names of all readable properties of the target.
158      */

159     public static List JavaDoc getReadableProperties(Object JavaDoc target)
160     {
161         return getAdaptor(target).getReadableProperties();
162     }
163
164     /**
165      * Returns an unordered List of the names of all writable properties of the target.
166      */

167     public static List JavaDoc getWriteableProperties(Object JavaDoc target)
168     {
169         return getAdaptor(target).getWriteableProperties();
170     }
171
172     private static ClassAdaptor getAdaptor(Object JavaDoc target)
173     {
174         if (target == null)
175             throw new ApplicationRuntimeException(UtilMessages.nullObject());
176
177         Class JavaDoc targetClass = target.getClass();
178
179         synchronized (HiveMind.INTROSPECTOR_MUTEX)
180         {
181             ClassAdaptor result = (ClassAdaptor) _classAdaptors.get(targetClass);
182
183             if (result == null)
184             {
185                 result = buildClassAdaptor(target, targetClass);
186                 _classAdaptors.put(targetClass, result);
187             }
188
189             return result;
190         }
191     }
192
193     private static ClassAdaptor buildClassAdaptor(Object JavaDoc target, Class JavaDoc targetClass)
194     {
195         try
196         {
197             BeanInfo JavaDoc info = Introspector.getBeanInfo(targetClass);
198
199             return new ClassAdaptor(info.getPropertyDescriptors());
200         }
201         catch (Exception JavaDoc ex)
202         {
203             throw new ApplicationRuntimeException(UtilMessages.unableToIntrospect(targetClass, ex),
204                     target, null, ex);
205         }
206     }
207
208     /**
209      * Clears all cached information. Invokes {@link Introspector#flushCaches()}.
210      */

211     public static void clearCache()
212     {
213         synchronized (HiveMind.INTROSPECTOR_MUTEX)
214         {
215             _classAdaptors.clear();
216             Introspector.flushCaches();
217         }
218     }
219
220 }
Popular Tags