From: Roland Häder Date: Wed, 7 Jun 2017 20:45:21 +0000 (+0200) Subject: updated jar(s) X-Git-Url: https://git.mxchange.org/?a=commitdiff_plain;h=fa70f70594c5b1bd2b9545d0cd67e0b46d4fbddf;p=jcoreee.git updated jar(s) Signed-off-by: Roland Häder --- diff --git a/src/org/mxchange/jcoreee/bean/BaseBean.java b/src/org/mxchange/jcoreee/bean/BaseBean.java new file mode 100644 index 0000000..0304483 --- /dev/null +++ b/src/org/mxchange/jcoreee/bean/BaseBean.java @@ -0,0 +1,332 @@ +/* + * Copyright (C) 2017 Roland Häder + * + * 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 . + */ +package org.mxchange.jcoreee.bean; + +import java.io.Serializable; +import java.security.Principal; +import java.text.MessageFormat; +import java.util.Locale; +import java.util.MissingResourceException; +import java.util.ResourceBundle; +import javax.faces.FacesException; +import javax.faces.application.FacesMessage; +import javax.faces.context.FacesContext; +import javax.jms.Connection; +import javax.jms.JMSException; +import javax.jms.MessageProducer; +import javax.jms.ObjectMessage; +import javax.jms.Queue; +import javax.jms.QueueConnectionFactory; +import javax.jms.Session; +import javax.naming.Context; +import javax.naming.InitialContext; +import javax.naming.NamingException; +import org.mxchange.jcoreeelogger.beans.local.logger.Log; +import org.mxchange.jcoreeelogger.beans.local.logger.LoggerBeanLocal; + +/** + * A generic bean class + *

