View Javadoc

1   /*
2    * $Id: GetBeanProperty.java 7963 2007-08-21 08:53:15Z 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.transformers.simple;
12  
13  import org.mule.transformers.AbstractTransformer;
14  import org.mule.umo.transformer.TransformerException;
15  
16  import org.apache.commons.beanutils.PropertyUtils;
17  
18  /**
19   * Looks up a property from a JavaBean using PropertyUtils.getProperty().
20   * Nested properties are valid, assuming they follow JavaBean conventions.
21   * 
22   *   <transformer name="ExtractCustomer" className="org.mule.transformers.simple.GetBeanProperty">
23   *       <properties>
24   *           <property name="propertyName" value="customerRequest.customer" />
25   *       </properties>
26   *   </transformer>
27   */
28  public class GetBeanProperty extends AbstractTransformer
29  {
30      private String propertyName;
31      
32      public GetBeanProperty()
33      {
34          super();
35          registerSourceType(Object.class);
36          setReturnClass(Object.class);
37      }
38  
39      public Object doTransform(Object src, String encoding) throws TransformerException
40      {
41          try
42          {
43              return PropertyUtils.getProperty(src, this.propertyName);
44          }
45          catch (Exception e)
46          {
47              throw new TransformerException(this, e);
48          }
49      }
50  
51      public String getPropertyName()
52      {
53          return propertyName;
54      }
55  
56      public void setPropertyName(String propertyName)
57      {
58          this.propertyName = propertyName;
59      }
60  
61  }