org.apache.click.util
Class MessagesMap

java.lang.Object
  extended by org.apache.click.util.MessagesMap
All Implemented Interfaces:
Map<String,String>

public class MessagesMap
extends Object
implements Map<String,String>

Provides a localized read only messages Map for Page and Control classes.

A MessagesMap instance is available in each Velocity page using the name "messages".

For example suppose you have a localized page title, which is stored in the Page's properties file. You can access page "title" message in your page template via:

 $messages.title 
This is roughly equivalent to making the call:
 public void onInit() {
    ..
    addModel("title", getMessage("title");
 } 
Please note if the specified message does not exist in your Page's properties file, or if the Page does not have a properties file, then a MissingResourceException will be thrown.

The ClickServlet adds a MessagesMap instance to the Velocity Context before it is merged with the page template.


Nested Class Summary
 
Nested classes/interfaces inherited from interface java.util.Map
Map.Entry<K,V>
 
Field Summary
protected  Class<?> baseClass
          The base class.
protected static Object CACHE_LOAD_LOCK
          The cache key set load lock.
protected  String globalBaseName
          The class global resource bundle base name.
protected  Locale locale
          The resource bundle locale.
protected  Map<String,String> messages
          The map of localized messages.
protected static Map<Object,Map<String,String>> MESSAGES_CACHE
          Cache of messages keyed by bundleName + Locale name.
protected static Set<String> NOT_FOUND_CACHE
          Cache of resource bundle and locales which were not found.
 
Constructor Summary
MessagesMap(Class<?> baseClass, String globalResource)
          Create a resource bundle messages Map adaptor for the given object's class resource bundle, the global resource bundle and Context.
MessagesMap(Class<?> baseClass, String globalResource, Locale locale)
          Create a resource bundle messages Map adaptor for the given object's class resource bundle, the global resource bundle and Context.
 
Method Summary
 void clear()
          This method is not supported and will throw UnsupportedOperationException if invoked.
 boolean containsKey(Object key)
           
 boolean containsValue(Object value)
           
protected  ResourceBundle createResourceBundle(String resourceName, Locale locale)
          Return the ResourceBundle for the given resource name and locale.
protected  void ensureInitialized()
          This method initializes and populates the internalmessages map and MESSAGES_CACHE if it is not already initialized.
 Set<Map.Entry<String,String>> entrySet()
           
 String get(Object key)
          Return localized resource message for the given key.
 boolean isEmpty()
           
 Set<String> keySet()
           
protected  void loadResourceValuesIntoMap(String resourceBundleName, Map<String,String> map)
          Load the values of the given resourceBundleName into the map.
 String put(String key, String value)
          This method is not supported and will throw UnsupportedOperationException if invoked.
 void putAll(Map<? extends String,? extends String> map)
          This method is not supported and will throw UnsupportedOperationException if invoked.
 String remove(Object key)
          This method is not supported and will throw UnsupportedOperationException if invoked.
 int size()
           
 String toString()
           
 Collection<String> values()
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface java.util.Map
equals, hashCode
 

Field Detail

NOT_FOUND_CACHE

protected static final Set<String> NOT_FOUND_CACHE
Cache of resource bundle and locales which were not found.


MESSAGES_CACHE

protected static final Map<Object,Map<String,String>> MESSAGES_CACHE
Cache of messages keyed by bundleName + Locale name.


CACHE_LOAD_LOCK

protected static final Object CACHE_LOAD_LOCK
The cache key set load lock.


baseClass

protected final Class<?> baseClass
The base class.


globalBaseName

protected final String globalBaseName
The class global resource bundle base name.


messages

protected Map<String,String> messages
The map of localized messages.


locale

protected final Locale locale
The resource bundle locale.

Constructor Detail

MessagesMap

public MessagesMap(Class<?> baseClass,
                   String globalResource)
Create a resource bundle messages Map adaptor for the given object's class resource bundle, the global resource bundle and Context.

Messages located in the object's resource bundle will override any messages defined in the global resource bundle.

Parameters:
baseClass - the target class
globalResource - the global resource bundle name

MessagesMap

public MessagesMap(Class<?> baseClass,
                   String globalResource,
                   Locale locale)
Create a resource bundle messages Map adaptor for the given object's class resource bundle, the global resource bundle and Context.

Messages located in the object's resource bundle will override any messages defined in the global resource bundle.

Parameters:
baseClass - the target class
globalResource - the global resource bundle name
locale - the resource bundle locale.
Method Detail

size

public int size()
Specified by:
size in interface Map<String,String>
See Also:
Map.size()

isEmpty

public boolean isEmpty()
Specified by:
isEmpty in interface Map<String,String>
See Also:
Map.isEmpty()

containsKey

public boolean containsKey(Object key)
Specified by:
containsKey in interface Map<String,String>
See Also:
Map.containsKey(Object)

containsValue

public boolean containsValue(Object value)
Specified by:
containsValue in interface Map<String,String>
See Also:
Map.containsValue(Object)

get

public String get(Object key)
Return localized resource message for the given key. If the message is not found a MissingResourceException will be thrown.

Specified by:
get in interface Map<String,String>
Throws:
MissingResourceException - if the given key was not found
See Also:
Map.get(Object)

put

public String put(String key,
                  String value)
This method is not supported and will throw UnsupportedOperationException if invoked.

Specified by:
put in interface Map<String,String>
See Also:
Map.put(Object, Object)

remove

public String remove(Object key)
This method is not supported and will throw UnsupportedOperationException if invoked.

Specified by:
remove in interface Map<String,String>
See Also:
Map.remove(Object)

putAll

public void putAll(Map<? extends String,? extends String> map)
This method is not supported and will throw UnsupportedOperationException if invoked.

Specified by:
putAll in interface Map<String,String>
See Also:
Map.putAll(Map)

clear

public void clear()
This method is not supported and will throw UnsupportedOperationException if invoked.

Specified by:
clear in interface Map<String,String>
See Also:
Map.clear()

keySet

public Set<String> keySet()
Specified by:
keySet in interface Map<String,String>
See Also:
Map.keySet()

values

public Collection<String> values()
Specified by:
values in interface Map<String,String>
See Also:
Map.values()

entrySet

public Set<Map.Entry<String,String>> entrySet()
Specified by:
entrySet in interface Map<String,String>
See Also:
Map.entrySet()

toString

public String toString()
Overrides:
toString in class Object
See Also:
toString()

createResourceBundle

protected ResourceBundle createResourceBundle(String resourceName,
                                              Locale locale)
Return the ResourceBundle for the given resource name and locale. By default this method will create a ResourceBundle using the standard JDK method: ResourceBundle.getBundle(java.lang.String, java.util.Locale, java.lang.ClassLoader).

You can create your own custom ResourceBundle by overriding this method.

In order for Click to use your custom MessagesMap implementation, you need to provide your own MessagesMapService or extend DefaultMessagesMapService.

The method createMessagesMap, can be implemented to return your custom MessagesMap instances.

Parameters:
resourceName - the resource bundle name
locale - the resource bundle locale.
Returns:
the ResourceBundle for the given resource name and locale

ensureInitialized

protected void ensureInitialized()
This method initializes and populates the internalmessages map and MESSAGES_CACHE if it is not already initialized.

Please Note: populating MESSAGES_CACHE is not thread safe and access to the cache must be properly synchronized.


loadResourceValuesIntoMap

protected void loadResourceValuesIntoMap(String resourceBundleName,
                                         Map<String,String> map)
Load the values of the given resourceBundleName into the map.

Parameters:
resourceBundleName - the resource bundle name
map - the map to load resource values into