DefaultMessageSource.java
01 /*
02  * Copyright 2010-2013 the original author or authors.
03  *
04  * Licensed under the Apache License, Version 2.0 (the "License");
05  * you may not use this file except in compliance with the License.
06  * You may obtain a copy of the License at
07  *
08  *      http://www.apache.org/licenses/LICENSE-2.0
09  *
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.codehaus.griffon.runtime.core.i18n;
18 
19 import griffon.core.i18n.NoSuchMessageException;
20 import org.codehaus.griffon.runtime.util.CompositeResourceBundle;
21 
22 import java.util.Locale;
23 import java.util.Map;
24 import java.util.ResourceBundle;
25 import java.util.concurrent.ConcurrentHashMap;
26 
27 /**
28  @author Andres Almiray
29  @since 1.1.0
30  */
31 public class DefaultMessageSource extends AbstractMessageSource {
32     private final String basename;
33     private final Map<Locale, ResourceBundle> bundles = new ConcurrentHashMap<Locale, ResourceBundle>();
34 
35     public DefaultMessageSource(String basename) {
36         this.basename = basename;
37     }
38 
39     public String getBasename() {
40         return basename;
41     }
42 
43     protected String resolveMessage(String key, Locale localethrows NoSuchMessageException {
44         return getBundle(locale).getString(key);
45     }
46 
47     protected ResourceBundle getBundle(Locale locale) {
48         if (null == localelocale = Locale.getDefault();
49         ResourceBundle rb = bundles.get(locale);
50         if (null == rb) {
51             rb = CompositeResourceBundle.create(basename, locale);
52             bundles.put(locale, rb);
53         }
54         return rb;
55     }
56 }