KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mule > util > BeanUtils


1 /*
2  * $Id: BeanUtils.java 3798 2006-11-04 04:07:14Z aperepel $
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 MuleSource MPL
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 org.apache.commons.logging.Log;
14 import org.apache.commons.logging.LogFactory;
15
16 import java.util.Iterator JavaDoc;
17 import java.util.Map JavaDoc;
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 JavaDoc 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 JavaDoc object, Map JavaDoc 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(SET_PROPERTIES_METHOD, new Class JavaDoc[]{Map JavaDoc.class}, object.getClass()) != null)
41         {
42             try
43             {
44                 BeanUtils.setProperty(object, "properties", props);
45             }
46             catch (Exception JavaDoc 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 JavaDoc.class.getName()
53                                 + " not found on object: " + object.getClass().getName());
54                 }
55             }
56         }
57         else
58         {
59             for (Iterator JavaDoc iterator = props.entrySet().iterator(); iterator.hasNext();)
60             {
61                 Map.Entry JavaDoc entry = (Map.Entry JavaDoc)iterator.next();
62
63                 try
64                 {
65                     BeanUtils.setProperty(object, entry.getKey().toString(), entry.getValue());
66                 }
67                 catch (Exception JavaDoc 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 }
80
Popular Tags