INT-664 Added a 'prevent-duplicates' flag to the file namespace's inbound-channel-adapter element.
This commit is contained in:
@@ -19,10 +19,8 @@ package org.springframework.integration.file.config;
|
||||
import org.w3c.dom.Element;
|
||||
|
||||
import org.springframework.beans.factory.config.BeanDefinition;
|
||||
import org.springframework.beans.factory.config.RuntimeBeanReference;
|
||||
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
|
||||
import org.springframework.beans.factory.support.BeanDefinitionReaderUtils;
|
||||
import org.springframework.beans.factory.support.ManagedList;
|
||||
import org.springframework.beans.factory.xml.ParserContext;
|
||||
import org.springframework.integration.config.xml.AbstractPollingInboundChannelAdapterParser;
|
||||
import org.springframework.util.StringUtils;
|
||||
@@ -39,7 +37,6 @@ public class FileInboundChannelAdapterParser extends AbstractPollingInboundChann
|
||||
|
||||
|
||||
@Override
|
||||
@SuppressWarnings("unchecked")
|
||||
protected String parseSource(Element element, ParserContext parserContext) {
|
||||
BeanDefinitionBuilder builder = BeanDefinitionBuilder.genericBeanDefinition(
|
||||
PACKAGE_NAME + ".FileReadingMessageSource");
|
||||
@@ -54,9 +51,18 @@ public class FileInboundChannelAdapterParser extends AbstractPollingInboundChann
|
||||
}
|
||||
builder.addPropertyValue("inputDirectory", directory);
|
||||
}
|
||||
String filterBeanName = this.registerFileListFilter(element, parserContext);
|
||||
builder.addPropertyReference("filter", filterBeanName);
|
||||
return BeanDefinitionReaderUtils.registerWithGeneratedName(builder.getBeanDefinition(), parserContext.getRegistry());
|
||||
}
|
||||
|
||||
private String registerFileListFilter(Element element, ParserContext parserContext) {
|
||||
BeanDefinitionBuilder factoryBeanBuilder = BeanDefinitionBuilder.genericBeanDefinition(
|
||||
PACKAGE_NAME + ".config.FileListFilterFactoryBean");
|
||||
factoryBeanBuilder.setRole(BeanDefinition.ROLE_SUPPORT);
|
||||
String filter = element.getAttribute("filter");
|
||||
if (StringUtils.hasText(filter)){
|
||||
builder.addPropertyReference("filter", filter);
|
||||
if (StringUtils.hasText(filter)) {
|
||||
factoryBeanBuilder.addPropertyReference("filterReference", filter);
|
||||
}
|
||||
String filenamePattern = element.getAttribute("filename-pattern");
|
||||
if (StringUtils.hasText(filenamePattern)) {
|
||||
@@ -64,26 +70,14 @@ public class FileInboundChannelAdapterParser extends AbstractPollingInboundChann
|
||||
parserContext.getReaderContext().error(
|
||||
"At most one of 'filter' and 'filename-pattern' may be provided.", element);
|
||||
}
|
||||
String acceptOnceFilterBeanName = this.parseFilter("AcceptOnceFileListFilter", null, parserContext);
|
||||
String patternFilterBeanName = this.parseFilter("PatternMatchingFileListFilter", filenamePattern, parserContext);
|
||||
ManagedList filters = new ManagedList();
|
||||
filters.add(new RuntimeBeanReference(acceptOnceFilterBeanName));
|
||||
filters.add(new RuntimeBeanReference(patternFilterBeanName));
|
||||
String compositeFilterBeanName = this.parseFilter("CompositeFileListFilter", filters, parserContext);
|
||||
builder.addPropertyReference("filter", compositeFilterBeanName);
|
||||
factoryBeanBuilder.addPropertyValue("filenamePattern", filenamePattern);
|
||||
}
|
||||
return BeanDefinitionReaderUtils.registerWithGeneratedName(builder.getBeanDefinition(), parserContext.getRegistry());
|
||||
}
|
||||
|
||||
private String parseFilter(String shortClassName, Object constructorArgValue, ParserContext parserContext) {
|
||||
BeanDefinitionBuilder filterBuilder = BeanDefinitionBuilder.genericBeanDefinition(
|
||||
PACKAGE_NAME + "." + shortClassName);
|
||||
filterBuilder.getBeanDefinition().setRole(BeanDefinition.ROLE_SUPPORT);
|
||||
if (constructorArgValue != null) {
|
||||
filterBuilder.addConstructorArgValue(constructorArgValue);
|
||||
String preventDuplicates = element.getAttribute("prevent-duplicates");
|
||||
if (StringUtils.hasText(preventDuplicates)) {
|
||||
factoryBeanBuilder.addPropertyValue("preventDuplicates", preventDuplicates);
|
||||
}
|
||||
return BeanDefinitionReaderUtils.registerWithGeneratedName(
|
||||
filterBuilder.getBeanDefinition(), parserContext.getRegistry());
|
||||
factoryBeanBuilder.getBeanDefinition(), parserContext.getRegistry());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -58,6 +58,18 @@
|
||||
</xsd:annotation>
|
||||
</xsd:attribute>
|
||||
<xsd:attribute name="filename-pattern" type="xsd:string"/>
|
||||
<xsd:attribute name="prevent-duplicates" type="xsd:string">
|
||||
<xsd:annotation>
|
||||
<xsd:documentation><![CDATA[
|
||||
A boolean flag indicating whether duplicates should be prevented. If a 'filter' reference is
|
||||
provided, duplicate prevention will not be enabled by default (the assumption is that the
|
||||
provided filter is sufficient), but setting this to true will enable it. If a 'filename-pattern'
|
||||
is provided, duplicate prevention will be enabled by default (preceding the pattern matching),
|
||||
but setting this to false will disable it. If neither 'filter' or 'filename-pattern' is provided,
|
||||
duplicate prevention is enabled by default, but setting this to false will disable it.
|
||||
]]></xsd:documentation>
|
||||
</xsd:annotation>
|
||||
</xsd:attribute>
|
||||
<xsd:attribute name="auto-startup" type="xsd:string" default="true"/>
|
||||
</xsd:complexType>
|
||||
</xsd:element>
|
||||
|
||||
@@ -0,0 +1,114 @@
|
||||
<?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-2.5.xsd
|
||||
http://www.springframework.org/schema/integration
|
||||
http://www.springframework.org/schema/integration/spring-integration-1.0.xsd
|
||||
http://www.springframework.org/schema/integration/file
|
||||
http://www.springframework.org/schema/integration/file/spring-integration-file-1.0.xsd">
|
||||
|
||||
<beans:bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer" />
|
||||
|
||||
<integration:channel id="channel">
|
||||
<integration:queue/>
|
||||
</integration:channel>
|
||||
|
||||
<inbound-channel-adapter id="filterAndNull"
|
||||
directory="file:${java.io.tmpdir}"
|
||||
filter="testFilter"
|
||||
channel="channel"
|
||||
auto-startup="false">
|
||||
<integration:poller>
|
||||
<integration:interval-trigger interval="10000"/>
|
||||
</integration:poller>
|
||||
</inbound-channel-adapter>
|
||||
|
||||
<inbound-channel-adapter id="filterAndTrue"
|
||||
directory="file:${java.io.tmpdir}"
|
||||
filter="testFilter"
|
||||
prevent-duplicates="true"
|
||||
channel="channel"
|
||||
auto-startup="false">
|
||||
<integration:poller>
|
||||
<integration:interval-trigger interval="10000"/>
|
||||
</integration:poller>
|
||||
</inbound-channel-adapter>
|
||||
|
||||
<inbound-channel-adapter id="filterAndFalse"
|
||||
directory="file:${java.io.tmpdir}"
|
||||
filter="testFilter"
|
||||
prevent-duplicates="false"
|
||||
channel="channel"
|
||||
auto-startup="false">
|
||||
<integration:poller>
|
||||
<integration:interval-trigger interval="10000"/>
|
||||
</integration:poller>
|
||||
</inbound-channel-adapter>
|
||||
|
||||
<inbound-channel-adapter id="patternAndNull"
|
||||
directory="file:${java.io.tmpdir}"
|
||||
filename-pattern="test"
|
||||
channel="channel"
|
||||
auto-startup="false">
|
||||
<integration:poller>
|
||||
<integration:interval-trigger interval="10000"/>
|
||||
</integration:poller>
|
||||
</inbound-channel-adapter>
|
||||
|
||||
<inbound-channel-adapter id="patternAndTrue"
|
||||
directory="file:${java.io.tmpdir}"
|
||||
filename-pattern="test"
|
||||
prevent-duplicates="true"
|
||||
channel="channel"
|
||||
auto-startup="false">
|
||||
<integration:poller>
|
||||
<integration:interval-trigger interval="10000"/>
|
||||
</integration:poller>
|
||||
</inbound-channel-adapter>
|
||||
|
||||
<inbound-channel-adapter id="patternAndFalse"
|
||||
directory="file:${java.io.tmpdir}"
|
||||
filename-pattern="test"
|
||||
prevent-duplicates="false"
|
||||
channel="channel"
|
||||
auto-startup="false">
|
||||
<integration:poller>
|
||||
<integration:interval-trigger interval="10000"/>
|
||||
</integration:poller>
|
||||
</inbound-channel-adapter>
|
||||
|
||||
<inbound-channel-adapter id="defaultAndNull"
|
||||
directory="file:${java.io.tmpdir}"
|
||||
channel="channel"
|
||||
auto-startup="false">
|
||||
<integration:poller>
|
||||
<integration:interval-trigger interval="10000"/>
|
||||
</integration:poller>
|
||||
</inbound-channel-adapter>
|
||||
|
||||
<inbound-channel-adapter id="defaultAndTrue"
|
||||
directory="file:${java.io.tmpdir}"
|
||||
prevent-duplicates="true"
|
||||
channel="channel"
|
||||
auto-startup="false">
|
||||
<integration:poller>
|
||||
<integration:interval-trigger interval="10000"/>
|
||||
</integration:poller>
|
||||
</inbound-channel-adapter>
|
||||
|
||||
<inbound-channel-adapter id="defaultAndFalse"
|
||||
directory="file:${java.io.tmpdir}"
|
||||
prevent-duplicates="false"
|
||||
channel="channel"
|
||||
auto-startup="false">
|
||||
<integration:poller>
|
||||
<integration:interval-trigger interval="10000"/>
|
||||
</integration:poller>
|
||||
</inbound-channel-adapter>
|
||||
|
||||
<beans:bean id="testFilter" class="org.springframework.integration.file.TestFileListFilter"/>
|
||||
|
||||
</beans:beans>
|
||||
@@ -0,0 +1,155 @@
|
||||
/*
|
||||
* Copyright 2002-2009 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 static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import static org.junit.Assert.assertSame;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.Collection;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
|
||||
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.file.AcceptOnceFileListFilter;
|
||||
import org.springframework.integration.file.CompositeFileListFilter;
|
||||
import org.springframework.integration.file.FileListFilter;
|
||||
import org.springframework.integration.file.PatternMatchingFileListFilter;
|
||||
import org.springframework.integration.file.TestFileListFilter;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
|
||||
/**
|
||||
* @author Mark Fisher
|
||||
*/
|
||||
@ContextConfiguration
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
public class FileInboundChannelAdapterWithPreventDuplicatesFlagTests {
|
||||
|
||||
@Autowired
|
||||
private ApplicationContext context;
|
||||
|
||||
@Autowired
|
||||
@Qualifier("testFilter")
|
||||
private TestFileListFilter testFilter;
|
||||
|
||||
|
||||
@Test
|
||||
public void filterAndNull() {
|
||||
FileListFilter filter = this.extractFilter("filterAndNull");
|
||||
assertFalse(filter instanceof CompositeFileListFilter);
|
||||
assertSame(testFilter, filter);
|
||||
}
|
||||
|
||||
@Test
|
||||
@SuppressWarnings("unchecked")
|
||||
public void filterAndTrue() {
|
||||
FileListFilter filter = this.extractFilter("filterAndTrue");
|
||||
assertTrue(filter instanceof CompositeFileListFilter);
|
||||
Collection filters = (Collection) new DirectFieldAccessor(filter).getPropertyValue("fileFilters");
|
||||
assertTrue(filters.iterator().next() instanceof AcceptOnceFileListFilter);
|
||||
assertTrue(filters.contains(testFilter));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void filterAndFalse() throws Exception {
|
||||
FileListFilter filter = this.extractFilter("filterAndFalse");
|
||||
assertFalse(filter instanceof CompositeFileListFilter);
|
||||
assertSame(testFilter, filter);
|
||||
}
|
||||
|
||||
@Test
|
||||
@SuppressWarnings("unchecked")
|
||||
public void patternAndNull() throws Exception {
|
||||
FileListFilter filter = this.extractFilter("patternAndNull");
|
||||
assertTrue(filter instanceof CompositeFileListFilter);
|
||||
Collection filters = (Collection) new DirectFieldAccessor(filter).getPropertyValue("fileFilters");
|
||||
Iterator<FileListFilter> iterator = filters.iterator();
|
||||
assertTrue(iterator.next() instanceof AcceptOnceFileListFilter);
|
||||
assertTrue(iterator.next() instanceof PatternMatchingFileListFilter);
|
||||
}
|
||||
|
||||
@Test
|
||||
@SuppressWarnings("unchecked")
|
||||
public void patternAndTrue() throws Exception {
|
||||
FileListFilter filter = this.extractFilter("patternAndTrue");
|
||||
assertTrue(filter instanceof CompositeFileListFilter);
|
||||
Collection filters = (Collection) new DirectFieldAccessor(filter).getPropertyValue("fileFilters");
|
||||
Iterator<FileListFilter> iterator = filters.iterator();
|
||||
assertTrue(iterator.next() instanceof AcceptOnceFileListFilter);
|
||||
assertTrue(iterator.next() instanceof PatternMatchingFileListFilter);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void patternAndFalse() throws Exception {
|
||||
FileListFilter filter = this.extractFilter("patternAndFalse");
|
||||
assertFalse(filter instanceof CompositeFileListFilter);
|
||||
assertTrue(filter instanceof PatternMatchingFileListFilter);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void defaultAndNull() throws Exception {
|
||||
FileListFilter filter = this.extractFilter("defaultAndNull");
|
||||
assertNotNull(filter);
|
||||
assertFalse(filter instanceof CompositeFileListFilter);
|
||||
assertTrue(filter instanceof AcceptOnceFileListFilter);
|
||||
File testFile = new File("test");
|
||||
File[] files = new File[] { testFile, testFile, testFile };
|
||||
List<File> result = filter.filterFiles(files);
|
||||
assertEquals(1, result.size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void defaultAndTrue() throws Exception {
|
||||
FileListFilter filter = this.extractFilter("defaultAndTrue");
|
||||
assertFalse(filter instanceof CompositeFileListFilter);
|
||||
assertTrue(filter instanceof AcceptOnceFileListFilter);
|
||||
File testFile = new File("test");
|
||||
File[] files = new File[] { testFile, testFile, testFile };
|
||||
List<File> result = filter.filterFiles(files);
|
||||
assertEquals(1, result.size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void defaultAndFalse() throws Exception {
|
||||
FileListFilter filter = this.extractFilter("defaultAndFalse");
|
||||
assertNotNull(filter);
|
||||
assertFalse(filter instanceof CompositeFileListFilter);
|
||||
assertFalse(filter instanceof AcceptOnceFileListFilter);
|
||||
File testFile = new File("test");
|
||||
File[] files = new File[] { testFile, testFile, testFile };
|
||||
List<File> result = filter.filterFiles(files);
|
||||
assertEquals(3, result.size());
|
||||
}
|
||||
|
||||
|
||||
private FileListFilter extractFilter(String beanName) {
|
||||
return (FileListFilter) new DirectFieldAccessor(
|
||||
new DirectFieldAccessor(context.getBean(beanName)).getPropertyValue("source"))
|
||||
.getPropertyValue("filter");
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user