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.language;
016    
017    import com.liferay.portal.kernel.concurrent.ConcurrentReferenceValueHashMap;
018    import com.liferay.portal.kernel.memory.FinalizeManager;
019    import com.liferay.portal.kernel.util.StringPool;
020    
021    import java.io.IOException;
022    import java.io.InputStream;
023    import java.io.InputStreamReader;
024    
025    import java.net.URL;
026    import java.net.URLConnection;
027    
028    import java.util.Locale;
029    import java.util.Map;
030    import java.util.PropertyResourceBundle;
031    import java.util.ResourceBundle;
032    import java.util.ResourceBundle.Control;
033    
034    /**
035     * @author Raymond Aug??
036     * @author Shuyang Zhou
037     */
038    public class UTF8Control extends Control {
039    
040            public static final UTF8Control INSTANCE = new UTF8Control();
041    
042            @Override
043            public ResourceBundle newBundle(
044                            String baseName, Locale locale, String format,
045                            ClassLoader classLoader, boolean reload)
046                    throws IOException {
047    
048                    URL url = classLoader.getResource(
049                            toResourceName(toBundleName(baseName, locale), "properties"));
050    
051                    if (url == null) {
052                            return null;
053                    }
054    
055                    if (!reload) {
056                            ResourceBundle resourceBundle = _resourceBundles.get(url);
057    
058                            if (resourceBundle != null) {
059                                    return resourceBundle;
060                            }
061                    }
062    
063                    URLConnection urlConnection = url.openConnection();
064    
065                    urlConnection.setUseCaches(!reload);
066    
067                    try (InputStream inputStream = urlConnection.getInputStream()) {
068                            ResourceBundle resourceBundle = new PropertyResourceBundle(
069                                    new InputStreamReader(inputStream, StringPool.UTF8));
070    
071                            _resourceBundles.put(url, resourceBundle);
072    
073                            return resourceBundle;
074                    }
075            }
076    
077            private static final Map<URL, ResourceBundle> _resourceBundles =
078                    new ConcurrentReferenceValueHashMap<>(
079                            FinalizeManager.SOFT_REFERENCE_FACTORY);
080    
081    }