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

com.sap.it.commons.nls.NlsResourceBundleImpl Maven / Gradle / Ivy

The newest version!
package com.sap.it.commons.nls;

import java.security.AccessController;
import java.security.PrivilegedAction;
import java.text.MessageFormat;
import java.util.Locale;
import java.util.MissingResourceException;
import java.util.PropertyResourceBundle;
import java.util.ResourceBundle;

import com.sap.it.commons.tools.StringTools;

class NlsResourceBundleImpl implements NlsResourceBundle {

    private final ResourceBundle messages;

    private NlsResourceBundleImpl(ResourceBundle messages) {
        this.messages = messages;
    }

    static NlsResourceBundleImpl createAndInitialize(final String baseName, final Class hostClass) {
        NlsResourceBundleImpl lret = null;

        if (System.getSecurityManager() == null) {
            ResourceBundle messages;
            messages = load(baseName, hostClass);
            lret = new NlsResourceBundleImpl(messages);

        } else {
            lret = (NlsResourceBundleImpl) AccessController.doPrivileged(new PrivilegedAction() {
                @Override
                public Object run() {
                    ResourceBundle messages;
                    messages = load(baseName, hostClass);
                    return new NlsResourceBundleImpl(messages);

                }
            });
        }
        return lret;
    }

    private static ResourceBundle load(final String bundleName, Class hostClass) {

        try {

            ResourceBundle lret = PropertyResourceBundle.getBundle(buildFullQualifiedBundleName(bundleName, hostClass),
                    Locale.getDefault(), hostClass.getClassLoader());

            return lret;

        } catch (MissingResourceException e) {
            //don't die if this resource bundle is not found
            return null;
        }

    }

    private static String buildFullQualifiedBundleName(String bundleName, Class hostClass) {
        String lfullQualifedClassName = hostClass.getName();
        //cut off the class name in order to get the qualifier
        String lqualifier = lfullQualifedClassName.substring(0, lfullQualifedClassName.lastIndexOf("."));
        return lqualifier + "." + bundleName;
    }

    public String getLocalizedMessage(String id) {
        if (this.messages == null) {
            return StringTools.EMPTY_STRING;
        }
        try {
            return this.messages.getString(id);
        } catch (MissingResourceException e) {
            return StringTools.EMPTY_STRING;
        }
    }

    public String getFormatedLocalizedMessage(String id, Object... arguments) {
        if (this.messages == null) {
            return "";
        }
        try {
            String template = this.messages.getString(id);
            String formatedMessage = MessageFormat.format(template, arguments);
            return formatedMessage;
        } catch (MissingResourceException e) {
            return StringTools.EMPTY_STRING;
        }
    }
}