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

StringManager

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

One StringManager per package can be created and accessed by the getManager method call. The ResourceBundle name is constructed from the given package name in the constructor plus the suffix of "LocalStrings". Thie means that localized information will be contained in a LocalStrings.properties file located in the package directory of the classpath.

Example: [LocalStrings.properties] test=At {1,time} on {1,date}, there was {2} on planet {0,number,integer} StringManager sm = StringManager.getManager(this); ..... 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
RES_BUNDLE_NM
name of the resource bundle property file name
private static Hashtable
managers
cache for all the local string managers (per pkg)
Constructors Summary
private StringManager(String packageName)
Initializes the resource bundle.

param
packageName name of the package


                         
       
        super(packageName + RES_BUNDLE_NM);        
    
Methods Summary
public static synchronized com.sun.enterprise.util.i18n.StringManagergetManager(java.lang.String packageName)
Returns a local string manager for the given package name.

param
packageName name of the package of the src
return
a local string manager for the given package name


        StringManager mgr = (StringManager) managers.get(packageName);

        if (mgr == null) {
            mgr = new StringManager(packageName);
            try {
                managers.put(packageName, mgr);
            } catch (Exception e) {
                _logger.log(Level.SEVERE,"iplanet_util.error_while_caching",e);
            }
        }
        return mgr;
    
public static synchronized com.sun.enterprise.util.i18n.StringManagergetManager(java.lang.Class callerClass)
Returns a local string manager for the given package name.

param
callerClass the object making the call
return
a local string manager for the given package name


        try {
            Package pkg = callerClass.getPackage();
            if (pkg != null) {
                String pkgName = pkg.getName();
                return getManager(pkgName);
            } else {
                // class does not belong to any pkg
                String pkgName = callerClass.getName();
                return getManager(pkgName);
            }
        } catch (Exception e) {
            _logger.log(Level.SEVERE, "iplanet_util.error_in_getMgr", e);

            // dummy string manager
            return getManager("");
        }
    
public static voidmain(java.lang.String[] args)
Unit test code.

param
args arguments from command line


        long b = System.currentTimeMillis();
        try {
            StringManager sm = 
                StringManager.getManager("com.sun.enterprise.util.i18n");

            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) );
        }