GH-152: Scanner & filters are mutually exclusive

Fix spring-projects/spring-integration-java-dsl#152

* Add Logic to the `FileInboundChannelAdapterSpec` do not populate
`FileListFilterFactoryBean` to the `FileReadingMessageSource`
when the `scanner` is provided.
Otherwise call the `FileReadingMessageSource` and rely on its logic to
refuse mutually exclusive `scanner` and `filter/locker` options
This commit is contained in:
Artem Bilan
2017-07-31 10:52:29 -04:00
parent 0f78d4b2ec
commit f00eafb4f8
2 changed files with 29 additions and 9 deletions

View File

@@ -57,6 +57,10 @@ public class FileInboundChannelAdapterSpec
private ExpressionFileListFilter<File> expressionFileListFilter;
private DirectoryScanner scanner;
private boolean filtersSet;
FileInboundChannelAdapterSpec() {
this.target = new FileReadingMessageSource();
}
@@ -66,11 +70,14 @@ public class FileInboundChannelAdapterSpec
@Override
protected void onInit() {
try {
setFilter(FileInboundChannelAdapterSpec.this.fileListFilterFactoryBean.getObject());
}
catch (Exception e) {
throw new BeanCreationException("The bean for the [" + this + "] can not be instantiated.", e);
if (FileInboundChannelAdapterSpec.this.scanner == null ||
FileInboundChannelAdapterSpec.this.filtersSet) {
try {
setFilter(FileInboundChannelAdapterSpec.this.fileListFilterFactoryBean.getObject());
}
catch (Exception e) {
throw new BeanCreationException("The bean for the [" + this + "] can not be instantiated.", e);
}
}
super.onInit();
}
@@ -96,6 +103,7 @@ public class FileInboundChannelAdapterSpec
* @see FileReadingMessageSource#setScanner(DirectoryScanner)
*/
public FileInboundChannelAdapterSpec scanner(DirectoryScanner scanner) {
this.scanner = scanner;
this.target.setScanner(scanner);
return _this();
}
@@ -123,6 +131,7 @@ public class FileInboundChannelAdapterSpec
*/
public FileInboundChannelAdapterSpec filter(FileListFilter<File> filter) {
this.fileListFilterFactoryBean.setFilter(filter);
this.filtersSet = true;
return _this();
}
@@ -158,6 +167,7 @@ public class FileInboundChannelAdapterSpec
*/
public FileInboundChannelAdapterSpec preventDuplicates(boolean preventDuplicates) {
this.fileListFilterFactoryBean.setPreventDuplicates(preventDuplicates);
this.filtersSet = true;
return _this();
}
@@ -169,6 +179,7 @@ public class FileInboundChannelAdapterSpec
*/
public FileInboundChannelAdapterSpec ignoreHidden(boolean ignoreHidden) {
this.fileListFilterFactoryBean.setIgnoreHidden(ignoreHidden);
this.filtersSet = true;
return _this();
}
@@ -181,6 +192,7 @@ public class FileInboundChannelAdapterSpec
*/
public FileInboundChannelAdapterSpec patternFilter(String pattern) {
this.fileListFilterFactoryBean.setFilenamePattern(pattern);
this.filtersSet = true;
return _this();
}
@@ -193,6 +205,7 @@ public class FileInboundChannelAdapterSpec
*/
public FileInboundChannelAdapterSpec regexFilter(String regex) {
this.fileListFilterFactoryBean.setFilenameRegex(regex);
this.filtersSet = true;
return _this();
}
@@ -208,6 +221,7 @@ public class FileInboundChannelAdapterSpec
"The 'locker' (" + this.locker + ") is already configured for the FileReadingMessageSource");
this.locker = locker;
this.target.setLocker(locker);
this.filtersSet = true;
return _this();
}

View File

@@ -47,6 +47,7 @@ import org.junit.runner.RunWith;
import org.springframework.aop.TargetSource;
import org.springframework.aop.framework.Advised;
import org.springframework.beans.DirectFieldAccessor;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.ListableBeanFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
@@ -67,6 +68,7 @@ import org.springframework.integration.dsl.Pollers;
import org.springframework.integration.dsl.StandardIntegrationFlow;
import org.springframework.integration.dsl.channel.MessageChannels;
import org.springframework.integration.expression.FunctionExpression;
import org.springframework.integration.file.DefaultDirectoryScanner;
import org.springframework.integration.file.DefaultFileNameGenerator;
import org.springframework.integration.file.FileHeaders;
import org.springframework.integration.file.FileReadingMessageSource;
@@ -403,13 +405,16 @@ public class FileTests {
@Bean
public IntegrationFlow fileSplitterFlow() {
public IntegrationFlow fileSplitterFlow(BeanFactory beanFactory) {
ExpressionFileListFilter<File> fileExpressionFileListFilter =
new ExpressionFileListFilter<>(new FunctionExpression<File>(f -> "foo.tmp".equals(f.getName())));
fileExpressionFileListFilter.setBeanFactory(beanFactory);
return IntegrationFlows
.from(Files.inboundAdapter(tmpDir.getRoot())
.filter(new ChainFileListFilter<File>()
.addFilter(new AcceptOnceFileListFilter<>())
.addFilter(new ExpressionFileListFilter<>(
new FunctionExpression<File>(f -> "foo.tmp".equals(f.getName()))))),
.addFilter(fileExpressionFileListFilter)),
e -> e.poller(p -> p.fixedDelay(100)))
.split(Files.splitter()
.markers()
@@ -440,7 +445,8 @@ public class FileTests {
void pollDirectories(File... directories) {
for (File directory : directories) {
StandardIntegrationFlow integrationFlow = IntegrationFlows
.from(Files.inboundAdapter(directory),
.from(Files.inboundAdapter(directory)
.scanner(new DefaultDirectoryScanner()),
e -> e.poller(p -> p.fixedDelay(1000))
.id(directory.getName() + ".adapter"))
.transform(Files.toStringTransformer(),