jp.terasoluna.fw.web.struts.action
Class DBMessageResources

java.lang.Object
  Extend the aboveorg.apache.struts.util.MessageResources
      Extend the abovejp.terasoluna.fw.web.struts.action.DBMessageResources
All implemented interfaces:
java.io.Serializable

public class DBMessageResources
extends org.apache.struts.util.MessageResources

Message resource class.

Message resource function is a function that fetches message for a specific key such as error message displayed in JSP.
Using this class enables to use not only message resource definition file (message resource of property file format which is generally used in Struts) but also message resource fetched from DB by referring to DB at the time of class load.

Description wherein sample is used

Message resources set in DB are shared across all modules.However, message resources of message resource definition file are independent for each module of Struts. Following sample explains how the message resource function behaves when this class is used.

Sample settings

For example, there are multiple modules such as module A and module B. Message resource settings (message resource definition file)of each module contains the following specifications.

Message resource list
Module name Message key Message statement Message registration destination
Module A message.propMessageResource "moduleA" Message resource definition file
message.dbMessageResource "DB" DB
Module B message.propMessageResource "moduleB" Message resource definition file
message.subMessageResource "subModule" Message resource definition file

Visible range of message resources among modules
In the above list, the visible range of message resources of module A is
The visible range of message resource of module B is
In module A and B, the key of same message resource seems to be message.propMessageResource. However, the message that could be fetched in each module is Please note that the settings of message resource definition file cannot be shared among each module.
DB Message resource shared among modules fetches DB for message.dbMessageResource for module A as well as module B.
Further, as per Struts specifications, Message resource which is set in message resource definition file of module B, cannot be fetched from module A. (Message cannot be fetched even if it is referred by the key module.subMessageResource from module A.)
Points to be noted
Following points should be noted for the message resource handling. Priority sequence for DBMessageResources to fetch the message for a key is as follows.
  1. Message resource which is defined in message resource definition file
  2. Message resource which is defined in DB
  3. Message resource which is defined in message resource definition file which is common to all modules(application-messages.properties)
  4. Message resource which is defined in system message resource definition file (system-messages.properties)
For the contents related to message resource and system message resource which is common to all modules, refer to GlobalMessageResources.

Usage method
For using this class, specify the following in the <message-resource> element of Struts configuration file
Following is a configuration example of Struts configuration file(struts-config.xml).
 <struts-config>
   ...
   <message-resources parameter="MessageResources"
                      factory="jp.terasoluna.fw.web.struts.action.DBMessageResourcesFactory"
   />
   ...
 </struts-config>
 

Configuraition example wherein message resources in DB are fetched
Perform the following settings in the system settings property file (system.properties) for fetching the message resources from DB.

 messages.sql=<SQL(SELECT)statement>
 messages.dao=Implementation class of MessageResourcesDAO
 

Following is an example.

 messages.sql=SELECT MESSAGE_KEY, MESSAGE_VALUE FROM MESSAGES
 messages.dao=jp.terasoluna.fw.web.struts.action.MessageResourcesDAOImpl
 
Points to be noted for performing settings
Message resource configuration example of message resource definition file(property file)
The definition of message resource wherein property file is used, is registered as defined in the property file.

 <Message key>=<Message statement>
 

Following is an example.

 errors.requiredArray={1} of item number {0} is mandatory. 
 errors.alphaNumericStringArray={1} of item number {0} should be half-width alphanumeric character.
 

See Also:
GlobalMessageResources, DBMessageResourcesFactory, MessageResourcesDAO, MessageResourcesDAOImpl, Serialized form

Field Summary
private static java.lang.String DB_MESSAGE_RESOURCES_ERROR
          Error code that indicates failure of message retrieval.
private static java.lang.String DB_MESSAGE_RESOURCES_ERROR_INIT
          Error code that indicates failure of message resource initialization.
private static java.util.Map dbMessages
          Map that stores message key and message statement fetched from DB.
private static org.apache.commons.logging.Log log
          Log class.
private  java.util.Map<java.lang.String,java.lang.String> messages
          Map that stores message key and message statement fetched from message resource definition file.
static java.lang.String MESSAGES_DAO
          Key that is used while fetching DAO in system settings property file (system.properties).
static java.lang.String MESSAGES_SQL
          Key that is used while fetching SQL in system setting property file(system.properties).
private static long serialVersionUID
          Serial version ID
 
Field inherited from class org.apache.struts.util.MessageResources
config, defaultFactory, defaultLocale, factory, formats, returnNull
 
Constructor Summary
DBMessageResources(org.apache.struts.util.MessageResourcesFactory factory, java.lang.String config)
          Generate DBMessageResources by the specified parameter.
