1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
package org.apache.camel.util; |
19 |
|
|
20 |
|
import java.beans.PropertyEditor; |
21 |
|
import java.beans.PropertyEditorManager; |
22 |
|
import java.lang.reflect.Field; |
23 |
|
import java.lang.reflect.Method; |
24 |
|
import java.lang.reflect.Modifier; |
25 |
|
import java.net.URI; |
26 |
|
import java.net.URISyntaxException; |
27 |
|
import java.util.Arrays; |
28 |
|
import java.util.HashMap; |
29 |
|
import java.util.Iterator; |
30 |
|
import java.util.LinkedHashMap; |
31 |
|
import java.util.Map; |
32 |
|
import java.util.Map.Entry; |
33 |
|
import java.util.Set; |
34 |
|
|
35 |
0 |
public class IntrospectionSupport { |
36 |
|
|
37 |
|
|
38 |
|
static public boolean getProperties(Object target, Map props, String optionPrefix) { |
39 |
|
|
40 |
0 |
boolean rc = false; |
41 |
0 |
if( target == null ) |
42 |
0 |
throw new IllegalArgumentException("target was null."); |
43 |
0 |
if( props == null ) |
44 |
0 |
throw new IllegalArgumentException("props was null."); |
45 |
|
|
46 |
0 |
if( optionPrefix == null ) |
47 |
0 |
optionPrefix=""; |
48 |
|
|
49 |
0 |
Class clazz = target.getClass(); |
50 |
0 |
Method[] methods = clazz.getMethods(); |
51 |
0 |
for (int i = 0; i < methods.length; i++) { |
52 |
0 |
Method method = methods[i]; |
53 |
0 |
String name = method.getName(); |
54 |
0 |
Class type = method.getReturnType(); |
55 |
0 |
Class params[] = method.getParameterTypes(); |
56 |
0 |
if( name.startsWith("get") && params.length==0 && |
57 |
|
type!=null && isSettableType(type)) { |
58 |
|
|
59 |
|
try { |
60 |
|
|
61 |
0 |
Object value = method.invoke(target, new Object[]{}); |
62 |
0 |
if( value == null ) |
63 |
0 |
continue; |
64 |
|
|
65 |
0 |
String strValue = convertToString(value, type); |
66 |
0 |
if( strValue ==null ) |
67 |
0 |
continue; |
68 |
|
|
69 |
0 |
name = name.substring(3,4).toLowerCase()+name.substring(4); |
70 |
0 |
props.put(optionPrefix+name, strValue); |
71 |
0 |
rc = true; |
72 |
|
|
73 |
0 |
} catch ( Throwable ignore) { |
74 |
0 |
} |
75 |
|
|
76 |
|
} |
77 |
|
} |
78 |
|
|
79 |
0 |
return rc; |
80 |
|
} |
81 |
|
|
82 |
|
|
83 |
|
|
84 |
|
static public boolean setProperties(Object target, Map props, String optionPrefix) { |
85 |
0 |
boolean rc = false; |
86 |
0 |
if( target == null ) |
87 |
0 |
throw new IllegalArgumentException("target was null."); |
88 |
0 |
if( props == null ) |
89 |
0 |
throw new IllegalArgumentException("props was null."); |
90 |
|
|
91 |
0 |
for (Iterator iter = props.keySet().iterator(); iter.hasNext();) { |
92 |
0 |
String name = (String) iter.next(); |
93 |
0 |
if( name.startsWith(optionPrefix) ) { |
94 |
0 |
Object value = props.get(name); |
95 |
0 |
name = name.substring(optionPrefix.length()); |
96 |
0 |
if( setProperty(target, name, value) ) { |
97 |
0 |
iter.remove(); |
98 |
0 |
rc = true; |
99 |
|
} |
100 |
|
} |
101 |
0 |
} |
102 |
0 |
return rc; |
103 |
|
} |
104 |
|
|
105 |
|
public static Map extractProperties(Map props, String optionPrefix) { |
106 |
1 |
if( props == null ) |
107 |
0 |
throw new IllegalArgumentException("props was null."); |
108 |
|
|
109 |
1 |
HashMap rc = new HashMap(props.size()); |
110 |
|
|
111 |
1 |
for (Iterator iter = props.keySet().iterator(); iter.hasNext();) { |
112 |
0 |
String name = (String) iter.next(); |
113 |
0 |
if( name.startsWith(optionPrefix) ) { |
114 |
0 |
Object value = props.get(name); |
115 |
0 |
name = name.substring(optionPrefix.length()); |
116 |
0 |
rc.put(name, value); |
117 |
0 |
iter.remove(); |
118 |
|
} |
119 |
0 |
} |
120 |
|
|
121 |
1 |
return rc; |
122 |
|
} |
123 |
|
|
124 |
|
public static boolean setProperties(Object target, Map props) { |
125 |
106 |
boolean rc = false; |
126 |
|
|
127 |
106 |
if( target == null ) |
128 |
0 |
throw new IllegalArgumentException("target was null."); |
129 |
106 |
if( props == null ) |
130 |
0 |
throw new IllegalArgumentException("props was null."); |
131 |
|
|
132 |
106 |
for (Iterator iter = props.entrySet().iterator(); iter.hasNext();) { |
133 |
3 |
Map.Entry entry = (Entry) iter.next(); |
134 |
3 |
if( setProperty(target, (String) entry.getKey(), entry.getValue()) ) { |
135 |
3 |
iter.remove(); |
136 |
3 |
rc=true; |
137 |
|
} |
138 |
3 |
} |
139 |
|
|
140 |
106 |
return rc; |
141 |
|
} |
142 |
|
|
143 |
|
public static boolean setProperty(Object target, String name, Object value) { |
144 |
|
try { |
145 |
3 |
Class clazz = target.getClass(); |
146 |
3 |
Method setter = findSetterMethod(clazz, name); |
147 |
3 |
if( setter == null ) |
148 |
0 |
return false; |
149 |
|
|
150 |
|
|
151 |
3 |
if( value == null || value.getClass()==setter.getParameterTypes()[0] ) { |
152 |
0 |
setter.invoke(target, new Object[]{value}); |
153 |
0 |
} else { |
154 |
|
|
155 |
3 |
setter.invoke(target, new Object[]{ convert(value, setter.getParameterTypes()[0]) }); |
156 |
|
} |
157 |
3 |
return true; |
158 |
0 |
} catch (Throwable ignore) { |
159 |
0 |
return false; |
160 |
|
} |
161 |
|
} |
162 |
|
|
163 |
|
private static Object convert(Object value, Class type) throws URISyntaxException { |
164 |
3 |
PropertyEditor editor = PropertyEditorManager.findEditor(type); |
165 |
3 |
if( editor != null ) { |
166 |
3 |
editor.setAsText(value.toString()); |
167 |
3 |
return editor.getValue(); |
168 |
|
} |
169 |
0 |
if( type == URI.class ) { |
170 |
0 |
return new URI(value.toString()); |
171 |
|
} |
172 |
0 |
return null; |
173 |
|
} |
174 |
|
|
175 |
|
private static String convertToString(Object value, Class type) throws URISyntaxException { |
176 |
0 |
PropertyEditor editor = PropertyEditorManager.findEditor(type); |
177 |
0 |
if( editor != null ) { |
178 |
0 |
editor.setValue(value); |
179 |
0 |
return editor.getAsText(); |
180 |
|
} |
181 |
0 |
if( type == URI.class ) { |
182 |
0 |
return ((URI)value).toString(); |
183 |
|
} |
184 |
0 |
return null; |
185 |
|
} |
186 |
|
|
187 |
|
private static Method findSetterMethod(Class clazz, String name) { |
188 |
|
|
189 |
3 |
name = "set"+name.substring(0,1).toUpperCase()+name.substring(1); |
190 |
3 |
Method[] methods = clazz.getMethods(); |
191 |
35 |
for (int i = 0; i < methods.length; i++) { |
192 |
35 |
Method method = methods[i]; |
193 |
35 |
Class params[] = method.getParameterTypes(); |
194 |
35 |
if( method.getName().equals(name) |
195 |
|
&& params.length==1 |
196 |
|
&& isSettableType(params[0])) { |
197 |
3 |
return method; |
198 |
|
} |
199 |
|
} |
200 |
0 |
return null; |
201 |
|
} |
202 |
|
|
203 |
|
private static boolean isSettableType(Class clazz) { |
204 |
3 |
if( PropertyEditorManager.findEditor(clazz)!=null ) |
205 |
3 |
return true; |
206 |
0 |
if( clazz == URI.class ) |
207 |
0 |
return true; |
208 |
0 |
if( clazz == Boolean.class ) |
209 |
0 |
return true; |
210 |
0 |
return false; |
211 |
|
} |
212 |
|
|
213 |
|
static public String toString(Object target) { |
214 |
0 |
return toString(target, Object.class); |
215 |
|
} |
216 |
|
|
217 |
|
static public String toString(Object target, Class stopClass) { |
218 |
0 |
LinkedHashMap map = new LinkedHashMap(); |
219 |
0 |
addFields(target, target.getClass(), stopClass, map); |
220 |
0 |
StringBuffer buffer = new StringBuffer(simpleName(target.getClass())); |
221 |
0 |
buffer.append(" {"); |
222 |
0 |
Set entrySet = map.entrySet(); |
223 |
0 |
boolean first = true; |
224 |
0 |
for (Iterator iter = entrySet.iterator(); iter.hasNext();) { |
225 |
0 |
Map.Entry entry = (Map.Entry) iter.next(); |
226 |
0 |
if (first) { |
227 |
0 |
first = false; |
228 |
0 |
} |
229 |
|
else { |
230 |
0 |
buffer.append(", "); |
231 |
|
} |
232 |
0 |
buffer.append(entry.getKey()); |
233 |
0 |
buffer.append(" = "); |
234 |
0 |
appendToString(buffer, entry.getValue()); |
235 |
0 |
} |
236 |
0 |
buffer.append("}"); |
237 |
0 |
return buffer.toString(); |
238 |
|
} |
239 |
|
|
240 |
|
protected static void appendToString(StringBuffer buffer, Object value) { |
241 |
|
|
242 |
|
|
243 |
|
|
244 |
|
|
245 |
|
|
246 |
0 |
buffer.append(value); |
247 |
|
|
248 |
0 |
} |
249 |
|
|
250 |
|
static public String simpleName(Class clazz) { |
251 |
0 |
String name = clazz.getName(); |
252 |
0 |
int p = name.lastIndexOf("."); |
253 |
0 |
if( p >= 0 ) { |
254 |
0 |
name = name.substring(p+1); |
255 |
|
} |
256 |
0 |
return name; |
257 |
|
} |
258 |
|
|
259 |
|
|
260 |
|
static private void addFields(Object target, Class startClass, Class stopClass, LinkedHashMap map) { |
261 |
|
|
262 |
0 |
if( startClass!=stopClass ) |
263 |
0 |
addFields( target, startClass.getSuperclass(), stopClass, map ); |
264 |
|
|
265 |
0 |
Field[] fields = startClass.getDeclaredFields(); |
266 |
0 |
for (int i = 0; i < fields.length; i++) { |
267 |
0 |
Field field = fields[i]; |
268 |
0 |
if( Modifier.isStatic(field.getModifiers()) || |
269 |
|
Modifier.isTransient(field.getModifiers()) || |
270 |
|
Modifier.isPrivate(field.getModifiers()) ) { |
271 |
0 |
continue; |
272 |
|
} |
273 |
|
|
274 |
|
try { |
275 |
0 |
field.setAccessible(true); |
276 |
0 |
Object o = field.get(target); |
277 |
0 |
if( o!=null && o.getClass().isArray() ) { |
278 |
|
try { |
279 |
0 |
o = Arrays.asList((Object[]) o); |
280 |
0 |
} catch (Throwable e) { |
281 |
0 |
} |
282 |
|
} |
283 |
0 |
map.put(field.getName(), o); |
284 |
0 |
} catch (Throwable e) { |
285 |
0 |
e.printStackTrace(); |
286 |
0 |
} |
287 |
|
} |
288 |
|
|
289 |
0 |
} |
290 |
|
|
291 |
|
|
292 |
|
} |