com.github.aynu.yukar.framework.util.PropertiesHelper.java Source code

Java tutorial

Introduction

Here is the source code for com.github.aynu.yukar.framework.util.PropertiesHelper.java

Source

// ----------------------------------------------------------------------------
// Copyright (C) Yukar Evolution Laboratory. All rights reserved.
// GNU GENERAL PUBLIC LICENSE Version 3, 29 June 2007
// http://www.gnu.org/licenses/gpl-3.0-standalone.html
// ----------------------------------------------------------------------------
package com.github.aynu.yukar.framework.util;

import java.net.URL;
import java.text.MessageFormat;
import java.util.Collection;
import java.util.Locale;
import java.util.ResourceBundle.Control;
import org.apache.commons.configuration.ConfigurationException;
import org.apache.commons.configuration.PropertiesConfiguration;
import org.apache.commons.lang.Validate;
import org.apache.commons.lang3.SystemUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.github.aynu.yukar.framework.lang.StandardRuntimeException;

/**
 * 
 * @author nilcy
 */
public final class PropertiesHelper {
    /**  */
    private static final Logger LOG = LoggerFactory.getLogger(PropertiesHelper.class);
    /**  */
    private final PropertiesConfiguration config;

    /**
     * 
     * @param baseName ??
     */
    public PropertiesHelper(final String baseName) {
        config = getPropertiesConfiguration(baseName);
        config.setThrowExceptionOnMissing(true);
    }

    /**
     * ??
     * @param key 
     * @return 
     */
    public String getText(final String key) {
        return config.getString(key);
    }

    /**
     * ??
     * @param key 
     * @param arguments ?
     * @return 
     */
    public String getText(final String key, final Object... arguments) {
        final String text = config.getString(key);
        Validate.notNull(text);
        return MessageFormat.format(text, arguments);
    }

    /**
     * ??
     * @param key 
     * @return 
     */
    public String[] getTexts(final String key) {
        return config.getStringArray(key);
    }

    /**
     * ??
     * @param key 
     * @return 
     */
    public int getNumeric(final String key) {
        return config.getInt(key);
    }

    /**
     * ?
     * @param key 
     * @param value 
     */
    public void setProperty(final String key, final Object value) {
        config.setProperty(key, value);
    }

    /**
     * ??
     * @throws ConfigurationException 
     */
    public void save() throws ConfigurationException {
        config.save();
    }

    /**
     * ????
     * @param baseName ??
     * @return ??
     */
    private PropertiesConfiguration getPropertiesConfiguration(final String baseName) {
        final String languageTag = String.format("%s-%s", SystemUtils.USER_LANGUAGE, SystemUtils.USER_COUNTRY);
        final Control control = Control.getControl(Control.FORMAT_DEFAULT);
        final Collection<Locale> locales = control.getCandidateLocales("messages",
                Locale.forLanguageTag(languageTag));
        final StringBuilder builder = new StringBuilder();
        for (final Locale locale : locales) {
            try {
                final String bundleName = control.toBundleName(baseName, locale);
                final String resourceName = control.toResourceName(bundleName, "properties");
                final URL url = getClass().getResource("/" + resourceName);
                // final URL url = ClassLoader.getSystemResource(resourceName);
                if (url != null) {
                    if (builder.length() > 0) {
                        LOG.debug("Resource could not be found ({}).", builder.toString());
                    }
                    LOG.debug("Resource could be found ({}).", resourceName);
                    return new PropertiesConfiguration(url);
                }
                builder.append(" " + resourceName);
            } catch (final ConfigurationException e) {
                LOG.warn(e.toString(), e);
                throw new StandardRuntimeException(e);
            }
        }
        throw new StandardRuntimeException(String.format("PROPERTY is NOT_FOUND. [baseName=%s]", baseName));
    }
}