INT-1542: Add file-regex attribute to file:inbound-channel-adapter
This commit is contained in:
@@ -86,6 +86,7 @@ public class FileInboundChannelAdapterParser extends AbstractPollingInboundChann
|
||||
if (StringUtils.hasText(filter)) {
|
||||
factoryBeanBuilder.addPropertyReference("filterReference", filter);
|
||||
}
|
||||
|
||||
String filenamePattern = element.getAttribute("filename-pattern");
|
||||
if (StringUtils.hasText(filenamePattern)) {
|
||||
if (StringUtils.hasText(filter)) {
|
||||
@@ -94,6 +95,15 @@ public class FileInboundChannelAdapterParser extends AbstractPollingInboundChann
|
||||
}
|
||||
factoryBeanBuilder.addPropertyValue("filenamePattern", filenamePattern);
|
||||
}
|
||||
String filenameRegex = element.getAttribute("filename-regex");
|
||||
if (StringUtils.hasText(filenameRegex)) {
|
||||
if (StringUtils.hasText(filter)) {
|
||||
parserContext.getReaderContext().error(
|
||||
"At most one of 'filter' and 'filename-regex' may be provided.", element);
|
||||
}
|
||||
factoryBeanBuilder.addPropertyValue("filenameRegex", filenameRegex);
|
||||
}
|
||||
|
||||
IntegrationNamespaceUtils.setValueIfAttributeDefined(factoryBeanBuilder, element, "prevent-duplicates");
|
||||
return BeanDefinitionReaderUtils.registerWithGeneratedName(
|
||||
factoryBeanBuilder.getBeanDefinition(), parserContext.getRegistry());
|
||||
|
||||
@@ -17,14 +17,12 @@
|
||||
package org.springframework.integration.file.config;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
|
||||
import org.springframework.beans.factory.FactoryBean;
|
||||
import org.springframework.integration.file.filters.AcceptAllFileListFilter;
|
||||
import org.springframework.integration.file.filters.AcceptOnceFileListFilter;
|
||||
import org.springframework.integration.file.filters.CompositeFileListFilter;
|
||||
import org.springframework.integration.file.filters.FileListFilter;
|
||||
import org.springframework.integration.file.filters.SimplePatternFileListFilter;
|
||||
import org.springframework.integration.file.filters.*;
|
||||
|
||||
/**
|
||||
* @author Mark Fisher
|
||||
@@ -38,6 +36,8 @@ public class FileListFilterFactoryBean implements FactoryBean<FileListFilter<Fil
|
||||
|
||||
private volatile String filenamePattern;
|
||||
|
||||
private volatile String filenameRegex;
|
||||
|
||||
private volatile Boolean preventDuplicates;
|
||||
|
||||
private final Object monitor = new Object();
|
||||
@@ -57,6 +57,10 @@ public class FileListFilterFactoryBean implements FactoryBean<FileListFilter<Fil
|
||||
this.filenamePattern = filenamePattern;
|
||||
}
|
||||
|
||||
public void setFilenameRegex(String filenameRegex) {
|
||||
this.filenameRegex = filenameRegex;
|
||||
}
|
||||
|
||||
public void setPreventDuplicates(Boolean preventDuplicates) {
|
||||
this.preventDuplicates = preventDuplicates;
|
||||
}
|
||||
@@ -83,11 +87,12 @@ public class FileListFilterFactoryBean implements FactoryBean<FileListFilter<Fil
|
||||
return;
|
||||
}
|
||||
FileListFilter<File> filter;
|
||||
if ((this.filterReference != null) && (this.filenamePattern != null)) {
|
||||
throw new IllegalArgumentException("The 'filter' reference and "
|
||||
+ "'filename-pattern' attributes are mutually exclusive.");
|
||||
if ((this.filterReference != null) && (this.filenamePattern != null || this.filenameRegex!=null)) {
|
||||
throw new IllegalArgumentException("The 'filter' reference is mutually exclusive with "
|
||||
+ "'filename-pattern' and 'filename-regex' attributes.");
|
||||
}
|
||||
|
||||
//'filter' is set
|
||||
if (this.filterReference != null) {
|
||||
if (Boolean.TRUE.equals(this.preventDuplicates)) {
|
||||
filter = this.createCompositeWithAcceptOnceFilter(this.filterReference);
|
||||
@@ -96,15 +101,29 @@ public class FileListFilterFactoryBean implements FactoryBean<FileListFilter<Fil
|
||||
filter = this.filterReference;
|
||||
}
|
||||
}
|
||||
else if (this.filenamePattern != null) {
|
||||
SimplePatternFileListFilter patternFilter = new SimplePatternFileListFilter(this.filenamePattern);
|
||||
if (Boolean.FALSE.equals(this.preventDuplicates)) {
|
||||
filter = patternFilter;
|
||||
|
||||
// 'file-pattern' or 'file-regex' is set
|
||||
else if (this.filenamePattern != null || this.filenameRegex!=null) {
|
||||
List<FileListFilter<File>> filtersNeeded = new ArrayList<FileListFilter<File>>();
|
||||
if (!Boolean.FALSE.equals(this.preventDuplicates)) {
|
||||
//preventDuplicates is either null or true
|
||||
filtersNeeded.add(new AcceptOnceFileListFilter<File>());
|
||||
}
|
||||
else { // preventDuplicates is either TRUE or NULL
|
||||
filter = this.createCompositeWithAcceptOnceFilter(patternFilter);
|
||||
if (this.filenamePattern!=null){
|
||||
filtersNeeded.add(new SimplePatternFileListFilter(this.filenamePattern));
|
||||
}
|
||||
if (this.filenameRegex!=null){
|
||||
filtersNeeded.add(new PatternMatchingFileListFilter(this.filenameRegex));
|
||||
}
|
||||
if (filtersNeeded.size()==1){
|
||||
filter = filtersNeeded.get(0);
|
||||
}
|
||||
else {
|
||||
filter = new CompositeFileListFilter<File>(filtersNeeded);
|
||||
}
|
||||
}
|
||||
|
||||
// no filters are provided
|
||||
else if (Boolean.FALSE.equals(this.preventDuplicates)) {
|
||||
filter = new AcceptAllFileListFilter<File>();
|
||||
}
|
||||
@@ -126,10 +145,12 @@ public class FileListFilterFactoryBean implements FactoryBean<FileListFilter<Fil
|
||||
this.fileListFilter = filter;
|
||||
}
|
||||
|
||||
private CompositeFileListFilter<File> createCompositeWithAcceptOnceFilter(FileListFilter<File> otherFilter) {
|
||||
private CompositeFileListFilter<File> createCompositeWithAcceptOnceFilter(FileListFilter<File>... otherFilters) {
|
||||
CompositeFileListFilter<File> compositeFilter = new CompositeFileListFilter<File>();
|
||||
compositeFilter.addFilter(new AcceptOnceFileListFilter<File>());
|
||||
compositeFilter.addFilter(otherFilter);
|
||||
for (FileListFilter<File> otherFilter : otherFilters) {
|
||||
compositeFilter.addFilter(otherFilter);
|
||||
}
|
||||
return compositeFilter;
|
||||
}
|
||||
|
||||
|
||||
@@ -61,7 +61,22 @@
|
||||
</xsd:appinfo>
|
||||
</xsd:annotation>
|
||||
</xsd:attribute>
|
||||
<xsd:attribute name="filename-pattern" type="xsd:string"/>
|
||||
<xsd:attribute name="filename-pattern" type="xsd:string">
|
||||
<xsd:annotation>
|
||||
<xsd:documentation><![CDATA[
|
||||
Only files matching this ant style path will be picked up by this adapter. Note that
|
||||
in Spring Integration 1.0 this attribute accepted a regular expression, but from 2.0
|
||||
filename-regex should be used for that purpose instead.
|
||||
]]></xsd:documentation>
|
||||
</xsd:annotation>
|
||||
</xsd:attribute>
|
||||
<xsd:attribute name="filename-regex" type="xsd:string">
|
||||
<xsd:annotation>
|
||||
<xsd:documentation><![CDATA[
|
||||
Only files matching this regular expression will be picked up by this adapter.
|
||||
]]></xsd:documentation>
|
||||
</xsd:annotation>
|
||||
</xsd:attribute>
|
||||
<xsd:attribute name="scanner" type="xsd:string">
|
||||
<xsd:annotation>
|
||||
<xsd:appinfo>
|
||||
|
||||
Reference in New Issue
Block a user