KickJava   Java API By Example, From Geeks To Geeks.

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


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.PropertyEditor JavaDoc;
18 import java.beans.PropertyEditorManager JavaDoc;
19 import java.lang.reflect.Constructor JavaDoc;
20 import java.lang.reflect.Method JavaDoc;
21
22 import org.apache.hivemind.ApplicationRuntimeException;
23
24 /**
25  * Used to manage dynamic access to a property of a specific class.
26  *
27  * @author Howard Lewis Ship
28  */

29 public class PropertyAdaptor
30 {
31     private String JavaDoc _propertyName;
32
33     private Class JavaDoc _propertyType;
34
35     private Method JavaDoc _readMethod;
36
37     private Method JavaDoc _writeMethod;
38
39     PropertyAdaptor(String JavaDoc propertyName, Class JavaDoc propertyType, Method JavaDoc readMethod, Method JavaDoc writeMethod)
40     {
41         _propertyName = propertyName;
42         _propertyType = propertyType;
43         _readMethod = readMethod;
44         _writeMethod = writeMethod;
45     }
46
47     /**
48      * Returns the name of the method used to read the property, or null if the property is not
49      * readable.
50      */

51     public String JavaDoc getReadMethodName()
52     {
53         return _readMethod == null ? null : _readMethod.getName();
54     }
55
56     /**
57      * Returns the name of the method used to write the property, or null if the property is not
58      * writable.
59      */

60     public String JavaDoc getWriteMethodName()
61     {
62         return _writeMethod == null ? null : _writeMethod.getName();
63     }
64
65     public String JavaDoc getPropertyName()
66     {
67         return _propertyName;
68     }
69
70     public Class JavaDoc getPropertyType()
71     {
72         return _propertyType;
73     }
74
75     /**
76      * Updates the property of the target object.
77      *
78      * @param target
79      * the object to update
80      * @param value
81      * the value to be stored into the target object property
82      */

83     public void write(Object JavaDoc target, Object JavaDoc value)
84     {
85         if (_writeMethod == null)
86             throw new ApplicationRuntimeException(UtilMessages.noPropertyWriter(
87                     _propertyName,
88                     target), target, null, null);
89
90         try
91         {
92             _writeMethod.invoke(target, new Object JavaDoc[]
93             { value });
94
95         }
96         catch (Exception JavaDoc ex)
97         {
98             throw new ApplicationRuntimeException(UtilMessages.writeFailure(
99                     _propertyName,
100                     target,
101                     ex), target, null, ex);
102         }
103     }
104
105     public void smartWrite(Object JavaDoc target, String JavaDoc value)
106     {
107         Object JavaDoc convertedValue = convertValueForAssignment(target, value);
108
109         write(target, convertedValue);
110     }
111
112     /** @since 1.1 */
113     private Object JavaDoc convertValueForAssignment(Object JavaDoc target, String JavaDoc value)
114     {
115         if (value == null || _propertyType.isInstance(value))
116             return value;
117
118         PropertyEditor JavaDoc e = PropertyEditorManager.findEditor(_propertyType);
119
120         if (e == null)
121         {
122             Object JavaDoc convertedValue = instantiateViaStringConstructor(target, value);
123
124             if (convertedValue != null)
125                 return convertedValue;
126
127             throw new ApplicationRuntimeException(UtilMessages.noPropertyEditor(
128                     _propertyName,
129                     target.getClass()));
130         }
131
132         try
133         {
134             e.setAsText(value);
135
136             return e.getValue();
137         }
138         catch (Exception JavaDoc ex)
139         {
140             throw new ApplicationRuntimeException(UtilMessages.unableToConvert(
141                     value,
142                     _propertyType,
143                     _propertyName,
144                     target,
145                     ex), null, ex);
146         }
147     }
148
149     /**
150      * Checks to see if this adaptor's property type has a public constructor that takes a single
151      * String argument.
152      */

153
154     private Object JavaDoc instantiateViaStringConstructor(Object JavaDoc target, String JavaDoc value)
155     {
156         try
157         {
158             Constructor JavaDoc c = _propertyType.getConstructor(new Class JavaDoc[]
159             { String JavaDoc.class });
160
161             return c.newInstance(new Object JavaDoc[]
162             { value });
163         }
164         catch (Exception JavaDoc ex)
165         {
166             return null;
167         }
168     }
169
170     /**
171      * Returns true if there's a write method for the property.
172      */

173     public boolean isWritable()
174     {
175         return _writeMethod != null;
176     }
177
178     /**
179      * Reads the property of the target object.
180      *
181      * @param target
182      * the object to read a property from
183      */

184     public Object JavaDoc read(Object JavaDoc target)
185     {
186         if (_readMethod == null)
187             throw new ApplicationRuntimeException(UtilMessages.noReader(_propertyName, target),
188                     target, null, null);
189
190         try
191         {
192             return _readMethod.invoke(target, (Object JavaDoc []) null);
193
194         }
195         catch (Exception JavaDoc ex)
196         {
197             throw new ApplicationRuntimeException(UtilMessages.readFailure(
198                     _propertyName,
199                     target,
200                     ex), target, null, ex);
201         }
202     }
203
204     /**
205      * Returns true if there's a read method for the property.
206      */

207
208     public boolean isReadable()
209     {
210         return _readMethod != null;
211     }
212
213 }
Popular Tags