+++ /dev/null
-/*
- * Copyright (C) 2017, 2018 Free Software Foundation
- *
- * This program is free software: you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation, either version 3 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program. If not, see <http://www.gnu.org/licenses/>.
- */
-package org.mxchange.jcoreee.bean.faces;
-
-import java.io.Serializable;
-import java.security.Principal;
-import java.text.MessageFormat;
-import java.util.ArrayList;
-import java.util.List;
-import java.util.Locale;
-import java.util.MissingResourceException;
-import java.util.ResourceBundle;
-import javax.faces.application.FacesMessage;
-import javax.faces.context.FacesContext;
-
-/**
- * An abstract bean for faces (web) projects.
- * <p>
- * @author Roland Häder<roland@mxchange.org>
- */
-public abstract class BaseFacesBean implements Serializable {
-
- /**
- * Loaded resource bundles ("cached")
- */
- private static final List<ResourceBundle> RESOURCE_BUNDLES;
-
- /**
- * Serial number
- */
- private static final long serialVersionUID = 18_605_498_672_261L;
-
- /**
- * Static initializer
- */
- static {
- // Init resource bundle list
- RESOURCE_BUNDLES = new ArrayList<>(3);
- }
-
- /**
- * Removes all bundles from web application. Typically you want to invoke
- * this method in a ServletContextListener implemetation on the
- * contextDestroyed() method.
- */
- public static void removeBundles () {
- // Clear bundles
- RESOURCE_BUNDLES.clear();
- }
-
- /**
- * Getter for resource bundle list
- * <p>
- * @return Resource bundle list
- */
- @SuppressWarnings ("ReturnOfCollectionOrArrayField")
- protected static List<ResourceBundle> getBundles () {
- return RESOURCE_BUNDLES;
- }
-
- /**
- * Protected constructor
- */
- protected BaseFacesBean () {
- // Call super constructor
- super();
- }
-
- /**
- * 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
- final 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>
- * @param parameterKey Property key
- * <p>
- * @return Property value
- */
- protected int getIntegerContextParameter (final String parameterKey) throws NullPointerException, NumberFormatException {
- // Get context parameter
- final Integer contextValue = Integer.parseInt(this.getStringContextParameter(parameterKey));
-
- // Return it
- return contextValue;
- }
-
- /**
- * Returns a message based on given i18nKey or puts it into three question
- * marks each side when not found.
- * <p>
- * @param i18nKey I18n key
- * <p>
- * @return Localized message
- * <p>
- * @throws NullPointerException If the parameter is null
- * @throws IllegalArgumentException If the parameter is empty
- */
- protected String getMessageFromBundle (final String i18nKey) {
- // Validate parameter
- if (null == i18nKey) {
- // Throw NPE
- throw new NullPointerException("i18nKey is null"); //NOI18N
- } else if (i18nKey.isEmpty()) {
- // Is empty
- throw new IllegalArgumentException("i18nKey is empty"); //NOI18N
- }
-
- // Get current locale
- final Locale locale = FacesContext.getCurrentInstance().getViewRoot().getLocale();
-
- // Get bundle bundle
- this.loadResourceBundles(locale);
-
- // Default is i18nKey
- String message = MessageFormat.format("???{0}???", i18nKey); //NOI18N
-
- // Loop through all
- for (final ResourceBundle bundle : getBundles()) {
- // Found message?
- // Try it
- try {
- // Get message
- message = bundle.getString(i18nKey);
- break;
- } catch (final MissingResourceException ex) {
- // Did not find it, ignored
- }
- }
-
- // Return it
- return message;
- }
-
- /**
- * Returns given property key or throws an exception if not found.
- * <p>
- * @param parameterKey Property key
- * <p>
- * @return Property value
- * <p>
- * @throws NullPointerException If given key is not found
- */
- protected String getStringContextParameter (final String parameterKey) throws NullPointerException {
- // Get context parameter
- final String contextValue = FacesContext.getCurrentInstance().getExternalContext().getInitParameter(parameterKey);
-
- // Is it null?
- if (null == contextValue) {
- // Throw NPE
- throw new NullPointerException(MessageFormat.format("parameterKey={0} is not set.", parameterKey)); //NOI18N
- }
-
- // Return it
- return contextValue;
- }
-
- /**
- * Checks whether debug mode is enabled for given controller
- * <p>
- * @param controllerName Name of controller
- * <p>
- * @return Whether debug mode is enabled
- */
- protected boolean isDebugModeEnabled (final String controllerName) {
- // Parameters should be valid
- if (null == controllerName) {
- // Throw NPE
- throw new NullPointerException("controllerName is null"); //NOI18N
- } else if (controllerName.isEmpty()) {
- // Is empty
- throw new IllegalArgumentException("controllerName is empty"); //NOI18N
- }
-
- // Try to get context parameter
- final String contextParameter = this.getStringContextParameter(String.format("is_debug_%s_enabled", controllerName)); //NOI18N
-
- // Is it set and true?
- final boolean isEnabled = Boolean.parseBoolean(contextParameter) == Boolean.TRUE;
-
- // Return it
- return isEnabled;
- }
-
- /**
- * Loads resource bundles for given locale. This must be implemented per
- * project so all projects can still customize their methods. Calling
- * ResourceBundleloadBundle() in this class means that also the bundle files
- * must be present here.
- * <p>
- * @param locale Locale from e.g. FacesContext
- */
- protected abstract void loadResourceBundles (final Locale locale);
-
- /**
- * Shows a faces message for given causing exception. The message from the
- * exception is being inserted into the message.
- * <p>
- * @param clientId Client id to send message to
- * @param cause Causing exception
- */
- protected void showFacesMessage (final String clientId, final Throwable cause) {
- // Get context and add message
- this.showFacesMessage(clientId, cause.getMessage());
- }
-
- /**
- * Shows a faces message with given message (i18n) key.
- * <p>
- * @param clientId Client id to send message to
- * @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) 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 message from bundle
- final String message = this.getMessageFromBundle(i18nKey);
-
- // Get context and add message
- FacesContext.getCurrentInstance().addMessage(clientId, new FacesMessage(message));
- }
-
-}