Skip to content

Commit 170addd

Browse files
committed
Remove deprecated usage of com.mongodb.util.JSON in MongoItemReader
(cherry picked from commit e0f78d1)
1 parent eb3e839 commit 170addd

File tree

2 files changed

+16
-25
lines changed

2 files changed

+16
-25
lines changed

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

Lines changed: 13 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2017 the original author or authors.
2+
* Copyright 2012-2020 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.
@@ -20,13 +20,12 @@
2020
import java.util.Iterator;
2121
import java.util.List;
2222
import java.util.Map;
23-
import java.util.regex.Matcher;
24-
import java.util.regex.Pattern;
25-
26-
import com.mongodb.util.JSON;
2723

24+
import org.bson.Document;
25+
import org.bson.codecs.DecoderContext;
2826
import org.slf4j.Logger;
2927
import org.slf4j.LoggerFactory;
28+
3029
import org.springframework.batch.item.ExecutionContext;
3130
import org.springframework.batch.item.ItemReader;
3231
import org.springframework.beans.factory.InitializingBean;
@@ -36,6 +35,8 @@
3635
import org.springframework.data.mongodb.core.MongoOperations;
3736
import org.springframework.data.mongodb.core.query.BasicQuery;
3837
import org.springframework.data.mongodb.core.query.Query;
38+
import org.springframework.data.mongodb.util.json.ParameterBindingDocumentCodec;
39+
import org.springframework.data.mongodb.util.json.ParameterBindingJsonReader;
3940
import org.springframework.util.Assert;
4041
import org.springframework.util.ClassUtils;
4142
import org.springframework.util.StringUtils;
@@ -77,12 +78,12 @@
7778
*
7879
* @author Michael Minella
7980
* @author Takaaki Iida
81+
* @author Mahmoud Ben Hassine
8082
*/
8183
public class MongoItemReader<T> extends AbstractPaginatedDataItemReader<T> implements InitializingBean {
8284

8385
private static final Logger log = LoggerFactory.getLogger(MongoItemReader.class);
8486

85-
private static final Pattern PLACEHOLDER = Pattern.compile("\\?(\\d+)");
8687
private MongoOperations template;
8788
private Query query;
8889
private String queryString;
@@ -91,7 +92,7 @@ public class MongoItemReader<T> extends AbstractPaginatedDataItemReader<T> imple
9192
private String hint;
9293
private String fields;
9394
private String collection;
94-
private List<Object> parameterValues;
95+
private List<Object> parameterValues = new ArrayList<>();
9596

9697
public MongoItemReader() {
9798
super();
@@ -145,6 +146,7 @@ public void setTargetType(Class<? extends T> type) {
145146
* @param parameterValues values
146147
*/
147148
public void setParameterValues(List<Object> parameterValues) {
149+
Assert.notNull(parameterValues, "Parameter values must not be null");
148150
this.parameterValues = parameterValues;
149151
}
150152

@@ -245,23 +247,11 @@ public void afterPropertiesSet() throws Exception {
245247
}
246248
}
247249

248-
// Copied from StringBasedMongoQuery...is there a place where this type of logic is already exposed?
249250
private String replacePlaceholders(String input, List<Object> values) {
250-
Matcher matcher = PLACEHOLDER.matcher(input);
251-
String result = input;
252-
253-
while (matcher.find()) {
254-
String group = matcher.group();
255-
int index = Integer.parseInt(matcher.group(1));
256-
result = result.replace(group, getParameterWithIndex(values, index));
257-
}
258-
259-
return result;
260-
}
261-
262-
// Copied from StringBasedMongoQuery...is there a place where this type of logic is already exposed?
263-
private String getParameterWithIndex(List<Object> values, int index) {
264-
return JSON.serialize(values.get(index));
251+
ParameterBindingJsonReader reader = new ParameterBindingJsonReader(input, values.toArray());
252+
DecoderContext decoderContext = DecoderContext.builder().build();
253+
Document document = new ParameterBindingDocumentCodec().decode(reader, decoderContext);
254+
return document.toJson();
265255
}
266256

267257
private Sort convertToSort(Map<String, Sort.Direction> sorts) {

spring-batch-infrastructure/src/main/java/org/springframework/batch/item/data/builder/MongoItemReaderBuilder.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2017-2018 the original author or authors.
2+
* Copyright 2017-2020 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,7 @@
1616

1717
package org.springframework.batch.item.data.builder;
1818

19+
import java.util.ArrayList;
1920
import java.util.Arrays;
2021
import java.util.List;
2122
import java.util.Map;
@@ -51,7 +52,7 @@ public class MongoItemReaderBuilder<T> {
5152

5253
private String collection;
5354

54-
private List<Object> parameterValues;
55+
private List<Object> parameterValues = new ArrayList<>();
5556

5657
protected int pageSize = 10;
5758

0 commit comments

Comments
 (0)