001    /**
002     * Copyright (c) 2000-present Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.kernel.util;
016    
017    import java.util.HashMap;
018    import java.util.Map;
019    import java.util.ResourceBundle;
020    
021    /**
022     * @author Carlos Sierra Andr??s
023     */
024    public class CacheResourceBundleLoader implements ResourceBundleLoader {
025    
026            public CacheResourceBundleLoader(
027                    ResourceBundleLoader resourceBundleLoader) {
028    
029                    _resourceBundleLoader = resourceBundleLoader;
030            }
031    
032            @Override
033            public ResourceBundle loadResourceBundle(String languageId) {
034                    if (_resourceBundles.containsKey(languageId)) {
035                            return _resourceBundles.get(languageId);
036                    }
037    
038                    synchronized (_resourceBundles) {
039                            if (_resourceBundles.containsKey(languageId)) {
040                                    return _resourceBundles.get(languageId);
041                            }
042    
043                            ResourceBundle resourceBundle;
044    
045                            try {
046                                    resourceBundle = _resourceBundleLoader.loadResourceBundle(
047                                            languageId);
048                            }
049                            catch (Exception e) {
050                                    resourceBundle = null;
051                            }
052    
053                            _resourceBundles.put(languageId, resourceBundle);
054    
055                            return resourceBundle;
056                    }
057            }
058    
059            private final ResourceBundleLoader _resourceBundleLoader;
060            private final Map<String, ResourceBundle> _resourceBundles =
061                    new HashMap<>();
062    
063    }