1 /*
2  * Copyright 2002-2010 the original author or authors.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package org.springframework.validation.beanvalidation;
18 
19 import java.util.Locale;
20 import java.util.ResourceBundle;
21 
22 import org.hibernate.validator.resourceloading.ResourceBundleLocator;
23 
24 import org.springframework.context.MessageSource;
25 import org.springframework.context.support.MessageSourceResourceBundle;
26 import org.springframework.util.Assert;
27 
28 /**
29  * Implementation of Hibernate Validator 4.1's {@link ResourceBundleLocator} interface,
30  * exposing a Spring {@link MessageSource} as localized {@link MessageSourceResourceBundle}.
31  *
32  * @author Juergen Hoeller
33  * @since 3.0.4
34  * @see ResourceBundleLocator
35  * @see MessageSource
36  * @see MessageSourceResourceBundle
37  */
38 public class MessageSourceResourceBundleLocator implements ResourceBundleLocator {
39 
40 	private final MessageSource messageSource;
41 
42 	/**
43 	 * Build a MessageSourceResourceBundleLocator for the given MessageSource.
44 	 * @param messageSource the Spring MessageSource to wrap
45 	 */
MessageSourceResourceBundleLocator(MessageSource messageSource)46 	public MessageSourceResourceBundleLocator(MessageSource messageSource) {
47 		Assert.notNull(messageSource, "MessageSource must not be null");
48 		this.messageSource = messageSource;
49 	}
50 
getResourceBundle(Locale locale)51 	public ResourceBundle getResourceBundle(Locale locale) {
52 		return new MessageSourceResourceBundle(this.messageSource, locale);
53 	}
54 
55 }
56