From 06e6bdbc98799bb100ba6fedd4e6ba876fd1be07 Mon Sep 17 00:00:00 2001 From: sjh836 Date: Wed, 15 Sep 2021 03:52:32 +0900 Subject: [PATCH] Add method in ExecutionContext to expose the internal map as read-only Resolves #4004 --- .../batch/core/scope/context/JobContext.java | 6 +----- .../batch/core/scope/context/StepContext.java | 12 ++---------- .../batch/item/ExecutionContext.java | 18 +++++++++++++----- 3 files changed, 16 insertions(+), 20 deletions(-) diff --git a/spring-batch-core/src/main/java/org/springframework/batch/core/scope/context/JobContext.java b/spring-batch-core/src/main/java/org/springframework/batch/core/scope/context/JobContext.java index 769cde72d..25e51964c 100644 --- a/spring-batch-core/src/main/java/org/springframework/batch/core/scope/context/JobContext.java +++ b/spring-batch-core/src/main/java/org/springframework/batch/core/scope/context/JobContext.java @@ -82,11 +82,7 @@ public class JobContext extends SynchronizedAttributeAccessor { * @return a map containing the items from the job {@link ExecutionContext} */ public Map getJobExecutionContext() { - Map result = new HashMap<>(); - for (Entry entry : jobExecution.getExecutionContext().entrySet()) { - result.put(entry.getKey(), entry.getValue()); - } - return Collections.unmodifiableMap(result); + return jobExecution.getExecutionContext().toMap(); } /** diff --git a/spring-batch-core/src/main/java/org/springframework/batch/core/scope/context/StepContext.java b/spring-batch-core/src/main/java/org/springframework/batch/core/scope/context/StepContext.java index 63d7a3906..a076b5bee 100644 --- a/spring-batch-core/src/main/java/org/springframework/batch/core/scope/context/StepContext.java +++ b/spring-batch-core/src/main/java/org/springframework/batch/core/scope/context/StepContext.java @@ -111,22 +111,14 @@ public class StepContext extends SynchronizedAttributeAccessor { * @return a map containing the items from the step {@link ExecutionContext} */ public Map getStepExecutionContext() { - Map result = new HashMap<>(); - for (Entry entry : stepExecution.getExecutionContext().entrySet()) { - result.put(entry.getKey(), entry.getValue()); - } - return Collections.unmodifiableMap(result); + return stepExecution.getExecutionContext().toMap(); } /** * @return a map containing the items from the job {@link ExecutionContext} */ public Map getJobExecutionContext() { - Map result = new HashMap<>(); - for (Entry entry : stepExecution.getJobExecution().getExecutionContext().entrySet()) { - result.put(entry.getKey(), entry.getValue()); - } - return Collections.unmodifiableMap(result); + return stepExecution.getJobExecution().getExecutionContext().toMap(); } /** diff --git a/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/ExecutionContext.java b/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/ExecutionContext.java index bb3ff312d..a7b7479e7 100644 --- a/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/ExecutionContext.java +++ b/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/ExecutionContext.java @@ -17,6 +17,7 @@ package org.springframework.batch.item; import java.io.Serializable; +import java.util.Collections; import java.util.Map; import java.util.Map.Entry; import java.util.Set; @@ -69,9 +70,7 @@ public class ExecutionContext implements Serializable { if (executionContext == null) { return; } - for (Entry entry : executionContext.entrySet()) { - this.map.put(entry.getKey(), entry.getValue()); - } + this.map.putAll(executionContext.toMap()); } /** @@ -293,11 +292,20 @@ public class ExecutionContext implements Serializable { /** * Returns the entry set containing the contents of this context. - * @return A set representing the contents of the context + * @return An unmodifiable set representing the contents of the context * @see java.util.Map#entrySet() */ public Set> entrySet() { - return this.map.entrySet(); + return Collections.unmodifiableSet(this.map.entrySet()); + } + + /** + * Returns the internal map as read-only. + * @return An unmodifiable map containing all contents. + * @see java.util.Map + */ + public Map toMap() { + return Collections.unmodifiableMap(this.map); } /**