KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mule > util > properties > PayloadPropertyExtractor


1 /*
2  * $Id: PayloadPropertyExtractor.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.properties;
12
13 import org.mule.umo.UMOMessage;
14 import org.mule.util.StringUtils;
15 import org.apache.commons.beanutils.PropertyUtils;
16 import org.apache.commons.logging.LogFactory;
17 import org.apache.commons.logging.Log;
18
19 import java.lang.reflect.InvocationTargetException JavaDoc;
20
21 /**
22  * Checks the payload object for a bean property matching the property name
23  */

24 public class PayloadPropertyExtractor implements PropertyExtractor
25 {
26     /**
27      * logger used by this class
28      */

29     protected transient Log logger = LogFactory.getLog(getClass());
30
31     public Object JavaDoc getProperty(String JavaDoc name, Object JavaDoc message)
32     {
33         Object JavaDoc payload = message;
34         if (message instanceof UMOMessage)
35         {
36             payload = ((UMOMessage)message).getPayload();
37         }
38         Object JavaDoc value = null;
39         try
40         {
41             if ((PropertyUtils.getPropertyDescriptor(payload, name) != null))
42             {
43                 value = PropertyUtils.getProperty(payload, name);
44                 if (value == null)
45                 {
46                     value = StringUtils.EMPTY;
47                 }
48             }
49         }
50         catch (IllegalAccessException JavaDoc e)
51         {
52             logger.warn("Failed to read property: " + name, e);
53         }
54         catch (InvocationTargetException JavaDoc e)
55         {
56             logger.warn("Failed to read property: " + name, e);
57         }
58         catch (NoSuchMethodException JavaDoc e)
59         {
60             // will never happen
61
}
62         return value;
63     }
64 }
65
Popular Tags