Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

org.common.i18n
Class ContextUtility  view ContextUtility download ContextUtility.java

java.lang.Object
  extended byorg.common.i18n.ContextUtility
Direct Known Subclasses:
ExceptionUtility, LoggingUtility

public class ContextUtility
extends java.lang.Object

This class serves as a delegate of the ResourceBundle class, the purpose is to have a mechanism which can perform lookups property resource bundles, and which can also cache previously loaded bundles for quicker access.

First of all, this class keeps a inner lookup table that will keep track of the already loaded resource bundles, by default a cached entry will be returned when it is requested. This class also allows to force a reloading of the resource bundle in case it has already been cached, for example when the resource bundle has been changed by an external process.

Second, it is desired to have a more intelligent lookup in the resource bundles. The keys are called contexts because the are formatted according to a very specific syntax. See below for some examples. Furthermore, when a context is not found in a resource bundle, the parent context will looked up, until a proper value is found. In the exceptional case no value can be returned the context key itself will be returned, this way we avoid throwing unnecessary exceptions.

The context keys can be described as follows:

So here are some examples of valid and non-valid context keys:

abc.def.ghi VALID
a.def.g VALID
abc.DEF.Gh VALID
abc.def.123 Only alphabetical characters in words
abc.def..ghi A dot must always follow a word
abc.d-f.ghi Only alphabetical characters in words


Nested Class Summary
private static class ContextUtility.ResourceBundleArguments
          This private inner class is simply a holder object that contains the three required parameters for the resource bundle loading.
 
Field Summary
private static java.util.HashMap lookupTable
           
private  java.util.ResourceBundle resourceBundle
           
 
Constructor Summary
protected ContextUtility()
          Constructor for the ContextUtility object, for the sake of inheritance it has been made protected.
private ContextUtility(ContextUtility.ResourceBundleArguments arguments)
          This private constructor takes the arguments as a parameter, this will be used as a key in the lookup table.
 
Method Summary
protected  java.lang.String findMessage(java.lang.String context)
          Recursively goes through all the ancestors in case a resource bundle key lookup fails.
static ContextUtility getInstance(java.lang.String baseName)
          Gets the instance attribute of the ContextUtility class based only on the name of the resource bundle.
static ContextUtility getInstance(java.lang.String baseName, java.util.Locale locale)
          Gets the instance attribute of the ContextUtility class based on the name of the resource bundle and the locale to use.
static ContextUtility getInstance(java.lang.String baseName, java.util.Locale locale, java.lang.ClassLoader classLoader)
          Gets the instance attribute of the ContextUtility class based on the name of the resource bundle, the locale to use and the class loader used to load this resource.
static ContextUtility getInstance(java.lang.String baseName, java.util.Locale locale, java.lang.ClassLoader classLoader, boolean forceReload)
          Gets the instance attribute of the ContextUtility class based on the name of the resource bundle, the locale to use and the class loader used to load this resource.
 java.util.Locale getLocale()
          Returns the locale of this ContextUtility's instance.
 java.lang.String getMessage(java.lang.String context)
          Finds the message associated to the argument context.
 java.lang.String getMessage(java.lang.String context, java.lang.Object parameter)
          Finds the message associated to the argument context.
 java.lang.String getMessage(java.lang.String context, java.lang.Object[] parameters)
          Finds the message associated to the argument context.
protected static java.lang.String getParentContext(java.lang.String context)
          Finds the parent context of the argument context, in case the argument is null or has no parent context null is returned.
static void invalidateCache()
          This method should be called if the resource bundles have been changed and the internal cache needs to be reloaded.
static ContextUtility newInstance(java.lang.String baseName, java.util.Locale locale, java.lang.ClassLoader classLoader)
          Instantiates and returns a new instance of this class using the argument parameters.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

lookupTable

private static final java.util.HashMap lookupTable

resourceBundle

private java.util.ResourceBundle resourceBundle
Constructor Detail

ContextUtility

protected ContextUtility()
Constructor for the ContextUtility object, for the sake of inheritance it has been made protected.


ContextUtility

private ContextUtility(ContextUtility.ResourceBundleArguments arguments)
This private constructor takes the arguments as a parameter, this will be used as a key in the lookup table.

Method Detail

getInstance

public static ContextUtility getInstance(java.lang.String baseName)
Gets the instance attribute of the ContextUtility class based only on the name of the resource bundle.

This is equivalent to getInstance( baseName, Locale.getDefault() ) .


getInstance

public static ContextUtility getInstance(java.lang.String baseName,
                                         java.util.Locale locale)
Gets the instance attribute of the ContextUtility class based on the name of the resource bundle and the locale to use.

This is equivalent to getInstance( baseName, Locale.getDefault(), Thread.currentThread().getContextClassLoader() ) .


getInstance

public static ContextUtility getInstance(java.lang.String baseName,
                                         java.util.Locale locale,
                                         java.lang.ClassLoader classLoader)
Gets the instance attribute of the ContextUtility class based on the name of the resource bundle, the locale to use and the class loader used to load this resource.

This is equivalent to getInstance( baseName, Locale.getDefault(), ClassLoader.getSystemClassLoader(), false ) .


getInstance

public static ContextUtility getInstance(java.lang.String baseName,
                                         java.util.Locale locale,
                                         java.lang.ClassLoader classLoader,
                                         boolean forceReload)
Gets the instance attribute of the ContextUtility class based on the name of the resource bundle, the locale to use and the class loader used to load this resource.

A boolean argument is used to indicate whether or not to force the method to load this resource bundle even if it might have been cached before. In case it is forced the cached copy is overwritten.

For any of the arguments you may pass null, this will result in the default value to be used.
argument-default value map
baseName baseName
locale Locale.getDefault()
classLoader ClassLoader.getSystemClassLoader()


newInstance

public static ContextUtility newInstance(java.lang.String baseName,
                                         java.util.Locale locale,
                                         java.lang.ClassLoader classLoader)
Instantiates and returns a new instance of this class using the argument parameters. The difference with the getInstance(...) method is that this instance will not be registered in the internal cache, if the user loses the reference it will be garbage collected by the Virtual Machine.


getParentContext

protected static java.lang.String getParentContext(java.lang.String context)
Finds the parent context of the argument context, in case the argument is null or has no parent context null is returned.


getLocale

public java.util.Locale getLocale()
Returns the locale of this ContextUtility's instance.


getMessage

public java.lang.String getMessage(java.lang.String context)
Finds the message associated to the argument context.

This is equivalent to getMessage( context, null ).


getMessage

public java.lang.String getMessage(java.lang.String context,
                                   java.lang.Object parameter)
Finds the message associated to the argument context.

The parameter in the message will be replaced by the object in the parameter argument.


getMessage

public java.lang.String getMessage(java.lang.String context,
                                   java.lang.Object[] parameters)
Finds the message associated to the argument context.

Any parameters in the message will be replaced by the objects in the parameters object array, the i-th parameter will be replaced by the i-th entry in the array. Take a look at java.text.MessageFormat for more details.

In case there were no properties found at construction time the context will be returned directly.


invalidateCache

public static void invalidateCache()
This method should be called if the resource bundles have been changed and the internal cache needs to be reloaded.

After this call any resource bundles will be lazy loaded as if this class has been newly constructed.


findMessage

protected java.lang.String findMessage(java.lang.String context)
Recursively goes through all the ancestors in case a resource bundle key lookup fails.

Returns null if the argument is null or if it was not possible to find any message.