diff --git a/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/validator/ValidatingItemProcessor.java b/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/validator/ValidatingItemProcessor.java index e43e85416..a54d6b8a0 100644 --- a/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/validator/ValidatingItemProcessor.java +++ b/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/validator/ValidatingItemProcessor.java @@ -13,27 +13,38 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - package org.springframework.batch.item.validator; import org.springframework.batch.item.ItemProcessor; +import org.springframework.beans.factory.InitializingBean; import org.springframework.util.Assert; /** - * Simple implementation of {@link ItemProcessor} validates and input and - * returns it without modifications. + * Simple implementation of {@link ItemProcessor} that validates input and + * returns it without modifications. Should the given {@link Validator} throw a + * {@link ValidationException} this processor will re-throw it to indicate the + * item should be skipped, unless {@link #setFilter(boolean)} is set to + * true, in which case null will be returned to + * indicate the item should be filtered. * * @author Robert Kasanicky - * */ -public class ValidatingItemProcessor implements ItemProcessor { +public class ValidatingItemProcessor implements ItemProcessor, InitializingBean { private Validator validator; - + private boolean filter = false; - - public ValidatingItemProcessor(Validator validator){ - Assert.notNull(validator, "Validator must not be null."); + + /** + * Default constructor + */ + public ValidatingItemProcessor() { + } + + /** + * Creates a ValidatingItemProcessor based on the given Validator. + */ + public ValidatingItemProcessor(Validator validator) { this.validator = validator; } @@ -64,7 +75,8 @@ public class ValidatingItemProcessor implements ItemProcessor { public T process(T item) throws ValidationException { try { validator.validate(item); - } catch (ValidationException e) { + } + catch (ValidationException e) { if (filter) { return null; // filter the item } @@ -75,4 +87,8 @@ public class ValidatingItemProcessor implements ItemProcessor { return item; } + public void afterPropertiesSet() throws Exception { + Assert.notNull(validator, "Validator must not be null."); + } + }