INT-664 Added a FileListFilterFactoryBean to create the filter based on the combined settings of a custom filter reference, a filename pattern, and the boolean preventDuplicates flag.
This commit is contained in:
@@ -0,0 +1,122 @@
|
||||
/*
|
||||
* 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 java.io.File;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
import org.springframework.beans.factory.FactoryBean;
|
||||
import org.springframework.integration.file.AbstractFileListFilter;
|
||||
import org.springframework.integration.file.AcceptOnceFileListFilter;
|
||||
import org.springframework.integration.file.CompositeFileListFilter;
|
||||
import org.springframework.integration.file.FileListFilter;
|
||||
import org.springframework.integration.file.PatternMatchingFileListFilter;
|
||||
|
||||
/**
|
||||
* @author Mark Fisher
|
||||
* @since 1.0.3
|
||||
*/
|
||||
public class FileListFilterFactoryBean implements FactoryBean {
|
||||
|
||||
private volatile FileListFilter fileListFilter;
|
||||
|
||||
private volatile FileListFilter filterReference;
|
||||
|
||||
private volatile Pattern filenamePattern;
|
||||
|
||||
private volatile Boolean preventDuplicates;
|
||||
|
||||
private final Object monitor = new Object();
|
||||
|
||||
|
||||
public void setFilterReference(FileListFilter filterReference) {
|
||||
this.filterReference = filterReference;
|
||||
}
|
||||
|
||||
public void setFilenamePattern(Pattern filenamePattern) {
|
||||
this.filenamePattern = filenamePattern;
|
||||
}
|
||||
|
||||
public void setPreventDuplicates(Boolean preventDuplicates) {
|
||||
this.preventDuplicates = preventDuplicates;
|
||||
}
|
||||
|
||||
public Object getObject() throws Exception {
|
||||
if (this.fileListFilter == null) {
|
||||
synchronized (this.monitor) {
|
||||
this.intializeFileListFilter();
|
||||
}
|
||||
}
|
||||
return this.fileListFilter;
|
||||
}
|
||||
|
||||
public Class<?> getObjectType() {
|
||||
return (this.fileListFilter != null)
|
||||
? this.fileListFilter.getClass() : FileListFilter.class;
|
||||
}
|
||||
|
||||
public boolean isSingleton() {
|
||||
return true;
|
||||
}
|
||||
|
||||
private void intializeFileListFilter() {
|
||||
if (this.fileListFilter != null) {
|
||||
return;
|
||||
}
|
||||
FileListFilter flf = null;
|
||||
if (this.filterReference != null && this.filenamePattern != null) {
|
||||
throw new IllegalArgumentException("The 'filter' reference and " +
|
||||
"'filename-pattern' attributes are mutually exclusive.");
|
||||
}
|
||||
if (this.filterReference != null) {
|
||||
if (Boolean.TRUE.equals(this.preventDuplicates)) {
|
||||
flf = this.createCompositeWithAcceptOnceFilter(this.filterReference);
|
||||
}
|
||||
else { // preventDuplicates is either FALSE or NULL
|
||||
flf = this.filterReference;
|
||||
}
|
||||
}
|
||||
else if (this.filenamePattern != null) {
|
||||
PatternMatchingFileListFilter patternFilter = new PatternMatchingFileListFilter(this.filenamePattern);
|
||||
if (Boolean.FALSE.equals(this.preventDuplicates)) {
|
||||
flf = patternFilter;
|
||||
}
|
||||
else { // preventDuplicates is either TRUE or NULL
|
||||
flf = this.createCompositeWithAcceptOnceFilter(patternFilter);
|
||||
}
|
||||
}
|
||||
else if (Boolean.FALSE.equals(this.preventDuplicates)) {
|
||||
flf = new AbstractFileListFilter() {
|
||||
@Override
|
||||
protected boolean accept(File file) {
|
||||
return true;
|
||||
}
|
||||
};
|
||||
}
|
||||
else { // preventDuplicates is either TRUE or NULL
|
||||
flf = new AcceptOnceFileListFilter();
|
||||
}
|
||||
this.fileListFilter = flf;
|
||||
}
|
||||
|
||||
private FileListFilter createCompositeWithAcceptOnceFilter(FileListFilter otherFilter) {
|
||||
CompositeFileListFilter compositeFilter = new CompositeFileListFilter();
|
||||
compositeFilter.addFilter(new AcceptOnceFileListFilter(), otherFilter);
|
||||
return compositeFilter;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,131 @@
|
||||
/*
|
||||
* 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.assertFalse;
|
||||
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.regex.Pattern;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import org.springframework.beans.DirectFieldAccessor;
|
||||
import org.springframework.integration.file.AbstractFileListFilter;
|
||||
import org.springframework.integration.file.AcceptOnceFileListFilter;
|
||||
import org.springframework.integration.file.CompositeFileListFilter;
|
||||
import org.springframework.integration.file.FileListFilter;
|
||||
import org.springframework.integration.file.PatternMatchingFileListFilter;
|
||||
|
||||
/**
|
||||
* @author Mark Fisher
|
||||
*/
|
||||
public class FileListFilterFactoryBeanTests {
|
||||
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void customFilterAndFilenamePatternAreMutuallyExclusive() throws Exception {
|
||||
FileListFilterFactoryBean factory = new FileListFilterFactoryBean();
|
||||
factory.setFilterReference(new TestFilter());
|
||||
factory.setFilenamePattern(Pattern.compile("foo"));
|
||||
factory.getObject();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void customFilterAndPreventDuplicatesNull() throws Exception {
|
||||
FileListFilterFactoryBean factory = new FileListFilterFactoryBean();
|
||||
TestFilter testFilter = new TestFilter();
|
||||
factory.setFilterReference(testFilter);
|
||||
FileListFilter result = (FileListFilter) factory.getObject();
|
||||
assertFalse(result instanceof CompositeFileListFilter);
|
||||
assertSame(testFilter, result);
|
||||
}
|
||||
|
||||
@Test
|
||||
@SuppressWarnings("unchecked")
|
||||
public void customFilterAndPreventDuplicatesTrue() throws Exception {
|
||||
FileListFilterFactoryBean factory = new FileListFilterFactoryBean();
|
||||
TestFilter testFilter = new TestFilter();
|
||||
factory.setFilterReference(testFilter);
|
||||
factory.setPreventDuplicates(Boolean.TRUE);
|
||||
FileListFilter result = (FileListFilter) factory.getObject();
|
||||
assertTrue(result instanceof CompositeFileListFilter);
|
||||
Collection filters = (Collection) new DirectFieldAccessor(result).getPropertyValue("fileFilters");
|
||||
assertTrue(filters.iterator().next() instanceof AcceptOnceFileListFilter);
|
||||
assertTrue(filters.contains(testFilter));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void customFilterAndPreventDuplicatesFalse() throws Exception {
|
||||
FileListFilterFactoryBean factory = new FileListFilterFactoryBean();
|
||||
TestFilter testFilter = new TestFilter();
|
||||
factory.setFilterReference(testFilter);
|
||||
factory.setPreventDuplicates(Boolean.FALSE);
|
||||
FileListFilter result = (FileListFilter) factory.getObject();
|
||||
assertFalse(result instanceof CompositeFileListFilter);
|
||||
assertSame(testFilter, result);
|
||||
}
|
||||
|
||||
@Test
|
||||
@SuppressWarnings("unchecked")
|
||||
public void filenamePatternAndPreventDuplicatesNull() throws Exception {
|
||||
FileListFilterFactoryBean factory = new FileListFilterFactoryBean();
|
||||
factory.setFilenamePattern(Pattern.compile("foo"));
|
||||
FileListFilter result = (FileListFilter) factory.getObject();
|
||||
assertTrue(result instanceof CompositeFileListFilter);
|
||||
Collection filters = (Collection) new DirectFieldAccessor(result).getPropertyValue("fileFilters");
|
||||
Iterator<FileListFilter> iterator = filters.iterator();
|
||||
assertTrue(iterator.next() instanceof AcceptOnceFileListFilter);
|
||||
assertTrue(iterator.next() instanceof PatternMatchingFileListFilter);
|
||||
}
|
||||
|
||||
@Test
|
||||
@SuppressWarnings("unchecked")
|
||||
public void filenamePatternAndPreventDuplicatesTrue() throws Exception {
|
||||
FileListFilterFactoryBean factory = new FileListFilterFactoryBean();
|
||||
factory.setFilenamePattern(Pattern.compile("foo"));
|
||||
factory.setPreventDuplicates(Boolean.TRUE);
|
||||
FileListFilter result = (FileListFilter) factory.getObject();
|
||||
assertTrue(result instanceof CompositeFileListFilter);
|
||||
Collection filters = (Collection) new DirectFieldAccessor(result).getPropertyValue("fileFilters");
|
||||
Iterator<FileListFilter> iterator = filters.iterator();
|
||||
assertTrue(iterator.next() instanceof AcceptOnceFileListFilter);
|
||||
assertTrue(iterator.next() instanceof PatternMatchingFileListFilter);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void filenamePatternAndPreventDuplicatesFalse() throws Exception {
|
||||
FileListFilterFactoryBean factory = new FileListFilterFactoryBean();
|
||||
factory.setFilenamePattern(Pattern.compile("foo"));
|
||||
factory.setPreventDuplicates(Boolean.FALSE);
|
||||
FileListFilter result = (FileListFilter) factory.getObject();
|
||||
assertFalse(result instanceof CompositeFileListFilter);
|
||||
assertTrue(result instanceof PatternMatchingFileListFilter);
|
||||
}
|
||||
|
||||
|
||||
private static class TestFilter extends AbstractFileListFilter {
|
||||
|
||||
@Override
|
||||
protected boolean accept(File file) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user