Java Thread Lock getAllMessages()

Here you can find the source of getAllMessages()

Description

get All Messages

License

Open Source License

Return

the map of all available messages for the current locale

Declaration

public static Map<String, String> getAllMessages() 

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   www. ja v  a  2  s .c  o  m*/
     * Thread-local containing the general Locale for the current thread
     */
    private static ThreadLocal<Locale> threadLocale = new ThreadLocal<Locale>();
    /**
     * 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();

    /**
     * @return the map of all available messages for the current locale
     */
    public static Map<String, String> getAllMessages() {
        return getLocaleProperties(getLocale());
    }

    /**
     * @return the map of all available messages for the specified locale
     */
    public static Map<String, String> getAllMessages(Locale locale) {
        return getLocaleProperties(locale);
    }

    /**
     * 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;
    }

    /**
     * Get the general local for the current thread, will revert to the default locale if none
     * specified for this thread.
     *
     * @return  the general locale
     */
    public static Locale getLocale() {
        Locale locale = threadLocale.get();
        if (locale == null) {
            // Get the default locale
            locale = Locale.getDefault();
        }
        return locale;
    }
}

Related

  1. canCreateNewThread()
  2. createConcurrentStack()
  3. downgradeWriteLock(final ReadWriteLock lock)
  4. fillReadWriteLocks(List readLocks, List writeLocks, int curSize, int size)
  5. getGradingReadLock()
  6. getInstance()
  7. getLocaleProperties(Locale locale)
  8. getPropLock(String s)