Skip to content

Commit 5bce152

Browse files
committed
Change Resource to WritableResource in file-based item writers
Resolves #756
1 parent 74e8caa commit 5bce152

16 files changed

+73
-70
lines changed

spring-batch-infrastructure/src/main/java/org/springframework/batch/item/file/ResourceAwareItemWriterItemStream.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2006-2007 the original author or authors.
2+
* Copyright 2006-2022 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -19,15 +19,15 @@
1919
import org.springframework.batch.item.ItemStream;
2020
import org.springframework.batch.item.ItemStreamWriter;
2121
import org.springframework.batch.item.ItemWriter;
22-
import org.springframework.core.io.Resource;
22+
import org.springframework.core.io.WritableResource;
2323

2424
/**
2525
* Interface for {@link ItemWriter}s that implement {@link ItemStream} and write
26-
* output to {@link Resource}.
26+
* output to {@link WritableResource}.
2727
*
2828
* @author Robert Kasanicky
2929
*/
3030
public interface ResourceAwareItemWriterItemStream<T> extends ItemStreamWriter<T> {
3131

32-
void setResource(Resource resource);
32+
void setResource(WritableResource resource);
3333
}

spring-batch-infrastructure/src/main/java/org/springframework/batch/item/file/builder/FlatFileItemWriterBuilder.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2016-2019 the original author or authors.
2+
* Copyright 2016-2022 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -31,7 +31,7 @@
3131
import org.springframework.batch.item.file.transform.FieldExtractor;
3232
import org.springframework.batch.item.file.transform.FormatterLineAggregator;
3333
import org.springframework.batch.item.file.transform.LineAggregator;
34-
import org.springframework.core.io.Resource;
34+
import org.springframework.core.io.WritableResource;
3535
import org.springframework.util.Assert;
3636

