From 46577add6b4939b7a937df8754c8fa1be3911951 Mon Sep 17 00:00:00 2001 From: Oleg Zhurakousky Date: Thu, 7 Apr 2011 12:39:40 -0400 Subject: [PATCH] INT-1856 added support for filename-generator-expression to outbound file adapters, added tests --- .../FileOutboundChannelAdapterParser.java | 29 +++++++++++++++++-- ...boundChannelAdapterParserTests-context.xml | 3 +- ...FileOutboundChannelAdapterParserTests.java | 23 ++++++++++----- ...FileOutboundGatewayParserTests-context.xml | 3 +- .../FileOutboundGatewayParserTests.java | 8 +++++ 5 files changed, 55 insertions(+), 11 deletions(-) diff --git a/spring-integration-file/src/main/java/org/springframework/integration/file/config/FileOutboundChannelAdapterParser.java b/spring-integration-file/src/main/java/org/springframework/integration/file/config/FileOutboundChannelAdapterParser.java index 0ec13d4e51..c1e6dfb1be 100644 --- a/spring-integration-file/src/main/java/org/springframework/integration/file/config/FileOutboundChannelAdapterParser.java +++ b/spring-integration-file/src/main/java/org/springframework/integration/file/config/FileOutboundChannelAdapterParser.java @@ -18,11 +18,13 @@ package org.springframework.integration.file.config; import org.w3c.dom.Element; +import org.springframework.beans.factory.BeanDefinitionStoreException; import org.springframework.beans.factory.support.AbstractBeanDefinition; import org.springframework.beans.factory.support.BeanDefinitionBuilder; import org.springframework.beans.factory.xml.ParserContext; import org.springframework.integration.config.xml.AbstractOutboundChannelAdapterParser; import org.springframework.integration.context.IntegrationContextUtils; +import org.springframework.util.StringUtils; /** * Parser for the <outbound-channel-adapter/> element of the 'file' @@ -30,14 +32,37 @@ import org.springframework.integration.context.IntegrationContextUtils; * * @author Mark Fisher * @author Iwein Fuld + * @author Oleg Zhurakousky */ public class FileOutboundChannelAdapterParser extends AbstractOutboundChannelAdapterParser { @Override protected AbstractBeanDefinition parseConsumer(Element element, ParserContext parserContext) { - BeanDefinitionBuilder builder = FileWritingMessageHandlerBeanDefinitionBuilder.configure( + BeanDefinitionBuilder handlerBuilder = FileWritingMessageHandlerBeanDefinitionBuilder.configure( element, IntegrationContextUtils.NULL_CHANNEL_BEAN_NAME, parserContext); - return (builder != null ? builder.getBeanDefinition() : null); + if (handlerBuilder != null){ + String remoteFileNameGenerator = element.getAttribute("filename-generator"); + String remoteFileNameGeneratorExpression = element.getAttribute("filename-generator-expression"); + boolean hasRemoteFileNameGenerator = StringUtils.hasText(remoteFileNameGenerator); + boolean hasRemoteFileNameGeneratorExpression = StringUtils.hasText(remoteFileNameGeneratorExpression); + if (hasRemoteFileNameGenerator || hasRemoteFileNameGeneratorExpression) { + if (hasRemoteFileNameGenerator && hasRemoteFileNameGeneratorExpression) { + throw new BeanDefinitionStoreException("at most one of 'filename-generator-expression' or 'filename-generator' " + + "is allowed on file outbound adapter/gateway"); + } + if (hasRemoteFileNameGenerator) { + handlerBuilder.addPropertyReference("fileNameGenerator", remoteFileNameGenerator); + } + else { + BeanDefinitionBuilder fileNameGeneratorBuilder = BeanDefinitionBuilder.genericBeanDefinition( + "org.springframework.integration.file.DefaultFileNameGenerator"); + fileNameGeneratorBuilder.addPropertyValue("expression", remoteFileNameGeneratorExpression); + handlerBuilder.addPropertyValue("fileNameGenerator", fileNameGeneratorBuilder.getBeanDefinition()); + } + } + } + + return (handlerBuilder != null ? handlerBuilder.getBeanDefinition() : null); } } diff --git a/spring-integration-file/src/test/java/org/springframework/integration/file/config/FileOutboundChannelAdapterParserTests-context.xml b/spring-integration-file/src/test/java/org/springframework/integration/file/config/FileOutboundChannelAdapterParserTests-context.xml index 41320fec7d..3bacbd2c57 100644 --- a/spring-integration-file/src/test/java/org/springframework/integration/file/config/FileOutboundChannelAdapterParserTests-context.xml +++ b/spring-integration-file/src/test/java/org/springframework/integration/file/config/FileOutboundChannelAdapterParserTests-context.xml @@ -18,7 +18,8 @@ + temporary-file-suffix=".foo" + filename-generator-expression="'foo.txt'"/> + order="777" + filename-generator-expression="'foo.txt'"/> diff --git a/spring-integration-file/src/test/java/org/springframework/integration/file/config/FileOutboundGatewayParserTests.java b/spring-integration-file/src/test/java/org/springframework/integration/file/config/FileOutboundGatewayParserTests.java index 44f7bd4e65..5d9663063f 100644 --- a/spring-integration-file/src/test/java/org/springframework/integration/file/config/FileOutboundGatewayParserTests.java +++ b/spring-integration-file/src/test/java/org/springframework/integration/file/config/FileOutboundGatewayParserTests.java @@ -21,11 +21,14 @@ import org.junit.runner.RunWith; import org.springframework.beans.DirectFieldAccessor; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.ApplicationContext; +import org.springframework.integration.file.DefaultFileNameGenerator; import org.springframework.integration.file.FileWritingMessageHandler; +import org.springframework.integration.test.util.TestUtils; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; /** * @author Mark Fisher @@ -47,6 +50,11 @@ public class FileOutboundGatewayParserTests { assertEquals(Boolean.FALSE, gatewayAccessor.getPropertyValue("autoStartup")); DirectFieldAccessor handlerAccessor = new DirectFieldAccessor(handler); assertEquals(777, handlerAccessor.getPropertyValue("order")); + DefaultFileNameGenerator fileNameGenerator = (DefaultFileNameGenerator) handlerAccessor.getPropertyValue("fileNameGenerator"); + assertNotNull(fileNameGenerator); + String expression = (String) TestUtils.getPropertyValue(fileNameGenerator, "expression"); + assertNotNull(expression); + assertEquals("'foo.txt'", expression); } }