diff --git a/org.springframework.integration.file/src/main/java/org/springframework/integration/file/AcceptOnceFileFilter.java b/org.springframework.integration.file/src/main/java/org/springframework/integration/file/AcceptOnceFileFilter.java index d0c73108e0..cffff26b07 100644 --- a/org.springframework.integration.file/src/main/java/org/springframework/integration/file/AcceptOnceFileFilter.java +++ b/org.springframework.integration.file/src/main/java/org/springframework/integration/file/AcceptOnceFileFilter.java @@ -1,3 +1,19 @@ +/* + * 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; import java.io.File; @@ -7,9 +23,9 @@ import java.util.Queue; import java.util.concurrent.LinkedBlockingQueue; /** - * {@link FileListFilter} that passes files only one time. This can conveniently - * be used to prevent duplication of files, as is done in - * {@link PollableFileSource} + * {@link FileListFilter} that passes files only one time. This can + * conveniently be used to prevent duplication of files, as is done in + * {@link PollableFileSource}. * * @author Iwein Fuld * @@ -20,26 +36,30 @@ public class AcceptOnceFileFilter implements FileListFilter { 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 pass this filter again if - * passed to the {@link #filterFiles(File[])} method - * @param maxCapacity + * 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 AcceptOnceFileFilter(int maxCapacity) { - seen = new LinkedBlockingQueue(maxCapacity); + this.seen = new LinkedBlockingQueue(maxCapacity); } /** - * Creates an AcceptOnceFileFilter based on an unbounded queue + * Creates an AcceptOnceFileFilter based on an unbounded queue. */ public AcceptOnceFileFilter() { - seen = new LinkedBlockingQueue(); + this.seen = new LinkedBlockingQueue(); } + /** - * Returns the list of files that have not been filtered by this instance - * before + * Returns the list of files that have not already been filtered by this + * instance. */ public List filterFiles(File[] files) { List accepted = new ArrayList(); @@ -53,14 +73,15 @@ public class AcceptOnceFileFilter implements FileListFilter { private boolean accept(File pathname) { synchronized (monitor) { - if (!seen.contains(pathname)) { - if (!seen.offer(pathname)) { - seen.poll(); - seen.add(pathname); - } - return true; + if (seen.contains(pathname)) { + return false; } - return false; + if (!seen.offer(pathname)) { + seen.poll(); + seen.add(pathname); + } + return true; } } + } diff --git a/org.springframework.integration.file/src/main/java/org/springframework/integration/file/CompositeFileFilter.java b/org.springframework.integration.file/src/main/java/org/springframework/integration/file/CompositeFileFilter.java index 40160b20b5..c880d96d10 100644 --- a/org.springframework.integration.file/src/main/java/org/springframework/integration/file/CompositeFileFilter.java +++ b/org.springframework.integration.file/src/main/java/org/springframework/integration/file/CompositeFileFilter.java @@ -13,6 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ + package org.springframework.integration.file; import java.io.File; diff --git a/org.springframework.integration.file/src/main/java/org/springframework/integration/file/transformer/AbstractFilePayloadTransformer.java b/org.springframework.integration.file/src/main/java/org/springframework/integration/file/transformer/AbstractFilePayloadTransformer.java new file mode 100644 index 0000000000..70c96c84be --- /dev/null +++ b/org.springframework.integration.file/src/main/java/org/springframework/integration/file/transformer/AbstractFilePayloadTransformer.java @@ -0,0 +1,63 @@ +/* + * 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.transformer; + +import java.io.File; + +import org.springframework.integration.message.MessagingException; +import org.springframework.integration.transformer.AbstractPayloadTransformer; +import org.springframework.util.Assert; + +/** + * Base class for transformers that convert a File payload. + * + * @author Mark Fisher + */ +public abstract class AbstractFilePayloadTransformer extends AbstractPayloadTransformer { + + private volatile boolean deleteFile; + + + /** + * Specify whether to delete the File after transformation. + */ + public void setDeleteFile(boolean deleteFile) { + this.deleteFile = deleteFile; + } + + @Override + protected final T transformPayload(File file) throws Exception { + Assert.notNull(file, "File must not be null"); + if (!file.exists()) { + throw new MessagingException("File '" + file + "' no longer exists."); + } + if (!file.canRead()) { + throw new MessagingException("Unable to read File '" + file + "'"); + } + T result = transformFile(file); + if (this.deleteFile) { + file.delete(); + } + return result; + } + + /** + * Subclasses must implement this method to transform the File contents. + */ + protected abstract T transformFile(File file) throws Exception; + +} diff --git a/org.springframework.integration.file/src/main/java/org/springframework/integration/file/transformer/FileToByteArrayTransformer.java b/org.springframework.integration.file/src/main/java/org/springframework/integration/file/transformer/FileToByteArrayTransformer.java new file mode 100644 index 0000000000..83039f7361 --- /dev/null +++ b/org.springframework.integration.file/src/main/java/org/springframework/integration/file/transformer/FileToByteArrayTransformer.java @@ -0,0 +1,35 @@ +/* + * 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.transformer; + +import java.io.File; + +import org.springframework.util.FileCopyUtils; + +/** + * A payload transformer that copies a File's contents to a byte array. + * + * @author Mark Fisher + */ +public class FileToByteArrayTransformer extends AbstractFilePayloadTransformer { + + @Override + protected final byte[] transformFile(File file) throws Exception { + return FileCopyUtils.copyToByteArray(file); + } + +} diff --git a/org.springframework.integration.file/src/main/java/org/springframework/integration/file/transformer/FileToStringTransformer.java b/org.springframework.integration.file/src/main/java/org/springframework/integration/file/transformer/FileToStringTransformer.java new file mode 100644 index 0000000000..a4de74cf3b --- /dev/null +++ b/org.springframework.integration.file/src/main/java/org/springframework/integration/file/transformer/FileToStringTransformer.java @@ -0,0 +1,36 @@ +/* + * 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.transformer; + +import java.io.File; +import java.io.FileReader; + +import org.springframework.util.FileCopyUtils; + +/** + * A payload transformer that copies a File's contents to a String. + * + * @author Mark Fisher + */ +public class FileToStringTransformer extends AbstractFilePayloadTransformer { + + @Override + protected final String transformFile(File file) throws Exception { + return FileCopyUtils.copyToString(new FileReader(file)); + } + +}