Coverage Report - org.mule.transformer.simple.MapToBean
 
Classes in this File Line Coverage Branch Coverage Complexity
MapToBean
0%
0/24
0%
0/8
0
 
 1  
 /*
 2  
  * $Id: MapToBean.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.lifecycle.InitialisationException;
 13  
 import org.mule.api.transformer.DiscoverableTransformer;
 14  
 import org.mule.api.transformer.TransformerException;
 15  
 import org.mule.config.i18n.CoreMessages;
 16  
 import org.mule.transformer.AbstractTransformer;
 17  
 import org.mule.transformer.types.DataTypeFactory;
 18  
 import org.mule.util.BeanUtils;
 19  
 import org.mule.util.ClassUtils;
 20  
 
 21  
 import java.util.Map;
 22  
 
 23  
 /**
 24  
  * Creates and object of type {@link #getReturnClass()} and populates values of a
 25  
  * {@link java.util.Map} as bean properties on the object.
 26  
  * The bean class name can also be passed in as a property on the Map (which gets removed once read).
 27  
  * The {@link MapToBean#CLASS_PROPERTY} should be set as a fully qualified class name string.
 28  
  */
 29  
 public class MapToBean extends AbstractTransformer implements DiscoverableTransformer
 30  
 {
 31  
     /**
 32  
      * {@value}
 33  
      */
 34  
     public static final String CLASS_PROPERTY = "className";
 35  
 
 36  0
     private int priorityWeighting = DiscoverableTransformer.DEFAULT_PRIORITY_WEIGHTING;
 37  
 
 38  
     public MapToBean()
 39  0
     {
 40  0
         registerSourceType(DataTypeFactory.create(Map.class));
 41  0
         setReturnDataType(DataTypeFactory.OBJECT);
 42  0
     }
 43  
 
 44  
     @Override
 45  
     public void initialise() throws InitialisationException
 46  
     {
 47  0
         super.initialise();
 48  0
         if(getReturnClass().equals(Object.class))
 49  
         {
 50  0
             throw new InitialisationException(CoreMessages.propertiesNotSet("returnClass"), this);
 51  
         }
 52  0
     }
 53  
 
 54  
     @Override
 55  
     protected Object doTransform(Object src, String encoding) throws TransformerException
 56  
     {
 57  
         try
 58  
         {
 59  0
             Map props = (Map)src;
 60  0
             String c = (String)props.remove(CLASS_PROPERTY);
 61  0
             Class clazz = getReturnClass();
 62  0
             if(c==null && clazz.equals(Object.class))
 63  
             {
 64  0
                 throw new TransformerException(CoreMessages.transforemrMapBeanClassNotSet());
 65  
             }
 66  0
             else if (c!=null)
 67  
             {
 68  0
                 clazz = ClassUtils.loadClass(c, getClass());
 69  
             }
 70  
 
 71  0
             Object result = ClassUtils.instanciateClass(clazz, ClassUtils.NO_ARGS);
 72  0
             BeanUtils.populate(result, props);
 73  
 
 74  0
             return result;
 75  
         }
 76  0
         catch (Exception e)
 77  
         {
 78  0
             throw new TransformerException(this, e);
 79  
         }
 80  
     }
 81  
 
 82  
     public int getPriorityWeighting()
 83  
     {
 84  0
         return priorityWeighting;
 85  
     }
 86  
 
 87  
     public void setPriorityWeighting(int weighting)
 88  
     {
 89  0
         priorityWeighting = weighting;
 90  0
     }
 91  
 }