INT-3827: ResettableFileListFilter

JIRA: https://jira.spring.io/browse/INT-3827

Provide a hook to enable removing a file from an `AcceptOnceFileListFilter`,
for example after a message processing failure.

Make the `CompositeFileListFilter` a `ReversibleFileListFilter` so it can
delegate to any of its composed filters that are reversible.

INT-3827: Polishing - PR Comments
(cherry picked from commit 0d721739e9)

Conflicts:
	src/reference/asciidoc/ftp.adoc
	src/reference/asciidoc/sftp.adoc
This commit is contained in:
Artem Bilan
2015-09-29 14:03:42 -04:00
parent 10ae8a7aff
commit 6e838b50ab
9 changed files with 192 additions and 11 deletions

View File

@@ -35,7 +35,7 @@ import org.springframework.util.Assert;
*
*/
public abstract class AbstractPersistentAcceptOnceFileListFilter<F> extends AbstractFileListFilter<F>
implements ReversibleFileListFilter<F>, Closeable {
implements ReversibleFileListFilter<F>, ResettableFileListFilter<F>, Closeable {
protected final ConcurrentMetadataStore store;
@@ -102,12 +102,18 @@ public abstract class AbstractPersistentAcceptOnceFileListFilter<F> extends Abst
rollingBack = true;
}
if (rollingBack) {
this.store.remove(buildKey(fileToRollback));
flushIfNeeded();
remove(fileToRollback);
}
}
}
@Override
public boolean remove(F fileToRemove) {
String removed = this.store.remove(buildKey(fileToRemove));
flushIfNeeded();
return removed != null;
}
@Override
public void close() throws IOException {
if (this.store instanceof Closeable) {

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2014 the original author or authors.
* Copyright 2002-2015 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.
@@ -34,7 +34,8 @@ import java.util.concurrent.LinkedBlockingQueue;
* @author Gary Russell
* @since 1.0.0
*/
public class AcceptOnceFileListFilter<F> extends AbstractFileListFilter<F> implements ReversibleFileListFilter<F> {
public class AcceptOnceFileListFilter<F> extends AbstractFileListFilter<F> implements ReversibleFileListFilter<F>,
ResettableFileListFilter<F> {
private final Queue<F> seen;
@@ -93,13 +94,19 @@ public class AcceptOnceFileListFilter<F> extends AbstractFileListFilter<F> imple
rollingBack = true;
}
if (rollingBack) {
this.seenSet.remove(fileToRollback);
if (this.seen != null) {
this.seen.remove(fileToRollback);
}
remove(fileToRollback);
}
}
}
}
@Override
public boolean remove(F fileToRemove) {
boolean removed = this.seenSet.remove(fileToRemove);
if (this.seen != null) {
this.seen.remove(fileToRemove);
}
return removed;
}
}

View File

@@ -39,7 +39,7 @@ import org.springframework.util.Assert;
*
* @param <F> The type that will be filtered.
*/
public class CompositeFileListFilter<F> implements FileListFilter<F>, Closeable {
public class CompositeFileListFilter<F> implements ReversibleFileListFilter<F>, Closeable {
private final Set<FileListFilter<F>> fileFilters;
@@ -111,4 +111,13 @@ public class CompositeFileListFilter<F> implements FileListFilter<F>, Closeable
return results;
}
@Override
public void rollback(F file, List<F> files) {
for (FileListFilter<F> fileFilter : this.fileFilters) {
if (fileFilter instanceof ReversibleFileListFilter) {
((ReversibleFileListFilter<F>) fileFilter).rollback(file, files);
}
}
}
}

View File

@@ -0,0 +1,34 @@
/*
* Copyright 2015 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.filters;
/**
* A {@link FileListFilter} that can be reset by removing a specific file from its
* state.
* @author Gary Russell
* @since 4.1.7
*
*/
public interface ResettableFileListFilter<F> extends FileListFilter<F> {
/**
* Remove the specified file from the filter so it will pass on the next attempt.
* @param f the element to remove.
* @return true if the file was removed as a result of this call.
*/
boolean remove(F f);
}

View File

@@ -24,6 +24,7 @@ import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Queue;
import java.util.Set;
@@ -76,6 +77,14 @@ public class AcceptOnceFileListFilterTests {
doTestRollback(filter);
}
@Test
public void testRollbackComposite() {
AcceptOnceFileListFilter<String> filter = new AcceptOnceFileListFilter<String>();
CompositeFileListFilter<String> composite = new CompositeFileListFilter<String>(
Collections.singletonList(filter));
doTestRollback(composite);
}
protected void doTestRollback(ReversibleFileListFilter<String> filter) {
String[] files = new String[] {"foo", "bar", "baz"};
List<String> passed = filter.filterFiles(files);