001/*
002 * Copyright 2002-2014 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 java.util.Locale;
020import java.util.ResourceBundle;
021
022import org.hibernate.validator.spi.resourceloading.ResourceBundleLocator;
023
024import org.springframework.context.MessageSource;
025import org.springframework.context.support.MessageSourceResourceBundle;
026import org.springframework.util.Assert;
027
028/**
029 * Implementation of Hibernate Validator 4.3/5.x's {@link ResourceBundleLocator} interface,
030 * exposing a Spring {@link MessageSource} as localized {@link MessageSourceResourceBundle}.
031 *
032 * @author Juergen Hoeller
033 * @since 3.0.4
034 * @see ResourceBundleLocator
035 * @see MessageSource
036 * @see MessageSourceResourceBundle
037 */
038public class MessageSourceResourceBundleLocator implements ResourceBundleLocator {
039
040        private final MessageSource messageSource;
041
042        /**
043         * Build a MessageSourceResourceBundleLocator for the given MessageSource.
044         * @param messageSource the Spring MessageSource to wrap
045         */
046        public MessageSourceResourceBundleLocator(MessageSource messageSource) {
047                Assert.notNull(messageSource, "MessageSource must not be null");
048                this.messageSource = messageSource;
049        }
050
051        @Override
052        public ResourceBundle getResourceBundle(Locale locale) {
053                return new MessageSourceResourceBundle(this.messageSource, locale);
054        }
055
056}