View Javadoc

1   /*
2    * $Id: BeanUtils.java 7976 2007-08-21 14:26:13Z dirk.olmes $
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.util.Iterator;
14  import java.util.Map;
15  
16  import org.apache.commons.logging.Log;
17  import org.apache.commons.logging.LogFactory;
18  
19  /**
20   * <code>BeanUtils</code> provides functions for altering the way commons BeanUtils
21   * works
22   */
23  // @ThreadSafe
24  public class BeanUtils extends org.apache.commons.beanutils.BeanUtils
25  {
26      public static final String SET_PROPERTIES_METHOD = "setProperties";
27  
28      /**
29       * logger used by this class
30       */
31      private static final Log logger = LogFactory.getLog(BeanUtils.class);
32  
33      /**
34       * Exception safe version of BeanUtils.populateWithoutFail
35       */
36      public static void populateWithoutFail(Object object, Map props, boolean logWarnings)
37      {
38          // Check to see if our object has a setProperties method where the properties
39          // map should be set
40          if (ClassUtils.getMethod(object.getClass(), SET_PROPERTIES_METHOD, new Class[]{Map.class}) != null)
41          {
42              try
43              {
44                  BeanUtils.setProperty(object, "properties", props);
45              }
46              catch (Exception e)
47              {
48                  // this should never happen since we explicitly check for the method
49                  // above
50                  if (logWarnings)
51                  {
52                      logger.warn("Property: " + SET_PROPERTIES_METHOD + "=" + Map.class.getName()
53                                  + " not found on object: " + object.getClass().getName());
54                  }
55              }
56          }
57          else
58          {
59              for (Iterator iterator = props.entrySet().iterator(); iterator.hasNext();)
60              {
61                  Map.Entry entry = (Map.Entry) iterator.next();
62  
63                  try
64                  {
65                      BeanUtils.setProperty(object, entry.getKey().toString(), entry.getValue());
66                  }
67                  catch (Exception e)
68                  {
69                      if (logWarnings)
70                      {
71                          logger.warn("Property: " + entry.getKey() + "=" + entry.getValue()
72                                      + " not found on object: " + object.getClass().getName());
73                      }
74                  }
75              }
76          }
77      }
78  
79  }