Create messages
If your command throws an ECApplicationException that uses a new message, we must create this new message. Creating a new message involves the following steps:
Procedure
- Creating a new class containing the message keys.
The first step in creating new user messages is to create a class containing the new message keys. A message key is a unique indicator used by the logging service to locate the corresponding message text in a resource bundle. This new class should be created within our own package and stored in the WebSphereCommerceServerExtensionsLogic project.
Consider an example, called MyNewMessages, in which you create a new class, called MyMessageKeys containing the _ERR_CUSTOMER and _ERR_CUSTOMER_INVALID_ID message keys and you put this class in the com.mycompany.messages package. In this case, the class definition appears as follows:
public class MyMessageKeys { public static String _ERR_CUSTOMER="_ERR_CUSTOMER"; public static String _ERR_CUSTOMER_INVALID_ID="_ERR_CUSTOMER_INVALID_ID"; }
Providing String wrappers for message keys allows the compiler to check their validity.
- Creating a new class containing the ECMessage objects.
Within the same package createdd the class for our message keys, create another class containing the ECMessage objects. The ECMessage class defines the structure of a message object. It is used to retrieve and persist locale-sensitive text messages.
The message object has the following attributes: severity, type, key, resource bundle and associated resource bundle. There are several constructor methods for this class.
Following the MyNewMessages example, create a new class called MyMessages within the com.mycompany.messages package, as follows:
/** * An error message that an invalid customer id was specified. */ public static final ECMessage _ERR_CUSTOMER_INVALID_ID = new ECMessage(WcContentMessageKey._ERR_CUSTOMER_INVALID_ID_REASON_CODE, ECMessageType.USER, WcContentMessageKey._ERR_CUSTOMER_INVALID_ID, USER_RESOURCE_BUNDLE, ECMessageSeverity.ERR, null, null, null);
- Creating a resource bundle.
We must create a new resource bundle, in which the message keys with corresponding message text are stored. This resource bundle can be implemented either as a Java object, or as a properties file. IBM recommends that we use properties files, since they are easier to translate and maintain. Properties files are used for WebSphere Commerce messages.
To continue the MyNewMessages example, create a text file by the name of ecCustomerMessages.properties. Place the messages in the appropriate directory:
- single store servlet
(Developer) workspace_dir\Stores\Web Content\WEB-INF\classes\storeDirwhere storeDir is the name of the store.
- WebSphere Commerce Accelerator
(Developer) workspace_dir\CommerceAccelerator\Web Content\WEB-INF\classes
- Administration Console
(Developer) workspace_dir\SiteAdministration\Web Content\WEB-INF\classes
- Organization Administration Console
(Developer) workspace_dir\OrganizationAdministration\Web Content\WEB-INF\classes
- globally by any servlet in the enterprise application
(Developer) workspace_dir\WebSphereCommerceServer\properties
The preceding directories are specified within the context of the development environment. The messages can also be within the WebSphereCommerceExtensionLogic project.
Since the properties file contains pairs of message keys and the corresponding message text, the ecCustomerMessages.properties file contains the following lines:
_ERR_CUSTOMER_MESSAGE = The customer message "{0}". _ERR_CUSTOMER_INVALID_ID = Invalid ID "{0}".
Related concepts
Command error handling
Logging and tracing