Skip to content

Commit e61065a

Browse files
hpoettkerfmbenhassine
authored andcommitted
Reduce use of deprecated APIs
1 parent 2b53657 commit e61065a

File tree

7 files changed

+50
-43
lines changed

7 files changed

+50
-43
lines changed

spring-batch-core/src/main/java/org/springframework/batch/core/repository/dao/DefaultExecutionContextSerializer.java

+22-12
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2022 the original author or authors.
2+
* Copyright 2012-2023 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.
@@ -15,16 +15,18 @@
1515
*/
1616
package org.springframework.batch.core.repository.dao;
1717

18+
import java.io.ByteArrayOutputStream;
1819
import java.io.IOException;
1920
import java.io.InputStream;
21+
import java.io.ObjectInputStream;
22+
import java.io.ObjectOutputStream;
2023
import java.io.OutputStream;
2124
import java.io.Serializable;
25+
import java.util.Base64;
2226
import java.util.Map;
2327

2428
import org.springframework.batch.core.repository.ExecutionContextSerializer;
2529
import org.springframework.util.Assert;
26-
import org.springframework.util.Base64Utils;
27-
import org.springframework.util.SerializationUtils;
2830

2931
/**
3032
* An implementation of the {@link ExecutionContextSerializer} that produces/consumes
@@ -34,7 +36,6 @@
3436
* @author Mahmoud Ben Hassine
3537
* @since 2.2
3638
*/
37-
@SuppressWarnings("rawtypes")
3839
public class DefaultExecutionContextSerializer implements ExecutionContextSerializer {
3940

4041
/**
@@ -45,7 +46,6 @@ public class DefaultExecutionContextSerializer implements ExecutionContextSerial
4546
* written.
4647
*/
4748
@Override
48-
@SuppressWarnings("unchecked")
4949
public void serialize(Map<String, Object> context, OutputStream out) throws IOException {
5050
Assert.notNull(context, "context is required");
5151
Assert.notNull(out, "OutputStream is required");
@@ -58,9 +58,12 @@ public void serialize(Map<String, Object> context, OutputStream out) throws IOEx
5858
+ value.getClass().getName() + "] must be an instance of " + Serializable.class);
5959
}
6060
}
61-
byte[] serializedContext = SerializationUtils.serialize(context);
62-
String base64EncodedContext = Base64Utils.encodeToString(serializedContext);
63-
out.write(base64EncodedContext.getBytes());
61+
var byteArrayOutputStream = new ByteArrayOutputStream(1024);
62+
var encodingStream = Base64.getEncoder().wrap(byteArrayOutputStream);
63+
try (var objectOutputStream = new ObjectOutputStream(encodingStream)) {
64+
objectOutputStream.writeObject(context);
65+
}
66+
out.write(byteArrayOutputStream.toByteArray());
6467
}
6568

