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