RESOLVED - BATCH-1091: Add strict flag to file readers (flat and XML).

added strict flag - when true reader throws exception on open if input resource doesn't exist
This commit is contained in:
robokaso
2009-02-23 23:49:19 +00:00
parent 03f22512d6
commit fa2076594d
4 changed files with 63 additions and 7 deletions

View File

@@ -70,13 +70,25 @@ public class FlatFileItemReader<T> extends AbstractItemCountingItemStreamItemRea
private LineCallbackHandler skippedLinesCallback;
private boolean strict = false;
public FlatFileItemReader() {
setName(ClassUtils.getShortName(FlatFileItemReader.class));
}
/**
* @param skippedLinesCallback will be called for each one of the initial skipped
* lines before any items are read.
* In strict mode the reader will throw an exception on
* {@link #open(org.springframework.batch.item.ExecutionContext)} if the
* input resource does not exist.
* @param strict false by default
*/
public void setStrict(boolean strict) {
this.strict = strict;
}
/**
* @param skippedLinesCallback will be called for each one of the initial
* skipped lines before any items are read.
*/
public void setSkippedLinesCallback(LineCallbackHandler skippedLinesCallback) {
this.skippedLinesCallback = skippedLinesCallback;
@@ -194,7 +206,7 @@ public class FlatFileItemReader<T> extends AbstractItemCountingItemStreamItemRea
}
}
catch (IOException e) {
throw new FlatFileParseException("Unable to read from resource: [" + resource + "]", e, line, lineCount );
throw new FlatFileParseException("Unable to read from resource: [" + resource + "]", e, line, lineCount);
}
return line;
}
@@ -211,8 +223,7 @@ public class FlatFileItemReader<T> extends AbstractItemCountingItemStreamItemRea
@Override
protected void doClose() throws Exception {
lineCount = 0;
if(resource.exists())
{
if (resource.exists()) {
reader.close();
}
}
@@ -224,6 +235,9 @@ public class FlatFileItemReader<T> extends AbstractItemCountingItemStreamItemRea
noInput = false;
if (!resource.exists()) {
if (strict) {
throw new IllegalStateException("Input resource must exist (reader is in 'strict' mode)");
}
noInput = true;
logger.warn("Input resource does not exist " + resource.getDescription());
return;

View File

@@ -69,10 +69,22 @@ public class StaxEventItemReader<T> extends AbstractItemCountingItemStreamItemRe
private boolean noInput;
private boolean strict = false;
public StaxEventItemReader() {
setName(ClassUtils.getShortName(StaxEventItemReader.class));
}
/**
* In strict mode the reader will throw an exception on
* {@link #open(org.springframework.batch.item.ExecutionContext)} if the
* input resource does not exist.
* @param strict false by default
*/
public void setStrict(boolean strict) {
this.strict = strict;
}
public void setResource(Resource resource) {
this.resource = resource;
}
@@ -162,6 +174,9 @@ public class StaxEventItemReader<T> extends AbstractItemCountingItemStreamItemRe
noInput = false;
if (!resource.exists()) {
if (strict) {
throw new IllegalStateException("Input resource must exist (reader is in 'strict' mode)");
}
noInput = true;
logger.warn("Input resource does not exist " + resource.getDescription());
return;

View File

@@ -8,6 +8,7 @@ import java.io.InputStream;
import org.junit.Before;
import org.junit.Test;
import org.springframework.batch.item.ExecutionContext;
import org.springframework.batch.item.ItemStreamException;
import org.springframework.batch.item.file.mapping.PassThroughLineMapper;
import org.springframework.batch.item.file.separator.RecordSeparatorPolicy;
import org.springframework.core.io.AbstractResource;
@@ -135,6 +136,22 @@ public class FlatFileItemReaderTests {
assertEquals(TEST_STRING, reader.read());
}
/**
* In strict mode, resource must exist at the time reader is opened.
*/
@Test(expected = ItemStreamException.class)
public void testStrictness() throws Exception {
Resource resource = new NonExistentResource();
reader.setResource(resource);
reader.setStrict(true);
reader.afterPropertiesSet();
reader.open(executionContext);
}
private Resource getInputResource(String input) {
return new ByteArrayResource(input.getBytes());
}

View File

@@ -231,10 +231,9 @@ public class StaxEventItemReaderTests {
source.setResource(new NonExistentResource());
source.afterPropertiesSet();
source.open(executionContext);
assertNull(source.read());
}
@Test
@@ -248,6 +247,17 @@ public class StaxEventItemReaderTests {
source.read();
}
@Test(expected = ItemStreamException.class)
public void testStrictness() throws Exception {
source.setResource(new NonExistentResource());
source.setStrict(true);
source.afterPropertiesSet();
source.open(executionContext);
}
private StaxEventItemReader<List<XMLEvent>> createNewInputSouce() {
Resource resource = new ByteArrayResource(xml.getBytes());