Java Thread Lock getLocaleProperties(Locale locale)

Here you can find the source of getLocaleProperties(Locale locale)

Description

Get the messages for a locale.

License

Open Source License

Parameter

Parameter Description
locale the locale

Return

message map

Declaration

private static Map<String, String> getLocaleProperties(Locale locale) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.util.Enumeration;
import java.util.HashMap;
import java.util.HashSet;

import java.util.Locale;
import java.util.Map;
import java.util.ResourceBundle;
import java.util.Set;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReadWriteLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;

public class Main {
    /**/*from ww w.  j  a  v  a 2s.  co  m*/
     * List of registered bundles
     */
    private static Set<String> resouceBundleBaseNames = new HashSet<String>();
    /**
     * Map of loaded bundles by Locale
     */
    private static Map<Locale, Set<String>> loadedResourceBundles = new HashMap<Locale, Set<String>>();
    /**
     * Map of cached messaged by Locale
     */
    private static Map<Locale, Map<String, String>> cachedMessages = new HashMap<Locale, Map<String, String>>();
    /**
     * Lock objects
     */
    private static ReadWriteLock lock = new ReentrantReadWriteLock();
    private static Lock readLock = lock.readLock();
    private static Lock writeLock = lock.writeLock();

    /**
     * Get the messages for a locale.
     * <p>
     * Will use cache where available otherwise will load into cache from bundles.
     *
     * @param locale    the locale
     * @return          message map
     */
    private static Map<String, String> getLocaleProperties(Locale locale) {
        Set<String> loadedBundles = null;
        Map<String, String> props = null;
        int loadedBundleCount = 0;
        try {
            readLock.lock();
            loadedBundles = loadedResourceBundles.get(locale);
            props = cachedMessages.get(locale);
            loadedBundleCount = resouceBundleBaseNames.size();
        } finally {
            readLock.unlock();
        }

        if (loadedBundles == null) {
            try {
                writeLock.lock();
                loadedBundles = new HashSet<String>();
                loadedResourceBundles.put(locale, loadedBundles);
            } finally {
                writeLock.unlock();
            }
        }

        if (props == null) {
            try {
                writeLock.lock();
                props = new HashMap<String, String>();
                cachedMessages.put(locale, props);
            } finally {
                writeLock.unlock();
            }
        }

        if (loadedBundles.size() != loadedBundleCount) {
            try {
                writeLock.lock();
                for (String resourceBundleBaseName : resouceBundleBaseNames) {
                    if (loadedBundles.contains(resourceBundleBaseName) == false) {
                        ResourceBundle resourcebundle = ResourceBundle.getBundle(resourceBundleBaseName, locale);
                        Enumeration<String> enumKeys = resourcebundle.getKeys();
                        while (enumKeys.hasMoreElements() == true) {
                            String key = enumKeys.nextElement();
                            props.put(key, resourcebundle.getString(key));
                        }
                        loadedBundles.add(resourceBundleBaseName);
                    }
                }
            } finally {
                writeLock.unlock();
            }
        }

        return props;
    }
}

Related

  1. downgradeWriteLock(final ReadWriteLock lock)
  2. fillReadWriteLocks(List readLocks, List writeLocks, int curSize, int size)
  3. getAllMessages()
  4. getGradingReadLock()
  5. getInstance()
  6. getPropLock(String s)
  7. loadClass(String className)
  8. loadClassWithRegisteredClassLoaders(String className)
  9. lock(Lock lock)