1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
package org.mule.util; |
8 | |
|
9 | |
import org.mule.config.i18n.CoreMessages; |
10 | |
|
11 | |
import java.lang.reflect.Field; |
12 | |
import java.lang.reflect.InvocationTargetException; |
13 | |
import java.lang.reflect.Method; |
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 | |
|
32 | |
|
33 | 0 | private static final Log logger = LogFactory.getLog(BeanUtils.class); |
34 | |
|
35 | |
|
36 | |
|
37 | |
|
38 | |
|
39 | |
|
40 | |
|
41 | |
|
42 | |
public static void populateWithoutFail(Object object, Map props, boolean logWarnings) |
43 | |
{ |
44 | |
|
45 | |
|
46 | 0 | if (ClassUtils.getMethod(object.getClass(), SET_PROPERTIES_METHOD, new Class[]{Map.class}) != null) |
47 | |
{ |
48 | |
try |
49 | |
{ |
50 | 0 | BeanUtils.setProperty(object, "properties", props); |
51 | |
} |
52 | 0 | catch (Exception e) |
53 | |
{ |
54 | |
|
55 | |
|
56 | 0 | if (logWarnings) |
57 | |
{ |
58 | 0 | logger.warn("Property: " + SET_PROPERTIES_METHOD + "=" + Map.class.getName() |
59 | |
+ " not found on object: " + object.getClass().getName()); |
60 | |
} |
61 | 0 | } |
62 | |
} |
63 | |
else |
64 | |
{ |
65 | 0 | for (Iterator iterator = props.entrySet().iterator(); iterator.hasNext();) |
66 | |
{ |
67 | 0 | Map.Entry entry = (Map.Entry) iterator.next(); |
68 | |
|
69 | |
try |
70 | |
{ |
71 | 0 | BeanUtils.setProperty(object, entry.getKey().toString(), entry.getValue()); |
72 | |
} |
73 | 0 | catch (Exception e) |
74 | |
{ |
75 | 0 | if (logWarnings) |
76 | |
{ |
77 | 0 | logger.warn("Property: " + entry.getKey() + "=" + entry.getValue() |
78 | |
+ " not found on object: " + object.getClass().getName()); |
79 | |
} |
80 | 0 | } |
81 | 0 | } |
82 | |
} |
83 | 0 | } |
84 | |
|
85 | |
|
86 | |
|
87 | |
|
88 | |
|
89 | |
|
90 | |
|
91 | |
|
92 | |
|
93 | |
|
94 | |
public static void populate(Object bean, Map props) throws IllegalAccessException, InvocationTargetException |
95 | |
{ |
96 | |
|
97 | |
|
98 | 0 | if (ClassUtils.getMethod(bean.getClass(), SET_PROPERTIES_METHOD, new Class[]{Map.class}) != null) |
99 | |
{ |
100 | 0 | BeanUtils.setProperty(bean, "properties", props); |
101 | |
} |
102 | |
else |
103 | |
{ |
104 | 0 | Map master = describe(bean); |
105 | 0 | for (Iterator iterator = props.keySet().iterator(); iterator.hasNext();) |
106 | |
{ |
107 | 0 | Object o = iterator.next(); |
108 | 0 | if (!master.containsKey(o)) |
109 | |
{ |
110 | 0 | throw new IllegalArgumentException(CoreMessages.propertyDoesNotExistOnObject(o.toString(), bean).getMessage()); |
111 | |
} |
112 | |
|
113 | 0 | } |
114 | 0 | org.apache.commons.beanutils.BeanUtils.populate(bean, props); |
115 | |
} |
116 | 0 | } |
117 | |
|
118 | |
|
119 | |
|
120 | |
|
121 | |
|
122 | |
|
123 | |
|
124 | |
|
125 | |
public static Map describe(Object object) |
126 | |
{ |
127 | 0 | Map props = new HashMap(object.getClass().getDeclaredFields().length); |
128 | 0 | for (int i = 0; i < object.getClass().getDeclaredFields().length; i++) |
129 | |
{ |
130 | 0 | Field field = object.getClass().getDeclaredFields()[i]; |
131 | 0 | field.setAccessible(true); |
132 | |
try |
133 | |
{ |
134 | 0 | props.put(field.getName(), field.get(object)); |
135 | |
} |
136 | 0 | catch (IllegalAccessException e) |
137 | |
{ |
138 | 0 | logger.debug("Unable to read field: " + field.getName() + " on object: " + object); |
139 | 0 | } |
140 | |
} |
141 | 0 | return props; |
142 | |
} |
143 | |
|
144 | |
|
145 | |
|
146 | |
|
147 | |
|
148 | |
|
149 | |
|
150 | |
|
151 | |
public static Map<String, Object> describeBean(Object object) |
152 | |
{ |
153 | 0 | Map<String, Object> props = new HashMap<String, Object>(); |
154 | 0 | for (int i = 0; i < object.getClass().getMethods().length; i++) |
155 | |
{ |
156 | 0 | Method method = object.getClass().getMethods()[i]; |
157 | 0 | if (method.getName().startsWith("get") || method.getName().startsWith("is")) |
158 | |
{ |
159 | 0 | String field = (method.getName().startsWith("is") ? method.getName().substring(2) : method.getName().substring(3)); |
160 | 0 | String setter = "set" + field; |
161 | |
try |
162 | |
{ |
163 | 0 | object.getClass().getMethod(setter, method.getReturnType()); |
164 | |
} |
165 | 0 | catch (NoSuchMethodException e) |
166 | |
{ |
167 | 0 | logger.debug("Ignoring bean property: " + e.getMessage()); |
168 | 0 | continue; |
169 | 0 | } |
170 | 0 | field = field.substring(0, 1).toLowerCase() + field.substring(1); |
171 | |
try |
172 | |
{ |
173 | 0 | props.put(field, method.invoke(object)); |
174 | |
} |
175 | 0 | catch (Exception e) |
176 | |
{ |
177 | 0 | logger.debug("unable to call bean method: " + method); |
178 | 0 | } |
179 | |
} |
180 | |
} |
181 | 0 | return props; |
182 | |
} |
183 | |
} |