View Javadoc

1   /*
2    * $Id: PayloadPropertyExtractor.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.properties;
12  
13  import org.mule.umo.UMOMessage;
14  import org.mule.util.StringUtils;
15  
16  import java.lang.reflect.InvocationTargetException;
17  
18  import org.apache.commons.beanutils.PropertyUtils;
19  import org.apache.commons.logging.Log;
20  import org.apache.commons.logging.LogFactory;
21  
22  /**
23   * Checks the payload object for a bean property matching the property name
24   */
25  public class PayloadPropertyExtractor implements PropertyExtractor
26  {
27      /**
28       * logger used by this class
29       */
30      protected transient Log logger = LogFactory.getLog(getClass());
31  
32      public Object getProperty(String name, Object message)
33      {
34          Object payload = message;
35          if (message instanceof UMOMessage)
36          {
37              payload = ((UMOMessage) message).getPayload();
38          }
39          Object value = null;
40          try
41          {
42              if ((PropertyUtils.getPropertyDescriptor(payload, name) != null))
43              {
44                  value = PropertyUtils.getProperty(payload, name);
45                  if (value == null)
46                  {
47                      value = StringUtils.EMPTY;
48                  }
49              }
50          }
51          catch (IllegalAccessException e)
52          {
53              logger.warn("Failed to read property: " + name, e);
54          }
55          catch (InvocationTargetException e)
56          {
57              logger.warn("Failed to read property: " + name, e);
58          }
59          catch (NoSuchMethodException e)
60          {
61              // will never happen
62          }
63          return value;
64      }
65  }