diff --git a/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/file/FileWriterCallback.java b/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/file/FlatFileFooterCallback.java similarity index 58% rename from spring-batch-infrastructure/src/main/java/org/springframework/batch/item/file/FileWriterCallback.java rename to spring-batch-infrastructure/src/main/java/org/springframework/batch/item/file/FlatFileFooterCallback.java index 75a657fd2..dbe0e4b22 100644 --- a/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/file/FileWriterCallback.java +++ b/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/file/FlatFileFooterCallback.java @@ -4,16 +4,15 @@ import java.io.Writer; import java.io.IOException; /** - * Callback interface for writing to a file - useful e.g. for handling headers - * and footers. + * Callback interface for writing a footer to a file. * * @author Robert Kasanicky */ -public interface FileWriterCallback { +public interface FlatFileFooterCallback { /** * Write contents to a file using the supplied {@link Writer}. It is not * required to flush the writer inside this method. */ - void write(Writer writer) throws IOException; + void writeFooter(Writer writer) throws IOException; } diff --git a/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/file/FlatFileHeaderCallback.java b/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/file/FlatFileHeaderCallback.java new file mode 100644 index 000000000..8c63fe703 --- /dev/null +++ b/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/file/FlatFileHeaderCallback.java @@ -0,0 +1,18 @@ +package org.springframework.batch.item.file; + +import java.io.Writer; +import java.io.IOException; + +/** + * Callback interface for writing to a header to a file. + * + * @author Robert Kasanicky + */ +public interface FlatFileHeaderCallback { + + /** + * Write contents to a file using the supplied {@link Writer}. It is not + * required to flush the writer inside this method. + */ + void writeHeader(Writer writer) throws IOException; +} diff --git a/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/file/FlatFileItemWriter.java b/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/file/FlatFileItemWriter.java index 71658ba58..8b46d2f0c 100644 --- a/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/file/FlatFileItemWriter.java +++ b/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/file/FlatFileItemWriter.java @@ -74,9 +74,9 @@ public class FlatFileItemWriter extends ExecutionContextUserSupport implement private String encoding = OutputState.DEFAULT_CHARSET; - private FileWriterCallback headerCallback; + private FlatFileHeaderCallback headerCallback; - private FileWriterCallback footerCallback; + private FlatFileFooterCallback footerCallback; private String lineSeparator = DEFAULT_LINE_SEPARATOR; @@ -151,7 +151,7 @@ public class FlatFileItemWriter extends ExecutionContextUserSupport implement * headerCallback will be called before writing the first item to file. * Newline will be automatically appended after the header is written. */ - public void setHeaderCallback(FileWriterCallback headerCallback) { + public void setHeaderCallback(FlatFileHeaderCallback headerCallback) { this.headerCallback = headerCallback; } @@ -159,7 +159,7 @@ public class FlatFileItemWriter extends ExecutionContextUserSupport implement * footerCallback will be called after writing the last item to file, but * before the file is closed. */ - public void setFooterCallback(FileWriterCallback footerCallback) { + public void setFooterCallback(FlatFileFooterCallback footerCallback) { this.footerCallback = footerCallback; } @@ -206,7 +206,7 @@ public class FlatFileItemWriter extends ExecutionContextUserSupport implement if (state != null) { try { if (footerCallback != null) { - footerCallback.write(state.outputBufferedWriter); + footerCallback.writeFooter(state.outputBufferedWriter); state.outputBufferedWriter.flush(); } } @@ -249,7 +249,7 @@ public class FlatFileItemWriter extends ExecutionContextUserSupport implement if (outputState.lastMarkedByteOffsetPosition == 0) { if (headerCallback != null) { try { - headerCallback.write(outputState.outputBufferedWriter); + headerCallback.writeHeader(outputState.outputBufferedWriter); outputState.write("\n"); } catch (IOException e) { diff --git a/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/file/FlatFileItemWriterTests.java b/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/file/FlatFileItemWriterTests.java index db608ead2..23ab09c93 100644 --- a/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/file/FlatFileItemWriterTests.java +++ b/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/file/FlatFileItemWriterTests.java @@ -16,7 +16,11 @@ package org.springframework.batch.item.file; -import static org.junit.Assert.*; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; import java.io.BufferedReader; import java.io.File; @@ -203,9 +207,9 @@ public class FlatFileItemWriterTests { @Test public void testRestart() throws Exception { - writer.setFooterCallback(new FileWriterCallback() { + writer.setFooterCallback(new FlatFileFooterCallback() { - public void write(Writer writer) throws IOException { + public void writeFooter(Writer writer) throws IOException { writer.write("footer"); } @@ -315,9 +319,9 @@ public class FlatFileItemWriterTests { @Test public void testWriteFooter() throws Exception { - writer.setFooterCallback(new FileWriterCallback() { + writer.setFooterCallback(new FlatFileFooterCallback() { - public void write(Writer writer) throws IOException { + public void writeFooter(Writer writer) throws IOException { writer.write("a\nb"); } @@ -332,9 +336,9 @@ public class FlatFileItemWriterTests { @Test public void testWriteHeader() throws Exception { - writer.setHeaderCallback(new FileWriterCallback() { + writer.setHeaderCallback(new FlatFileHeaderCallback() { - public void write(Writer writer) throws IOException { + public void writeHeader(Writer writer) throws IOException { writer.write("a\nb"); } @@ -352,9 +356,9 @@ public class FlatFileItemWriterTests { @Test public void testWriteHeaderAfterRestartOnFirstChunk() throws Exception { - writer.setHeaderCallback(new FileWriterCallback() { + writer.setHeaderCallback(new FlatFileHeaderCallback() { - public void write(Writer writer) throws IOException { + public void writeHeader(Writer writer) throws IOException { writer.write("a\nb"); } @@ -377,9 +381,9 @@ public class FlatFileItemWriterTests { @Test public void testWriteHeaderAfterRestartOnSecondChunk() throws Exception { - writer.setHeaderCallback(new FileWriterCallback() { + writer.setHeaderCallback(new FlatFileHeaderCallback() { - public void write(Writer writer) throws IOException { + public void writeHeader(Writer writer) throws IOException { writer.write("a\nb"); } diff --git a/spring-batch-samples/src/main/java/org/springframework/batch/sample/support/HeaderCopyCallback.java b/spring-batch-samples/src/main/java/org/springframework/batch/sample/support/HeaderCopyCallback.java index 266e77b16..eefa602c9 100644 --- a/spring-batch-samples/src/main/java/org/springframework/batch/sample/support/HeaderCopyCallback.java +++ b/spring-batch-samples/src/main/java/org/springframework/batch/sample/support/HeaderCopyCallback.java @@ -3,10 +3,10 @@ package org.springframework.batch.sample.support; import java.io.IOException; import java.io.Writer; -import org.springframework.batch.item.file.FileWriterCallback; +import org.springframework.batch.item.file.FlatFileHeaderCallback; +import org.springframework.batch.item.file.FlatFileItemReader; import org.springframework.batch.item.file.FlatFileItemWriter; import org.springframework.batch.item.file.LineCallbackHandler; -import org.springframework.batch.item.file.FlatFileItemReader; import org.springframework.util.Assert; /** @@ -14,7 +14,7 @@ import org.springframework.util.Assert; * {@link FlatFileItemWriter} and copy header line from input file to output * file. */ -public class HeaderCopyCallback implements LineCallbackHandler, FileWriterCallback { +public class HeaderCopyCallback implements LineCallbackHandler, FlatFileHeaderCallback { private String header = ""; @@ -23,7 +23,7 @@ public class HeaderCopyCallback implements LineCallbackHandler, FileWriterCallba this.header = line; } - public void write(Writer writer) throws IOException { + public void writeHeader(Writer writer) throws IOException { writer.write("header from input: " + header); } diff --git a/spring-batch-samples/src/main/java/org/springframework/batch/sample/support/SummaryFooterCallback.java b/spring-batch-samples/src/main/java/org/springframework/batch/sample/support/SummaryFooterCallback.java index cc0f89596..a21f8ed2a 100644 --- a/spring-batch-samples/src/main/java/org/springframework/batch/sample/support/SummaryFooterCallback.java +++ b/spring-batch-samples/src/main/java/org/springframework/batch/sample/support/SummaryFooterCallback.java @@ -5,16 +5,16 @@ import java.io.Writer; import org.springframework.batch.core.StepExecution; import org.springframework.batch.core.listener.StepExecutionListenerSupport; -import org.springframework.batch.item.file.FileWriterCallback; +import org.springframework.batch.item.file.FlatFileFooterCallback; /** * Writes summary info in the footer of a file. */ -public class SummaryFooterCallback extends StepExecutionListenerSupport implements FileWriterCallback{ +public class SummaryFooterCallback extends StepExecutionListenerSupport implements FlatFileFooterCallback{ private StepExecution stepExecution; - public void write(Writer writer) throws IOException { + public void writeFooter(Writer writer) throws IOException { writer.write("footer - number of items written: " + stepExecution.getWriteCount()); }