Skip to content

Commit 06e6bdb

Browse files
sjh836fmbenhassine
authored andcommitted
Add method in ExecutionContext to expose the internal map as read-only
Resolves #4004
1 parent d9bf57e commit 06e6bdb

File tree

3 files changed

+16
-20
lines changed

3 files changed

+16
-20
lines changed

spring-batch-core/src/main/java/org/springframework/batch/core/scope/context/JobContext.java

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -82,11 +82,7 @@ public Properties getSystemProperties() {
8282
* @return a map containing the items from the job {@link ExecutionContext}
8383
*/
8484
public Map<String, Object> getJobExecutionContext() {
85-
Map<String, Object> result = new HashMap<>();
86-
for (Entry<String, Object> entry : jobExecution.getExecutionContext().entrySet()) {
87-
result.put(entry.getKey(), entry.getValue());
88-
}
89-
return Collections.unmodifiableMap(result);
85+
return jobExecution.getExecutionContext().toMap();
9086
}
9187

9288
/**

spring-batch-core/src/main/java/org/springframework/batch/core/scope/context/StepContext.java

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -111,22 +111,14 @@ public Properties getSystemProperties() {
111111
* @return a map containing the items from the step {@link ExecutionContext}
112112
*/
113113
public Map<String, Object> getStepExecutionContext() {
114-
Map<String, Object> result = new HashMap<>();
115-
for (Entry<String, Object> entry : stepExecution.getExecutionContext().entrySet()) {
116-
result.put(entry.getKey(), entry.getValue());
117-
}
118-
return Collections.unmodifiableMap(result);
114+
return stepExecution.getExecutionContext().toMap();
119115
}
120116

121117
/**
122118
* @return a map containing the items from the job {@link ExecutionContext}
123119
*/
124120
public Map<String, Object> getJobExecutionContext() {
125-
Map<String, Object> result = new HashMap<>();
126-
for (Entry<String, Object> entry : stepExecution.getJobExecution().getExecutionContext().entrySet()) {
127-
result.put(entry.getKey(), entry.getValue());
128-
}
129-
return Collections.unmodifiableMap(result);
121+
return stepExecution.getJobExecution().getExecutionContext().toMap();
130122
}
131123

132124
/**

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

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package org.springframework.batch.item;
1818

1919
import java.io.Serializable;
20+
import java.util.Collections;
2021
import java.util.Map;
2122
import java.util.Map.Entry;
2223
import java.util.Set;
@@ -69,9 +70,7 @@ public ExecutionContext(ExecutionContext executionContext) {
6970
if (executionContext == null) {
7071
return;
7172
}
72-
for (Entry<String, Object> entry : executionContext.entrySet()) {
73-
this.map.put(entry.getKey(), entry.getValue());
74-
}
73+
this.map.putAll(executionContext.toMap());
7574
}
7675

7776
/**
@@ -293,11 +292,20 @@ public void clearDirtyFlag() {
293292

294293
/**
295294
* Returns the entry set containing the contents of this context.
296-
* @return A set representing the contents of the context
295+
* @return An unmodifiable set representing the contents of the context
297296
* @see java.util.Map#entrySet()
298297
*/
299298
public Set<Entry<String, Object>> entrySet() {
300-
return this.map.entrySet();
299+
return Collections.unmodifiableSet(this.map.entrySet());
300+
}
301+
302+
/**
303+
* Returns the internal map as read-only.
304+
* @return An unmodifiable map containing all contents.
305+
* @see java.util.Map
306+
*/
307+
public Map<String, Object> toMap() {
308+
return Collections.unmodifiableMap(this.map);
301309
}
302310

303311
/**

0 commit comments

Comments
 (0)