From c22ccb1d26734d06ac068e0ba0e2bd3686801d59 Mon Sep 17 00:00:00 2001 From: Josh Long Date: Wed, 18 Aug 2010 10:23:36 +0000 Subject: [PATCH] Adding a slew of classes that can be genericized and then reused whole in the File, SFTP, and FTP adapters. This is experimental and may just as soon prove a dead end. --- .../file/entries/AbstractEntryListFilter.java | 40 +++++++++++ .../AcceptOnceEntryFileListFilter.java | 61 +++++++++++++++++ .../entries/CompositeEntryListFilter.java | 67 +++++++++++++++++++ .../file/entries/EntryListFilter.java | 23 +++++++ .../integration/file/entries/EntryNamer.java | 20 ++++++ .../PatternMatchingEntryListFilter.java | 62 +++++++++++++++++ 6 files changed, 273 insertions(+) create mode 100644 spring-integration-file/src/main/java/org/springframework/integration/file/entries/AbstractEntryListFilter.java create mode 100644 spring-integration-file/src/main/java/org/springframework/integration/file/entries/AcceptOnceEntryFileListFilter.java create mode 100644 spring-integration-file/src/main/java/org/springframework/integration/file/entries/CompositeEntryListFilter.java create mode 100644 spring-integration-file/src/main/java/org/springframework/integration/file/entries/EntryListFilter.java create mode 100644 spring-integration-file/src/main/java/org/springframework/integration/file/entries/EntryNamer.java create mode 100644 spring-integration-file/src/main/java/org/springframework/integration/file/entries/PatternMatchingEntryListFilter.java diff --git a/spring-integration-file/src/main/java/org/springframework/integration/file/entries/AbstractEntryListFilter.java b/spring-integration-file/src/main/java/org/springframework/integration/file/entries/AbstractEntryListFilter.java new file mode 100644 index 0000000000..3f00a62660 --- /dev/null +++ b/spring-integration-file/src/main/java/org/springframework/integration/file/entries/AbstractEntryListFilter.java @@ -0,0 +1,40 @@ +/* + * 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.entries; + +import java.util.ArrayList; +import java.util.List; + + +public abstract class AbstractEntryListFilter implements EntryListFilter { + + protected abstract boolean accept(T t); + + public List filterEntries(T[] entries) { + List accepted = new ArrayList(); + + if (entries != null) { + for (T t : entries) { + if (this.accept(t)) { + accepted.add(t); + } + } + } + + return accepted; + } +} diff --git a/spring-integration-file/src/main/java/org/springframework/integration/file/entries/AcceptOnceEntryFileListFilter.java b/spring-integration-file/src/main/java/org/springframework/integration/file/entries/AcceptOnceEntryFileListFilter.java new file mode 100644 index 0000000000..789c8c00a5 --- /dev/null +++ b/spring-integration-file/src/main/java/org/springframework/integration/file/entries/AcceptOnceEntryFileListFilter.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.entries; + + +import java.util.Queue; +import java.util.concurrent.LinkedBlockingQueue; + + +public class AcceptOnceEntryFileListFilter extends AbstractEntryListFilter { + private final Queue seen; + private final Object monitor = new Object(); + + /** + * 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 #filterEntries(Object[])} method. + * + * @param maxCapacity the maximum number of Files to maintain in the 'seen' + * queue. + */ + public AcceptOnceEntryFileListFilter(int maxCapacity) { + this.seen = new LinkedBlockingQueue(maxCapacity); + } + + /** + * Creates an AcceptOnceFileFilter based on an unbounded queue. + */ + public AcceptOnceEntryFileListFilter() { + this.seen = new LinkedBlockingQueue(); + } + + protected boolean accept(T pathname) { + synchronized (this.monitor) { + if (seen.contains(pathname)) { + return false; + } + + if (!seen.offer(pathname)) { + seen.poll(); + seen.add(pathname); + } + + return true; + } + } +} diff --git a/spring-integration-file/src/main/java/org/springframework/integration/file/entries/CompositeEntryListFilter.java b/spring-integration-file/src/main/java/org/springframework/integration/file/entries/CompositeEntryListFilter.java new file mode 100644 index 0000000000..82ecdc00e8 --- /dev/null +++ b/spring-integration-file/src/main/java/org/springframework/integration/file/entries/CompositeEntryListFilter.java @@ -0,0 +1,67 @@ +/* + * 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.entries; + +import org.springframework.util.Assert; +import java.util.*; + + +public class CompositeEntryListFilter implements EntryListFilter { + private final Set fileFilters; + + public CompositeEntryListFilter(EntryListFilter... fileFilters) { + this.fileFilters = new LinkedHashSet(Arrays.asList(fileFilters)); + } + + public CompositeEntryListFilter(Collection fileFilters) { + this.fileFilters = new LinkedHashSet(fileFilters); + } + + @SuppressWarnings("unchecked") + public List filterEntries(T[] entries) { + Assert.notNull(entries, "'files' should not be null"); + List leftOver = Arrays.asList(entries); + for (EntryListFilter fileFilter : this.fileFilters) { + T[] ts =(T[]) leftOver.toArray(); + leftOver = fileFilter.filterEntries(ts); + } + return leftOver; + } + + /** + * @param filters one or more new filters to add + * @return this CompositeFileFilter instance with the added filters + * @see #addFilters(Collection) + */ + public CompositeEntryListFilter addFilter(EntryListFilter... filters) { + return addFilters(Arrays.asList(filters)); + } + + /** + * Not thread safe. Only a single thread may add filters at a time. + *

+ * Add the new filters to this CompositeFileFilter while maintaining the existing filters. + * + * @param filtersToAdd a list of filters to add + * @return this CompositeEntryListFilter instance with the added filters + */ + public CompositeEntryListFilter addFilters(Collection filtersToAdd) { + this.fileFilters.addAll(filtersToAdd); + + return this; + } +} diff --git a/spring-integration-file/src/main/java/org/springframework/integration/file/entries/EntryListFilter.java b/spring-integration-file/src/main/java/org/springframework/integration/file/entries/EntryListFilter.java new file mode 100644 index 0000000000..8ab82e15cc --- /dev/null +++ b/spring-integration-file/src/main/java/org/springframework/integration/file/entries/EntryListFilter.java @@ -0,0 +1,23 @@ +/* + * 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.entries; + +import java.util.List; + + +public interface EntryListFilter { + List filterEntries(T [] entries ); +} diff --git a/spring-integration-file/src/main/java/org/springframework/integration/file/entries/EntryNamer.java b/spring-integration-file/src/main/java/org/springframework/integration/file/entries/EntryNamer.java new file mode 100644 index 0000000000..1bc181cb42 --- /dev/null +++ b/spring-integration-file/src/main/java/org/springframework/integration/file/entries/EntryNamer.java @@ -0,0 +1,20 @@ +/* + * 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.entries; + +public interface EntryNamer { + String nameOf(T entry); +} diff --git a/spring-integration-file/src/main/java/org/springframework/integration/file/entries/PatternMatchingEntryListFilter.java b/spring-integration-file/src/main/java/org/springframework/integration/file/entries/PatternMatchingEntryListFilter.java new file mode 100644 index 0000000000..1d9d604b06 --- /dev/null +++ b/spring-integration-file/src/main/java/org/springframework/integration/file/entries/PatternMatchingEntryListFilter.java @@ -0,0 +1,62 @@ +/* + * 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.entries; + +import org.springframework.beans.factory.InitializingBean; + +import org.springframework.util.Assert; +import org.springframework.util.StringUtils; + +import java.util.regex.Pattern; + + +/** + * experimental + * + * @author Josh Long + * @param the type of entry + */ +public abstract class PatternMatchingEntryListFilter extends AbstractEntryListFilter implements InitializingBean { + private Pattern pattern; + private String patternExpression; + private EntryNamer entryNamer; + + public void setPattern(Pattern pattern) { + this.pattern = pattern; + } + + public void setPatternExpression(String patternExpression) { + this.patternExpression = patternExpression; + } + + public void afterPropertiesSet() throws Exception { + if (StringUtils.hasText(this.patternExpression) && (pattern == null)) { + this.pattern = Pattern.compile(this.patternExpression); + } + + Assert.notNull(this.entryNamer,"'entryNamer' must not be null!"); + Assert.notNull(this.pattern, "'pattern' mustn't be null!"); + } + + @Override + protected boolean accept(T t) { + return (t != null) && this.pattern.matcher(this.entryNamer.nameOf(t)).matches(); + } + + public void setEntryNamer(EntryNamer entryNamer) { + this.entryNamer = entryNamer; + } +}