FileDocCategorySizeDatePackage
StringTranslator.javaAPI DocGlassfish v2 API15146Fri May 04 22:30:28 BST 2007com.sun.enterprise.jbi.serviceengine.util.soap

StringTranslator

public class StringTranslator extends Object
This is the implementation of the String Translator, which provides services for internationalization of messages to all services running inside the JBI environment.
author
Sun Microsystems Inc.

Fields Summary
private static final String
LOGGER_NAME
Logger name
public static final String
RESOURCE_BUNDLE_NAME
Unqualified name for resource bundles.
private static final String
LOG_NEW_INSTANCE
Log message for creation of new instance.
private static final String
LOG_CURRENT_LOCALE
Log message for locale.
private static final String
LOG_UNABLE_TO_LOAD_BUNDLE
Log message for failure loading resource bundle.
private static final String
LOG_USING_BUNDLE
Log message for using alternate resource bundle.
private static final String
LOG_TRANSLATION_USING_FALLBACK
Log message for using fallback resource bundle to look up a message.
private static final String
LOG_NO_TRANSLATION_FOR_KEY
Log message for no translation available for a message key in any resource bundle.
private static final String
LOG_NO_TRANSLATION_FOR_KEY_IN_BUNDLE
Log message for no translation available for a message key in a particular resource bundle.
private static final String
MSG_NO_TRANSLATION
Message text used when no translation is available for a message key.
private Locale
mDefaultLocale
The default locale at the time this StringTranslator was created.
private Logger
mLog
Logger for this instance
private ResourceBundle
mFallbackBundle
Fallback ResourceBundle for a single package name. This is used only when the default locale is something other than Locale.US. In that case, this is the bundle for Locale.US for looking up messages that are not found in the default locale.
private ResourceBundle
mResourceBundle
ResourceBundle for a single package name.
private static StringTranslator
defaultInstance
Constructors Summary
public StringTranslator()


      
        this("com.sun.enterprise.jbi.serviceengine.core", null);
    
public StringTranslator(String packageName, ClassLoader classLoader)
Constructor. This loads the Resource Bundle for the current locale, and if the current locale is not Locale.US, it loads the Resource Bundle for Locale.US and stores it as the backup for string lookup.

param
packageName - the name of the package that contains the resource bundle.
param
classLoader - the class loader to be used for loading the resource bundle. If this parameter is null, the current class loader is used.

        mLog = Logger.getLogger(packageName);

        String bundleName = packageName + "." + RESOURCE_BUNDLE_NAME;
        mDefaultLocale = Locale.getDefault();

        // Always load the bundle for english
        mFallbackBundle = null;

        ResourceBundle englishBundle = null;

        try
        {
            if (null == classLoader)
            {
                englishBundle = ResourceBundle.getBundle(bundleName, Locale.US);
            }
            else
            {
                englishBundle =
                    ResourceBundle.getBundle(bundleName, Locale.US, classLoader);
            }
        }
        catch (java.util.MissingResourceException mrEx)
        {
            mLog.warning(MessageFormat.format(LOG_UNABLE_TO_LOAD_BUNDLE,
                    new Object [] {bundleName, Locale.US, mrEx}));
        }

        // If the default locale is English, set it as the primary bundle.
        // If the default locale is not English, attempt to load the bundle.
        // If it is found, save it as the primary and set the fallback to
        // English. If it is not found, set the primary to English.
        if (mDefaultLocale.equals(Locale.US))
        {
            mResourceBundle = englishBundle;
        }
        else
        {
            try
            {
                if (null == classLoader)
                {
                    mResourceBundle = ResourceBundle.getBundle(bundleName);
                    mFallbackBundle = englishBundle;
                }
                else
                {
                    mResourceBundle =
                        ResourceBundle.getBundle(bundleName, mDefaultLocale,
                            classLoader);
                    mFallbackBundle = englishBundle;
                }
            }
            catch (java.util.MissingResourceException mrEx)
            {
                mLog.warning(MessageFormat.format(LOG_UNABLE_TO_LOAD_BUNDLE,
                        new Object [] {bundleName, mDefaultLocale, mrEx}));
                mLog.warning(MessageFormat.format(LOG_USING_BUNDLE,
                        new Object [] {Locale.US}));
                mResourceBundle = englishBundle;
            }
        }
    
Methods Summary
private java.lang.StringformatInserts(java.lang.Object[] inserts)
Format an array of message inserts into a string. The ouptut string is in the format "insert1,insert2,....,insertn".

param
inserts - the array of message inserts.
return
the string formatted with the message inserts.

        StringBuffer formatted = new StringBuffer("");

        for (int i = 0; i < inserts.length; i++)
        {
            if (i > 0)
            {
                formatted.append(",");
            }

            formatted.append(inserts[i].toString());
        }

        return formatted.toString();
    
