Skip to content

Commit 26e42af

Browse files
committed
Change DefaultExecutionContextSerializer to produce Base64
Related to spring-projects#3983
1 parent 08f8c75 commit 26e42af

File tree

1 file changed

+14
-6
lines changed

1 file changed

+14
-6
lines changed

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

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2017 the original author or authors.
2+
* Copyright 2012-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,24 +19,29 @@
1919
import java.io.InputStream;
2020
import java.io.OutputStream;
2121
import java.io.Serializable;
22+
import java.util.Base64;
2223
import java.util.Map;
24+
import java.util.Objects;
2325

2426
import org.springframework.batch.core.repository.ExecutionContextSerializer;
2527
import org.springframework.core.serializer.DefaultDeserializer;
2628
import org.springframework.core.serializer.DefaultSerializer;
2729
import org.springframework.core.serializer.Deserializer;
2830
import org.springframework.core.serializer.Serializer;
2931
import org.springframework.util.Assert;
32+
import org.springframework.util.Base64Utils;
33+
import org.springframework.util.ObjectUtils;
34+
import org.springframework.util.SerializationUtils;
3035

3136
/**
3237
* An implementation of the {@link ExecutionContextSerializer} using the default
3338
* serialization implementations from Spring ({@link DefaultSerializer} and
3439
* {@link DefaultDeserializer}).
3540
*
3641
* @author Michael Minella
42+
* @author Mahmoud Ben Hassine
3743
* @since 2.2
3844
*/
39-
@SuppressWarnings("rawtypes")
4045
public class DefaultExecutionContextSerializer implements ExecutionContextSerializer {
4146

4247
private Serializer serializer = new DefaultSerializer();
@@ -51,7 +56,6 @@ public class DefaultExecutionContextSerializer implements ExecutionContextSerial
5156
* written.
5257
*/
5358
@Override
54-
@SuppressWarnings("unchecked")
5559
public void serialize(Map<String, Object> context, OutputStream out) throws IOException {
5660
Assert.notNull(context, "context is required");
5761
Assert.notNull(out, "OutputStream is required");
@@ -64,7 +68,9 @@ public void serialize(Map<String, Object> context, OutputStream out) throws IOEx
6468
+ value.getClass().getName() + "] must be an instance of " + Serializable.class);
6569
}
6670
}
67-
serializer.serialize(context, out);
71+
byte[] serializedContext = SerializationUtils.serialize(context);
72+
String base64EncodedContext = Base64Utils.encodeToString(serializedContext);
73+
serializer.serialize(base64EncodedContext, out);
6874
}
6975

7076
/**
@@ -73,10 +79,12 @@ public void serialize(Map<String, Object> context, OutputStream out) throws IOEx
7379
* deserialized.
7480
* @return the object serialized in the provided {@link InputStream}
7581
*/
76-
@SuppressWarnings("unchecked")
7782
@Override
7883
public Map<String, Object> deserialize(InputStream inputStream) throws IOException {
79-
return (Map<String, Object>) deserializer.deserialize(inputStream);
84+
String base64EncodedContext = (String) deserializer.deserialize(inputStream);
85+
byte[] decodedContext = Base64Utils.decodeFromString(base64EncodedContext);
86+
// FIXME use replacement of the following SerializationUtils.deserialize
87+
return (Map<String, Object>) SerializationUtils.deserialize(decodedContext);
8088
}
8189

8290
}

0 commit comments

Comments
 (0)