6669
/**
@@ -72,10 +75,17 @@ public void serialize(Map<String, Object> context, OutputStream out) throws IOEx
7275
@SuppressWarnings("unchecked")
7376
@Override
7477
public Map<String, Object> deserialize(InputStream inputStream) throws IOException {
75-
String base64EncodedContext = new String(inputStream.readAllBytes());
76-
byte[] decodedContext = Base64Utils.decodeFromString(base64EncodedContext);
77-
// FIXME use replacement of the following SerializationUtils.deserialize
78-
return (Map<String, Object>) SerializationUtils.deserialize(decodedContext);
78+
var decodingStream = Base64.getDecoder().wrap(inputStream);
79+
try {
80+
var objectInputStream = new ObjectInputStream(decodingStream);
81+
return (Map<String, Object>) objectInputStream.readObject();
82+
}
83+
catch (IOException ex) {
84+
throw new IllegalArgumentException("Failed to deserialize object", ex);
85+
}
86+
catch (ClassNotFoundException ex) {
87+
throw new IllegalStateException("Failed to deserialize object type", ex);
88+
}
7989
}
8090

8191
}

spring-batch-core/src/main/java/org/springframework/batch/core/step/job/DefaultJobParametersExtractor.java

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2006-2022 the original author or authors.
2+
* Copyright 2006-2023 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.
@@ -16,7 +16,6 @@
1616
package org.springframework.batch.core.step.job;
1717

1818
import java.util.Arrays;
19-
import java.util.Date;
2019
import java.util.HashSet;
2120
import java.util.Map;
2221
import java.util.Properties;
@@ -72,7 +71,7 @@ public JobParameters getJobParameters(Job job, StepExecution stepExecution) {
7271
ExecutionContext executionContext = stepExecution.getExecutionContext();
7372
if (useAllParentParameters) {
7473
for (String key : jobParameters.keySet()) {
75-
builder.addParameter(key, jobParameters.get(key));
74+
builder.addJobParameter(key, jobParameters.get(key));
7675
}
7776
}
7877
Properties properties = new Properties();

spring-batch-core/src/test/java/org/springframework/batch/core/configuration/xml/PartitionStepParserTests.java

+4-8
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2006-2022 the original author or authors.
2+
* Copyright 2006-2023 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.
@@ -45,7 +45,6 @@
4545
import org.springframework.context.ApplicationContext;
4646
import org.springframework.context.ApplicationContextAware;
4747
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
48-
import org.springframework.util.ReflectionUtils;
4948

5049
/**
5150
* @author Dave Syer
@@ -97,13 +96,10 @@ void setUp() {
9796
}
9897

9998
@SuppressWarnings("unchecked")
100-
private <T> T accessPrivateField(Object o, String fieldName) {
101-
Field field = ReflectionUtils.findField(o.getClass(), fieldName);
102-
boolean previouslyAccessibleValue = field.isAccessible();
99+
private <T> T accessPrivateField(Object o, String fieldName) throws ReflectiveOperationException {
100+
Field field = o.getClass().getDeclaredField(fieldName);
103101
field.setAccessible(true);
104-
T val = (T) ReflectionUtils.getField(field, o);
105-
field.setAccessible(previouslyAccessibleValue);
106-
return val;
102+
return (T) field.get(o);
107103
}
108104

109105
@Test

spring-batch-core/src/test/java/org/springframework/batch/core/test/concurrent/ConcurrentTransactionTests.java

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2015-2022 the original author or authors.
2+
* Copyright 2015-2023 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.
@@ -48,7 +48,6 @@
4848
import org.springframework.core.io.DefaultResourceLoader;
4949
import org.springframework.core.io.ResourceLoader;
5050
import org.springframework.core.task.SimpleAsyncTaskExecutor;
51-
import org.springframework.core.task.SyncTaskExecutor;
5251
import org.springframework.core.task.TaskExecutor;
5352
import org.springframework.jdbc.datasource.embedded.ConnectionProperties;
5453
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseConfigurer;
@@ -109,7 +108,7 @@ public RepeatStatus execute(StepContribution contribution, ChunkContext chunkCon
109108
return RepeatStatus.FINISHED;
110109
}
111110
}, transactionManager).build())
112-
.next(new StepBuilder("flow.step2").repository(jobRepository).tasklet(new Tasklet() {
111+
.next(new StepBuilder("flow.step2", jobRepository).tasklet(new Tasklet() {
113112
@Nullable
114113
@Override
115114
public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext)

spring-batch-infrastructure/src/test/java/org/springframework/batch/item/xml/StaxTestUtils.java

+1-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2015-2018 the original author or authors.
2+
* Copyright 2015-2023 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.
@@ -29,19 +29,15 @@ public final class StaxTestUtils {
2929

3030
public static XMLEventWriter getXmlEventWriter(Result r) throws Exception {
3131
Method m = r.getClass().getDeclaredMethod("getXMLEventWriter");
32-
boolean accessible = m.isAccessible();
3332
m.setAccessible(true);
3433
Object result = m.invoke(r);
35-
m.setAccessible(accessible);
3634
return (XMLEventWriter) result;
3735
}
3836

3937
public static XMLEventReader getXmlEventReader(Source s) throws Exception {
4038
Method m = s.getClass().getDeclaredMethod("getXMLEventReader");
41-
boolean accessible = m.isAccessible();
4239
m.setAccessible(true);
4340
Object result = m.invoke(s);
44-
m.setAccessible(accessible);
4541
return (XMLEventReader) result;
4642
}
4743

spring-batch-integration/src/test/java/org/springframework/batch/integration/chunk/RemoteChunkingManagerStepBuilderTests.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2018-2022 the original author or authors.
2+
* Copyright 2018-2023 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.
@@ -265,8 +265,8 @@ else if (count < items.size()) {
265265
}
266266
};
267267

268-
TaskletStep taskletStep = new RemoteChunkingManagerStepBuilder<String, String>("step").reader(itemReader)
269-
.readerIsTransactionalQueue().processor(itemProcessor).repository(this.jobRepository)
268+
TaskletStep taskletStep = new RemoteChunkingManagerStepBuilder<String, String>("step", this.jobRepository)
269+
.reader(itemReader).readerIsTransactionalQueue().processor(itemProcessor)
270270
.transactionManager(this.transactionManager).transactionAttribute(transactionAttribute)
271271
.inputChannel(this.inputChannel).outputChannel(this.outputChannel).listener(annotatedListener)
272272
.listener(skipListener).listener(chunkListener).listener(stepExecutionListener)

spring-batch-samples/src/main/java/org/springframework/batch/sample/common/StagingItemReader.java

+16-9
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2006-2022 the original author or authors.
2+
* Copyright 2006-2023 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.
@@ -16,6 +16,8 @@
1616

1717
package org.springframework.batch.sample.common;
1818

19+
import java.io.InputStream;
20+
import java.io.ObjectInputStream;
1921
import java.sql.ResultSet;
2022
import java.sql.SQLException;
2123
import java.util.Iterator;
@@ -38,7 +40,6 @@
3840
import org.springframework.jdbc.core.RowMapper;
3941
import org.springframework.lang.Nullable;
4042
import org.springframework.util.Assert;
41-
import org.springframework.util.SerializationUtils;
4243

4344
/**
4445
* Thread-safe database {@link ItemReader} implementing the process indicator pattern.
@@ -117,17 +118,23 @@ public ProcessIndicatorItemWrapper<T> read() {
117118
}
118119
@SuppressWarnings("unchecked")
119120
T result = (T) jdbcTemplate.queryForObject("SELECT VALUE FROM BATCH_STAGING WHERE ID=?",
120-
new RowMapper<Object>() {
121-
@Override
122-
public Object mapRow(ResultSet rs, int rowNum) throws SQLException {
123-
byte[] blob = rs.getBytes(1);
124-
return SerializationUtils.deserialize(blob);
125-
}
126-
}, id);
121+
(rs, rowNum) -> deserialize(rs.getBinaryStream(1)), id);
127122

128123
return new ProcessIndicatorItemWrapper<>(id, result);
129124
}
130125

126+
private static Object deserialize(InputStream inputStream) {
127+
if (inputStream == null) {
128+
return null;
129+
}
130+
try (var objectInputStream = new ObjectInputStream(inputStream)) {
131+
return objectInputStream.readObject();
132+
}
133+
catch (Exception e) {
134+
throw new IllegalArgumentException("Failed to deserialize object", e);
135+
}
136+
}
137+
131138
@Nullable
132139
@Override
133140
public ExitStatus afterStep(StepExecution stepExecution) {

0 commit comments

Comments
 (0)