001package votorola.g.util;
002
003import java.io.*;
004import java.net.*;
005import java.security.*;
006import java.util.*;
007import votorola.g.lang.*;
008
009
010/** A resource bundle control to enable loading of UTF-8 property files, as opposed the
011  * ISO-8859-1 encoding expected by the default control.
012  *
013  *     @see java.util.PropertyResourceBundle
014  */
015public @ThreadSafe final class BundleControlU extends ResourceBundle.Control
016{
017
018    // as BalusC recommends, http://stackoverflow.com/a/4660195
019
020
021    public @Override ResourceBundle newBundle( final String baseName, final Locale locale,
022      final String format, final ClassLoader loader, final boolean reload )
023      throws IllegalAccessException, InstantiationException, IOException
024    {
025        if( !"java.properties".equals( format ))
026        {
027            return super.newBundle( baseName, locale, format, loader, reload );
028        }
029
030        final String bundleName = toBundleName( baseName, locale );
031        ResourceBundle bundle = null;
032
033      // Modified from Oracle JDK 1.7.0_02 where marked "MCA", under /_/licence/none.txt
034      // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
035        final String resourceName = toResourceName(bundleName, "properties");
036        final ClassLoader classLoader = loader;
037        final boolean reloadFlag = reload;
038        InputStream stream = null;
039        try {
040            stream = AccessController.doPrivileged(
041                new PrivilegedExceptionAction<InputStream>() {
042                    public InputStream run() throws IOException {
043                        InputStream is = null;
044                        if (reloadFlag) {
045                            URL url = classLoader.getResource(resourceName);
046                            if (url != null) {
047                                URLConnection connection = url.openConnection();
048                                if (connection != null) {
049                                    // Disable caches to get fresh data for
050                                    // reloading.
051                                    connection.setUseCaches(false);
052                                    is = connection.getInputStream();
053                                }
054                            }
055                        } else {
056                            is = classLoader.getResourceAsStream(resourceName);
057                        }
058                        return is;
059                    }
060                });
061        } catch (PrivilegedActionException e) {
062            throw (IOException) e.getException();
063        }
064        if (stream != null) {
065            try {
066             // bundle = new PropertyResourceBundle(stream); // MCA:
067                bundle = new PropertyResourceBundle(new InputStreamReader(stream, "UTF-8"));
068            } finally {
069                stream.close();
070            }
071        }
072        return bundle;
073    }
074
075
076}