From 2bc8b31255925a38930b6104ff578048b56dae72 Mon Sep 17 00:00:00 2001 From: Iwein Fuld Date: Fri, 15 Oct 2010 10:35:13 +0200 Subject: [PATCH] Restored filter implementations for backwards compatibility reasons. - Restore FileListFilter implementations - Have FileListFilter implementations extend their EntryListFilter counterpart. - Deprecate AbstractFileListFilter in favor of EntryListFilter hierarchy --- .../file/filters/AbstractFileListFilter.java | 56 +++++++++++++++++ .../filters/AcceptOnceFileListFilter.java | 61 +++++++++++++++++++ .../file/filters/CompositeFileListFilter.java | 56 +++++++++++++++++ .../PatternMatchingFileListFilter.java | 57 +++++++++++++++++ ...nelAdapterWithRecursiveDirectoryTests.java | 6 +- 5 files changed, 233 insertions(+), 3 deletions(-) create mode 100644 spring-integration-file/src/main/java/org/springframework/integration/file/filters/AbstractFileListFilter.java create mode 100644 spring-integration-file/src/main/java/org/springframework/integration/file/filters/AcceptOnceFileListFilter.java create mode 100644 spring-integration-file/src/main/java/org/springframework/integration/file/filters/CompositeFileListFilter.java create mode 100644 spring-integration-file/src/main/java/org/springframework/integration/file/filters/PatternMatchingFileListFilter.java diff --git a/spring-integration-file/src/main/java/org/springframework/integration/file/filters/AbstractFileListFilter.java b/spring-integration-file/src/main/java/org/springframework/integration/file/filters/AbstractFileListFilter.java new file mode 100644 index 0000000000..484876148f --- /dev/null +++ b/spring-integration-file/src/main/java/org/springframework/integration/file/filters/AbstractFileListFilter.java @@ -0,0 +1,56 @@ +/* + * Copyright 2002-2008 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; + +import java.io.File; +import java.util.ArrayList; +import java.util.List; + +/** + * A convenience base class for any {@link FileListFilter} whose criteria can be + * evaluated against each File in isolation. If the entire List of files is + * required for evaluation, implement the FileListFilter interface directly. + * + * @author Mark Fisher + * @author Iwein Fuld + * + * @deprecated Replaced by AbstractEntryListFilter in 2.0.0 + */ +@Deprecated +public abstract class AbstractFileListFilter implements FileListFilter { + + /** + * {@inheritDoc} + */ + public final List filterFiles(File[] files) { + List accepted = new ArrayList(); + if (files != null) { + for (File file : files) { + if (this.accept(file)) { + accepted.add(file); + } + } + } + return accepted; + } + + /** + * Subclasses must implement this method. + */ + protected abstract boolean accept(File file); + +} diff --git a/spring-integration-file/src/main/java/org/springframework/integration/file/filters/AcceptOnceFileListFilter.java b/spring-integration-file/src/main/java/org/springframework/integration/file/filters/AcceptOnceFileListFilter.java new file mode 100644 index 0000000000..43522376c6 --- /dev/null +++ b/spring-integration-file/src/main/java/org/springframework/integration/file/filters/AcceptOnceFileListFilter.java @@ -0,0 +1,61 @@ +/* + * Copyright 2002-2010 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; + +import org.springframework.integration.file.entries.AcceptOnceEntryFileListFilter; + +import java.io.File; +import java.util.List; + + +/** + * {@link FileListFilter} that passes files only one time. This can + * conveniently be used to prevent duplication of files, as is done in + * {@link org.springframework.integration.file.FileReadingMessageSource}. + *

+ * This implementation is thread safe. + * + * @author Iwein Fuld + * @since 1.0.0 + */ +public class AcceptOnceFileListFilter extends AcceptOnceEntryFileListFilter implements FileListFilter{ + + /** + * Creates an AcceptOnceFileFilter that is based on a bounded queue. If the + * queue overflows, files that fall out will be passed through this filter + * again if passed to the {@link #filterFiles(File[])} method. + * + * @param maxCapacity the maximum number of Files to maintain in the 'seen' + * queue. + */ + public AcceptOnceFileListFilter(int maxCapacity) { + super(maxCapacity); + } + + /** + * Creates an AcceptOnceFileFilter based on an unbounded queue. + */ + public AcceptOnceFileListFilter() { + super(); + } + + /** + * Filter out all the files that this instance has seen before. + */ + public List filterFiles(File[] files) { + return this.filterEntries(files); + } +} diff --git a/spring-integration-file/src/main/java/org/springframework/integration/file/filters/CompositeFileListFilter.java b/spring-integration-file/src/main/java/org/springframework/integration/file/filters/CompositeFileListFilter.java new file mode 100644 index 0000000000..a28a9a8707 --- /dev/null +++ b/spring-integration-file/src/main/java/org/springframework/integration/file/filters/CompositeFileListFilter.java @@ -0,0 +1,56 @@ +/* + * 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.filters; + +import org.springframework.integration.file.entries.CompositeEntryListFilter; +import org.springframework.integration.file.entries.EntryListFilter; +import org.springframework.util.Assert; + +import java.io.File; +import java.io.FileFilter; +import java.util.*; + + +/** + * Composition that delegates to multiple {@link FileFilter}s. The composition is AND based, meaning that a file must + * pass through each filter's {@link #filterFiles(java.io.File[])} method in order to be accepted by the composite. + * + * @author Iwein Fuld + * @author Mark Fisher + */ +public class CompositeFileListFilter extends CompositeEntryListFilter implements FileListFilter{ + + public CompositeFileListFilter(EntryListFilter... fileFilters) { + this(Arrays.asList(fileFilters)); + } + + public CompositeFileListFilter(Collection> fileFilters) { + super(fileFilters); + } + + /** + * {@inheritDoc} + *

+ * This implementation delegates to a collection of filters and returns only files that pass all the filters. + * @deprecated use {@link #filterEntries} instead + */ + @Deprecated + public List filterFiles(File[] files) { + Assert.notNull(files, "'files' should not be null"); + + return this.filterEntries(files); + } +} diff --git a/spring-integration-file/src/main/java/org/springframework/integration/file/filters/PatternMatchingFileListFilter.java b/spring-integration-file/src/main/java/org/springframework/integration/file/filters/PatternMatchingFileListFilter.java new file mode 100644 index 0000000000..6607547af8 --- /dev/null +++ b/spring-integration-file/src/main/java/org/springframework/integration/file/filters/PatternMatchingFileListFilter.java @@ -0,0 +1,57 @@ +/* + * Copyright 2002-2008 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; + +import org.springframework.integration.file.entries.FileEntryNamer; +import org.springframework.integration.file.entries.PatternMatchingEntryListFilter; +import org.springframework.util.Assert; + +import java.io.File; +import java.util.List; +import java.util.regex.Pattern; + +/** + * An {@link org.springframework.integration.file.entries.EntryListFilter} implementation that matches a File against a {@link Pattern}. + * + * @author Iwein Fuld + * @author Mark Fisher + * + * @since 1.0.0 + */ +public class PatternMatchingFileListFilter extends PatternMatchingEntryListFilter implements FileListFilter{ + + /** + * Create a file filter for the given pattern. + */ + public PatternMatchingFileListFilter(Pattern pattern) { + super(new FileEntryNamer(), pattern); + } + + public PatternMatchingFileListFilter(String pattern) { + super(new FileEntryNamer(), pattern); + } + + /** + * Filter out the files of which the name doesn't match the pattern of this filter + * + * @deprecated use {@link #filterEntries} instead + */ + public List filterFiles(File[] files) { + Assert.notNull(files, "'files' must not be null"); + return this.filterEntries(files); + } +} diff --git a/spring-integration-file/src/test/java/org/springframework/integration/file/recursive/FileInboundChannelAdapterWithRecursiveDirectoryTests.java b/spring-integration-file/src/test/java/org/springframework/integration/file/recursive/FileInboundChannelAdapterWithRecursiveDirectoryTests.java index 680d8fdba3..53b846a7be 100644 --- a/spring-integration-file/src/test/java/org/springframework/integration/file/recursive/FileInboundChannelAdapterWithRecursiveDirectoryTests.java +++ b/spring-integration-file/src/test/java/org/springframework/integration/file/recursive/FileInboundChannelAdapterWithRecursiveDirectoryTests.java @@ -31,6 +31,7 @@ import java.util.Arrays; import java.util.List; import static org.junit.Assert.assertThat; +import static org.junit.Assert.assertTrue; import static org.junit.matchers.JUnitMatchers.hasItems; import static org.springframework.integration.test.matcher.PayloadMatcher.hasPayload; @@ -53,21 +54,20 @@ public class FileInboundChannelAdapterWithRecursiveDirectoryTests { //when File folder = directory.newFolder("foo"); File file = new File(folder, "bar"); - file.createNewFile(); + assertTrue(file.createNewFile()); //verify assertThat(files.receive(), hasPayload(file)); } @Test(timeout = 2000) - @SuppressWarnings("unchecked") public void shouldReturnFilesMultipleLevels() throws IOException { //when File folder = directory.newFolder("foo"); File siblingFile = directory.newFile("bar"); File childFile = new File(folder, "baz"); - childFile.createNewFile(); + assertTrue(childFile.createNewFile()); List> received = Arrays.asList(files.receive(), files.receive()); //verify