KickJava   Java API By Example, From Geeks To Geeks.

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


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.PropertyDescriptor JavaDoc;
18 import java.util.ArrayList JavaDoc;
19 import java.util.HashMap JavaDoc;
20 import java.util.Iterator JavaDoc;
21 import java.util.List JavaDoc;
22 import java.util.Map JavaDoc;
23 import java.util.StringTokenizer JavaDoc;
24
25 import org.apache.hivemind.ApplicationRuntimeException;
26
27 /**
28  * Provides access to an object (of a particular class) as a set of individual property that may be
29  * read or updated.
30  *
31  * @author Howard Lewis Ship
32  */

33 class ClassAdaptor
34 {
35     private final Map JavaDoc _propertyAdaptorMap = new HashMap JavaDoc();
36
37     ClassAdaptor(PropertyDescriptor JavaDoc[] properties)
38     {
39         for (int i = 0; i < properties.length; i++)
40         {
41             PropertyDescriptor JavaDoc d = properties[i];
42
43             String JavaDoc name = d.getName();
44
45             _propertyAdaptorMap.put(name, new PropertyAdaptor(name, d.getPropertyType(), d
46                     .getReadMethod(), d.getWriteMethod()));
47         }
48     }
49
50     /**
51      * Updates the property of the target object.
52      *
53      * @param target
54      * the object to update
55      * @param value
56      * the value to be stored into the target object property
57      */

58     public void write(Object JavaDoc target, String JavaDoc propertyName, Object JavaDoc value)
59     {
60         PropertyAdaptor a = getPropertyAdaptor(target, propertyName);
61
62         a.write(target, value);
63     }
64
65     /**
66      * An improved version of {@link #write(Object, String, Object)} that can convert a string value
67      * to an appropriate property type value.
68      *
69      * @since 1.1
70      */

71
72     public void smartWrite(Object JavaDoc target, String JavaDoc propertyName, String JavaDoc value)
73     {
74         PropertyAdaptor a = getPropertyAdaptor(target, propertyName);
75
76         a.smartWrite(target, value);
77     }
78
79     /**
80      * Reads the property of the target object.
81      *
82      * @param target
83      * the object to read
84      * @param propertyName
85      * the name of the property to read
86      */

87     public Object JavaDoc read(Object JavaDoc target, String JavaDoc propertyName)
88     {
89         PropertyAdaptor a = getPropertyAdaptor(target, propertyName);
90
91         return a.read(target);
92     }
93
94     /**
95      * Returns the type of the named property.
96      *
97      * @param target
98      * the object to examine
99      * @param propertyName
100      * the name of the property to check
101      */

102     public Class JavaDoc getPropertyType(Object JavaDoc target, String JavaDoc propertyName)
103     {
104         PropertyAdaptor a = getPropertyAdaptor(target, propertyName);
105
106         return a.getPropertyType();
107     }
108
109     /**
110      * Returns true if the named property exists and is readable.
111      */

112
113     public boolean isReadable(String JavaDoc propertyName)
114     {
115         PropertyAdaptor result = (PropertyAdaptor) _propertyAdaptorMap.get(propertyName);
116
117         return result != null && result.isReadable();
118     }
119
120     /**
121      * Returns true if the named property exists and is writable.
122      */

123
124     public boolean isWritable(String JavaDoc propertyName)
125     {
126         PropertyAdaptor result = (PropertyAdaptor) _propertyAdaptorMap.get(propertyName);
127
128         return result != null && result.isWritable();
129     }
130
131     PropertyAdaptor getPropertyAdaptor(Object JavaDoc target, String JavaDoc propertyName)
132     {
133         PropertyAdaptor result = (PropertyAdaptor) _propertyAdaptorMap.get(propertyName);
134
135         if (result == null)
136             throw new ApplicationRuntimeException(
137                     UtilMessages.noSuchProperty(target, propertyName), target, null, null);
138
139         return result;
140     }
141
142     /**
143      * Returns a List of the names of readable properties (properties with a non-null getter).
144      */

145     public List JavaDoc getReadableProperties()
146     {
147         List JavaDoc result = new ArrayList JavaDoc(_propertyAdaptorMap.size());
148
149         Iterator JavaDoc i = _propertyAdaptorMap.values().iterator();
150
151         while (i.hasNext())
152         {
153             PropertyAdaptor a = (PropertyAdaptor) i.next();
154
155             if (a.isReadable())
156                 result.add(a.getPropertyName());
157         }
158
159         return result;
160     }
161
162     /**
163      * Returns a List of the names of readable properties (properties with a non-null setter).
164      */

165     public List JavaDoc getWriteableProperties()
166     {
167         List JavaDoc result = new ArrayList JavaDoc(_propertyAdaptorMap.size());
168
169         Iterator JavaDoc i = _propertyAdaptorMap.values().iterator();
170
171         while (i.hasNext())
172         {
173             PropertyAdaptor a = (PropertyAdaptor) i.next();
174
175             if (a.isWritable())
176                 result.add(a.getPropertyName());
177         }
178
179         return result;
180     }
181
182     /**
183      * Does the grunt work for
184      * {@link org.apache.hivemind.util.PropertyUtils#configureProperties(Object, String)}.
185      *
186      * @since 1.1
187      */

188
189     public void configureProperties(Object JavaDoc target, String JavaDoc initializer)
190     {
191         StringTokenizer JavaDoc tokenizer = new StringTokenizer JavaDoc(initializer, ",");
192
193         while (tokenizer.hasMoreTokens())
194         {
195             configurePropertyFromToken(target, tokenizer.nextToken());
196         }
197     }
198
199     /**
200      * The token is either:
201      * <ul>
202      * <li>propertyName=value</li>
203      * <li>propertyName</li>
204      * <li>!propertyName</li>
205      * </ul>
206      * The later two are for boolean properties (true and false, respectively).
207      *
208      * @since 1.1
209      */

210     private void configurePropertyFromToken(Object JavaDoc target, String JavaDoc token)
211     {
212         int equalsx = token.indexOf('=');
213
214         if (equalsx > 0)
215         {
216             String JavaDoc propertyName = token.substring(0, equalsx).trim();
217             String JavaDoc value = token.substring(equalsx + 1);
218
219             smartWrite(target, propertyName, value);
220             return;
221         }
222
223         boolean negate = token.startsWith("!");
224
225         String JavaDoc propertyName = negate ? token.substring(1) : token;
226
227         Boolean JavaDoc value = negate ? Boolean.FALSE : Boolean.TRUE;
228
229         write(target, propertyName, value);
230     }
231 }
Popular Tags