BATCH-1854 Created ResourceAware and added a check to MultiResourceItemReader. If an item implements ResourceAware, the current resource will be set on it

This commit is contained in:
Lucas Ward
2012-10-28 21:23:37 -05:00
parent 786cb09c9c
commit 00763bd57a
3 changed files with 137 additions and 10 deletions

View File

@@ -0,0 +1,16 @@
package org.springframework.batch.item;
import org.springframework.core.io.Resource;
import org.springframework.batch.item.file.MultiResourceItemReader;
/**
* Marker interface indicating that an item should have the Spring {@link Resource} in which it was read from, set on it.
* The canonical example is within {@link MultiResourceItemReader}, which will set the current resource on any items
* that implement this interface.
*
* @author Lucas Ward
*/
public interface ResourceAware {
void setResource(Resource resource);
}

View File

@@ -21,12 +21,7 @@ import java.util.Comparator;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.batch.item.ExecutionContext;
import org.springframework.batch.item.ItemReader;
import org.springframework.batch.item.ItemStream;
import org.springframework.batch.item.ItemStreamException;
import org.springframework.batch.item.ParseException;
import org.springframework.batch.item.UnexpectedInputException;
import org.springframework.batch.item.*;
import org.springframework.batch.item.util.ExecutionContextUserSupport;
import org.springframework.core.io.Resource;
import org.springframework.util.Assert;
@@ -116,7 +111,7 @@ public class MultiResourceItemReader<T> implements ItemReader<T>, ItemStream {
*/
private T readNextItem() throws Exception {
T item = delegate.read();
T item = readFromDelegate();
while (item == null) {
@@ -130,13 +125,21 @@ public class MultiResourceItemReader<T> implements ItemReader<T>, ItemStream {
delegate.setResource(resources[currentResource]);
delegate.open(new ExecutionContext());
item = delegate.read();
}
item = readFromDelegate();
}
return item;
}
/**
private T readFromDelegate() throws Exception {
T item = delegate.read();
if(item instanceof ResourceAware){
((ResourceAware) item).setResource(getCurrentResource());
}
return item;
}
/**
* Close the {@link #setDelegate(ResourceAwareItemReaderItemStream)} reader and reset instance variable values.
*/
public void close() throws ItemStreamException {