Skip to content

Reduce use of deprecated APIs #4334

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2012-2022 the original author or authors.
* Copyright 2012-2023 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 @@ -15,16 +15,18 @@
*/
package org.springframework.batch.core.repository.dao;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.io.Serializable;
import java.util.Base64;
import java.util.Map;

import org.springframework.batch.core.repository.ExecutionContextSerializer;
import org.springframework.util.Assert;
import org.springframework.util.Base64Utils;
import org.springframework.util.SerializationUtils;

/**
* An implementation of the {@link ExecutionContextSerializer} that produces/consumes
Expand All @@ -34,7 +36,6 @@
* @author Mahmoud Ben Hassine
* @since 2.2
*/
@SuppressWarnings("rawtypes")
public class DefaultExecutionContextSerializer implements ExecutionContextSerializer {

/**
Expand All @@ -45,7 +46,6 @@ public class DefaultExecutionContextSerializer implements ExecutionContextSerial
* written.
*/
@Override
@SuppressWarnings("unchecked")
public void serialize(Map<String, Object> context, OutputStream out) throws IOException {
Assert.notNull(context, "context is required");
Assert.notNull(out, "OutputStream is required");
Expand All @@ -58,9 +58,12 @@ public void serialize(Map<String, Object> context, OutputStream out) throws IOEx
+ value.getClass().getName() + "] must be an instance of " + Serializable.class);
}
}
byte[] serializedContext = SerializationUtils.serialize(context);
String base64EncodedContext = Base64Utils.encodeToString(serializedContext);
out.write(base64EncodedContext.getBytes());
var byteArrayOutputStream = new ByteArrayOutputStream(1024);
var encodingStream = Base64.getEncoder().wrap(byteArrayOutputStream);
try (var objectOutputStream = new ObjectOutputStream(encodingStream)) {
objectOutputStream.writeObject(context);
}
out.write(byteArrayOutputStream.toByteArray());
}

