View Javadoc

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