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