/**
Expand All @@ -72,10 +75,17 @@ public void serialize(Map<String, Object> context, OutputStream out) throws IOEx
@SuppressWarnings("unchecked")
@Override
public Map<String, Object> deserialize(InputStream inputStream) throws IOException {
String base64EncodedContext = new String(inputStream.readAllBytes());
byte[] decodedContext = Base64Utils.decodeFromString(base64EncodedContext);
// FIXME use replacement of the following SerializationUtils.deserialize
return (Map<String, Object>) SerializationUtils.deserialize(decodedContext);
var decodingStream = Base64.getDecoder().wrap(inputStream);
try {
var objectInputStream = new ObjectInputStream(decodingStream);
return (Map<String, Object>) objectInputStream.readObject();
}
catch (IOException ex) {
throw new IllegalArgumentException("Failed to deserialize object", ex);
}
catch (ClassNotFoundException ex) {
throw new IllegalStateException("Failed to deserialize object type", ex);
}
}

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2006-2022 the original author or authors.
* Copyright 2006-2023 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,7 +16,6 @@
package org.springframework.batch.core.step.job;

import java.util.Arrays;
import java.util.Date;
import java.util.HashSet;
import java.util.Map;
import java.util.Properties;
Expand Down Expand Up @@ -72,7 +71,7 @@ public JobParameters getJobParameters(Job job, StepExecution stepExecution) {
ExecutionContext executionContext = stepExecution.getExecutionContext();
if (useAllParentParameters) {
for (String key : jobParameters.keySet()) {
builder.addParameter(key, jobParameters.get(key));
builder.addJobParameter(key, jobParameters.get(key));
}
}
Properties properties = new Properties();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2006-2022 the original author or authors.
* Copyright 2006-2023 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 Down Expand Up @@ -45,7 +45,6 @@
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
import org.springframework.util.ReflectionUtils;

/**
* @author Dave Syer
Expand Down Expand Up @@ -97,13 +96,10 @@ void setUp() {
}

@SuppressWarnings("unchecked")
private <T> T accessPrivateField(Object o, String fieldName) {
Field field = ReflectionUtils.findField(o.getClass(), fieldName);
boolean previouslyAccessibleValue = field.isAccessible();
private <T> T accessPrivateField(Object o, String fieldName) throws ReflectiveOperationException {
Field field = o.getClass().getDeclaredField(fieldName);
field.setAccessible(true);
T val = (T) ReflectionUtils.getField(field, o);
field.setAccessible(previouslyAccessibleValue);
return val;
return (T) field.get(o);
}

@Test
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2015-2022 the original author or authors.
* Copyright 2015-2023 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 Down Expand Up @@ -48,7 +48,6 @@
import org.springframework.core.io.DefaultResourceLoader;
import org.springframework.core.io.ResourceLoader;
import org.springframework.core.task.SimpleAsyncTaskExecutor;
import org.springframework.core.task.SyncTaskExecutor;
import org.springframework.core.task.TaskExecutor;
import org.springframework.jdbc.datasource.embedded.ConnectionProperties;
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseConfigurer;
Expand Down Expand Up @@ -109,7 +108,7 @@ public RepeatStatus execute(StepContribution contribution, ChunkContext chunkCon
return RepeatStatus.FINISHED;
}
}, transactionManager).build())
.next(new StepBuilder("flow.step2").repository(jobRepository).tasklet(new Tasklet() {
.next(new StepBuilder("flow.step2", jobRepository).tasklet(new Tasklet() {
@Nullable
@Override
public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2015-2018 the original author or authors.
* Copyright 2015-2023 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 Down Expand Up @@ -29,19 +29,15 @@ public final class StaxTestUtils {

public static XMLEventWriter getXmlEventWriter(Result r) throws Exception {
Method m = r.getClass().getDeclaredMethod("getXMLEventWriter");
boolean accessible = m.isAccessible();
m.setAccessible(true);
Object result = m.invoke(r);
m.setAccessible(accessible);
return (XMLEventWriter) result;
}

public static XMLEventReader getXmlEventReader(Source s) throws Exception {
Method m = s.getClass().getDeclaredMethod("getXMLEventReader");
boolean accessible = m.isAccessible();
m.setAccessible(true);
Object result = m.invoke(s);
m.setAccessible(accessible);
return (XMLEventReader) result;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2018-2022 the original author or authors.
* Copyright 2018-2023 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 Down Expand Up @@ -265,8 +265,8 @@ else if (count < items.size()) {
}
};

TaskletStep taskletStep = new RemoteChunkingManagerStepBuilder<String, String>("step").reader(itemReader)
.readerIsTransactionalQueue().processor(itemProcessor).repository(this.jobRepository)
TaskletStep taskletStep = new RemoteChunkingManagerStepBuilder<String, String>("step", this.jobRepository)
.reader(itemReader).readerIsTransactionalQueue().processor(itemProcessor)
.transactionManager(this.transactionManager).transactionAttribute(transactionAttribute)
.inputChannel(this.inputChannel).outputChannel(this.outputChannel).listener(annotatedListener)
.listener(skipListener).listener(chunkListener).listener(stepExecutionListener)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2006-2022 the original author or authors.
* Copyright 2006-2023 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,8 @@

package org.springframework.batch.sample.common;

import java.io.InputStream;
import java.io.ObjectInputStream;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Iterator;
Expand All @@ -38,7 +40,6 @@
import org.springframework.jdbc.core.RowMapper;
import org.springframework.lang.Nullable;
import org.springframework.util.Assert;
import org.springframework.util.SerializationUtils;

/**
* Thread-safe database {@link ItemReader} implementing the process indicator pattern.
Expand Down Expand Up @@ -117,17 +118,23 @@ public ProcessIndicatorItemWrapper<T> read() {
}
@SuppressWarnings("unchecked")
T result = (T) jdbcTemplate.queryForObject("SELECT VALUE FROM BATCH_STAGING WHERE ID=?",
new RowMapper<Object>() {
@Override
public Object mapRow(ResultSet rs, int rowNum) throws SQLException {
byte[] blob = rs.getBytes(1);
return SerializationUtils.deserialize(blob);
}
}, id);
(rs, rowNum) -> deserialize(rs.getBinaryStream(1)), id);

return new ProcessIndicatorItemWrapper<>(id, result);
}

private static Object deserialize(InputStream inputStream) {
if (inputStream == null) {
return null;
}
try (var objectInputStream = new ObjectInputStream(inputStream)) {
return objectInputStream.readObject();
}
catch (Exception e) {
throw new IllegalArgumentException("Failed to deserialize object", e);
}
}

@Nullable
@Override
public ExitStatus afterStep(StepExecution stepExecution) {
Expand Down