Cleanup of close methods.

Removed the doCloseWorkbook method in favor of the normal doClose method.

The PoiItemReader now closes the InputStream and Workbook (for Apache POI >= 3.11)
This commit is contained in:
Marten Deinum
2015-01-19 20:54:19 +01:00
committed by Michael Minella
parent 1b2e749bcd
commit 30ff7bb1cd
4 changed files with 36 additions and 24 deletions

View File

@@ -28,7 +28,7 @@
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<spring.batch.version>3.0.3.RELEASE</spring.batch.version>
<jxl.version>2.6.12</jxl.version>
<poi.version>3.10.1</poi.version>
<poi.version>3.11</poi.version>
</properties>
<dependencies>

View File

@@ -136,27 +136,6 @@ public abstract class AbstractExcelItemReader<T> extends AbstractItemCountingIte
}
@Override
protected final void doClose() throws Exception {
doCloseWorkbook();
if (this.resource != null) {
try {
InputStream is = this.resource.getInputStream();
is.close();
} catch (IOException ioe) {
logger.warn("Exception whilst obtaining or closing the inputstream.", ioe);
}
}
}
/**
* Method which can be overriden by subclasses to do cleanup additional resources.
*
* @throws Exception
*/
protected void doCloseWorkbook() throws Exception {
}
public void setResource(final Resource resource) {
this.resource = resource;
}

View File

@@ -51,7 +51,7 @@ public class JxlItemReader<T> extends AbstractExcelItemReader<T> {
}
@Override
protected void doCloseWorkbook() throws Exception {
protected void doClose() throws Exception {
if (this.workbook != null) {
this.workbook.close();
}

View File

@@ -23,6 +23,10 @@ import org.springframework.batch.item.excel.AbstractExcelItemReader;
import org.springframework.batch.item.excel.Sheet;
import org.springframework.core.io.Resource;
import java.io.Closeable;
import java.io.InputStream;
import java.io.PushbackInputStream;
/**
* {@link org.springframework.batch.item.ItemReader} implementation which uses apache POI to read an Excel
* file. It will read the file sheet for sheet and row for row. It is based on
@@ -36,6 +40,8 @@ public class PoiItemReader<T> extends AbstractExcelItemReader<T> {
private Workbook workbook;
private InputStream workbookStream;
@Override
protected Sheet getSheet(final int sheet) {
return new PoiSheet(this.workbook.getSheetAt(sheet));
@@ -46,9 +52,36 @@ public class PoiItemReader<T> extends AbstractExcelItemReader<T> {
return this.workbook.getNumberOfSheets();
}
@Override
protected void doClose() throws Exception {
// As of Apache POI 3.11 there is a close method on the Workbook, prior version
// lack this method.
if (workbook instanceof Closeable) {
this.workbook.close();
}
if (workbookStream != null) {
workbookStream.close();
}
this.workbook=null;
this.workbookStream=null;
}
/**
* Open the underlying file using the {@code WorkbookFactory}. We keep track of the used {@code InputStream} so that
* it can be closed cleanly on the end of reading the file. This to be able to release the resources used by
* Apache POI.
*
* @param resource
* @throws Exception is thrown for any errors.
*/
@Override
protected void openExcelFile(final Resource resource) throws Exception {
this.workbook = WorkbookFactory.create(resource.getInputStream());
workbookStream = resource.getInputStream();
if (!workbookStream.markSupported() && !(workbookStream instanceof PushbackInputStream)) {
throw new IllegalStateException("InputStream MUST either support mark/reset, or be wrapped as a PushbackInputStream");
}
this.workbook = WorkbookFactory.create(workbookStream);
this.workbook.setMissingCellPolicy(Row.CREATE_NULL_AS_BLANK);
}