From e80fdae0aa5fb80f859cc08b73d6391611644898 Mon Sep 17 00:00:00 2001 From: robokaso Date: Wed, 10 Sep 2008 10:59:53 +0000 Subject: [PATCH] RESOLVED - BATCH-828: org.springframework.batch.item.file.MultiResourceItemReader should allow for no resources return null on first read if the resource list is empty --- .../item/file/MultiResourceItemReader.java | 21 ++++++++++++++++++- ...ltiResourceItemReaderIntegrationTests.java | 9 ++++++++ 2 files changed, 29 insertions(+), 1 deletion(-) 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 aed273990..e24618227 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 @@ -3,6 +3,8 @@ package org.springframework.batch.item.file; import java.util.Arrays; 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; @@ -29,6 +31,8 @@ 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(); @@ -39,6 +43,9 @@ 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 emptyInput; private Comparator comparator = new Comparator() { @@ -60,6 +67,10 @@ public class MultiResourceItemReader implements ItemReader, ItemStream { */ public T read() throws Exception, UnexpectedInputException, NoWorkFoundException, ParseException { + if (emptyInput) { + return null; + } + T item; item = readNextItem(); index.incrementItemCount(); @@ -101,6 +112,7 @@ public class MultiResourceItemReader implements ItemReader, ItemStream { public void close(ExecutionContext executionContext) throws ItemStreamException { index = new MultiResourceIndex(); delegate.close(new ExecutionContext()); + emptyInput = false; } /** @@ -109,7 +121,14 @@ public class MultiResourceItemReader implements ItemReader, ItemStream { */ public void open(ExecutionContext executionContext) throws ItemStreamException { - Assert.notEmpty(resources, "There must be at least one input resource"); + Assert.notNull(resources, "Resources must be set"); + + emptyInput = false; + if (resources.length == 0) { + logger.info("No resources to read"); + emptyInput = true; + return; + } Arrays.sort(resources, comparator); 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 a3c1c4d5a..e8d52e4e6 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 @@ -200,6 +200,15 @@ public class MultiResourceItemReaderIntegrationTests extends TestCase { assertSame(r2, resources[2]); } + /** + * Empty resource list is OK. + */ + public void testNoResourcesFound() throws Exception { + tested.setResources(new Resource[] {}); + tested.open(ctx); + + assertNull(tested.read()); + } private String readItem() throws Exception { Object result = tested.read(); return result == null ? null : ((FieldSet) result).readString(0);