Package javax.jdo.spi
Class I18NHelper
- java.lang.Object
-
- javax.jdo.spi.I18NHelper
-
public class I18NHelper extends java.lang.ObjectHelper class for constructing messages from bundles. The intended usage of this class is to construct a new instance bound to a bundle, as inI18NHelper msg = I18NHelper.getInstance("javax.jdo.Bundle");This call uses the class loader that loaded the I18NHelper class to find the specified Bundle. The class provides two overloaded getInstance methods allowing to specify a different class loader:
getInstance(Class cls)looks for a bundle called "Bundle.properties" located in the package of the specified class object andgetInstance(String bundleName,ClassLoader loader)uses the specified class loader to find the bundle.Subsequently, instance methods can be used to format message strings using the text from the bundle, as in
throw new JDOFatalInternalException (msg.msg("ERR_NoMetadata", cls.getName()));- Since:
- 1.0.1
- Version:
- 1.1
-
-
Field Summary
Fields Modifier and Type Field Description private java.util.ResourceBundlebundleThe bundle used by this instance of the helper.private static java.util.Hashtable<java.lang.String,java.util.ResourceBundle>bundlesBundles that have already been loadedprivate static java.lang.StringbundleSuffixThe unqualified standard name of a bundle.private java.lang.ThrowablefailureThrowable if ResourceBundle couldn't be loadedprivate static java.util.Hashtable<java.lang.String,I18NHelper>helpersHelper instances that have already been createdprivate static java.util.LocalelocaleThe default locale for this VM.
-
Constructor Summary
Constructors Modifier Constructor Description privateI18NHelper()ConstructorprivateI18NHelper(java.lang.String bundleName, java.lang.ClassLoader loader)Constructor for an instance bound to a bundle.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description private voidassertBundle()Assert resources availableprivate voidassertBundle(java.lang.String key)Assert resources availablestatic I18NHelpergetInstance(java.lang.Class cls)An instance bound to a bundle.static I18NHelpergetInstance(java.lang.String bundleName)An instance bound to a bundle.static I18NHelpergetInstance(java.lang.String bundleName, java.lang.ClassLoader loader)An instance bound to a bundle.private static java.lang.StringgetMessage(java.util.ResourceBundle messages, java.lang.String messageKey)Returns message asStringprivate static java.lang.StringgetMessage(java.util.ResourceBundle messages, java.lang.String messageKey, boolean arg)Formats message by adding abooleanas an argument.private static java.lang.StringgetMessage(java.util.ResourceBundle messages, java.lang.String messageKey, int arg)Formats message by adding anintas an argument.private static java.lang.StringgetMessage(java.util.ResourceBundle messages, java.lang.String messageKey, java.lang.Object arg)Formats message by adding anObjectargument.private static java.lang.StringgetMessage(java.util.ResourceBundle messages, java.lang.String messageKey, java.lang.Object[] msgArgs)Formats message by adding array of argumentsprivate static java.lang.StringgetMessage(java.util.ResourceBundle messages, java.lang.String messageKey, java.lang.Object arg1, java.lang.Object arg2)Formats message by adding twoObjectarguments.private static java.lang.StringgetMessage(java.util.ResourceBundle messages, java.lang.String messageKey, java.lang.Object arg1, java.lang.Object arg2, java.lang.Object arg3)Formats message by adding threeObjectarguments.private static java.lang.StringgetPackageName(java.lang.String className)Returns the package portion of the specified class.java.util.ResourceBundlegetResourceBundle()Returns the resource bundle used by this I18NHelper.private static java.lang.ClassLoadergetSystemClassLoaderPrivileged()Get the system class loader.private static java.util.ResourceBundleloadBundle(java.lang.String bundleName, java.lang.ClassLoader loader)Load ResourceBundle by bundle namejava.lang.Stringmsg(java.lang.String messageKey)Message formatterjava.lang.Stringmsg(java.lang.String messageKey, boolean arg)Message formatterjava.lang.Stringmsg(java.lang.String messageKey, int arg)Message formatterjava.lang.Stringmsg(java.lang.String messageKey, java.lang.Object arg1)Message formatterjava.lang.Stringmsg(java.lang.String messageKey, java.lang.Object[] args)Message formatterjava.lang.Stringmsg(java.lang.String messageKey, java.lang.Object arg1, java.lang.Object arg2)Message formatterjava.lang.Stringmsg(java.lang.String messageKey, java.lang.Object arg1, java.lang.Object arg2, java.lang.Object arg3)Message formatter
-
-
-
Field Detail
-
bundles
private static java.util.Hashtable<java.lang.String,java.util.ResourceBundle> bundles
Bundles that have already been loaded
-
helpers
private static java.util.Hashtable<java.lang.String,I18NHelper> helpers
Helper instances that have already been created
-
locale
private static java.util.Locale locale
The default locale for this VM.
-
bundle
private java.util.ResourceBundle bundle
The bundle used by this instance of the helper.
-
failure
private java.lang.Throwable failure
Throwable if ResourceBundle couldn't be loaded
-
bundleSuffix
private static final java.lang.String bundleSuffix
The unqualified standard name of a bundle.- See Also:
- Constant Field Values
-
-
Constructor Detail
-
I18NHelper
private I18NHelper()
Constructor
-
I18NHelper
private I18NHelper(java.lang.String bundleName, java.lang.ClassLoader loader)Constructor for an instance bound to a bundle.- Parameters:
bundleName- the name of the resource bundleloader- the class loader from which to load the resource bundle
-
-
Method Detail
-
getInstance
public static I18NHelper getInstance(java.lang.String bundleName)
An instance bound to a bundle. This method uses the current class loader to find the bundle.- Parameters:
bundleName- the name of the bundle- Returns:
- the helper instance bound to the bundle
-
getInstance
public static I18NHelper getInstance(java.lang.Class cls)
An instance bound to a bundle. This method figures out the bundle name for the class object's package and uses the class' class loader to find the bundle. Note, the specified class object must not benull.- Parameters:
cls- the class object from which to load the resource bundle- Returns:
- the helper instance bound to the bundle
-
getInstance
public static I18NHelper getInstance(java.lang.String bundleName, java.lang.ClassLoader loader)
An instance bound to a bundle. This method uses the specified class loader to find the bundle. Note, the specified class loader must not benull.- Parameters:
bundleName- the name of the bundleloader- the class loader from which to load the resource bundle- Returns:
- the helper instance bound to the bundle
-
msg
public java.lang.String msg(java.lang.String messageKey)
Message formatter- Parameters:
messageKey- the message key- Returns:
- the resolved message text
-
msg
public java.lang.String msg(java.lang.String messageKey, java.lang.Object arg1)Message formatter- Parameters:
messageKey- the message keyarg1- the first argument- Returns:
- the resolved message text
-
msg
public java.lang.String msg(java.lang.String messageKey, java.lang.Object arg1, java.lang.Object arg2)Message formatter- Parameters:
messageKey- the message keyarg1- the first argumentarg2- the second argument- Returns:
- the resolved message text
-
msg
public java.lang.String msg(java.lang.String messageKey, java.lang.Object arg1, java.lang.Object arg2, java.lang.Object arg3)Message formatter- Parameters:
messageKey- the message keyarg1- the first argumentarg2- the second argumentarg3- the third argument- Returns:
- the resolved message text
-
msg
public java.lang.String msg(java.lang.String messageKey, java.lang.Object[] args)Message formatter- Parameters:
messageKey- the message keyargs- the array of arguments- Returns:
- the resolved message text
-
msg
public java.lang.String msg(java.lang.String messageKey, int arg)Message formatter- Parameters:
messageKey- the message keyarg- the argument- Returns:
- the resolved message text
-
msg
public java.lang.String msg(java.lang.String messageKey, boolean arg)Message formatter- Parameters:
messageKey- the message keyarg- the argument- Returns:
- the resolved message text
-
getResourceBundle
public java.util.ResourceBundle getResourceBundle()
Returns the resource bundle used by this I18NHelper.- Returns:
- the associated resource bundle
- Since:
- 1.1
-
loadBundle
private static final java.util.ResourceBundle loadBundle(java.lang.String bundleName, java.lang.ClassLoader loader)Load ResourceBundle by bundle name- Parameters:
bundleName- the name of the bundleloader- the class loader from which to load the resource bundle- Returns:
- the ResourceBundle
-
assertBundle
private void assertBundle()
Assert resources available- Throws:
JDOFatalInternalException- if the resource bundle could not be loaded during construction.- Since:
- 1.1
-
assertBundle
private void assertBundle(java.lang.String key)
Assert resources available- Parameters:
key- the message key- Throws:
JDOFatalInternalException- if the resource bundle could not be loaded during construction.- Since:
- 1.0.2
-
getMessage
private static final java.lang.String getMessage(java.util.ResourceBundle messages, java.lang.String messageKey)Returns message asString- Parameters:
messages- the resource bundlemessageKey- the message key- Returns:
- the resolved message text
-
getMessage
private static final java.lang.String getMessage(java.util.ResourceBundle messages, java.lang.String messageKey, java.lang.Object[] msgArgs)Formats message by adding array of arguments- Parameters:
messages- the resource bundlemessageKey- the message keymsgArgs- an array of arguments to substitute into the message- Returns:
- the resolved message text
-
getMessage
private static final java.lang.String getMessage(java.util.ResourceBundle messages, java.lang.String messageKey, java.lang.Object arg)Formats message by adding anObjectargument.- Parameters:
messages- the resource bundlemessageKey- the message keyarg- the argument- Returns:
- the resolved message text
-
getMessage
private static final java.lang.String getMessage(java.util.ResourceBundle messages, java.lang.String messageKey, java.lang.Object arg1, java.lang.Object arg2)Formats message by adding twoObjectarguments.- Parameters:
messages- the resource bundlemessageKey- the message keyarg1- the first argumentarg2- the second argument- Returns:
- the resolved message text
-
getMessage
private static final java.lang.String getMessage(java.util.ResourceBundle messages, java.lang.String messageKey, java.lang.Object arg1, java.lang.Object arg2, java.lang.Object arg3)Formats message by adding threeObjectarguments.- Parameters:
messages- the resource bundlemessageKey- the message keyarg1- the first argumentarg2- the second argumentarg3- the third argument- Returns:
- the resolved message text
-
getMessage
private static final java.lang.String getMessage(java.util.ResourceBundle messages, java.lang.String messageKey, int arg)Formats message by adding anintas an argument.- Parameters:
messages- the resource bundlemessageKey- the message keyarg- the argument- Returns:
- the resolved message text
-
getMessage
private static final java.lang.String getMessage(java.util.ResourceBundle messages, java.lang.String messageKey, boolean arg)Formats message by adding abooleanas an argument.- Parameters:
messages- the resource bundlemessageKey- the message keyarg- the argument- Returns:
- the resolved message text
-
getPackageName
private static final java.lang.String getPackageName(java.lang.String className)
Returns the package portion of the specified class.- Parameters:
className- the name of the class from which to extract the package- Returns:
- package portion of the specified class
-
getSystemClassLoaderPrivileged
private static java.lang.ClassLoader getSystemClassLoaderPrivileged()
Get the system class loader. This must be done in a doPrivileged block because of security.
-
-