RESOLVED - BATCH-1243: Expose the current resource of MultiResourceItemReader

This commit is contained in:
dhgarrette
2009-05-15 17:27:40 +00:00
parent d6c6d1ba05
commit fd8ee7c985
2 changed files with 42 additions and 7 deletions

View File

@@ -46,7 +46,7 @@ import org.springframework.util.ClassUtils;
* @author Robert Kasanicky
*/
public class MultiResourceItemReader<T> implements ItemReader<T>, ItemStream {
private static final Log logger = LogFactory.getLog(MultiResourceItemReader.class);
private final ExecutionContextUserSupport executionContextUserSupport = new ExecutionContextUserSupport();
@@ -58,7 +58,7 @@ public class MultiResourceItemReader<T> implements ItemReader<T>, ItemStream {
private MultiResourceIndex index = new MultiResourceIndex();
private boolean saveState = true;
// signals there are no resources to read -> just return null on first read
private boolean noInput;
@@ -85,7 +85,7 @@ public class MultiResourceItemReader<T> implements ItemReader<T>, ItemStream {
if (noInput) {
return null;
}
T item;
item = readNextItem();
index.incrementItemCount();
@@ -96,6 +96,7 @@ public class MultiResourceItemReader<T> implements ItemReader<T>, ItemStream {
/**
* Use the delegate to read the next item, jump to next resource if current
* one is exhausted. Items are appended to the buffer.
*
* @return next item from input
*/
private T readNextItem() throws Exception {
@@ -137,7 +138,7 @@ public class MultiResourceItemReader<T> implements ItemReader<T>, ItemStream {
public void open(ExecutionContext executionContext) throws ItemStreamException {
Assert.notNull(resources, "Resources must be set");
noInput = false;
if (resources.length == 0) {
logger.warn("No resources to read");
@@ -192,7 +193,7 @@ public class MultiResourceItemReader<T> implements ItemReader<T>, ItemStream {
/**
* @param comparator used to order the injected resources, by default
* compares {@link Resource#getFilename()} values.
* compares {@link Resource#getFilename()} values.
*/
public void setComparator(Comparator<Resource> comparator) {
this.comparator = comparator;
@@ -205,6 +206,13 @@ public class MultiResourceItemReader<T> implements ItemReader<T>, ItemStream {
this.resources = resources;
}
public Resource getCurrentResource() {
if (index.currentResource >= resources.length) {
return null;
}
return resources[index.currentResource];
}
/**
* Facilitates keeping track of the position within multi-resource input.
*/

View File

@@ -35,7 +35,7 @@ public class MultiResourceItemReaderIntegrationTests extends TestCase {
* Setup the tested reader to read from the test resources.
*/
protected void setUp() throws Exception {
itemReader.setLineMapper(new PassThroughLineMapper());
tested.setDelegate(itemReader);
@@ -67,6 +67,33 @@ public class MultiResourceItemReaderIntegrationTests extends TestCase {
tested.close();
}
public void testGetCurrentResource() throws Exception {
tested.open(ctx);
assertSame(r1, tested.getCurrentResource());
assertEquals("1", tested.read());
assertSame(r1, tested.getCurrentResource());
assertEquals("2", tested.read());
assertSame(r1, tested.getCurrentResource());
assertEquals("3", tested.read());
assertSame(r1, tested.getCurrentResource());
assertEquals("4", tested.read());
assertSame(r2, tested.getCurrentResource());
assertEquals("5", tested.read());
assertSame(r2, tested.getCurrentResource());
assertEquals("6", tested.read());
assertSame(r4, tested.getCurrentResource());
assertEquals("7", tested.read());
assertSame(r5, tested.getCurrentResource());
assertEquals("8", tested.read());
assertSame(r5, tested.getCurrentResource());
assertEquals(null, tested.read());
assertSame(null, tested.getCurrentResource());
tested.close();
}
public void testRestartWhenStateNotSaved() throws Exception {
tested.setSaveState(false);
@@ -205,7 +232,7 @@ public class MultiResourceItemReaderIntegrationTests extends TestCase {
public void testNoResourcesFound() throws Exception {
tested.setResources(new Resource[] {});
tested.open(ctx);
assertNull(tested.read());
}