View Javadoc

1   /*
2    * $Id: BeanToMap.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  package org.mule.transformer.simple;
11  
12  import org.mule.api.transformer.DiscoverableTransformer;
13  import org.mule.api.transformer.TransformerException;
14  import org.mule.transformer.AbstractTransformer;
15  import org.mule.transformer.types.DataTypeFactory;
16  import org.mule.util.BeanUtils;
17  
18  import java.util.Map;
19  
20  /**
21   * Conversts a simple bean object to a Map. every property on the bean will become an entry in the
22   * result {@link java.util.Map}. Note that only exposed bean properties with getter and setter methods will be
23   * added to the map.
24   */
25  public class BeanToMap extends AbstractTransformer implements DiscoverableTransformer
26  {
27  
28      private int priorityWeighting = DiscoverableTransformer.DEFAULT_PRIORITY_WEIGHTING;
29  
30      public BeanToMap()
31      {
32          registerSourceType(DataTypeFactory.OBJECT);
33          setReturnDataType(DataTypeFactory.create(Map.class));
34      }
35  
36      @Override
37      protected Object doTransform(Object src, String encoding) throws TransformerException
38      {
39          Map result = BeanUtils.describeBean(src);
40          return result;
41      }
42  
43      public int getPriorityWeighting()
44      {
45          return priorityWeighting;
46      }
47  
48      public void setPriorityWeighting(int weighting)
49      {
50          priorityWeighting = weighting;
51      }
52  
53      
54  }