From ddeda001da664b5f83afebdf364e069975ee05a0 Mon Sep 17 00:00:00 2001 From: Mark Fisher Date: Fri, 12 Mar 2010 05:11:19 +0000 Subject: [PATCH] checking for prototype scope in setBeanFactory --- .../transformer/MapToObjectTransformer.java | 56 +++++++++++-------- .../MapToObjectTransformerTests.java | 18 ++++-- 2 files changed, 46 insertions(+), 28 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 360e90537c..0f251a68eb 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 @@ -1,5 +1,5 @@ /* - * Copyright 2002-2008 the original author or authors. + * Copyright 2002-2010 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -13,6 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ + package org.springframework.integration.transformer; import java.util.Map; @@ -41,51 +42,60 @@ import org.springframework.validation.DataBinder; * @since 2.0 */ public class MapToObjectTransformer extends AbstractPayloadTransformer, Object> implements BeanFactoryAware{ - private Class targetClass; - private String targetBeanName; - private ConfigurableBeanFactory beanFactory; + + private final Class targetClass; + + private final String targetBeanName; + + private volatile ConfigurableBeanFactory beanFactory; + + /** * @param targetClass */ - public MapToObjectTransformer(Class targetClass){ - try { - this.targetClass = targetClass; - } catch (Exception e) { - throw new MessageTransformationException("Can not create instance of " + targetClass, e); - } + public MapToObjectTransformer(Class targetClass) { + Assert.notNull(targetClass, "targetClass must not be null"); + this.targetClass = targetClass; + this.targetBeanName = null; } + /** - * * @param beanName */ - public MapToObjectTransformer(String beanName){ + public MapToObjectTransformer(String beanName) { + Assert.hasText(beanName, "beanName must not be empty"); this.targetBeanName = beanName; + this.targetClass = null; } + + /* * (non-Javadoc) * @see org.springframework.integration.transformer.AbstractPayloadTransformer#transformPayload(java.lang.Object) */ @SuppressWarnings("unchecked") protected Object transformPayload(Map payload) throws Exception { - Object target = null; - if (StringUtils.hasText(targetBeanName)){ - 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"); - } + Object target = (this.targetClass != null) + ? BeanUtils.instantiate(this.targetClass) + : this.beanFactory.getBean(this.targetBeanName); DataBinder binder = new DataBinder(target); - binder.setConversionService(beanFactory.getConversionService()); - binder.bind(new MutablePropertyValues((Map)payload)); + binder.setConversionService(this.beanFactory.getConversionService()); + binder.bind(new MutablePropertyValues((Map) payload)); return target; } + /* * (non-Javadoc) * @see org.springframework.beans.factory.BeanFactoryAware#setBeanFactory(org.springframework.beans.factory.BeanFactory) */ public void setBeanFactory(BeanFactory beanFactory) throws BeansException { + Assert.isTrue(beanFactory instanceof ConfigurableListableBeanFactory, + "A ConfigurableListableBeanFactory is required."); this.beanFactory = (ConfigurableListableBeanFactory) beanFactory; + if (StringUtils.hasText(this.targetBeanName)) { + Assert.isTrue(this.beanFactory.isPrototype(this.targetBeanName), + "target bean [" + targetBeanName + "] must have 'prototype' scope"); + } } + } 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 fcc666a493..363d39bc81 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 @@ -1,5 +1,5 @@ /* - * Copyright 2002-2008 the original author or authors. + * Copyright 2002-2010 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -13,6 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ + package org.springframework.integration.transformer; import static junit.framework.Assert.assertEquals; @@ -26,6 +27,8 @@ import java.util.Map; import org.junit.Test; import org.springframework.beans.factory.config.ConfigurableBeanFactory; import org.springframework.beans.factory.support.DefaultListableBeanFactory; +import org.springframework.beans.factory.support.RootBeanDefinition; +import org.springframework.context.support.GenericApplicationContext; import org.springframework.context.support.StaticApplicationContext; import org.springframework.core.convert.converter.Converter; import org.springframework.core.convert.support.ConversionServiceFactory; @@ -38,6 +41,7 @@ import org.springframework.integration.message.MessageBuilder; * @since 2.0 */ public class MapToObjectTransformerTests { + @SuppressWarnings("unchecked") @Test public void testMapToObjectTransformation(){ @@ -62,8 +66,9 @@ public class MapToObjectTransformerTests { assertTrue(person.getAddress() instanceof Address); assertEquals("1123 Main st", person.getAddress().getStreet()); } + @SuppressWarnings("unchecked") - @Test(expected=MessageTransformationException.class) + @Test(expected=IllegalArgumentException.class) public void testMapToObjectTransformationNonPrototype(){ Map map = new HashMap(); map.put("fname", "Justin"); @@ -73,10 +78,11 @@ public class MapToObjectTransformerTests { map.put("address", address); Message message = MessageBuilder.withPayload(map).build(); - ConfigurableBeanFactory beanFactory = this.getBeanFactory(); - beanFactory.registerSingleton("person", new Person()); + GenericApplicationContext context = new GenericApplicationContext(); + RootBeanDefinition personDef = new RootBeanDefinition(Person.class); + context.registerBeanDefinition("person", personDef); MapToObjectTransformer transformer = new MapToObjectTransformer("person"); - transformer.setBeanFactory(beanFactory); + transformer.setBeanFactory(context.getBeanFactory()); transformer.transform(message); } @@ -105,6 +111,7 @@ public class MapToObjectTransformerTests { assertTrue(person.getAddress() instanceof Address); assertEquals("1123 Main st", person.getAddress().getStreet()); } + @SuppressWarnings("unchecked") @Test public void testMapToObjectTransformationWithConversionService(){ @@ -136,6 +143,7 @@ public class MapToObjectTransformerTests { beanFactory.setConversionService(conversionService); return beanFactory; } + public static class Person{ private String fname; private String lname;