001/*
002 * Copyright 2011-2018 the original author or authors.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *      https://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package org.springframework.batch.core.job;
017
018import java.util.List;
019
020import org.springframework.batch.core.JobParameters;
021import org.springframework.batch.core.JobParametersInvalidException;
022import org.springframework.batch.core.JobParametersValidator;
023import org.springframework.beans.factory.InitializingBean;
024import org.springframework.lang.Nullable;
025import org.springframework.util.Assert;
026
027/**
028 * Composite {@link JobParametersValidator} that passes the job parameters through a sequence of
029 * injected <code>JobParametersValidator</code>s
030 *
031 * @author Morten Andersen-Gott
032 * @author Mahmoud Ben Hassine
033 *
034 */
035public class CompositeJobParametersValidator implements JobParametersValidator, InitializingBean {
036
037        private List<JobParametersValidator> validators;
038
039        /**
040         * Validates the JobParameters according to the injected JobParameterValidators
041         * Validation stops and exception is thrown on first validation error
042         *
043         * @param parameters some {@link JobParameters}
044         * @throws JobParametersInvalidException if the parameters are invalid
045         */
046        @Override
047        public void validate(@Nullable JobParameters parameters) throws JobParametersInvalidException {
048                for (JobParametersValidator validator : validators) {
049                        validator.validate(parameters);
050                }
051        }
052
053        /**
054         * Public setter for the validators
055         * @param validators list of validators to be used by the CompositeJobParametersValidator.
056         */
057        public void setValidators(List<JobParametersValidator> validators) {
058                this.validators = validators;
059        }
060
061        @Override
062        public void afterPropertiesSet() throws Exception {
063                Assert.notNull(validators, "The 'validators' may not be null");
064                Assert.notEmpty(validators, "The 'validators' may not be empty");
065        }
066
067
068
069}