com.seajas.search.utilities.spring.i18n.AccessibleReloadableResourceBundleMessageSource.java Source code

Java tutorial

Introduction

Here is the source code for com.seajas.search.utilities.spring.i18n.AccessibleReloadableResourceBundleMessageSource.java

Source

/**
 * Copyright (C) 2013 Seajas, the Netherlands.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 3, as
 * published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package com.seajas.search.utilities.spring.i18n;

import java.util.Enumeration;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
import java.util.Properties;

import org.springframework.context.support.ReloadableResourceBundleMessageSource;

/**
 * MessageSource which can return a list of all messages in the merged bundles.
 * 
 * @author Jasper van Veghel <jasper@seajas.com>
 */
public class AccessibleReloadableResourceBundleMessageSource extends ReloadableResourceBundleMessageSource {
    /**
     * Retrieve all messages for a given locale.
     * 
     * @param locale
     * @return Map<String, String>
     */
    public Map<String, String> getAllMessages(final Locale locale) {
        Map<String, String> messages = new HashMap<String, String>();

        // Should be internally synchronized, so we shouldn't run into any deadlock issues

        PropertiesHolder propertiesHolder = getMergedProperties(locale);
        Properties properties = propertiesHolder.getProperties();

        for (Enumeration<Object> enumeration = properties.keys(); enumeration.hasMoreElements();) {
            String key = (String) enumeration.nextElement();

            messages.put(key, propertiesHolder.getProperty(key));
        }

        return messages;
    }
}