Skip to content

Commit c65e0f2

Browse files
committed
Change DefaultExecutionContextSerializer to produce Base64 content
Resolves #4122 Related to #3983
1 parent e21f56c commit c65e0f2

File tree

2 files changed

+14
-17
lines changed

2 files changed

+14
-17
lines changed

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

Lines changed: 13 additions & 14 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.
@@ -22,27 +22,21 @@
2222
import java.util.Map;
2323

2424
import org.springframework.batch.core.repository.ExecutionContextSerializer;
25-
import org.springframework.core.serializer.DefaultDeserializer;
26-
import org.springframework.core.serializer.DefaultSerializer;
27-
import org.springframework.core.serializer.Deserializer;
28-
import org.springframework.core.serializer.Serializer;
2925
import org.springframework.util.Assert;
26+
import org.springframework.util.Base64Utils;
27+
import org.springframework.util.SerializationUtils;
3028

3129
/**
32-
* An implementation of the {@link ExecutionContextSerializer} using the default
33-
* serialization implementations from Spring ({@link DefaultSerializer} and
34-
* {@link DefaultDeserializer}).
30+
* An implementation of the {@link ExecutionContextSerializer} that produces/consumes
31+
* Base64 content.
3532
*
3633
* @author Michael Minella
34+
* @author Mahmoud Ben Hassine
3735
* @since 2.2
3836
*/
3937
@SuppressWarnings("rawtypes")
4038
public class DefaultExecutionContextSerializer implements ExecutionContextSerializer {
4139

42-
private Serializer serializer = new DefaultSerializer();
43-
44-
private Deserializer deserializer = new DefaultDeserializer();
45-
4640
/**
4741
* Serializes an execution context to the provided {@link OutputStream}. The stream is
4842
* not closed prior to it's return.
@@ -64,7 +58,9 @@ public void serialize(Map<String, Object> context, OutputStream out) throws IOEx
6458
+ value.getClass().getName() + "] must be an instance of " + Serializable.class);
6559
}
6660
}
67-
serializer.serialize(context, out);
61+
byte[] serializedContext = SerializationUtils.serialize(context);
62+
String base64EncodedContext = Base64Utils.encodeToString(serializedContext);
63+
out.write(base64EncodedContext.getBytes());
6864
}
6965

7066
/**
@@ -76,7 +72,10 @@ public void serialize(Map<String, Object> context, OutputStream out) throws IOEx
7672
@SuppressWarnings("unchecked")
7773
@Override
7874
public Map<String, Object> deserialize(InputStream inputStream) throws IOException {
79-
return (Map<String, Object>) deserializer.deserialize(inputStream);
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);
8079
}
8180

8281
}

spring-batch-core/src/test/java/org/springframework/batch/core/repository/dao/AbstractExecutionContextSerializerTests.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -180,9 +180,7 @@ protected Map<String, Object> serializationRoundTrip(Map<String, Object> m1) thr
180180
ByteArrayOutputStream out = new ByteArrayOutputStream();
181181
getSerializer().serialize(m1, out);
182182

183-
byte[] buf = out.toByteArray();
184-
System.out.println(new String(buf));
185-
InputStream in = new ByteArrayInputStream(buf);
183+
InputStream in = new ByteArrayInputStream(out.toByteArray());
186184
Map<String, Object> m2 = getSerializer().deserialize(in);
187185
return m2;
188186
}

0 commit comments

Comments
 (0)