Skip to content

Commit 9315a3b

Browse files
committed
Make charset configurable in JdbcExecutionContextDao
Issue #795
1 parent df8dac1 commit 9315a3b

File tree

3 files changed

+55
-5
lines changed

3 files changed

+55
-5
lines changed

spring-batch-core/src/main/java/org/springframework/batch/core/explore/support/JobExplorerFactoryBean.java

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2021 the original author or authors.
2+
* Copyright 2002-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.
@@ -16,6 +16,9 @@
1616

1717
package org.springframework.batch.core.explore.support;
1818

19+
import java.nio.charset.Charset;
20+
import java.nio.charset.StandardCharsets;
21+
1922
import javax.sql.DataSource;
2023

2124
import org.springframework.batch.core.explore.JobExplorer;
@@ -38,6 +41,7 @@
3841
import org.springframework.jdbc.support.incrementer.AbstractDataFieldMaxValueIncrementer;
3942
import org.springframework.jdbc.support.incrementer.DataFieldMaxValueIncrementer;
4043
import org.springframework.jdbc.support.lob.LobHandler;
44+
import org.springframework.lang.NonNull;
4145
import org.springframework.util.Assert;
4246

4347
/**
@@ -69,6 +73,8 @@ protected long getNextKey() {
6973

7074
private ExecutionContextSerializer serializer;
7175

76+
private Charset charset = StandardCharsets.UTF_8;
77+
7278
/**
7379
* A custom implementation of the {@link ExecutionContextSerializer}.
7480
* The default, if not injected, is the {@link Jackson2ExecutionContextStringSerializer}.
@@ -118,6 +124,18 @@ public void setLobHandler(LobHandler lobHandler) {
118124
this.lobHandler = lobHandler;
119125
}
120126

127+
/**
128+
* Set the {@link Charset} to use when deserializing the execution context.
129+
* Defaults to "UTF-8". Must not be {@code null}.
130+
* @param charset to use when deserializing the execution context.
131+
* @see JdbcExecutionContextDao#setCharset(Charset)
132+
* @since 5.0
133+
*/
134+
public void setCharset(@NonNull Charset charset) {
135+
Assert.notNull(charset, "Charset must not be null");
136+
this.charset = charset;
137+
}
138+
121139
@Override
122140
public void afterPropertiesSet() throws Exception {
123141

@@ -145,6 +163,7 @@ protected ExecutionContextDao createExecutionContextDao() throws Exception {
145163
dao.setLobHandler(lobHandler);
146164
dao.setTablePrefix(tablePrefix);
147165
dao.setSerializer(serializer);
166+
dao.setCharset(charset);
148167
dao.afterPropertiesSet();
149168
return dao;
150169
}

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

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import java.io.ByteArrayInputStream;
2020
import java.io.ByteArrayOutputStream;
2121
import java.io.IOException;
22+
import java.nio.charset.Charset;
2223
import java.nio.charset.StandardCharsets;
2324
import java.sql.PreparedStatement;
2425
import java.sql.ResultSet;
@@ -40,6 +41,7 @@
4041
import org.springframework.jdbc.core.RowMapper;
4142
import org.springframework.jdbc.support.lob.DefaultLobHandler;
4243
import org.springframework.jdbc.support.lob.LobHandler;
44+
import org.springframework.lang.NonNull;
4345
import org.springframework.util.Assert;
4446

4547
/**
@@ -75,7 +77,7 @@ public class JdbcExecutionContextDao extends AbstractJdbcBatchMetadataDao implem
7577
private static final String UPDATE_STEP_EXECUTION_CONTEXT = "UPDATE %PREFIX%STEP_EXECUTION_CONTEXT "
7678
+ "SET SHORT_CONTEXT = ?, SERIALIZED_CONTEXT = ? " + "WHERE STEP_EXECUTION_ID = ?";
7779

78-
private static final String CHARSET_NAME = StandardCharsets.UTF_8.name();
80+
private Charset charset = StandardCharsets.UTF_8;
7981

8082
private static final int DEFAULT_MAX_VARCHAR_LENGTH = 2500;
8183

@@ -109,6 +111,17 @@ public void setShortContextLength(int shortContextLength) {
109111
this.shortContextLength = shortContextLength;
110112
}
111113

114+
/**
115+
* Set the {@link Charset} to use when serializing/deserializing the execution context.
116+
* Must not be {@code null}. Defaults to "UTF-8".
117+
* @param charset to use when serializing/deserializing the execution context.
118+
* @since 5.0
119+
*/
120+
public void setCharset(@NonNull Charset charset) {
121+
Assert.notNull(charset, "Charset must not be null");
122+
this.charset = charset;
123+
}
124+
112125
@Override
113126
public ExecutionContext getExecutionContext(JobExecution jobExecution) {
114127
Long executionId = jobExecution.getId();
@@ -303,7 +316,7 @@ private String serializeContext(ExecutionContext ctx) {
303316

304317
try {
305318
serializer.serialize(m, out);
306-
results = new String(out.toByteArray(), CHARSET_NAME);
319+
results = new String(out.toByteArray(), charset.name());
307320
}
308321
catch (IOException ioe) {
309322
throw new IllegalArgumentException("Could not serialize the execution context", ioe);
@@ -324,7 +337,7 @@ public ExecutionContext mapRow(ResultSet rs, int i) throws SQLException {
324337

325338
Map<String, Object> map;
326339
try {
327-
ByteArrayInputStream in = new ByteArrayInputStream(serializedContext.getBytes(CHARSET_NAME));
340+
ByteArrayInputStream in = new ByteArrayInputStream(serializedContext.getBytes(charset.name()));
328341
map = serializer.deserialize(in);
329342
}
330343
catch (IOException ioe) {

spring-batch-core/src/main/java/org/springframework/batch/core/repository/support/JobRepositoryFactoryBean.java

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2021 the original author or authors.
2+
* Copyright 2002-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.
@@ -17,6 +17,8 @@
1717
package org.springframework.batch.core.repository.support;
1818

1919
import java.lang.reflect.Field;
20+
import java.nio.charset.Charset;
21+
import java.nio.charset.StandardCharsets;
2022
import java.sql.Types;
2123
import javax.sql.DataSource;
2224

@@ -43,6 +45,7 @@
4345
import org.springframework.jdbc.core.JdbcTemplate;
4446
import org.springframework.jdbc.support.lob.DefaultLobHandler;
4547
import org.springframework.jdbc.support.lob.LobHandler;
48+
import org.springframework.lang.NonNull;
4649
import org.springframework.util.Assert;
4750
import org.springframework.util.StringUtils;
4851

@@ -82,6 +85,8 @@ public class JobRepositoryFactoryBean extends AbstractJobRepositoryFactoryBean i
8285

8386
private Integer lobType;
8487

88+
private Charset charset = StandardCharsets.UTF_8;
89+
8590
/**
8691
* @param type a value from the {@link java.sql.Types} class to indicate the type to use for a CLOB
8792
*/
@@ -167,6 +172,18 @@ public void setIncrementerFactory(DataFieldMaxValueIncrementerFactory incremente
167172
this.incrementerFactory = incrementerFactory;
168173
}
169174

175+
/**
176+
* Set the {@link Charset} to use when serializing/deserializing the execution context.
177+
* Defaults to "UTF-8". Must not be {@code null}.
178+
* @param charset to use when serializing/deserializing the execution context.
179+
* @see JdbcExecutionContextDao#setCharset(Charset)
180+
* @since 5.0
181+
*/
182+
public void setCharset(@NonNull Charset charset) {
183+
Assert.notNull(charset, "Charset must not be null");
184+
this.charset = charset;
185+
}
186+
170187
@Override
171188
public void afterPropertiesSet() throws Exception {
172189

@@ -251,6 +268,7 @@ protected ExecutionContextDao createExecutionContextDao() throws Exception {
251268
dao.setTablePrefix(tablePrefix);
252269
dao.setClobTypeToUse(determineClobTypeToUse(this.databaseType));
253270
dao.setSerializer(serializer);
271+
dao.setCharset(charset);
254272

255273
if (lobHandler != null) {
256274
dao.setLobHandler(lobHandler);

0 commit comments

Comments
 (0)