DBMessageResources(org.apache.struts.util.MessageResourcesFactory factory, java.lang.String config, boolean returnNull)
          Generate DBMessageResources by the specified parameter.
 
Method Summary
protected static void dbInit()
          Fetch the pair of message key and message statement in DB.
 java.lang.String getMessage(java.util.Locale locale, java.lang.String key)
          Fetch the message statement based on the specified key.
 java.lang.String getMessage(java.lang.String key)
          Fetch the message statement based on the specified key.
protected  void propertyInit(java.lang.String propertyFile)
          Fetch the pair of message key and message statement from the message resource definition file.
 
Method inherited from class org.apache.struts.util.MessageResources
escape, getConfig, getFactory, getMessage, getMessage, getMessage, getMessage, getMessage, getMessage, getMessage, getMessage, getMessage, getMessage, getMessageResources, getReturnNull, isEscape, isPresent, isPresent, localeKey, log, log, messageKey, messageKey, setEscape, setReturnNull
 
Method inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Details

serialVersionUID

private static final long serialVersionUID
Serial version ID

See Also:
Constant field value

MESSAGES_DAO

public static final java.lang.String MESSAGES_DAO
Key that is used while fetching DAO in system settings property file(system.properties).

See Also:
Constant field value

MESSAGES_SQL

public static final java.lang.String MESSAGES_SQL
Key that is used while fetching SQL in system settings property file (system.properties).

See Also:
Constant field value

DB_MESSAGE_RESOURCES_ERROR

private static final java.lang.String DB_MESSAGE_RESOURCES_ERROR
Error code that indicates failure of message retrieval.

See Also:
Constant field value

DB_MESSAGE_RESOURCES_ERROR_INIT

private static final java.lang.String DB_MESSAGE_RESOURCES_ERROR_INIT
Error code that indicates failure of message resource initialization.

See Also:
Constant field value

log

private static org.apache.commons.logging.Log log
Log class.


dbMessages

private static java.util.Map dbMessages
Map that stores message key and message statement fetched from DB. It is shared in class.


messages

private java.util.Map<java.lang.String,java.lang.String> messages
Map that stores message key and message statement fetched from message resource definition file. It differs from the message resource of DB and can be made independent for each module of Struts.

Constructor Details

DBMessageResources

public DBMessageResources(org.apache.struts.util.MessageResourcesFactory factory,
                          java.lang.String config)
Generate DBMessageResources by specified parameter.

Parameter:
factory - Message resource factory
config - Message resource definition file name

DBMessageResources

public DBMessageResources(org.apache.struts.util.MessageResourcesFactory factory,
                          java.lang.String config,
                          boolean returnNull)
Generate DBMessageResources by the specified parameter.

Parameter:
factory - Message resource factory
config - Message resource definition file name
returnNull - When returnNull of org.apache.struts.util.MessageResources is set to false and the message corresponding to the key does not exist, return the message in ???Locale.key??? format.
Method Details

dbInit

protected static void dbInit()
Fetch the pair of message key and message statement in DB.


propertyInit

protected void propertyInit(java.lang.String propertyFile)
Fetch the pair of message key and message statement from the message resource definition file.

Parameter:
propertyFile - Message resource definition file name

getMessage

public java.lang.String getMessage(java.util.Locale locale,
                                   java.lang.String key)
Fetch the message statement based on the specified key. When definition location for different messages exist for the same key, priority sequence of retrieval is as follows.
  1. Message resource of message resource definition file
  2. Message resource that contain message key and message statements in DB
  3. Message resource which is defined in message resource definition file which is common to all modules(application-messages.properties)
  4. Message resource of system message resource definition file (system-messages.properties)

When the initialization process is not performed for all the message resources or when unable to fetch the value that corresponds to the message key from any of the definition, based on the value of "returnNull" at the time of generation, the value returned is either null or in the struts format(???Locale.key???).

The locale specified here is not considered. In short, performs the operation same as getMessage(key).

Definition:
getMessage in class org.apache.struts.util.MessageResources
Parameter:
locale - Message locale. It is not taken into consideration.
key- Message key
Returns:
Message statement that corresponds to locale and key

getMessage

public java.lang.String getMessage(java.lang.String key)
Fetch the message statement based on the specified key. When the same key exists in the defined location of different messages, priority sequence of retrieval is as follows.
  1. Message resource definition file
  2. Table that contains message key and message statement in DB
  3. Message resource which is defined in message resource definition file which is common to all modules(application-messages.properties)
  4. Messages of system message resource definition file(system-messages.properties)
When the initialization process is not performed for all the message resources or when unable to fetch the value corresponding to the message key from any of the definition, null is returned.

Override:
getMessage in class org.apache.struts.util.MessageResources
Parameter:
key - Message key
Returns:
Message statement that corresponds to the key