INT-1542: Add file-regex attribute to file:inbound-channel-adapter

This commit is contained in:
Iwein Fuld
2010-11-12 15:45:43 +01:00
parent 3ac6a6b72f
commit f77982a33e
5 changed files with 163 additions and 17 deletions

View File

@@ -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());

View File

@@ -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;
}

View File

@@ -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>

View File

@@ -0,0 +1,22 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/integration/file"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:integration="http://www.springframework.org/schema/integration"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/integration
http://www.springframework.org/schema/integration/spring-integration.xsd
http://www.springframework.org/schema/integration/file
http://www.springframework.org/schema/integration/file/spring-integration-file.xsd">
<beans:bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer" />
<inbound-channel-adapter id="adapterWithRegexPattern"
directory="file:${java.io.tmpdir}"
filename-regex="^.*\.txt$" auto-startup="false">
<integration:poller fixed-rate="1000"/>
</inbound-channel-adapter>
</beans:beans>

View File

@@ -0,0 +1,78 @@
/*
* Copyright 2002-2010 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.integration.file.config;
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;
import org.springframework.context.ApplicationContext;
import org.springframework.integration.channel.AbstractMessageChannel;
import org.springframework.integration.endpoint.AbstractEndpoint;
import org.springframework.integration.file.FileReadingMessageSource;
import org.springframework.integration.file.filters.*;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import java.io.File;
import java.util.Set;
import java.util.regex.Pattern;
import static org.hamcrest.Matchers.is;
import static org.junit.Assert.*;
/**
*
* @author Mark Fisher
* @author Iwein Fuld
*
* @see org.springframework.integration.file.config.FileInboundChannelAdapterWithPatternParserTests
*/
@ContextConfiguration
@RunWith(SpringJUnit4ClassRunner.class)
public class FileInboundChannelAdapterWithRegexPatternParserTests {
@Autowired(required = true)
private ApplicationContext context;
private DirectFieldAccessor accessor;
@Autowired(required = true)
public void setSource(FileReadingMessageSource source) {
this.accessor = new DirectFieldAccessor(source);
}
@Test
@SuppressWarnings("unchecked")
public void regexFilter() {
DirectFieldAccessor scannerAccessor = new DirectFieldAccessor(accessor.getPropertyValue("scanner"));
Object extractedFilter = scannerAccessor.getPropertyValue("filter");
assertThat(extractedFilter, is(CompositeFileListFilter.class));
Set<FileListFilter<?>> filters = (Set<FileListFilter<?>>) new DirectFieldAccessor(
extractedFilter).getPropertyValue("fileFilters");
Pattern pattern = null;
for (FileListFilter<?> filter : filters) {
if (filter instanceof PatternMatchingFileListFilter) {
pattern = (Pattern) new DirectFieldAccessor(filter).getPropertyValue("pattern");
}
}
assertNotNull("expected SimplePatternFileListFilterTest", pattern);
assertEquals("^.*\\.txt$", pattern.pattern());
}
}