Added support for a 'filename-pattern' attribute on <file-source/>. It creates a RegexPatternFilenameFilter instance and sets it on the FileSource (INT-252).
This commit is contained in:
@@ -29,6 +29,7 @@
|
||||
<xsd:attribute name="message-creator" type="xsd:string" use="optional"/>
|
||||
<xsd:attribute name="file-filter" type="xsd:string"/>
|
||||
<xsd:attribute name="filename-filter" type="xsd:string"/>
|
||||
<xsd:attribute name="filename-pattern" type="xsd:string"/>
|
||||
</xsd:complexType>
|
||||
</xsd:element>
|
||||
|
||||
|
||||
@@ -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();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -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 {
|
||||
|
||||
@@ -24,6 +24,8 @@
|
||||
|
||||
<si:file-source id="fileSourceWithCustomFilenameFilter" filename-filter="customFilenameFilter" directory="${java.io.tmpdir}"/>
|
||||
|
||||
<si:file-source id="fileSourceWithRegexFilter" filename-pattern="fo+\.[tx]{3}" directory="${java.io.tmpdir}"/>
|
||||
|
||||
<bean id="customMessageCreator" class="org.springframework.integration.adapter.file.config.CustomMessageCreator"/>
|
||||
|
||||
<bean id="customFileFilter" class="org.springframework.integration.adapter.file.config.CustomFileFilter"/>
|
||||
|
||||
Reference in New Issue
Block a user