2 * Copyright (C) 2017 Roland Häder
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 package org.mxchange.jcoreee.bean.faces;
19 import java.security.Principal;
20 import java.text.MessageFormat;
21 import java.util.ArrayList;
22 import java.util.List;
23 import java.util.Locale;
24 import java.util.MissingResourceException;
25 import java.util.ResourceBundle;
26 import javax.faces.application.FacesMessage;
27 import javax.faces.context.FacesContext;
28 import org.mxchange.jcoreee.bean.BaseBean;
31 * An abstract bean for faces (web) projects.
33 * @author Roland Häder<roland@mxchange.org>
35 public abstract class BaseFacesBean extends BaseBean {
38 * Loaded resource bundles ("cached")
40 private static final List<ResourceBundle> RESOURCE_BUNDLES;
45 private static final long serialVersionUID = 18_605_498_672_261L;
51 // Init resource bundle list
52 RESOURCE_BUNDLES = new ArrayList<>(3);
56 * Removes all bundles from web application. Typically you want to invoke
57 * this method in a ServletContextListener implemetation on the
58 * contextDestroyed() method.
60 public static void removeBundles () {
62 RESOURCE_BUNDLES.clear();
66 * Getter for resource bundle list
68 * @return Resource bundle list
70 @SuppressWarnings ("ReturnOfCollectionOrArrayField")
71 protected static List<ResourceBundle> getBundles () {
72 return RESOURCE_BUNDLES;
76 * Protected constructor
78 protected BaseFacesBean () {
79 // Call super constructor
84 * Determines principal's name or returns null if no principal (security) is
87 * @return Principal's name or null
89 protected String determinePrincipalName () {
91 final Principal userPrincipal = FacesContext.getCurrentInstance().getExternalContext().getUserPrincipal();
94 String principalName = null;
96 // Is the principal set?
97 if (userPrincipal instanceof Principal) {
98 // Get principal's name
99 principalName = userPrincipal.getName();
103 return principalName;
107 * Returns given property key or throws an exception if not found.
109 * @param parameterKey Property key
111 * @return Property value
113 protected int getIntegerContextParameter (final String parameterKey) throws NullPointerException, NumberFormatException {
114 // Get context parameter
115 final Integer contextValue = Integer.parseInt(this.getStringContextParameter(parameterKey));
122 * Returns a message based on given i18nKey or puts it into three question
123 * marks each side when not found.
125 * @param i18nKey I18n key
127 * @return Localized message
129 * @throws NullPointerException If the parameter is null
130 * @throws IllegalArgumentException If the parameter is empty
132 protected String getMessageFromBundle (final String i18nKey) {
133 // Validate parameter
134 if (null == i18nKey) {
136 throw new NullPointerException("i18nKey is null"); //NOI18N
137 } else if (i18nKey.isEmpty()) {
139 throw new IllegalArgumentException("i18nKey is empty"); //NOI18N
142 // Get current locale
143 final Locale locale = FacesContext.getCurrentInstance().getViewRoot().getLocale();
146 this.loadResourceBundles(locale);
148 // Default is i18nKey
149 String message = MessageFormat.format("???{0}???", i18nKey); //NOI18N
152 for (final ResourceBundle bundle : getBundles()) {
157 message = bundle.getString(i18nKey);
159 } catch (final MissingResourceException ex) {
160 // Did not find it, ignored
169 * Returns given property key or throws an exception if not found.
171 * @param parameterKey Property key
173 * @return Property value
175 * @throws NullPointerException If given key is not found
177 protected String getStringContextParameter (final String parameterKey) throws NullPointerException {
178 // Get context parameter
179 final String contextValue = FacesContext.getCurrentInstance().getExternalContext().getInitParameter(parameterKey);
182 if (null == contextValue) {
184 throw new NullPointerException(MessageFormat.format("parameterKey={0} is not set.", parameterKey)); //NOI18N
192 * Checks whether debug mode is enabled for given controller
194 * @param controllerName Name of controller
196 * @return Whether debug mode is enabled
198 protected boolean isDebugModeEnabled (final String controllerName) {
199 // Parameters should be valid
200 if (null == controllerName) {
202 throw new NullPointerException("controllerName is null"); //NOI18N
203 } else if (controllerName.isEmpty()) {
205 throw new IllegalArgumentException("controllerName is empty"); //NOI18N
208 // Try to get context parameter
209 final String contextParameter = this.getStringContextParameter(String.format("is_debug_%s_enabled", controllerName)); //NOI18N
211 // Is it set and true?
212 final boolean isEnabled = Boolean.parseBoolean(contextParameter) == Boolean.TRUE;
219 * Loads resource bundles for given locale. This must be implemented per
220 * project so all projects can still customize their methods. Calling
221 * ResourceBundleloadBundle() in this class means that also the bundle files
222 * must be present here.
224 * @param locale Locale from e.g. FacesContext
226 protected abstract void loadResourceBundles (final Locale locale);
229 * Shows a faces message for given causing exception. The message from the
230 * exception is being inserted into the message.
232 * @param clientId Client id to send message to
233 * @param cause Causing exception
235 protected void showFacesMessage (final String clientId, final Throwable cause) {
236 // Get context and add message
237 this.showFacesMessage(clientId, cause.getMessage());
241 * Shows a faces message with given message (i18n) key.
243 * @param clientId Client id to send message to
244 * @param i18nKey Message key
246 * @throws NullPointerException If clientId or i18nKey is null
247 * @throws IllegalArgumentException If clientId or i18nKey is empty
249 protected void showFacesMessage (final String clientId, final String i18nKey) throws NullPointerException, IllegalArgumentException {
250 // Both parameter must be valid
251 if (null == clientId) {
253 throw new NullPointerException("clientId is null"); //NOI18N
254 } else if (clientId.isEmpty()) {
256 throw new IllegalArgumentException("clientId is null"); //NOI18N
257 } else if (null == i18nKey) {
259 throw new NullPointerException("i18nKey is null"); //NOI18N
260 } else if (i18nKey.isEmpty()) {
262 throw new IllegalArgumentException("i18nKey is null"); //NOI18N
265 // Get message from bundle
266 final String message = this.getMessageFromBundle(i18nKey);
268 // Get context and add message
269 FacesContext.getCurrentInstance().addMessage(clientId, new FacesMessage(message));