+ * @author Roland Häder + */ +public abstract class BaseBean implements Serializable { + + /** + * Serial number + */ + private static final long serialVersionUID = 18_305_698_567_265L; + + /** + * Connection + */ + private Connection connection; + + /** + * Logger instance + */ + @Log + private LoggerBeanLocal loggerBeanLocal; + + /** + * Message producer + */ + private MessageProducer messageProducer; + + /** + * Mailer message queue + */ + private Queue queue; + + /** + * Session instance + */ + private Session session; + + /** + * This class' default protected constructor. Please call + * super("jms/project-queue-factory", "jms/project-email-queue"); if you + * need to send emails. + */ + protected BaseBean () { + try { + // Get initial context + Context context = new InitialContext(); + + // Lookup logger + this.loggerBeanLocal = (LoggerBeanLocal) context.lookup("java:global/jcore-logger-ejb/logger!org.mxchange.jcoreeelogger.beans.local.logger.LoggerBeanLocal"); //NOI18N + } catch (final NamingException ex) { + // Continue to throw + throw new RuntimeException(MessageFormat.format("context.lookup() failed: {0}", ex.getMessage()), ex); //NOI18N + } + } + + /** + * Constructor with queue factory JNDI and email-queue JNDI names + *

+ * @param factoryJndi JNDI name for queue factory + * @param emailQueueJndi JNDI name for email queue + */ + protected BaseBean (final String factoryJndi, final String emailQueueJndi) { + // Try it out + try { + // Get initial context + Context context = new InitialContext(); + + // Get factory from JMS resource + QueueConnectionFactory connectionFactory = (QueueConnectionFactory) context.lookup(factoryJndi); //NOI18N + + // Lookup queue + this.queue = (Queue) context.lookup(emailQueueJndi); //NOI18N + + // Create connection + this.connection = connectionFactory.createConnection(); + + // Init session instance + this.session = this.connection.createSession(false, Session.AUTO_ACKNOWLEDGE); + + // And message producer + this.messageProducer = this.session.createProducer(this.queue); + } catch (final NamingException | JMSException e) { + // Continued to throw + throw new FacesException(e); + } + } + + /** + * Determines principal's name or returns null if no principal (security) is + * set. + *

+ * @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. + *

+ * @param parameterKey Property key + *

+ * @return Property value + *

+ * @throws NullPointerException If given key is not found + * @throws NumberFormatException If no number is given in context parameter + */ + protected int getIntegerContextParameter (final String parameterKey) throws NullPointerException, NumberFormatException { + // Get context parameter + Integer contextValue = Integer.parseInt(this.getStringContextParameter(parameterKey)); + + // Return it + return contextValue; + } + + /** + * Getter for loggerBeanLocal + *

+ * @return Logger instance + */ + protected LoggerBeanLocal getLoggerBeanLocal () { + return this.loggerBeanLocal; + } + + /** + * Getter for configured message producer instance + *

+ * @return Message producer + */ + protected MessageProducer getMessageProducer () { + return this.messageProducer; + } + + /** + * Getter for configured session instance + *

+ * @return Session + */ + protected Session getSession () { + return this.session; + } + + /** + * Returns given property key or throws an exception if not found. + *

+ * @param parameterKey Property key + *

+ * @return Property value + *

+ * @throws NullPointerException If given key is not found + */ + protected String getStringContextParameter (final String parameterKey) throws NullPointerException { + // Get context parameter + 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 + *

+ * @param controllerName Name of controller + *

+ * @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 + String contextParameter = this.getStringContextParameter(String.format("is_debug_%s_enabled", controllerName)); //NOI18N + + // Is it set and true? + boolean isEnabled = (Boolean.parseBoolean(contextParameter) == Boolean.TRUE); + + // Return it + return isEnabled; + } + + /** + * Sends given message to configured queue + *

+ * @param message Message to send + *

+ * @throws JMSException if something went wrong + */ + protected void sendMessage (final ObjectMessage message) throws JMSException { + // Trace message + this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.sendMessage: message={1} - CALLED!", this.getClass().getSimpleName(), message)); //NOI18N + + // The parameter should be valid + if (null == message) { + // Throw NPE + throw new NullPointerException("message is null"); //NOI18N + } else if (null == this.getMessageProducer()) { + // Throw NPE again + throw new NullPointerException("this.messageProvider is null"); //NOI18N + } + + // Send it + this.getMessageProducer().send(message); + + // Trace message + this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.sendMessage: EXIT!", this.getClass().getSimpleName())); //NOI18N + } + + /** + * Shows a faces message for given causing exception. The message from the + * exception is being inserted into the message. + *

+ * @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. + *

+ * @param clientId Client id to send message to + * @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) 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 current locale + Locale locale = FacesContext.getCurrentInstance().getViewRoot().getLocale(); + + // Get bundle bundle + ResourceBundle bundle = ResourceBundle.getBundle("org.mxchange.localization.bundle", locale); + + // Default is i18nKey + String message = MessageFormat.format("!{0}!", i18nKey); //NOI18N + + // Try it + try { + // Get message + message = bundle.getString(i18nKey); + } catch (final MissingResourceException ex) { + // Did not find it, ignored + } + + // Get context and add message + FacesContext.getCurrentInstance().addMessage(clientId, new FacesMessage(message)); + } + +} diff --git a/src/org/mxchange/jcoreee/database/BaseDatabaseBean.java b/src/org/mxchange/jcoreee/database/BaseDatabaseBean.java index d5e0762..c6dab1a 100644 --- a/src/org/mxchange/jcoreee/database/BaseDatabaseBean.java +++ b/src/org/mxchange/jcoreee/database/BaseDatabaseBean.java @@ -16,84 +16,36 @@ */ package org.mxchange.jcoreee.database; -import java.io.Serializable; -import java.text.MessageFormat; -import javax.faces.FacesException; -import javax.jms.Connection; -import javax.jms.JMSException; -import javax.jms.MessageProducer; -import javax.jms.ObjectMessage; -import javax.jms.Queue; -import javax.jms.QueueConnectionFactory; -import javax.jms.Session; -import javax.naming.Context; -import javax.naming.InitialContext; -import javax.naming.NamingException; import javax.persistence.EntityManager; import javax.persistence.PersistenceContext; -import org.mxchange.jcoreeelogger.beans.local.logger.Log; -import org.mxchange.jcoreeelogger.beans.local.logger.LoggerBeanLocal; +import org.mxchange.jcoreee.bean.BaseBean; /** * A helper class for beans that access the database. *

* @author Roland Häder */ -public abstract class BaseDatabaseBean implements Serializable { +public abstract class BaseDatabaseBean extends BaseBean { /** * Serial number */ private static final long serialVersionUID = 217_687_175_985_875L; - /** - * Connection - */ - private Connection connection; - /** * Entity manager */ @PersistenceContext private EntityManager entityManager; - /** - * Logger instance - */ - @Log - private LoggerBeanLocal loggerBeanLocal; - - /** - * Message producer - */ - private MessageProducer messageProducer; - - /** - * Mailer message queue - */ - private Queue queue; - - /** - * Session instance - */ - private Session session; - /** * This class' default protected constructor. Please call * super("jms/project-queue-factory", "jms/project-email-queue"); if you * need to send emails. */ protected BaseDatabaseBean () { - try { - // Get initial context - Context context = new InitialContext(); - - // Lookup logger - this.loggerBeanLocal = (LoggerBeanLocal) context.lookup("java:global/jcore-logger-ejb/logger!org.mxchange.jcoreeelogger.beans.local.logger.LoggerBeanLocal"); //NOI18N - } catch (final NamingException ex) { - // Continue to throw - throw new RuntimeException(MessageFormat.format("context.lookup() failed: {0}", ex.getMessage()), ex); //NOI18N - } + // Call super constructor + super(); } /** @@ -103,31 +55,8 @@ public abstract class BaseDatabaseBean implements Serializable { * @param emailQueueJndi JNDI name for email queue */ protected BaseDatabaseBean (final String factoryJndi, final String emailQueueJndi) { - // Call default constructor - super(); - - try { - // Get initial context - Context context = new InitialContext(); - - // Get factory from JMS resource - QueueConnectionFactory connectionFactory = (QueueConnectionFactory) context.lookup(factoryJndi); //NOI18N - - // Lookup queue - this.queue = (Queue) context.lookup(emailQueueJndi); //NOI18N - - // Create connection - this.connection = connectionFactory.createConnection(); - - // Init session instance - this.session = this.connection.createSession(false, Session.AUTO_ACKNOWLEDGE); - - // And message producer - this.messageProducer = this.session.createProducer(this.queue); - } catch (final NamingException | JMSException e) { - // Continued to throw - throw new FacesException(e); - } + // Call super constructor + super(factoryJndi, emailQueueJndi); } /** @@ -139,58 +68,4 @@ public abstract class BaseDatabaseBean implements Serializable { return this.entityManager; } - /** - * Getter for loggerBeanLocal - *

- * @return Logger instance - */ - protected LoggerBeanLocal getLoggerBeanLocal () { - return this.loggerBeanLocal; - } - - /** - * Getter for configured message producer instance - *

- * @return Message producer - */ - protected MessageProducer getMessageProducer () { - return this.messageProducer; - } - - /** - * Getter for configured session instance - *

- * @return Session - */ - protected Session getSession () { - return this.session; - } - - /** - * Sends given message to configured queue - *

- * @param message Message to send - *

- * @throws JMSException if something went wrong - */ - protected void sendMessage (final ObjectMessage message) throws JMSException { - // Trace message - this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.sendMessage: message={1} - CALLED!", this.getClass().getSimpleName(), message)); //NOI18N - - // The parameter should be valid - if (null == message) { - // Throw NPE - throw new NullPointerException("message is null"); //NOI18N - } else if (null == this.getMessageProducer()) { - // Throw NPE again - throw new NullPointerException("this.messageProvider is null"); //NOI18N - } - - // Send it - this.getMessageProducer().send(message); - - // Trace message - this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.sendMessage: EXIT!", this.getClass().getSimpleName())); //NOI18N - } - }