org.springframework.batch.core.converter.DefaultJobParametersConverter Maven / Gradle / Ivy
/*
* 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.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.batch.core.converter;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Properties;
import org.springframework.batch.core.JobParameter;
import org.springframework.batch.core.JobParameters;
import org.springframework.batch.core.JobParametersBuilder;
import org.springframework.core.convert.support.ConfigurableConversionService;
import org.springframework.core.convert.support.DefaultConversionService;
import org.springframework.lang.NonNull;
import org.springframework.lang.Nullable;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;
/**
* Converter for {@link JobParameters} instances that uses a simple naming convention for
* converting job parameters. The expected notation is the following:
*
* key=value,type,identifying
*
* where:
*
*
* - value: string literal representing the value
* - type (optional): fully qualified name of the type of the value. Defaults to
* String.
* - identifying (optional): boolean to flag the job parameter as identifying or not.
* Defaults to true
*
*
* For example, schedule.date=2022-12-12,java.time.LocalDate will be converted to an
* identifying job parameter of type {@link java.time.LocalDate} with value "2022-12-12".
*
* The literal values are converted to the target type by using the default Spring
* conversion service, augmented if necessary by any custom converters. The conversion
* service should be configured with a converter to and from string literals to job
* parameter types.
*
* By default, the Spring conversion service is augmented to support the conversion of the
* following types:
*
*
* - {@link java.util.Date}: in the
* {@link java.time.format.DateTimeFormatter#ISO_INSTANT} format
* - {@link java.time.LocalDate}: in the
* {@link java.time.format.DateTimeFormatter#ISO_LOCAL_DATE} format
* - {@link java.time.LocalTime}: in the
* {@link java.time.format.DateTimeFormatter#ISO_LOCAL_TIME} format
* - {@link java.time.LocalDateTime}: in the
* {@link java.time.format.DateTimeFormatter#ISO_LOCAL_DATE_TIME} format
*
*
* @author Dave Syer
* @author Michael Minella
* @author Mahmoud Ben Hassine
*
*/
public class DefaultJobParametersConverter implements JobParametersConverter {
protected ConfigurableConversionService conversionService;
public DefaultJobParametersConverter() {
DefaultConversionService conversionService = new DefaultConversionService();
conversionService.addConverter(new DateToStringConverter());
conversionService.addConverter(new StringToDateConverter());
conversionService.addConverter(new LocalDateToStringConverter());
conversionService.addConverter(new StringToLocalDateConverter());
conversionService.addConverter(new LocalTimeToStringConverter());
conversionService.addConverter(new StringToLocalTimeConverter());
conversionService.addConverter(new LocalDateTimeToStringConverter());
conversionService.addConverter(new StringToLocalDateTimeConverter());
this.conversionService = conversionService;
}
/**
* @see org.springframework.batch.core.converter.JobParametersConverter#getJobParameters(java.util.Properties)
*/
@Override
public JobParameters getJobParameters(@Nullable Properties properties) {
if (properties == null || properties.isEmpty()) {
return new JobParameters();
}
JobParametersBuilder jobParametersBuilder = new JobParametersBuilder();
for (Entry