1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
package org.mule.util; |
12 | |
|
13 | |
import java.lang.reflect.Field; |
14 | |
import java.util.HashMap; |
15 | |
import java.util.Iterator; |
16 | |
import java.util.Map; |
17 | |
|
18 | |
import org.apache.commons.logging.Log; |
19 | |
import org.apache.commons.logging.LogFactory; |
20 | |
|
21 | |
|
22 | |
|
23 | |
|
24 | |
|
25 | |
|
26 | 0 | public class BeanUtils extends org.apache.commons.beanutils.BeanUtils |
27 | |
{ |
28 | |
public static final String SET_PROPERTIES_METHOD = "setProperties"; |
29 | |
|
30 | |
|
31 | 2 | private static final Log logger = LogFactory.getLog(BeanUtils.class); |
32 | |
|
33 | |
|
34 | |
public static void populateWithoutFail(Object object, Map props, boolean logWarnings) |
35 | |
{ |
36 | |
|
37 | |
|
38 | 34 | if (ClassUtils.getMethod(object.getClass(), SET_PROPERTIES_METHOD, new Class[]{Map.class}) != null) |
39 | |
{ |
40 | |
try |
41 | |
{ |
42 | 0 | BeanUtils.setProperty(object, "properties", props); |
43 | |
} |
44 | 0 | catch (Exception e) |
45 | |
{ |
46 | |
|
47 | |
|
48 | 0 | if (logWarnings) |
49 | |
{ |
50 | 0 | logger.warn("Property: " + SET_PROPERTIES_METHOD + "=" + Map.class.getName() |
51 | |
+ " not found on object: " + object.getClass().getName()); |
52 | |
} |
53 | 0 | } |
54 | |
} |
55 | |
else |
56 | |
{ |
57 | 34 | for (Iterator iterator = props.entrySet().iterator(); iterator.hasNext();) |
58 | |
{ |
59 | 68 | Map.Entry entry = (Map.Entry) iterator.next(); |
60 | |
|
61 | |
try |
62 | |
{ |
63 | 68 | BeanUtils.setProperty(object, entry.getKey().toString(), entry.getValue()); |
64 | |
} |
65 | 0 | catch (Exception e) |
66 | |
{ |
67 | 0 | if (logWarnings) |
68 | |
{ |
69 | 0 | logger.warn("Property: " + entry.getKey() + "=" + entry.getValue() |
70 | |
+ " not found on object: " + object.getClass().getName()); |
71 | |
} |
72 | 68 | } |
73 | 68 | } |
74 | |
} |
75 | 34 | } |
76 | |
|
77 | |
|
78 | |
|
79 | |
|
80 | |
|
81 | |
|
82 | |
|
83 | |
|
84 | |
public static Map describe(Object object) |
85 | |
{ |
86 | 0 | Map props = new HashMap(object.getClass().getDeclaredFields().length); |
87 | 0 | for (int i = 0; i < object.getClass().getDeclaredFields().length; i++) |
88 | |
{ |
89 | 0 | Field field = object.getClass().getDeclaredFields()[i]; |
90 | 0 | field.setAccessible(true); |
91 | |
try |
92 | |
{ |
93 | 0 | props.put(field.getName(), field.get(object)); |
94 | |
} |
95 | 0 | catch (IllegalAccessException e) |
96 | |
{ |
97 | 0 | logger.debug("Unable to read field: " + field.getName() + " on object: " + object); |
98 | 0 | } |
99 | |
} |
100 | 0 | return props; |
101 | |
} |
102 | |
} |