From 224861f5b835634d60d6723bc376aa17d072a3d4 Mon Sep 17 00:00:00 2001 From: Oleg Zhurakousky Date: Wed, 10 Mar 2010 02:06:56 +0000 Subject: [PATCH] INT-575, Added the following to MapToObjectTransformer: 1. Javadoc 2. Added check to only allow 'prototype' beans to be specified during the construction 3. Made sure that instance of the transformed Object is created only during the transformation --- .../transformer/MapToObjectTransformer.java | 23 ++++++++++++++----- .../MapToObjectTransformerTests.java | 2 +- 2 files changed, 18 insertions(+), 7 deletions(-) diff --git a/org.springframework.integration/src/main/java/org/springframework/integration/transformer/MapToObjectTransformer.java b/org.springframework.integration/src/main/java/org/springframework/integration/transformer/MapToObjectTransformer.java index 97c5540051..360e90537c 100644 --- a/org.springframework.integration/src/main/java/org/springframework/integration/transformer/MapToObjectTransformer.java +++ b/org.springframework.integration/src/main/java/org/springframework/integration/transformer/MapToObjectTransformer.java @@ -24,25 +24,32 @@ import org.springframework.beans.factory.BeanFactory; import org.springframework.beans.factory.BeanFactoryAware; import org.springframework.beans.factory.config.ConfigurableBeanFactory; import org.springframework.beans.factory.config.ConfigurableListableBeanFactory; +import org.springframework.core.convert.ConversionService; import org.springframework.util.Assert; import org.springframework.util.StringUtils; import org.springframework.validation.DataBinder; /** + * Will transform Map to an instance of Object. There are two ways to specify the type of the transformed Object. + * You can use one of two constructors. The constructor that takes the Class<?> as an argument will construct the Object of + * that type. There is another constructor that takes a 'beanName' as an argument and will populate this bean with transformed data. + * Such bean must be of 'prototype' scope otherwise {@link MessageTransformationException} will be thrown. + * This transformer is integrated with the {@link ConversionService} allowing values in the Map to be converted + * to types that represent the properties of the Object. + * * @author Oleg Zhurakousky * @since 2.0 */ public class MapToObjectTransformer extends AbstractPayloadTransformer, Object> implements BeanFactoryAware{ - private Object target; + private Class targetClass; private String targetBeanName; private ConfigurableBeanFactory beanFactory; /** - * * @param targetClass */ public MapToObjectTransformer(Class targetClass){ try { - this.target = BeanUtils.instantiate(targetClass); + this.targetClass = targetClass; } catch (Exception e) { throw new MessageTransformationException("Can not create instance of " + targetClass, e); } @@ -60,14 +67,18 @@ public class MapToObjectTransformer extends AbstractPayloadTransformer, */ @SuppressWarnings("unchecked") protected Object transformPayload(Map payload) throws Exception { + Object target = null; if (StringUtils.hasText(targetBeanName)){ - Assert.isTrue(!beanFactory.isSingleton(targetBeanName), "bean " + targetBeanName + " must be 'prototype'"); + Assert.isTrue(beanFactory.isPrototype(targetBeanName), "bean " + targetBeanName + " must be 'prototype' or not managed by Spring AC"); target = beanFactory.getBean(targetBeanName); + } else if (targetClass != null){ + target = BeanUtils.instantiate(targetClass); + } else { + throw new MessageTransformationException("'targetClass or target 'beanName' must be specified"); } DataBinder binder = new DataBinder(target); binder.setConversionService(beanFactory.getConversionService()); - MutablePropertyValues pv = new MutablePropertyValues((Map)payload); - binder.bind(pv); + binder.bind(new MutablePropertyValues((Map)payload)); return target; } /* diff --git a/org.springframework.integration/src/test/java/org/springframework/integration/transformer/MapToObjectTransformerTests.java b/org.springframework.integration/src/test/java/org/springframework/integration/transformer/MapToObjectTransformerTests.java index ae32bb02ee..fcc666a493 100644 --- a/org.springframework.integration/src/test/java/org/springframework/integration/transformer/MapToObjectTransformerTests.java +++ b/org.springframework.integration/src/test/java/org/springframework/integration/transformer/MapToObjectTransformerTests.java @@ -64,7 +64,7 @@ public class MapToObjectTransformerTests { } @SuppressWarnings("unchecked") @Test(expected=MessageTransformationException.class) - public void testMapToObjectTransformationWithSingleton(){ + public void testMapToObjectTransformationNonPrototype(){ Map map = new HashMap(); map.put("fname", "Justin"); map.put("lname", "Case");