public static com.sun.enterprise.jbi.serviceengine.util.soap.StringTranslatorgetDefaultInstance()

        if (defaultInstance == null) {
            defaultInstance = new StringTranslator();
        }
        return defaultInstance;
    
public java.lang.StringgetString(java.lang.String key)
Get a localized string using the specified resource key.

param
key - the key to the localized string in the resource bundle.
return
the localized string.

        Object [] inserts = new Object[0];

        return getString(key, inserts);
    
public java.lang.StringgetString(java.lang.String key, java.lang.Object insert1)
Get a localized string using the specified resource key. Handle one message insert.

param
key - the key to the localized string in the resource bundle.
param
insert1 - the message insert.
return
the localized string formatted with the message insert.

        Object [] inserts = {insert1};

        return getString(key, inserts);
    
public java.lang.StringgetString(java.lang.String key, java.lang.Object insert1, java.lang.Object insert2)
Get a localized string using the specified resource key. Handle two message inserts.

param
key - the key to the localized string in the resource bundle.
param
insert1 - the first message insert.
param
insert2 - the second message insert.
return
the localized string formatted with the message inserts.

        Object [] inserts = {insert1, insert2};

        return getString(key, inserts);
    
public java.lang.StringgetString(java.lang.String key, java.lang.Object insert1, java.lang.Object insert2, java.lang.Object insert3)
Get a localized string using the specified resource key. Handle three message inserts.

param
key - the key to the localized string in the resource bundle.
param
insert1 - the first message insert.
param
insert2 - the second message insert.
param
insert3 - the third message insert.
return
the localized string formatted with the message inserts.

        Object [] inserts = {insert1, insert2, insert3};

        return getString(key, inserts);
    
public java.lang.StringgetString(java.lang.String key, java.lang.Object insert1, java.lang.Object insert2, java.lang.Object insert3, java.lang.Object insert4)
Get a localized string using the specified resource key. Handle four message inserts.

param
key - the key to the localized string in the resource bundle.
param
insert1 - the first message insert.
param
insert2 - the second message insert.
param
insert3 - the third message insert.
param
insert4 - the fourth message insert.
return
the localized string formatted with the message inserts.

        Object [] inserts = {insert1, insert2, insert3, insert4};

        return getString(key, inserts);
    
public java.lang.StringgetString(java.lang.String key, java.lang.Object[] inserts)
Get a localized string using the specified resource key. Handle any number of message inserts. This method is called by all the other getString() methods in the class. The procedure for string lookup is to first look in the primary resource bundle, then in the fallback resource bundle. If the string is found in the primary, return the translated string quietly. If the string is not found in the primary but in the fallback, log a warning and return the translated string. If the string is not found in either bundle, log an error and return a message formatted with the key and insert values provided by the caller. If there is no resource bundle available, just return a message formatted with the key and insert values provided by the caller.

param
key - the key to the localized string in the resource bundle.
param
inserts - the array of message inserts.
return
the localized string formatted with the message inserts.

        String translated = null;

        if (null != mResourceBundle)
        {
            try
            {
                translated = mResourceBundle.getString(key);
                translated = MessageFormat.format(translated, inserts);
            }
            catch (java.util.MissingResourceException mrEx)
            {
                if (null != mFallbackBundle)
                {
                    try
                    {
                        translated = mFallbackBundle.getString(key);
                        translated = MessageFormat.format(translated, inserts);
                        mLog.fine(MessageFormat.format(
                                LOG_TRANSLATION_USING_FALLBACK,
                                new Object [] {key, mDefaultLocale, Locale.US}));
                    }
                    catch (java.util.MissingResourceException mreEx)
                    {
                        String fi = formatInserts(inserts);
                        translated =
                            MessageFormat.format(MSG_NO_TRANSLATION,
                                new Object [] {key, fi});
                        mLog.warning(MessageFormat.format(
                                LOG_NO_TRANSLATION_FOR_KEY,
                                new Object [] {key, fi}));
                    }
                }
                else
                {
                    String fi = formatInserts(inserts);
                    translated =
                        MessageFormat.format(MSG_NO_TRANSLATION,
                            new Object [] {key, fi});
                    mLog.warning(MessageFormat.format(
                            LOG_NO_TRANSLATION_FOR_KEY_IN_BUNDLE,
                            new Object [] {key, mDefaultLocale, fi}));
                }
            }
        }
        else
        {
            translated =
                MessageFormat.format(MSG_NO_TRANSLATION,
                    new Object [] {key, formatInserts(inserts)});
        }

        return translated;