01: /*
02: * $Id: BeanUtils.java 9852 2007-11-23 20:31:42Z rossmason $
03: * --------------------------------------------------------------------------------------
04: * Copyright (c) MuleSource, Inc. All rights reserved. http://www.mulesource.com
05: *
06: * The software in this package is published under the terms of the CPAL v1.0
07: * license, a copy of which has been included with this distribution in the
08: * LICENSE.txt file.
09: */
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: * <code>BeanUtils</code> provides functions for altering the way commons BeanUtils
23: * works
24: */
25: // @ThreadSafe
26: public class BeanUtils extends org.apache.commons.beanutils.BeanUtils {
27: public static final String SET_PROPERTIES_METHOD = "setProperties";
28:
29: /** logger used by this class */
30: private static final Log logger = LogFactory
31: .getLog(BeanUtils.class);
32:
33: /** Exception safe version of BeanUtils.populateWithoutFail */
34: public static void populateWithoutFail(Object object, Map props,
35: boolean logWarnings) {
36: // Check to see if our object has a setProperties method where the properties
37: // map should be set
38: if (ClassUtils.getMethod(object.getClass(),
39: SET_PROPERTIES_METHOD, new Class[] { Map.class }) != null) {
40: try {
41: BeanUtils.setProperty(object, "properties", props);
42: } catch (Exception e) {
43: // this should never happen since we explicitly check for the method
44: // above
45: if (logWarnings) {
46: logger.warn("Property: " + SET_PROPERTIES_METHOD
47: + "=" + Map.class.getName()
48: + " not found on object: "
49: + object.getClass().getName());
50: }
51: }
52: } else {
53: for (Iterator iterator = props.entrySet().iterator(); iterator
54: .hasNext();) {
55: Map.Entry entry = (Map.Entry) iterator.next();
56:
57: try {
58: BeanUtils.setProperty(object, entry.getKey()
59: .toString(), entry.getValue());
60: } catch (Exception e) {
61: if (logWarnings) {
62: logger.warn("Property: " + entry.getKey() + "="
63: + entry.getValue()
64: + " not found on object: "
65: + object.getClass().getName());
66: }
67: }
68: }
69: }
70: }
71:
72: /**
73: * The Apache BeanUtils version of this converts all values to String, which is pretty useless, it also includes
74: * stuff not defined by the user
75: *
76: * @param object the object to Describe
77: * @return a map of the properties on the object
78: */
79: public static Map describe(Object object) {
80: Map props = new HashMap(
81: object.getClass().getDeclaredFields().length);
82: for (int i = 0; i < object.getClass().getDeclaredFields().length; i++) {
83: Field field = object.getClass().getDeclaredFields()[i];
84: field.setAccessible(true);
85: try {
86: props.put(field.getName(), field.get(object));
87: } catch (IllegalAccessException e) {
88: logger.debug("Unable to read field: " + field.getName()
89: + " on object: " + object);
90: }
91: }
92: return props;
93: }
94: }
|