View Javadoc

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