From fd8ee7c9852a2480b2ce0dd52197ecfb598a3583 Mon Sep 17 00:00:00 2001 From: dhgarrette Date: Fri, 15 May 2009 17:27:40 +0000 Subject: [PATCH] RESOLVED - BATCH-1243: Expose the current resource of MultiResourceItemReader --- .../item/file/MultiResourceItemReader.java | 18 ++++++++--- ...ltiResourceItemReaderIntegrationTests.java | 31 +++++++++++++++++-- 2 files changed, 42 insertions(+), 7 deletions(-) diff --git a/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/file/MultiResourceItemReader.java b/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/file/MultiResourceItemReader.java index c3b0e673f..7d29319ed 100644 --- a/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/file/MultiResourceItemReader.java +++ b/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/file/MultiResourceItemReader.java @@ -46,7 +46,7 @@ import org.springframework.util.ClassUtils; * @author Robert Kasanicky */ public class MultiResourceItemReader implements ItemReader, ItemStream { - + private static final Log logger = LogFactory.getLog(MultiResourceItemReader.class); private final ExecutionContextUserSupport executionContextUserSupport = new ExecutionContextUserSupport(); @@ -58,7 +58,7 @@ public class MultiResourceItemReader implements ItemReader, 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 implements ItemReader, ItemStream { if (noInput) { return null; } - + T item; item = readNextItem(); index.incrementItemCount(); @@ -96,6 +96,7 @@ public class MultiResourceItemReader implements ItemReader, 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 implements ItemReader, 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 implements ItemReader, 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 comparator) { this.comparator = comparator; @@ -205,6 +206,13 @@ public class MultiResourceItemReader implements ItemReader, 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. */ diff --git a/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/file/MultiResourceItemReaderIntegrationTests.java b/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/file/MultiResourceItemReaderIntegrationTests.java index b848c1f2a..aa92d00d8 100644 --- a/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/file/MultiResourceItemReaderIntegrationTests.java +++ b/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/file/MultiResourceItemReaderIntegrationTests.java @@ -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()); }