Updated StaxEventItemWriter to assert that it has been opened.

This commit adds a proper assertion that the writer has been properly
opened prior to executing a write.  This now returns a
WriterNotOpenException instead of the previous NullPointerException.

Resolves BATCH-2343
This commit is contained in:
Michael Minella
2017-01-26 12:08:22 -06:00
parent 4917ac3017
commit 934fcad6c2
2 changed files with 31 additions and 11 deletions

View File

@@ -27,7 +27,6 @@ import java.nio.channels.FileChannel;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import javax.xml.namespace.QName;
import javax.xml.stream.FactoryConfigurationError;
import javax.xml.stream.XMLEventFactory;
@@ -38,10 +37,12 @@ import javax.xml.transform.Result;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.batch.item.ExecutionContext;
import org.springframework.batch.item.ItemStreamException;
import org.springframework.batch.item.ItemWriter;
import org.springframework.batch.item.WriteFailedException;
import org.springframework.batch.item.WriterNotOpenException;
import org.springframework.batch.item.file.ResourceAwareItemWriterItemStream;
import org.springframework.batch.item.support.AbstractItemStreamItemWriter;
import org.springframework.batch.item.util.FileUtils;
@@ -152,6 +153,8 @@ ResourceAwareItemWriterItemStream<T>, InitializingBean {
private boolean shouldDeleteIfEmpty = false;
private boolean restarted = false;
private boolean initialized = false;
// List holding the QName of elements that were opened in the header callback, but not closed
private List<QName> unclosedHeaderCallbackElements = Collections.emptyList();
@@ -412,6 +415,8 @@ ResourceAwareItemWriterItemStream<T>, InitializingBean {
}
}
this.initialized = true;
}
/**
@@ -710,6 +715,8 @@ ResourceAwareItemWriterItemStream<T>, InitializingBean {
}
}
}
this.initialized = false;
}
private void closeStream() {
@@ -731,6 +738,10 @@ ResourceAwareItemWriterItemStream<T>, InitializingBean {
@Override
public void write(List<? extends T> items) throws XmlMappingException, Exception {
if(!this.initialized) {
throw new WriterNotOpenException("Writer must be open before it can be written to");
}
currentRecordCount += items.size();
for (Object object : items) {