From 703b24541ff1b30a6ffec4d6d8e1a5a796688770 Mon Sep 17 00:00:00 2001 From: Gary Russell Date: Mon, 18 Nov 2013 22:20:56 -0500 Subject: [PATCH] INT-3209 DefaultFileNameGenerator - SpEL Parsing JIRA: https://jira.springsource.org/browse/INT-3209 Previously, the DefaultFileNameGenerator re-parsed the SpEL expression for every message. Compile the SpEL once (and if it is changed). Change unit tests to match changes. --- .../file/DefaultFileNameGenerator.java | 18 +++++++++++++----- .../file/DefaultFileNameGeneratorTests.java | 11 +++++++++++ .../FileOutboundChannelAdapterParserTests.java | 4 ++-- .../config/FileOutboundGatewayParserTests.java | 5 +++-- .../OutboundChannelAdapterParserTests.java | 5 +++-- 5 files changed, 32 insertions(+), 11 deletions(-) diff --git a/spring-integration-file/src/main/java/org/springframework/integration/file/DefaultFileNameGenerator.java b/spring-integration-file/src/main/java/org/springframework/integration/file/DefaultFileNameGenerator.java index 6658d3245a..3654ce4643 100644 --- a/spring-integration-file/src/main/java/org/springframework/integration/file/DefaultFileNameGenerator.java +++ b/spring-integration-file/src/main/java/org/springframework/integration/file/DefaultFileNameGenerator.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2010 the original author or authors. + * Copyright 2002-2013 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. @@ -18,6 +18,9 @@ package org.springframework.integration.file; import java.io.File; +import org.springframework.expression.Expression; +import org.springframework.expression.ExpressionParser; +import org.springframework.expression.spel.standard.SpelExpressionParser; import org.springframework.integration.Message; import org.springframework.integration.util.AbstractExpressionEvaluator; import org.springframework.util.Assert; @@ -34,13 +37,17 @@ import org.springframework.util.StringUtils; * associated with the header if no expression has been provided), it checks if * the Message payload is a File instance, and if so, it uses the same name. * Finally, it falls back to the Message ID and adds the suffix '.msg'. - * + * * @author Mark Fisher + * @author Gary Russell */ public class DefaultFileNameGenerator extends AbstractExpressionEvaluator implements FileNameGenerator { - private volatile String expression = "headers['" + FileHeaders.FILENAME + "']"; + private static final String DEFAULT_EXPRESSION = "headers['" + FileHeaders.FILENAME + "']"; + private final static ExpressionParser parser = new SpelExpressionParser(); + + private volatile Expression expression = parser.parseExpression(DEFAULT_EXPRESSION); /** * Specify an expression to be evaluated against the Message @@ -48,7 +55,7 @@ public class DefaultFileNameGenerator extends AbstractExpressionEvaluator implem */ public void setExpression(String expression) { Assert.hasText(expression, "expression must not be empty"); - this.expression = expression; + this.expression = parser.parseExpression(expression); } /** @@ -57,9 +64,10 @@ public class DefaultFileNameGenerator extends AbstractExpressionEvaluator implem */ public void setHeaderName(String headerName) { Assert.notNull(headerName, "'headerName' must not be null"); - this.expression = "headers['" + headerName + "']"; + this.expression = parser.parseExpression("headers['" + headerName + "']"); } + @Override public String generateFileName(Message message) { Object filenameProperty = this.evaluateExpression(this.expression, message); if (filenameProperty instanceof String && StringUtils.hasText((String) filenameProperty)) { diff --git a/spring-integration-file/src/test/java/org/springframework/integration/file/DefaultFileNameGeneratorTests.java b/spring-integration-file/src/test/java/org/springframework/integration/file/DefaultFileNameGeneratorTests.java index c2f1f4d7af..2093938a94 100644 --- a/spring-integration-file/src/test/java/org/springframework/integration/file/DefaultFileNameGeneratorTests.java +++ b/spring-integration-file/src/test/java/org/springframework/integration/file/DefaultFileNameGeneratorTests.java @@ -122,6 +122,17 @@ public class DefaultFileNameGeneratorTests { assertEquals("bar", filename); } + @Test + public void customExpressionTakesPrecedenceOverFilePayload() { + DefaultFileNameGenerator generator = new DefaultFileNameGenerator(); + generator.setBeanFactory(mock(BeanFactory.class)); + generator.setExpression("'foobar'"); + File payload = new File("/some/path/ignore"); + Message message = MessageBuilder.withPayload(payload).build(); + String filename = generator.generateFileName(message); + assertEquals("foobar", filename); + } + @Test public void customHeaderNameTakesPrecedenceOverDefault() { DefaultFileNameGenerator generator = new DefaultFileNameGenerator(); diff --git a/spring-integration-file/src/test/java/org/springframework/integration/file/config/FileOutboundChannelAdapterParserTests.java b/spring-integration-file/src/test/java/org/springframework/integration/file/config/FileOutboundChannelAdapterParserTests.java index 7dc76045c5..7341799290 100644 --- a/spring-integration-file/src/test/java/org/springframework/integration/file/config/FileOutboundChannelAdapterParserTests.java +++ b/spring-integration-file/src/test/java/org/springframework/integration/file/config/FileOutboundChannelAdapterParserTests.java @@ -106,9 +106,9 @@ public class FileOutboundChannelAdapterParserTests { assertThat(actual, is(expected)); DefaultFileNameGenerator fileNameGenerator = (DefaultFileNameGenerator) handlerAccessor.getPropertyValue("fileNameGenerator"); assertNotNull(fileNameGenerator); - String expression = (String) TestUtils.getPropertyValue(fileNameGenerator, "expression"); + Expression expression = TestUtils.getPropertyValue(fileNameGenerator, "expression", Expression.class); assertNotNull(expression); - assertEquals("'foo.txt'", expression); + assertEquals("'foo.txt'", expression.getExpressionString()); assertEquals(Boolean.FALSE, handlerAccessor.getPropertyValue("deleteSourceFiles")); } 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 296715ae40..37889383cb 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 @@ -26,6 +26,7 @@ import java.io.File; import org.junit.Test; import org.junit.runner.RunWith; + import org.springframework.beans.DirectFieldAccessor; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; @@ -93,9 +94,9 @@ public class FileOutboundGatewayParserTests { assertEquals(Boolean.TRUE, handlerAccessor.getPropertyValue("requiresReply")); DefaultFileNameGenerator fileNameGenerator = (DefaultFileNameGenerator) handlerAccessor.getPropertyValue("fileNameGenerator"); assertNotNull(fileNameGenerator); - String expression = (String) TestUtils.getPropertyValue(fileNameGenerator, "expression"); + Expression expression = TestUtils.getPropertyValue(fileNameGenerator, "expression", Expression.class); assertNotNull(expression); - assertEquals("'foo.txt'", expression); + assertEquals("'foo.txt'", expression.getExpressionString()); Long sendTimeout = TestUtils.getPropertyValue(handler, "messagingTemplate.sendTimeout", Long.class); assertEquals(Long.valueOf(777), sendTimeout); diff --git a/spring-integration-sftp/src/test/java/org/springframework/integration/sftp/config/OutboundChannelAdapterParserTests.java b/spring-integration-sftp/src/test/java/org/springframework/integration/sftp/config/OutboundChannelAdapterParserTests.java index d7ab2d1ecb..5c2321006e 100644 --- a/spring-integration-sftp/src/test/java/org/springframework/integration/sftp/config/OutboundChannelAdapterParserTests.java +++ b/spring-integration-sftp/src/test/java/org/springframework/integration/sftp/config/OutboundChannelAdapterParserTests.java @@ -108,8 +108,9 @@ public class OutboundChannelAdapterParserTests { assertNotNull(remoteDirectoryExpression); assertEquals("'foo' + '/' + 'bar'", remoteDirectoryExpression.getExpressionString()); FileNameGenerator generator = (FileNameGenerator) TestUtils.getPropertyValue(handler, "remoteFileTemplate.fileNameGenerator"); - String fileNameGeneratorExpression = (String) TestUtils.getPropertyValue(generator, "expression"); - assertEquals("payload.getName() + '-foo'", fileNameGeneratorExpression); + Expression fileNameGeneratorExpression = TestUtils.getPropertyValue(generator, "expression", Expression.class); + assertNotNull(fileNameGeneratorExpression); + assertEquals("payload.getName() + '-foo'", fileNameGeneratorExpression.getExpressionString()); assertEquals("UTF-8", TestUtils.getPropertyValue(handler, "remoteFileTemplate.charset")); assertNull(TestUtils.getPropertyValue(handler, "remoteFileTemplate.temporaryDirectoryExpressionProcessor"));