diff --git a/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/data/MongoItemReader.java b/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/data/MongoItemReader.java index 47a3e7805..a03e65b40 100644 --- a/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/data/MongoItemReader.java +++ b/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/data/MongoItemReader.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2017 the original author or authors. + * Copyright 2012-2020 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. @@ -20,13 +20,12 @@ import java.util.ArrayList; import java.util.Iterator; import java.util.List; import java.util.Map; -import java.util.regex.Matcher; -import java.util.regex.Pattern; - -import com.mongodb.util.JSON; +import org.bson.Document; +import org.bson.codecs.DecoderContext; import org.slf4j.Logger; import org.slf4j.LoggerFactory; + import org.springframework.batch.item.ExecutionContext; import org.springframework.batch.item.ItemReader; import org.springframework.beans.factory.InitializingBean; @@ -36,6 +35,8 @@ import org.springframework.data.domain.Sort; import org.springframework.data.mongodb.core.MongoOperations; import org.springframework.data.mongodb.core.query.BasicQuery; import org.springframework.data.mongodb.core.query.Query; +import org.springframework.data.mongodb.util.json.ParameterBindingDocumentCodec; +import org.springframework.data.mongodb.util.json.ParameterBindingJsonReader; import org.springframework.util.Assert; import org.springframework.util.ClassUtils; import org.springframework.util.StringUtils; @@ -77,12 +78,12 @@ import org.springframework.util.StringUtils; * * @author Michael Minella * @author Takaaki Iida + * @author Mahmoud Ben Hassine */ public class MongoItemReader extends AbstractPaginatedDataItemReader implements InitializingBean { private static final Logger log = LoggerFactory.getLogger(MongoItemReader.class); - private static final Pattern PLACEHOLDER = Pattern.compile("\\?(\\d+)"); private MongoOperations template; private Query query; private String queryString; @@ -91,7 +92,7 @@ public class MongoItemReader extends AbstractPaginatedDataItemReader imple private String hint; private String fields; private String collection; - private List parameterValues; + private List parameterValues = new ArrayList<>(); public MongoItemReader() { super(); @@ -145,6 +146,7 @@ public class MongoItemReader extends AbstractPaginatedDataItemReader imple * @param parameterValues values */ public void setParameterValues(List parameterValues) { + Assert.notNull(parameterValues, "Parameter values must not be null"); this.parameterValues = parameterValues; } @@ -245,23 +247,11 @@ public class MongoItemReader extends AbstractPaginatedDataItemReader imple } } - // Copied from StringBasedMongoQuery...is there a place where this type of logic is already exposed? private String replacePlaceholders(String input, List values) { - Matcher matcher = PLACEHOLDER.matcher(input); - String result = input; - - while (matcher.find()) { - String group = matcher.group(); - int index = Integer.parseInt(matcher.group(1)); - result = result.replace(group, getParameterWithIndex(values, index)); - } - - return result; - } - - // Copied from StringBasedMongoQuery...is there a place where this type of logic is already exposed? - private String getParameterWithIndex(List values, int index) { - return JSON.serialize(values.get(index)); + ParameterBindingJsonReader reader = new ParameterBindingJsonReader(input, values.toArray()); + DecoderContext decoderContext = DecoderContext.builder().build(); + Document document = new ParameterBindingDocumentCodec().decode(reader, decoderContext); + return document.toJson(); } private Sort convertToSort(Map sorts) { diff --git a/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/data/builder/MongoItemReaderBuilder.java b/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/data/builder/MongoItemReaderBuilder.java index 3dd4d6503..bae46216a 100644 --- a/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/data/builder/MongoItemReaderBuilder.java +++ b/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/data/builder/MongoItemReaderBuilder.java @@ -1,5 +1,5 @@ /* - * Copyright 2017-2018 the original author or authors. + * Copyright 2017-2020 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. @@ -16,6 +16,7 @@ package org.springframework.batch.item.data.builder; +import java.util.ArrayList; import java.util.Arrays; import java.util.List; import java.util.Map; @@ -51,7 +52,7 @@ public class MongoItemReaderBuilder { private String collection; - private List parameterValues; + private List parameterValues = new ArrayList<>(); protected int pageSize = 10;