001/*
002 * Copyright 2002-2012 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 */
016
017package org.springframework.validation.beanvalidation;
018
019import javax.validation.MessageInterpolator;
020import javax.validation.TraversableResolver;
021import javax.validation.Validation;
022import javax.validation.Validator;
023import javax.validation.ValidatorContext;
024import javax.validation.ValidatorFactory;
025
026import org.springframework.beans.factory.InitializingBean;
027
028/**
029 * Configurable bean class that exposes a specific JSR-303 Validator
030 * through its original interface as well as through the Spring
031 * {@link org.springframework.validation.Validator} interface.
032 *
033 * @author Juergen Hoeller
034 * @since 3.0
035 */
036public class CustomValidatorBean extends SpringValidatorAdapter implements Validator, InitializingBean {
037
038        private ValidatorFactory validatorFactory;
039
040        private MessageInterpolator messageInterpolator;
041
042        private TraversableResolver traversableResolver;
043
044
045        /**
046         * Set the ValidatorFactory to obtain the target Validator from.
047         * <p>Default is {@link javax.validation.Validation#buildDefaultValidatorFactory()}.
048         */
049        public void setValidatorFactory(ValidatorFactory validatorFactory) {
050                this.validatorFactory = validatorFactory;
051        }
052
053        /**
054         * Specify a custom MessageInterpolator to use for this Validator.
055         */
056        public void setMessageInterpolator(MessageInterpolator messageInterpolator) {
057                this.messageInterpolator = messageInterpolator;
058        }
059
060        /**
061         * Specify a custom TraversableResolver to use for this Validator.
062         */
063        public void setTraversableResolver(TraversableResolver traversableResolver) {
064                this.traversableResolver = traversableResolver;
065        }
066
067
068        @Override
069        public void afterPropertiesSet() {
070                if (this.validatorFactory == null) {
071                        this.validatorFactory = Validation.buildDefaultValidatorFactory();
072                }
073
074                ValidatorContext validatorContext = this.validatorFactory.usingContext();
075                MessageInterpolator targetInterpolator = this.messageInterpolator;
076                if (targetInterpolator == null) {
077                        targetInterpolator = this.validatorFactory.getMessageInterpolator();
078                }
079                validatorContext.messageInterpolator(new LocaleContextMessageInterpolator(targetInterpolator));
080                if (this.traversableResolver != null) {
081                        validatorContext.traversableResolver(this.traversableResolver);
082                }
083
084                setTargetValidator(validatorContext.getValidator());
085        }
086
087}