3737
/**
@@ -48,7 +48,7 @@ public class FlatFileItemWriterBuilder<T> {
4848

4949
protected Log logger = LogFactory.getLog(getClass());
5050

51-
private Resource resource;
51+
private WritableResource resource;
5252

5353
private boolean forceSync = false;
5454

@@ -108,13 +108,13 @@ public FlatFileItemWriterBuilder<T> name(String name) {
108108
}
109109

110110
/**
111-
* The {@link Resource} to be used as output.
111+
* The {@link WritableResource} to be used as output.
112112
*
113113
* @param resource the output of the writer.
114114
* @return The current instance of the builder.
115-
* @see FlatFileItemWriter#setResource(Resource)
115+
* @see FlatFileItemWriter#setResource(WritableResource)
116116
*/
117-
public FlatFileItemWriterBuilder<T> resource(Resource resource) {
117+
public FlatFileItemWriterBuilder<T> resource(WritableResource resource) {
118118
this.resource = resource;
119119

120120
return this;

spring-batch-infrastructure/src/main/java/org/springframework/batch/item/json/JsonFileItemWriter.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2018 the original author or authors.
2+
* Copyright 2018-2022 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -20,13 +20,13 @@
2020
import java.util.List;
2121

2222
import org.springframework.batch.item.support.AbstractFileItemWriter;
23-
import org.springframework.core.io.Resource;
23+
import org.springframework.core.io.WritableResource;
2424
import org.springframework.util.Assert;
2525
import org.springframework.util.ClassUtils;
2626

2727
/**
2828
* Item writer that writes data in json format to an output file. The location
29-
* of the output file is defined by a {@link Resource} and must represent a
29+
* of the output file is defined by a {@link WritableResource} and must represent a
3030
* writable file. Items are transformed to json format using a
3131
* {@link JsonObjectMarshaller}. Items will be enclosed in a json array as follows:
3232
*
@@ -61,7 +61,7 @@ public class JsonFileItemWriter<T> extends AbstractFileItemWriter<T> {
6161
* @param resource to write json data to
6262
* @param jsonObjectMarshaller used to marshal object into json representation
6363
*/
64-
public JsonFileItemWriter(Resource resource, JsonObjectMarshaller<T> jsonObjectMarshaller) {
64+
public JsonFileItemWriter(WritableResource resource, JsonObjectMarshaller<T> jsonObjectMarshaller) {
6565
Assert.notNull(resource, "resource must not be null");
6666
Assert.notNull(jsonObjectMarshaller, "json object marshaller must not be null");
6767
setResource(resource);

spring-batch-infrastructure/src/main/java/org/springframework/batch/item/json/builder/JsonFileItemWriterBuilder.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2018 the original author or authors.
2+
* Copyright 2018-2022 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -20,7 +20,7 @@
2020
import org.springframework.batch.item.file.FlatFileHeaderCallback;
2121
import org.springframework.batch.item.json.JsonFileItemWriter;
2222
import org.springframework.batch.item.json.JsonObjectMarshaller;
23-
import org.springframework.core.io.Resource;
23+
import org.springframework.core.io.WritableResource;
2424
import org.springframework.util.Assert;
2525

2626
/**
@@ -32,7 +32,7 @@
3232
*/
3333
public class JsonFileItemWriterBuilder<T> {
3434

35-
private Resource resource;
35+
private WritableResource resource;
3636
private JsonObjectMarshaller<T> jsonObjectMarshaller;
3737
private FlatFileHeaderCallback headerCallback;
3838
private FlatFileFooterCallback footerCallback;
@@ -119,13 +119,13 @@ public JsonFileItemWriterBuilder<T> jsonObjectMarshaller(JsonObjectMarshaller<T>
119119
}
120120

121121
/**
122-
* The {@link Resource} to be used as output.
122+
* The {@link WritableResource} to be used as output.
123123
*
124124
* @param resource the output of the writer.
125125
* @return The current instance of the builder.
126-
* @see JsonFileItemWriter#setResource(Resource)
126+
* @see JsonFileItemWriter#setResource(WritableResource)
127127
*/
128-
public JsonFileItemWriterBuilder<T> resource(Resource resource) {
128+
public JsonFileItemWriterBuilder<T> resource(WritableResource resource) {
129129
this.resource = resource;
130130

131131
return this;

spring-batch-infrastructure/src/main/java/org/springframework/batch/item/support/AbstractFileItemWriter.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2006-2021 the original author or authors.
2+
* Copyright 2006-2022 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -41,13 +41,13 @@
4141
import org.springframework.batch.item.util.FileUtils;
4242
import org.springframework.batch.support.transaction.TransactionAwareBufferedWriter;
4343
import org.springframework.beans.factory.InitializingBean;
44-
import org.springframework.core.io.Resource;
44+
import org.springframework.core.io.WritableResource;
4545
import org.springframework.util.Assert;
4646

4747
/**
4848
* Base class for item writers that write data to a file or stream.
4949
* This class provides common features like restart, force sync, append etc.
50-
* The location of the output file is defined by a {@link Resource} which must
50+
* The location of the output file is defined by a {@link WritableResource} which must
5151
* represent a writable file.<br>
5252
*
5353
* Uses buffered writer to improve performance.<br>
@@ -81,7 +81,7 @@ public abstract class AbstractFileItemWriter<T> extends AbstractItemStreamItemWr
8181

8282
private static final String RESTART_DATA_NAME = "current.count";
8383

84-
private Resource resource;
84+
private WritableResource resource;
8585

8686
protected OutputState state = null;
8787

@@ -128,12 +128,12 @@ public void setLineSeparator(String lineSeparator) {
128128
}
129129

130130
/**
131-
* Setter for resource. Represents a file that can be written.
131+
* Setter for a writable resource. Represents a file that can be written.
132132
*
133133
* @param resource the resource to be written to
134134
*/
135135
@Override
136-
public void setResource(Resource resource) {
136+
public void setResource(WritableResource resource) {
137137
this.resource = resource;
138138
}
139139

spring-batch-infrastructure/src/main/java/org/springframework/batch/item/xml/StaxEventItemWriter.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2006-2020 the original author or authors.
2+
* Copyright 2006-2022 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -51,7 +51,7 @@
5151
import org.springframework.batch.item.xml.stax.UnopenedElementClosingEventWriter;
5252
import org.springframework.batch.support.transaction.TransactionAwareBufferedWriter;
5353
import org.springframework.beans.factory.InitializingBean;
54-
import org.springframework.core.io.Resource;
54+
import org.springframework.core.io.WritableResource;
5555
import org.springframework.dao.DataAccessResourceFailureException;
5656
import org.springframework.oxm.Marshaller;
5757
import org.springframework.oxm.XmlMappingException;
@@ -103,7 +103,7 @@ public class StaxEventItemWriter<T> extends AbstractItemStreamItemWriter<T> impl
103103
private static final String WRITE_STATISTICS_NAME = "record.count";
104104

105105
// file system resource
106-
private Resource resource;
106+
private WritableResource resource;
107107

108108
// xml marshaller
109109
private Marshaller marshaller;
@@ -177,7 +177,7 @@ public StaxEventItemWriter() {
177177
* @param resource the output file
178178
*/
179179
@Override
180-
public void setResource(Resource resource) {
180+
public void setResource(WritableResource resource) {
181181
this.resource = resource;
182182
}
183183

spring-batch-infrastructure/src/main/java/org/springframework/batch/item/xml/builder/StaxEventItemWriterBuilder.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2017-2020 the original author or authors.
2+
* Copyright 2017-2022 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -19,7 +19,7 @@
1919

2020
import org.springframework.batch.item.xml.StaxEventItemWriter;
2121
import org.springframework.batch.item.xml.StaxWriterCallback;
22-
import org.springframework.core.io.Resource;
22+
import org.springframework.core.io.WritableResource;
2323
import org.springframework.oxm.Marshaller;
2424
import org.springframework.util.Assert;
2525

@@ -34,7 +34,7 @@
3434
*/
3535
public class StaxEventItemWriterBuilder<T> {
3636

37-
private Resource resource;
37+
private WritableResource resource;
3838

3939
private Marshaller marshaller;
4040

@@ -80,13 +80,13 @@ public StaxEventItemWriterBuilder<T> name(String name) {
8080
}
8181

8282
/**
83-
* The {@link Resource} to be used as output.
83+
* The {@link WritableResource} to be used as output.
8484
*
8585
* @param resource the output from the writer
8686
* @return the current instance of the builder.
87-
* @see StaxEventItemWriter#setResource(Resource)
87+
* @see StaxEventItemWriter#setResource(WritableResource)
8888
*/
89-
public StaxEventItemWriterBuilder<T> resource(Resource resource) {
89+
public StaxEventItemWriterBuilder<T> resource(WritableResource resource) {
9090
this.resource = resource;
9191

9292
return this;

spring-batch-infrastructure/src/test/java/org/springframework/batch/item/file/FlatFileItemWriterTests.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2006-2007 the original author or authors.
2+
* Copyright 2006-2022 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -39,7 +39,7 @@
3939
import org.springframework.batch.item.file.transform.PassThroughLineAggregator;
4040
import org.springframework.batch.support.transaction.ResourcelessTransactionManager;
4141
import org.springframework.core.io.FileSystemResource;
42-
import org.springframework.core.io.Resource;
42+
import org.springframework.core.io.WritableResource;
4343
import org.springframework.transaction.PlatformTransactionManager;
4444
import org.springframework.transaction.TransactionStatus;
4545
import org.springframework.transaction.support.TransactionCallback;
@@ -838,7 +838,7 @@ public String aggregate(String item) {
838838
* If append=true a new output file should still be created on the first run (not restart).
839839
*/
840840
public void testAppendToNotYetExistingFile() throws Exception {
841-
Resource toBeCreated = new FileSystemResource("target/FlatFileItemWriterTests.out");
841+
WritableResource toBeCreated = new FileSystemResource("target/FlatFileItemWriterTests.out");
842842

843843
outputFile = toBeCreated.getFile(); //enable easy content reading and auto-delete the file
844844

spring-batch-infrastructure/src/test/java/org/springframework/batch/item/file/builder/FlatFileItemWriterBuilderTests.java

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2016-2021 the original author or authors.
2+
* Copyright 2016-2022 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -30,6 +30,7 @@
3030
import org.springframework.batch.item.file.transform.PassThroughLineAggregator;
3131
import org.springframework.core.io.FileSystemResource;
3232
import org.springframework.core.io.Resource;
33+
import org.springframework.core.io.WritableResource;
3334
import org.springframework.test.util.ReflectionTestUtils;
3435

3536
import static org.junit.Assert.assertEquals;
@@ -55,7 +56,7 @@ public void testMissingLineAggregator() {
5556

5657
@Test(expected = IllegalStateException.class)
5758
public void testMultipleLineAggregators() throws IOException {
58-
Resource output = new FileSystemResource(File.createTempFile("foo", "txt"));
59+
WritableResource output = new FileSystemResource(File.createTempFile("foo", "txt"));
5960

6061
new FlatFileItemWriterBuilder<Foo>()
6162
.name("itemWriter")
@@ -72,7 +73,7 @@ public void testMultipleLineAggregators() throws IOException {
7273
@Test
7374
public void test() throws Exception {
7475

75-
Resource output = new FileSystemResource(File.createTempFile("foo", "txt"));
76+
WritableResource output = new FileSystemResource(File.createTempFile("foo", "txt"));
7677

7778
FlatFileItemWriter<Foo> writer = new FlatFileItemWriterBuilder<Foo>()
7879
.name("foo")
@@ -98,7 +99,7 @@ public void test() throws Exception {
9899
@Test
99100
public void testDelimitedOutputWithDefaultDelimiter() throws Exception {
100101

101-
Resource output = new FileSystemResource(File.createTempFile("foo", "txt"));
102+
WritableResource output = new FileSystemResource(File.createTempFile("foo", "txt"));
102103

103104
FlatFileItemWriter<Foo> writer = new FlatFileItemWriterBuilder<Foo>()
104105
.name("foo")
@@ -125,7 +126,7 @@ public void testDelimitedOutputWithDefaultDelimiter() throws Exception {
125126
@Test
126127
public void testDelimitedOutputWithEmptyDelimiter() throws Exception {
127128

128-
Resource output = new FileSystemResource(File.createTempFile("foo", "txt"));
129+
WritableResource output = new FileSystemResource(File.createTempFile("foo", "txt"));
129130

130131
FlatFileItemWriter<Foo> writer = new FlatFileItemWriterBuilder<Foo>()
131132
.name("foo")
@@ -153,7 +154,7 @@ public void testDelimitedOutputWithEmptyDelimiter() throws Exception {
153154
@Test
154155
public void testDelimitedOutputWithDefaultFieldExtractor() throws Exception {
155156

156-
Resource output = new FileSystemResource(File.createTempFile("foo", "txt"));
157+
WritableResource output = new FileSystemResource(File.createTempFile("foo", "txt"));
157158

158159
FlatFileItemWriter<Foo> writer = new FlatFileItemWriterBuilder<Foo>()
159160
.name("foo")
@@ -181,7 +182,7 @@ public void testDelimitedOutputWithDefaultFieldExtractor() throws Exception {
181182
@Test
182183
public void testDelimitedOutputWithCustomFieldExtractor() throws Exception {
183184

184-
Resource output = new FileSystemResource(File.createTempFile("foo", "txt"));
185+
WritableResource output = new FileSystemResource(File.createTempFile("foo", "txt"));
185186

186187
FlatFileItemWriter<Foo> writer = new FlatFileItemWriterBuilder<Foo>()
187188
.name("foo")
@@ -209,7 +210,7 @@ public void testDelimitedOutputWithCustomFieldExtractor() throws Exception {
209210
@Test
210211
public void testFormattedOutputWithDefaultFieldExtractor() throws Exception {
211212

212-
Resource output = new FileSystemResource(File.createTempFile("foo", "txt"));
213+
WritableResource output = new FileSystemResource(File.createTempFile("foo", "txt"));
213214

214215
FlatFileItemWriter<Foo> writer = new FlatFileItemWriterBuilder<Foo>()
215216
.name("foo")
@@ -237,7 +238,7 @@ public void testFormattedOutputWithDefaultFieldExtractor() throws Exception {
237238
@Test
238239
public void testFormattedOutputWithCustomFieldExtractor() throws Exception {
239240

240-
Resource output = new FileSystemResource(File.createTempFile("foo", "txt"));
241+
WritableResource output = new FileSystemResource(File.createTempFile("foo", "txt"));
241242

242243
FlatFileItemWriter<Foo> writer = new FlatFileItemWriterBuilder<Foo>()
243244
.name("foo")
@@ -265,7 +266,7 @@ public void testFormattedOutputWithCustomFieldExtractor() throws Exception {
265266
@Test
266267
public void testFlags() throws Exception {
267268

268-
Resource output = new FileSystemResource(File.createTempFile("foo", "txt"));
269+
WritableResource output = new FileSystemResource(File.createTempFile("foo", "txt"));
269270

270271
String encoding = Charset.defaultCharset().name();
271272

@@ -287,7 +288,7 @@ public void testFlags() throws Exception {
287288
@Test
288289
public void testFlagsWithEncoding() throws Exception {
289290

290-
Resource output = new FileSystemResource(File.createTempFile("foo", "txt"));
291+
WritableResource output = new FileSystemResource(File.createTempFile("foo", "txt"));
291292
String encoding = "UTF-8";
292293
FlatFileItemWriter<Foo> writer = new FlatFileItemWriterBuilder<Foo>()
293294
.name("foo")

0 commit comments

Comments
 (0)