Added FileToStringTransformer and FileToByteArrayTransformer.
This commit is contained in:
@@ -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<File>(maxCapacity);
|
||||
this.seen = new LinkedBlockingQueue<File>(maxCapacity);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates an AcceptOnceFileFilter based on an unbounded queue
|
||||
* Creates an AcceptOnceFileFilter based on an unbounded queue.
|
||||
*/
|
||||
public AcceptOnceFileFilter() {
|
||||
seen = new LinkedBlockingQueue<File>();
|
||||
this.seen = new LinkedBlockingQueue<File>();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 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<File> filterFiles(File[] files) {
|
||||
List<File> accepted = new ArrayList<File>();
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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<T> extends AbstractPayloadTransformer<File, T> {
|
||||
|
||||
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;
|
||||
|
||||
}
|
||||
@@ -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<byte[]> {
|
||||
|
||||
@Override
|
||||
protected final byte[] transformFile(File file) throws Exception {
|
||||
return FileCopyUtils.copyToByteArray(file);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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<String> {
|
||||
|
||||
@Override
|
||||
protected final String transformFile(File file) throws Exception {
|
||||
return FileCopyUtils.copyToString(new FileReader(file));
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user