| 
01 /*02  * Copyright 2008-2015 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 package org.codehaus.griffon.runtime.core.i18n;
 17
 18 import griffon.core.resources.NoSuchResourceException;
 19 import griffon.util.CompositeResourceBundle;
 20 import griffon.util.CompositeResourceBundleBuilder;
 21
 22 import javax.annotation.Nonnull;
 23 import java.util.Locale;
 24 import java.util.Map;
 25 import java.util.ResourceBundle;
 26 import java.util.concurrent.ConcurrentHashMap;
 27
 28 import static griffon.util.GriffonNameUtils.requireNonBlank;
 29 import static java.util.Objects.requireNonNull;
 30
 31 /**
 32  * @author Andres Almiray
 33  * @since 2.0.0
 34  */
 35 public class DefaultMessageSource extends AbstractMessageSource {
 36     private final String basename;
 37     private final Map<Locale, ResourceBundle> bundles = new ConcurrentHashMap<>();
 38     private final CompositeResourceBundleBuilder compositeResourceBundleBuilder;
 39
 40     public DefaultMessageSource(@Nonnull CompositeResourceBundleBuilder builder, @Nonnull String basename) {
 41         this.compositeResourceBundleBuilder = requireNonNull(builder, "Argument 'builder' must not be null");
 42         this.basename = requireNonBlank(basename, "Argument 'basename' must not be blank");
 43     }
 44
 45     @Nonnull
 46     public String getBasename() {
 47         return basename;
 48     }
 49
 50     @Nonnull
 51     protected Object doResolveMessageValue(@Nonnull String key, @Nonnull Locale locale) throws NoSuchResourceException {
 52         requireNonBlank(key, ERROR_KEY_BLANK);
 53         requireNonNull(locale, ERROR_LOCALE_NULL);
 54         return getBundle(locale).getObject(key);
 55     }
 56
 57     @Nonnull
 58     protected ResourceBundle getBundle(@Nonnull Locale locale) {
 59         requireNonNull(locale, ERROR_LOCALE_NULL);
 60         ResourceBundle rb = bundles.get(locale);
 61         if (null == rb) {
 62             rb = compositeResourceBundleBuilder.create(basename, locale);
 63             bundles.put(locale, rb);
 64         }
 65         return rb;
 66     }
 67
 68     @Nonnull
 69     @Override
 70     public ResourceBundle asResourceBundle() {
 71         // force initialization of default bundle
 72         if (bundles.isEmpty()) {
 73             getBundle(Locale.getDefault());
 74         }
 75         return new CompositeResourceBundle(bundles.values());
 76     }
 77 }
 |