From: Roland Häder Date: Fri, 12 Aug 2016 15:28:16 +0000 (+0200) Subject: Please cvherry-pick: X-Git-Url: https://git.mxchange.org/?a=commitdiff_plain;h=4d9fd803f8e45efccbc102bf629b849d14d21eae;p=jjobs-war.git Please cvherry-pick: - rewrote showFacesMessage() as this was the wrong way, maybe now it is better? Signed-off-by: Roland Häder --- diff --git a/src/java/org/mxchange/jjobs/beans/BaseJobsController.java b/src/java/org/mxchange/jjobs/beans/BaseJobsController.java index adc9ec6b..d57534b7 100644 --- a/src/java/org/mxchange/jjobs/beans/BaseJobsController.java +++ b/src/java/org/mxchange/jjobs/beans/BaseJobsController.java @@ -18,7 +18,6 @@ package org.mxchange.jjobs.beans; 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; @@ -70,7 +69,7 @@ public abstract class BaseJobsController implements Serializable { // 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 @@ -109,7 +108,7 @@ public abstract class BaseJobsController implements Serializable { * exception is being inserted into the message. *

* @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 @@ -123,33 +122,44 @@ public abstract class BaseJobsController implements Serializable { * Shows a faces message with given message (i18n) key. *

* @param clientId Client id to send message to - * @param i18nKey Message key + * @param i18nKey Message key + *

+ * @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)); }