All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.tngtech.configbuilder.configuration.ErrorMessageSetup Maven / Gradle / Ivy

Go to download

The Config Builder creates fully configured instances of config classes, using values from various sources like properties files, command line arguments etc.

The newest version!
package com.tngtech.configbuilder.configuration;

import com.tngtech.propertyloader.PropertyLoader;

import java.util.Enumeration;
import java.util.Properties;
import java.util.ResourceBundle;

/**
 * Stores error messages for the ConfigBuilder in a Properties object.
 */
public class ErrorMessageSetup {

    private Properties errorMessages;

    /**
     * loads the default error messages for the system locale, then merges them with additional error messages loaded with the PropertyLoader
     *
     * @param baseName       the filename for additional error messages
     * @param propertyLoader the PropertyLoader used to load additional error messages
     */
    public void initialize(String baseName, PropertyLoader propertyLoader) {
        ResourceBundle resourceBundle = ResourceBundle.getBundle("errors");
        errorMessages = convertResourceBundleToProperties(resourceBundle);
        if (baseName != null) {
            errorMessages.putAll(propertyLoader.load(baseName));
        }
    }

    private Properties convertResourceBundleToProperties(ResourceBundle resource) {
        Properties properties = new Properties();

        Enumeration keys = resource.getKeys();
        while (keys.hasMoreElements()) {
            String key = keys.nextElement();
            properties.put(key, resource.getString(key));
        }
        return properties;
    }

    public String getErrorMessage(Throwable e, String... variables) {
        String message = errorMessages.getProperty(e.getClass().getName());
        return message == null ? String.format(errorMessages.getProperty("standardMessage"), e.getClass().getName()) : String.format(message, variables);
    }

    public String getErrorMessage(Class exceptionClass, String... variables) {
        String message = errorMessages.getProperty(exceptionClass.getName());
        return message == null ? String.format(errorMessages.getProperty("standardMessage"), exceptionClass.getName()) : String.format(message, variables);
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy