]> git.mxchange.org Git - jjobs-war.git/blobdiff - src/java/org/mxchange/jjobs/beans/BaseJobsController.java
Please cherry-pick:
[jjobs-war.git] / src / java / org / mxchange / jjobs / beans / BaseJobsController.java
index adc9ec6b0c6dc74d19362301a17cad7e2e3a2cf1..a9f461ab756ed5bbe2cb4c4b700634ff96dfe256 100644 (file)
@@ -17,6 +17,7 @@
 package org.mxchange.jjobs.beans;
 
 import java.io.Serializable;
+import java.security.Principal;
 import java.text.MessageFormat;
 import java.util.Locale;
 import java.util.MissingResourceException;
@@ -36,6 +37,29 @@ public abstract class BaseJobsController implements Serializable {
         */
        private static final long serialVersionUID = 50_837_597_127_567_140L;
 
+       /**
+        * Determines principal's name or returns null if no principal (security) is
+        * set.
+        * <p>
+        * @return Principal's name or null
+        */
+       protected String determinePrincipalName () {
+               // Get principal
+               Principal userPrincipal = FacesContext.getCurrentInstance().getExternalContext().getUserPrincipal();
+
+               // Init with null
+               String principalName = null;
+
+               // Is the principal set?
+               if (userPrincipal instanceof Principal) {
+                       // Get principal's name
+                       principalName = userPrincipal.getName();
+               }
+
+               // Return it
+               return principalName;
+       }
+
        /**
         * Returns given property key or throws an exception if not found.
         * <p>
@@ -70,7 +94,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 +133,7 @@ public abstract class BaseJobsController implements Serializable {
         * 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
@@ -123,35 +147,46 @@ public abstract class BaseJobsController implements Serializable {
         * 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"); //NOI18N
+               } else if (clientId.isEmpty()) {
+                       // Is empty
+                       throw new IllegalArgumentException("clientId is null"); //NOI18N
+               } else if (null == i18nKey) {
+                       // Throw NPE
+                       throw new NullPointerException("i18nKey is null"); //NOI18N
+               } else if (i18nKey.isEmpty()) {
+                       // Is empty
+                       throw new IllegalArgumentException("i18nKey is null"); //NOI18N
+               }
 
-               // Get bundle name
-               String messageBundleName = context.getApplication().getMessageBundle();
+               // Get current locale
+               Locale locale = FacesContext.getCurrentInstance().getViewRoot().getLocale();
 
-               // Get locale
-               Locale locale = context.getViewRoot().getLocale();
+               // Get bundle bundle
+               ResourceBundle bundle = ResourceBundle.getBundle("org.mxchange.localization.bundle", locale);
 
-               // 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));
+               FacesContext.getCurrentInstance().addMessage(clientId, new FacesMessage(message));
        }
 
 }