FileDocCategorySizeDatePackage
StringManagerBase.javaAPI DocGlassfish v2 API11394Fri May 04 22:32:14 BST 2007com.sun.enterprise.util.i18n

StringManagerBase

public class StringManagerBase extends Object
Implementation of a local string manager. Provides access to i18n messages for classes that need them.

One StringManagerBase per resource bundle name can be created and accessed by the getManager method call.

Example: [LocalStrings.properties] test=At {1,time} on {1,date}, there was {2} on planet {0,number,integer} StringManagerBase sm = StringManagerBase.getStringManager("LocalStrings.properties"); ..... try { .... } catch (Exception e) { String localizedMsg = sm.getString("test", new Integer(7), new java.util.Date(System.currentTimeMillis()), "a disturbance in the Force"); throw new MyException(localizedMsg, e); } Localized message: At 2:27:41 PM on Jul 8, 2002, there was a disturbance in the Force on planet 7
author
Nazrul Islam
since
JDK1.4

Fields Summary
private static Logger
_logger
logger used for this class
private ResourceBundle
_resourceBundle
resource bundle to be used by this manager
private static final String
NO_DEFAULT
default value used for undefined local string
private static Hashtable
managers
cache for all the local string managers (per pkg)
Constructors Summary
protected StringManagerBase(String resourceBundleName)
Initializes the resource bundle.

param
resourceBundleName name of the resource bundle


                              
       
        try {            
            _resourceBundle = ResourceBundle.getBundle(resourceBundleName);
        } catch (Exception e) {
            _logger.log(Level.SEVERE, "iplanet_util.no_resource_bundle", e);
        }
    
Methods Summary
public java.lang.StringgetString(java.lang.String key, java.lang.Object[] args)
Returns a local string for the caller and format the arguments accordingly.

param
key the key to the local format string
param
args the array of arguments to be provided to the formatter
return
a formatted localized string


        return getStringWithDefault(key, NO_DEFAULT, args);
    
public java.lang.StringgetString(java.lang.String key)
Returns a localized string.

param
key the name of the resource to fetch
return
the localized string

        return getStringWithDefault(key, NO_DEFAULT);
    
public java.lang.StringgetString(java.lang.String key, java.lang.Object arg1)
Returns a local string for the caller and format the arguments accordingly.

param
key the key to the local format string
param
arg1 the one argument to be provided to the formatter
return
a formatted localized string


        return getStringWithDefault(key, NO_DEFAULT, new Object[] {arg1});
    
public java.lang.StringgetString(java.lang.String key, java.lang.Object arg1, java.lang.Object arg2)
Returns a local string for the caller and format the arguments accordingly.

param
key the key to the local format string
param
arg1 first argument to be provided to the formatter
param
arg2 second argument to be provided to the formatter
return
a formatted localized string


        return getStringWithDefault(key, NO_DEFAULT, new Object[] {arg1, arg2});
    
public java.lang.StringgetString(java.lang.String key, java.lang.Object arg1, java.lang.Object arg2, java.lang.Object arg3)
Returns a local string for the caller and format the arguments accordingly.

param
key the key to the local format string
param
arg1 first argument to be provided to the formatter
param
arg2 second argument to be provided to the formatter
param
arg3 third argument to be provided to the formatter
return
a formatted localized string


        return getStringWithDefault(key, NO_DEFAULT,
                                    new Object[] {arg1, arg2, arg3});
    
public java.lang.StringgetString(java.lang.String key, java.lang.Object arg1, java.lang.Object arg2, java.lang.Object arg3, java.lang.Object arg4)
Returns a local string for the caller and format the arguments accordingly.

param
key the key to the local format string
param
arg1 first argument to be provided to the formatter
param
arg2 second argument to be provided to the formatter
param
arg3 third argument to be provided to the formatter
param
arg4 fourth argument to be provided to the formatter
return
a formatted localized string


        return getStringWithDefault(key, NO_DEFAULT, 
                                    new Object[] {arg1, arg2, arg3, arg4});
    
public static synchronized com.sun.enterprise.util.i18n.StringManagerBasegetStringManager(java.lang.String resourceBundleName)
Returns a local string manager for the given resourceBundle name.

param
resourceBundleName name of the resource bundle
return
a local string manager for the given package name

        StringManagerBase mgr = (StringManagerBase) managers.get(resourceBundleName);
        if (mgr == null) {
            mgr = new StringManagerBase(resourceBundleName);
            try {
                managers.put(resourceBundleName, mgr);
            } catch (Exception e) {
                _logger.log(Level.SEVERE,"iplanet_util.error_while_caching",e);
            }
        }
        return mgr;
    
public java.lang.StringgetStringWithDefault(java.lang.String key, java.lang.String defaultValue)
Returns a localized string. If the key is not found, it will return the default given value.

param
key the name of the resource to fetch
param
defaultValue the default return value if not found
return
the localized string


        String value = null;

        try {
            value = this._resourceBundle.getString(key);
        } catch (Exception e) {
            _logger.log(Level.FINE,"No local string for: " + key, e);
        }

        if (value != null) {
            return value;
        } else {
            return defaultValue;
        }
    
public java.lang.StringgetStringWithDefault(java.lang.String key, java.lang.String defaultFormat, java.lang.Object[] arguments)
Returns a local string for the caller and format the arguments accordingly. If the key is not found, it will use the given default format.

param
key the key to the local format string
param
defaultFormat the default format if not found in the resources
param
arguments the set of arguments to provide to the formatter
return
a formatted localized string


        MessageFormat f =
            new MessageFormat( getStringWithDefault(key, defaultFormat) );

        for (int i=0; i<arguments.length; i++) {

            if ( arguments[i] == null ) {

                arguments[i] = "null";

            } else if  ( !(arguments[i] instanceof String) &&
                     !(arguments[i] instanceof Number) &&
                     !(arguments[i] instanceof java.util.Date)) {

                arguments[i] = arguments[i].toString();
            }
        }

        String fmtStr = null; 
        try {
            fmtStr =  f.format(arguments);
        } catch (Exception e) {
            _logger.log(Level.WARNING, "iplanet_util.error_while_formating", e);

            // returns default format
            fmtStr = defaultFormat;
        }
        return fmtStr;
    
public static voidmain(java.lang.String[] args)
Unit test code.

param
args arguments from command line


        long b = System.currentTimeMillis();
        try {
            StringManagerBase sm = 
                StringManagerBase.getStringManager("com.sun.enterprise.util.i18n.LocalStrings");

            String ls = sm.getString("test", Integer.valueOf(7),
                         new java.util.Date(System.currentTimeMillis()),
                         "a disturbance in the Force");

            System.out.println(ls);

            System.out.println( sm.getString("bad") );

        } catch (Exception e) {
            System.out.println("---- Error ---- ");
            e.printStackTrace();
        } finally {
            long a = System.currentTimeMillis();
            System.out.println("Time: " + (a-b) );
        }