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.PropertyDescriptor; |
18 |
| import java.util.ArrayList; |
19 |
| import java.util.HashMap; |
20 |
| import java.util.Iterator; |
21 |
| import java.util.List; |
22 |
| import java.util.Map; |
23 |
| import java.util.StringTokenizer; |
24 |
| |
25 |
| import org.apache.hivemind.ApplicationRuntimeException; |
26 |
| |
27 |
| |
28 |
| |
29 |
| |
30 |
| |
31 |
| |
32 |
| |
33 |
| class ClassAdaptor |
34 |
| { |
35 |
| private final Map _propertyAdaptorMap = new HashMap(); |
36 |
| |
37 |
1058
| ClassAdaptor(PropertyDescriptor[] properties)
|
38 |
| { |
39 |
1058
| for (int i = 0; i < properties.length; i++)
|
40 |
| { |
41 |
4134
| PropertyDescriptor d = properties[i];
|
42 |
| |
43 |
4134
| String name = d.getName();
|
44 |
| |
45 |
4134
| _propertyAdaptorMap.put(name, new PropertyAdaptor(name, d.getPropertyType(), d
|
46 |
| .getReadMethod(), d.getWriteMethod())); |
47 |
| } |
48 |
| } |
49 |
| |
50 |
| |
51 |
| |
52 |
| |
53 |
| |
54 |
| |
55 |
| |
56 |
| |
57 |
| |
58 |
8375
| public void write(Object target, String propertyName, Object value)
|
59 |
| { |
60 |
8374
| PropertyAdaptor a = getPropertyAdaptor(target, propertyName);
|
61 |
| |
62 |
8375
| a.write(target, value);
|
63 |
| } |
64 |
| |
65 |
| |
66 |
| |
67 |
| |
68 |
| |
69 |
| |
70 |
| |
71 |
| |
72 |
13
| public void smartWrite(Object target, String propertyName, String value)
|
73 |
| { |
74 |
13
| PropertyAdaptor a = getPropertyAdaptor(target, propertyName);
|
75 |
| |
76 |
13
| a.smartWrite(target, value);
|
77 |
| } |
78 |
| |
79 |
| |
80 |
| |
81 |
| |
82 |
| |
83 |
| |
84 |
| |
85 |
| |
86 |
| |
87 |
9
| public Object read(Object target, String propertyName)
|
88 |
| { |
89 |
9
| PropertyAdaptor a = getPropertyAdaptor(target, propertyName);
|
90 |
| |
91 |
8
| return a.read(target);
|
92 |
| } |
93 |
| |
94 |
| |
95 |
| |
96 |
| |
97 |
| |
98 |
| |
99 |
| |
100 |
| |
101 |
| |
102 |
8501
| public Class getPropertyType(Object target, String propertyName)
|
103 |
| { |
104 |
8520
| PropertyAdaptor a = getPropertyAdaptor(target, propertyName);
|
105 |
| |
106 |
8518
| return a.getPropertyType();
|
107 |
| } |
108 |
| |
109 |
| |
110 |
| |
111 |
| |
112 |
| |
113 |
4
| public boolean isReadable(String propertyName)
|
114 |
| { |
115 |
4
| PropertyAdaptor result = (PropertyAdaptor) _propertyAdaptorMap.get(propertyName);
|
116 |
| |
117 |
4
| return result != null && result.isReadable();
|
118 |
| } |
119 |
| |
120 |
| |
121 |
| |
122 |
| |
123 |
| |
124 |
4687
| public boolean isWritable(String propertyName)
|
125 |
| { |
126 |
4687
| PropertyAdaptor result = (PropertyAdaptor) _propertyAdaptorMap.get(propertyName);
|
127 |
| |
128 |
4687
| return result != null && result.isWritable();
|
129 |
| } |
130 |
| |
131 |
16917
| PropertyAdaptor getPropertyAdaptor(Object target, String propertyName)
|
132 |
| { |
133 |
16918
| PropertyAdaptor result = (PropertyAdaptor) _propertyAdaptorMap.get(propertyName);
|
134 |
| |
135 |
16918
| if (result == null)
|
136 |
2
| throw new ApplicationRuntimeException(
|
137 |
| UtilMessages.noSuchProperty(target, propertyName), target, null, null); |
138 |
| |
139 |
16915
| return result;
|
140 |
| } |
141 |
| |
142 |
| |
143 |
| |
144 |
| |
145 |
1
| public List getReadableProperties()
|
146 |
| { |
147 |
1
| List result = new ArrayList(_propertyAdaptorMap.size());
|
148 |
| |
149 |
1
| Iterator i = _propertyAdaptorMap.values().iterator();
|
150 |
| |
151 |
1
| while (i.hasNext())
|
152 |
| { |
153 |
3
| PropertyAdaptor a = (PropertyAdaptor) i.next();
|
154 |
| |
155 |
3
| if (a.isReadable())
|
156 |
2
| result.add(a.getPropertyName());
|
157 |
| } |
158 |
| |
159 |
1
| return result;
|
160 |
| } |
161 |
| |
162 |
| |
163 |
| |
164 |
| |
165 |
790
| public List getWriteableProperties()
|
166 |
| { |
167 |
790
| List result = new ArrayList(_propertyAdaptorMap.size());
|
168 |
| |
169 |
790
| Iterator i = _propertyAdaptorMap.values().iterator();
|
170 |
| |
171 |
790
| while (i.hasNext())
|
172 |
| { |
173 |
3030
| PropertyAdaptor a = (PropertyAdaptor) i.next();
|
174 |
| |
175 |
3033
| if (a.isWritable())
|
176 |
2237
| result.add(a.getPropertyName());
|
177 |
| } |
178 |
| |
179 |
790
| return result;
|
180 |
| } |
181 |
| |
182 |
| |
183 |
| |
184 |
| |
185 |
| |
186 |
| |
187 |
| |
188 |
| |
189 |
10
| public void configureProperties(Object target, String initializer)
|
190 |
| { |
191 |
10
| StringTokenizer tokenizer = new StringTokenizer(initializer, ",");
|
192 |
| |
193 |
10
| while (tokenizer.hasMoreTokens())
|
194 |
| { |
195 |
13
| configurePropertyFromToken(target, tokenizer.nextToken());
|
196 |
| } |
197 |
| } |
198 |
| |
199 |
| |
200 |
| |
201 |
| |
202 |
| |
203 |
| |
204 |
| |
205 |
| |
206 |
| |
207 |
| |
208 |
| |
209 |
| |
210 |
13
| private void configurePropertyFromToken(Object target, String token)
|
211 |
| { |
212 |
13
| int equalsx = token.indexOf('=');
|
213 |
| |
214 |
13
| if (equalsx > 0)
|
215 |
| { |
216 |
9
| String propertyName = token.substring(0, equalsx).trim();
|
217 |
9
| String value = token.substring(equalsx + 1);
|
218 |
| |
219 |
9
| smartWrite(target, propertyName, value);
|
220 |
8
| return;
|
221 |
| } |
222 |
| |
223 |
4
| boolean negate = token.startsWith("!");
|
224 |
| |
225 |
4
| String propertyName = negate ? token.substring(1) : token;
|
226 |
| |
227 |
4
| Boolean value = negate ? Boolean.FALSE : Boolean.TRUE;
|
228 |
| |
229 |
4
| write(target, propertyName, value);
|
230 |
| } |
231 |
| } |