Skip to content

Commit

Permalink
Make charset configurable in JdbcExecutionContextDao
Browse files Browse the repository at this point in the history
  • Loading branch information
fmbenhassine committed May 17, 2022
1 parent df8dac1 commit 9315a3b
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2021 the original author or authors.
* Copyright 2002-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -16,6 +16,9 @@

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

import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;

import javax.sql.DataSource;

import org.springframework.batch.core.explore.JobExplorer;
Expand All @@ -38,6 +41,7 @@
import org.springframework.jdbc.support.incrementer.AbstractDataFieldMaxValueIncrementer;
import org.springframework.jdbc.support.incrementer.DataFieldMaxValueIncrementer;
import org.springframework.jdbc.support.lob.LobHandler;
import org.springframework.lang.NonNull;
import org.springframework.util.Assert;

/**
Expand Down Expand Up @@ -69,6 +73,8 @@ protected long getNextKey() {

private ExecutionContextSerializer serializer;

private Charset charset = StandardCharsets.UTF_8;

/**
* A custom implementation of the {@link ExecutionContextSerializer}.
* The default, if not injected, is the {@link Jackson2ExecutionContextStringSerializer}.
Expand Down Expand Up @@ -118,6 +124,18 @@ public void setLobHandler(LobHandler lobHandler) {
this.lobHandler = lobHandler;
}

/**
* Set the {@link Charset} to use when deserializing the execution context.
* Defaults to "UTF-8". Must not be {@code null}.
* @param charset to use when deserializing the execution context.
* @see JdbcExecutionContextDao#setCharset(Charset)
* @since 5.0
*/
public void setCharset(@NonNull Charset charset) {
Assert.notNull(charset, "Charset must not be null");
this.charset = charset;
}

@Override
public void afterPropertiesSet() throws Exception {

Expand Down Expand Up @@ -145,6 +163,7 @@ protected ExecutionContextDao createExecutionContextDao() throws Exception {
dao.setLobHandler(lobHandler);
dao.setTablePrefix(tablePrefix);
dao.setSerializer(serializer);
dao.setCharset(charset);
dao.afterPropertiesSet();
return dao;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
Expand All @@ -40,6 +41,7 @@
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.support.lob.DefaultLobHandler;
import org.springframework.jdbc.support.lob.LobHandler;
import org.springframework.lang.NonNull;
import org.springframework.util.Assert;

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

private static final String CHARSET_NAME = StandardCharsets.UTF_8.name();
private Charset charset = StandardCharsets.UTF_8;

private static final int DEFAULT_MAX_VARCHAR_LENGTH = 2500;

Expand Down Expand Up @@ -109,6 +111,17 @@ public void setShortContextLength(int shortContextLength) {
this.shortContextLength = shortContextLength;
}

/**
* Set the {@link Charset} to use when serializing/deserializing the execution context.
* Must not be {@code null}. Defaults to "UTF-8".
* @param charset to use when serializing/deserializing the execution context.
* @since 5.0
*/
public void setCharset(@NonNull Charset charset) {
Assert.notNull(charset, "Charset must not be null");
this.charset = charset;
}

@Override
public ExecutionContext getExecutionContext(JobExecution jobExecution) {
Long executionId = jobExecution.getId();
Expand Down Expand Up @@ -303,7 +316,7 @@ private String serializeContext(ExecutionContext ctx) {

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

Map<String, Object> map;
try {
ByteArrayInputStream in = new ByteArrayInputStream(serializedContext.getBytes(CHARSET_NAME));
ByteArrayInputStream in = new ByteArrayInputStream(serializedContext.getBytes(charset.name()));
map = serializer.deserialize(in);
}
catch (IOException ioe) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2021 the original author or authors.
* Copyright 2002-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -17,6 +17,8 @@
package org.springframework.batch.core.repository.support;

import java.lang.reflect.Field;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.sql.Types;
import javax.sql.DataSource;

Expand All @@ -43,6 +45,7 @@
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.support.lob.DefaultLobHandler;
import org.springframework.jdbc.support.lob.LobHandler;
import org.springframework.lang.NonNull;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;

Expand Down Expand Up @@ -82,6 +85,8 @@ public class JobRepositoryFactoryBean extends AbstractJobRepositoryFactoryBean i

private Integer lobType;

private Charset charset = StandardCharsets.UTF_8;

/**
* @param type a value from the {@link java.sql.Types} class to indicate the type to use for a CLOB
*/
Expand Down Expand Up @@ -167,6 +172,18 @@ public void setIncrementerFactory(DataFieldMaxValueIncrementerFactory incremente
this.incrementerFactory = incrementerFactory;
}

/**
* Set the {@link Charset} to use when serializing/deserializing the execution context.
* Defaults to "UTF-8". Must not be {@code null}.
* @param charset to use when serializing/deserializing the execution context.
* @see JdbcExecutionContextDao#setCharset(Charset)
* @since 5.0
*/
public void setCharset(@NonNull Charset charset) {
Assert.notNull(charset, "Charset must not be null");
this.charset = charset;
}

@Override
public void afterPropertiesSet() throws Exception {

Expand Down Expand Up @@ -251,6 +268,7 @@ protected ExecutionContextDao createExecutionContextDao() throws Exception {
dao.setTablePrefix(tablePrefix);
dao.setClobTypeToUse(determineClobTypeToUse(this.databaseType));
dao.setSerializer(serializer);
dao.setCharset(charset);

if (lobHandler != null) {
dao.setLobHandler(lobHandler);
Expand Down

0 comments on commit 9315a3b

Please sign in to comment.