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

View File

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