import java.io.Serializable;
import java.text.MessageFormat;
-import java.util.Locale;
import java.util.MissingResourceException;
import java.util.ResourceBundle;
import javax.faces.application.FacesMessage;
// Is it null?
if (null == contextValue) {
// Throw NPE
- throw new NullPointerException("parameterKey=" + parameterKey + " is not set.");
+ throw new NullPointerException(MessageFormat.format("parameterKey={0} is not set.", parameterKey)); //NOI18N
}
// Return it
* exception is being inserted into the message.
* <p>
* @param clientId Client id to send message to
- * @param cause Causing exception
+ * @param cause Causing exception
*/
protected void showFacesMessage (final String clientId, final Throwable cause) {
// Trace message
* Shows a faces message with given message (i18n) key.
* <p>
* @param clientId Client id to send message to
- * @param i18nKey Message key
+ * @param i18nKey Message key
+ * <p>
+ * @throws NullPointerException If clientId or i18nKey is null
+ * @throws IllegalArgumentException If clientId or i18nKey is empty
*/
- protected void showFacesMessage (final String clientId, final String i18nKey) {
- // Get faces context
- FacesContext context = FacesContext.getCurrentInstance();
+ protected void showFacesMessage (final String clientId, final String i18nKey) throws NullPointerException, IllegalArgumentException {
+ // Both parameter must be valid
+ if (null == clientId) {
+ // Throw NPE
+ throw new NullPointerException("clientId is null");
+ } else if (clientId.isEmpty()) {
+ // Is empty
+ throw new IllegalArgumentException("clientId is null");
+ } else if (null == i18nKey) {
+ // Throw NPE
+ throw new NullPointerException("i18nKey is null");
+ } else if (i18nKey.isEmpty()) {
+ // Is empty
+ throw new IllegalArgumentException("i18nKey is null");
+ }
- // Get bundle name
- String messageBundleName = context.getApplication().getMessageBundle();
+ // Get facet context
+ FacesContext context = FacesContext.getCurrentInstance();
- // Get locale
- Locale locale = context.getViewRoot().getLocale();
+ // Get bundle
+ ResourceBundle bundle = context.getApplication().getResourceBundle(context, "msg"); //NOI18N
- // Init bundle variable
- ResourceBundle bundle;
+ // Default is i18nKey
+ String message = i18nKey;
+ // Try it
try {
- // Get bundle
- bundle = ResourceBundle.getBundle(messageBundleName, locale);
+ // Get message
+ message = bundle.getString(i18nKey);
} catch (final MissingResourceException ex) {
- // Call other method
- this.showFacesMessage(clientId, ex);
- return;
+ // Did not find it, ignored
}
- // Get message
- String message = bundle.getString(i18nKey);
-
// Get context and add message
context.addMessage(clientId, new FacesMessage(message));
}