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