diff --git a/org.springframework.integration.adapter/src/main/java/org/springframework/integration/adapter/config/spring-integration-adapters-1.0.xsd b/org.springframework.integration.adapter/src/main/java/org/springframework/integration/adapter/config/spring-integration-adapters-1.0.xsd index 27b8664acd..9dd0e2fb80 100644 --- a/org.springframework.integration.adapter/src/main/java/org/springframework/integration/adapter/config/spring-integration-adapters-1.0.xsd +++ b/org.springframework.integration.adapter/src/main/java/org/springframework/integration/adapter/config/spring-integration-adapters-1.0.xsd @@ -29,6 +29,7 @@ + diff --git a/org.springframework.integration.adapter/src/main/java/org/springframework/integration/adapter/file/RegexPatternFilenameFilter.java b/org.springframework.integration.adapter/src/main/java/org/springframework/integration/adapter/file/RegexPatternFilenameFilter.java index 48ba33dd58..dc4f4402bf 100644 --- a/org.springframework.integration.adapter/src/main/java/org/springframework/integration/adapter/file/RegexPatternFilenameFilter.java +++ b/org.springframework.integration.adapter/src/main/java/org/springframework/integration/adapter/file/RegexPatternFilenameFilter.java @@ -42,7 +42,7 @@ public class RegexPatternFilenameFilter implements FilenameFilter { if (this.pattern == null) { throw new ConfigurationException("no pattern available"); } - return this.pattern.matcher(name).matches(); + return (name != null) && this.pattern.matcher(name).matches(); } } diff --git a/org.springframework.integration.adapter/src/main/java/org/springframework/integration/adapter/file/config/FileSourceParser.java b/org.springframework.integration.adapter/src/main/java/org/springframework/integration/adapter/file/config/FileSourceParser.java index 1d5d77496a..d9aa2c5ad7 100644 --- a/org.springframework.integration.adapter/src/main/java/org/springframework/integration/adapter/file/config/FileSourceParser.java +++ b/org.springframework.integration.adapter/src/main/java/org/springframework/integration/adapter/file/config/FileSourceParser.java @@ -18,11 +18,13 @@ package org.springframework.integration.adapter.file.config; import java.net.MalformedURLException; import java.net.URL; +import java.util.regex.Pattern; import org.springframework.beans.factory.support.BeanDefinitionBuilder; import org.springframework.core.io.ResourceLoader; import org.springframework.integration.ConfigurationException; import org.springframework.integration.adapter.file.FileSource; +import org.springframework.integration.adapter.file.RegexPatternFilenameFilter; import org.springframework.util.StringUtils; import org.w3c.dom.Element; @@ -40,6 +42,8 @@ public class FileSourceParser extends AbstractDirectorySourceParser { public static final String FILENAME_FILTER_ATTRIBUTE = "filename-filter"; + public static final String FILENAME_PATTERN_ATTRIBUTE = "filename-pattern"; + public FileSourceParser() { super(false); @@ -53,10 +57,11 @@ public class FileSourceParser extends AbstractDirectorySourceParser { @Override protected boolean isEligibleAttribute(String attributeName) { - return !DIRECTORY_ATTRIBUTE.equals(attributeName) && - !FILE_FILTER_ATTRIBUTE.equals(attributeName) && - !FILENAME_FILTER_ATTRIBUTE.equals(attributeName) && - super.isEligibleAttribute(attributeName); + return !DIRECTORY_ATTRIBUTE.equals(attributeName) + && !FILE_FILTER_ATTRIBUTE.equals(attributeName) + && !FILENAME_FILTER_ATTRIBUTE.equals(attributeName) + && !FILENAME_PATTERN_ATTRIBUTE.equals(attributeName) + && super.isEligibleAttribute(attributeName); } @Override @@ -69,16 +74,19 @@ public class FileSourceParser extends AbstractDirectorySourceParser { beanDefinition.addConstructorArgValue(directoryLocation); String fileFilter = element.getAttribute(FILE_FILTER_ATTRIBUTE); String filenameFilter = element.getAttribute(FILENAME_FILTER_ATTRIBUTE); - if (StringUtils.hasText(fileFilter) && StringUtils.hasText(filenameFilter)) { - throw new ConfigurationException("FileSource does not support both '" + - FILE_FILTER_ATTRIBUTE + "' and '" + FILENAME_FILTER_ATTRIBUTE + "'."); - } - else if (StringUtils.hasText(fileFilter)) { + String filenamePattern = element.getAttribute(FILENAME_PATTERN_ATTRIBUTE); + this.verifyAtMostOneAttributeSpecified(fileFilter, filenameFilter, filenamePattern); + if (StringUtils.hasText(fileFilter)) { beanDefinition.addPropertyReference("fileFilter", fileFilter); } else if (StringUtils.hasText(filenameFilter)) { beanDefinition.addPropertyReference("filenameFilter", filenameFilter); } + else if (StringUtils.hasLength(filenamePattern)) { + RegexPatternFilenameFilter regexFilter = new RegexPatternFilenameFilter(); + regexFilter.setPattern(Pattern.compile(filenamePattern)); + beanDefinition.addPropertyValue("filenameFilter", regexFilter); + } super.postProcess(beanDefinition, element); } @@ -92,4 +100,18 @@ public class FileSourceParser extends AbstractDirectorySourceParser { } } + private void verifyAtMostOneAttributeSpecified(String ... attributes) { + boolean attributeSpecified = false; + for (String attribute : attributes) { + if (StringUtils.hasText(attribute)) { + if (attributeSpecified) { + throw new ConfigurationException("FileSource supports at most one of '" + + FILE_FILTER_ATTRIBUTE + "', '" + FILENAME_FILTER_ATTRIBUTE + + "', and '" + FILENAME_PATTERN_ATTRIBUTE + "'."); + } + attributeSpecified = true; + } + } + } + } diff --git a/org.springframework.integration.adapter/src/test/java/org/springframework/integration/adapter/file/config/FileSourceParserTests.java b/org.springframework.integration.adapter/src/test/java/org/springframework/integration/adapter/file/config/FileSourceParserTests.java index df44eae054..590f566859 100644 --- a/org.springframework.integration.adapter/src/test/java/org/springframework/integration/adapter/file/config/FileSourceParserTests.java +++ b/org.springframework.integration.adapter/src/test/java/org/springframework/integration/adapter/file/config/FileSourceParserTests.java @@ -16,14 +16,15 @@ package org.springframework.integration.adapter.file.config; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; + import java.io.File; import java.io.FileFilter; import java.io.FilenameFilter; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertTrue; -import static org.junit.Assert.fail; - import org.junit.Test; import org.springframework.beans.DirectFieldAccessor; @@ -35,6 +36,7 @@ import org.springframework.integration.ConfigurationException; import org.springframework.integration.adapter.file.ByteArrayFileMessageCreator; import org.springframework.integration.adapter.file.FileMessageCreator; import org.springframework.integration.adapter.file.FileSource; +import org.springframework.integration.adapter.file.RegexPatternFilenameFilter; import org.springframework.integration.adapter.file.TextFileMessageCreator; /** @@ -126,6 +128,16 @@ public class FileSourceParserTests { assertEquals(filter, accessor.getPropertyValue("filenameFilter")); } + @Test + public void testFileSourceWithRegexPatternFilenameFilter() { + ApplicationContext context = new ClassPathXmlApplicationContext("fileSourceParserTests.xml", this.getClass()); + FileSource fileSource = (FileSource) context.getBean("fileSourceWithRegexFilter"); + DirectFieldAccessor accessor = new DirectFieldAccessor(fileSource); + RegexPatternFilenameFilter filter = (RegexPatternFilenameFilter) accessor.getPropertyValue("filenameFilter"); + assertFalse(filter.accept(null, "foo.htm")); + assertTrue(filter.accept(null, "foo.txt")); + } + @Test(expected=ConfigurationException.class) public void testFileSourceWithFileFilterAndFilenameFilterNotAllowed() throws Throwable { try { diff --git a/org.springframework.integration.adapter/src/test/java/org/springframework/integration/adapter/file/config/fileSourceParserTests.xml b/org.springframework.integration.adapter/src/test/java/org/springframework/integration/adapter/file/config/fileSourceParserTests.xml index 2191daf6f4..6f9c1e9e12 100644 --- a/org.springframework.integration.adapter/src/test/java/org/springframework/integration/adapter/file/config/fileSourceParserTests.xml +++ b/org.springframework.integration.adapter/src/test/java/org/springframework/integration/adapter/file/config/fileSourceParserTests.xml @@ -24,6 +24,8 @@ + +