StringManagerBasepublic 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
|
Fields Summary |
---|
private static Logger | _loggerlogger used for this class | private ResourceBundle | _resourceBundleresource bundle to be used by this manager | private static final String | NO_DEFAULTdefault value used for undefined local string | private static Hashtable | managerscache for all the local string managers (per pkg) |
Constructors Summary |
---|
protected StringManagerBase(String resourceBundleName)Initializes 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.String | getString(java.lang.String key, java.lang.Object[] args)Returns a local string for the caller and format the arguments
accordingly.
return getStringWithDefault(key, NO_DEFAULT, args);
| public java.lang.String | getString(java.lang.String key)Returns a localized string.
return getStringWithDefault(key, NO_DEFAULT);
| public java.lang.String | getString(java.lang.String key, java.lang.Object arg1)Returns a local string for the caller and format the arguments
accordingly.
return getStringWithDefault(key, NO_DEFAULT, new Object[] {arg1});
| public java.lang.String | getString(java.lang.String key, java.lang.Object arg1, java.lang.Object arg2)Returns a local string for the caller and format the arguments
accordingly.
return getStringWithDefault(key, NO_DEFAULT, new Object[] {arg1, arg2});
| public java.lang.String | getString(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.
return getStringWithDefault(key, NO_DEFAULT,
new Object[] {arg1, arg2, arg3});
| public java.lang.String | getString(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.
return getStringWithDefault(key, NO_DEFAULT,
new Object[] {arg1, arg2, arg3, arg4});
| public static synchronized com.sun.enterprise.util.i18n.StringManagerBase | getStringManager(java.lang.String resourceBundleName)Returns a local string manager for the given resourceBundle 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.String | getStringWithDefault(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.
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.String | getStringWithDefault(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.
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 void | main(java.lang.String[] args)Unit test code.
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) );
}
|
|