|
1 |
| |
|
2 |
| |
|
3 |
| |
|
4 |
| |
|
5 |
| |
|
6 |
| |
|
7 |
| |
|
8 |
| |
|
9 |
| |
|
10 |
| |
|
11 |
| |
|
12 |
| |
|
13 |
| |
|
14 |
| |
|
15 |
| package org.apache.hivemind.util; |
|
16 |
| |
|
17 |
| import java.beans.PropertyEditor; |
|
18 |
| import java.beans.PropertyEditorManager; |
|
19 |
| import java.lang.reflect.Constructor; |
|
20 |
| import java.lang.reflect.Method; |
|
21 |
| |
|
22 |
| import org.apache.hivemind.ApplicationRuntimeException; |
|
23 |
| |
|
24 |
| |
|
25 |
| |
|
26 |
| |
|
27 |
| |
|
28 |
| |
|
29 |
| public class PropertyAdaptor |
|
30 |
| { |
|
31 |
| private String _propertyName; |
|
32 |
| |
|
33 |
| private Class _propertyType; |
|
34 |
| |
|
35 |
| private Method _readMethod; |
|
36 |
| |
|
37 |
| private Method _writeMethod; |
|
38 |
| |
|
39 |
4134
| PropertyAdaptor(String propertyName, Class propertyType, Method readMethod, Method writeMethod)
|
|
40 |
| { |
|
41 |
4134
| _propertyName = propertyName;
|
|
42 |
4134
| _propertyType = propertyType;
|
|
43 |
4134
| _readMethod = readMethod;
|
|
44 |
4134
| _writeMethod = writeMethod;
|
|
45 |
| } |
|
46 |
| |
|
47 |
| |
|
48 |
| |
|
49 |
| |
|
50 |
| |
|
51 |
2
| public String getReadMethodName()
|
|
52 |
| { |
|
53 |
2
| return _readMethod == null ? null : _readMethod.getName();
|
|
54 |
| } |
|
55 |
| |
|
56 |
| |
|
57 |
| |
|
58 |
| |
|
59 |
| |
|
60 |
2
| public String getWriteMethodName()
|
|
61 |
| { |
|
62 |
2
| return _writeMethod == null ? null : _writeMethod.getName();
|
|
63 |
| } |
|
64 |
| |
|
65 |
2240
| public String getPropertyName()
|
|
66 |
| { |
|
67 |
2239
| return _propertyName;
|
|
68 |
| } |
|
69 |
| |
|
70 |
8512
| public Class getPropertyType()
|
|
71 |
| { |
|
72 |
8518
| return _propertyType;
|
|
73 |
| } |
|
74 |
| |
|
75 |
| |
|
76 |
| |
|
77 |
| |
|
78 |
| |
|
79 |
| |
|
80 |
| |
|
81 |
| |
|
82 |
| |
|
83 |
8385
| public void write(Object target, Object value)
|
|
84 |
| { |
|
85 |
8385
| if (_writeMethod == null)
|
|
86 |
1
| throw new ApplicationRuntimeException(UtilMessages.noPropertyWriter(
|
|
87 |
| _propertyName, |
|
88 |
| target), target, null, null); |
|
89 |
| |
|
90 |
8384
| try
|
|
91 |
| { |
|
92 |
8384
| _writeMethod.invoke(target, new Object[]
|
|
93 |
| { value }); |
|
94 |
| |
|
95 |
| } |
|
96 |
| catch (Exception ex) |
|
97 |
| { |
|
98 |
2
| throw new ApplicationRuntimeException(UtilMessages.writeFailure(
|
|
99 |
| _propertyName, |
|
100 |
| target, |
|
101 |
| ex), target, null, ex); |
|
102 |
| } |
|
103 |
| } |
|
104 |
| |
|
105 |
13
| public void smartWrite(Object target, String value)
|
|
106 |
| { |
|
107 |
13
| Object convertedValue = convertValueForAssignment(target, value);
|
|
108 |
| |
|
109 |
10
| write(target, convertedValue);
|
|
110 |
| } |
|
111 |
| |
|
112 |
| |
|
113 |
13
| private Object convertValueForAssignment(Object target, String value)
|
|
114 |
| { |
|
115 |
13
| if (value == null || _propertyType.isInstance(value))
|
|
116 |
3
| return value;
|
|
117 |
| |
|
118 |
10
| PropertyEditor e = PropertyEditorManager.findEditor(_propertyType);
|
|
119 |
| |
|
120 |
10
| if (e == null)
|
|
121 |
| { |
|
122 |
3
| Object convertedValue = instantiateViaStringConstructor(target, value);
|
|
123 |
| |
|
124 |
3
| if (convertedValue != null)
|
|
125 |
2
| return convertedValue;
|
|
126 |
| |
|
127 |
1
| throw new ApplicationRuntimeException(UtilMessages.noPropertyEditor(
|
|
128 |
| _propertyName, |
|
129 |
| target.getClass())); |
|
130 |
| } |
|
131 |
| |
|
132 |
7
| try
|
|
133 |
| { |
|
134 |
7
| e.setAsText(value);
|
|
135 |
| |
|
136 |
5
| return e.getValue();
|
|
137 |
| } |
|
138 |
| catch (Exception ex) |
|
139 |
| { |
|
140 |
2
| throw new ApplicationRuntimeException(UtilMessages.unableToConvert(
|
|
141 |
| value, |
|
142 |
| _propertyType, |
|
143 |
| _propertyName, |
|
144 |
| target, |
|
145 |
| ex), null, ex); |
|
146 |
| } |
|
147 |
| } |
|
148 |
| |
|
149 |
| |
|
150 |
| |
|
151 |
| |
|
152 |
| |
|
153 |
| |
|
154 |
3
| private Object instantiateViaStringConstructor(Object target, String value)
|
|
155 |
| { |
|
156 |
3
| try
|
|
157 |
| { |
|
158 |
3
| Constructor c = _propertyType.getConstructor(new Class[]
|
|
159 |
| { String.class }); |
|
160 |
| |
|
161 |
2
| return c.newInstance(new Object[]
|
|
162 |
| { value }); |
|
163 |
| } |
|
164 |
| catch (Exception ex) |
|
165 |
| { |
|
166 |
1
| return null;
|
|
167 |
| } |
|
168 |
| } |
|
169 |
| |
|
170 |
| |
|
171 |
| |
|
172 |
| |
|
173 |
3781
| public boolean isWritable()
|
|
174 |
| { |
|
175 |
3782
| return _writeMethod != null;
|
|
176 |
| } |
|
177 |
| |
|
178 |
| |
|
179 |
| |
|
180 |
| |
|
181 |
| |
|
182 |
| |
|
183 |
| |
|
184 |
8
| public Object read(Object target)
|
|
185 |
| { |
|
186 |
8
| if (_readMethod == null)
|
|
187 |
1
| throw new ApplicationRuntimeException(UtilMessages.noReader(_propertyName, target),
|
|
188 |
| target, null, null); |
|
189 |
| |
|
190 |
7
| try
|
|
191 |
| { |
|
192 |
7
| return _readMethod.invoke(target, null);
|
|
193 |
| |
|
194 |
| } |
|
195 |
| catch (Exception ex) |
|
196 |
| { |
|
197 |
1
| throw new ApplicationRuntimeException(UtilMessages.readFailure(
|
|
198 |
| _propertyName, |
|
199 |
| target, |
|
200 |
| ex), target, null, ex); |
|
201 |
| } |
|
202 |
| } |
|
203 |
| |
|
204 |
| |
|
205 |
| |
|
206 |
| |
|
207 |
| |
|
208 |
6
| public boolean isReadable()
|
|
209 |
| { |
|
210 |
6
| return _readMethod != null;
|
|
211 |
| } |
|
212 |
| |
|
213 |
| } |