From c80a673c8725e4efa5fdbf499a168c9027a23859 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Roland=20H=C3=A4der?= Date: Thu, 6 Jul 2017 21:26:43 +0200 Subject: [PATCH] The local logger EJB is only available to EJBs, not web applications. Therefore it needs to be moved to BaseDatabaseBean (which is the super class for EJBs). MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Same with sendMessage() method. No web application shall send out mails directly. Only an EJB (message-driven, of course) should do that. Signed-off-by: Roland Häder --- src/org/mxchange/jcoreee/bean/BaseBean.java | 59 ------------------ .../jcoreee/bean/faces/BaseFacesBean.java | 8 +++ .../jcoreee/database/BaseDatabaseBean.java | 61 +++++++++++++++++++ 3 files changed, 69 insertions(+), 59 deletions(-) diff --git a/src/org/mxchange/jcoreee/bean/BaseBean.java b/src/org/mxchange/jcoreee/bean/BaseBean.java index c6d4816..09e581e 100644 --- a/src/org/mxchange/jcoreee/bean/BaseBean.java +++ b/src/org/mxchange/jcoreee/bean/BaseBean.java @@ -18,21 +18,17 @@ package org.mxchange.jcoreee.bean; import java.io.Serializable; import java.security.Principal; -import java.text.MessageFormat; import javax.faces.FacesException; 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 @@ -51,12 +47,6 @@ public abstract class BaseBean implements Serializable { */ private Connection connection; - /** - * Logger instance - */ - @Log - private LoggerBeanLocal loggerBeanLocal; - /** * Message producer */ @@ -78,16 +68,6 @@ public abstract class BaseBean implements Serializable { * 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 - } } /** @@ -148,16 +128,6 @@ public abstract class BaseBean implements Serializable { return principalName; } - - /** - * Getter for loggerBeanLocal - *

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

@@ -176,33 +146,4 @@ public abstract class BaseBean implements Serializable { 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 (this.getMessageProducer() == null) { - // 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 - } - - } diff --git a/src/org/mxchange/jcoreee/bean/faces/BaseFacesBean.java b/src/org/mxchange/jcoreee/bean/faces/BaseFacesBean.java index 9d5a850..454b2a3 100644 --- a/src/org/mxchange/jcoreee/bean/faces/BaseFacesBean.java +++ b/src/org/mxchange/jcoreee/bean/faces/BaseFacesBean.java @@ -36,6 +36,14 @@ public abstract class BaseFacesBean extends BaseBean { */ private static final long serialVersionUID = 18_605_498_672_261L; + /** + * Protected constructor + */ + protected BaseFacesBean () { + // Call super constructor + super(); + } + /** * Returns given property key or throws an exception if not found. *

diff --git a/src/org/mxchange/jcoreee/database/BaseDatabaseBean.java b/src/org/mxchange/jcoreee/database/BaseDatabaseBean.java index 69d3b9b..71a92fe 100644 --- a/src/org/mxchange/jcoreee/database/BaseDatabaseBean.java +++ b/src/org/mxchange/jcoreee/database/BaseDatabaseBean.java @@ -16,9 +16,17 @@ */ package org.mxchange.jcoreee.database; +import java.text.MessageFormat; +import javax.jms.JMSException; +import javax.jms.ObjectMessage; +import javax.naming.Context; +import javax.naming.InitialContext; +import javax.naming.NamingException; import javax.persistence.EntityManager; import javax.persistence.PersistenceContext; import org.mxchange.jcoreee.bean.BaseBean; +import org.mxchange.jcoreeelogger.beans.local.logger.Log; +import org.mxchange.jcoreeelogger.beans.local.logger.LoggerBeanLocal; /** * A helper class for beans that access the database. @@ -38,6 +46,12 @@ public abstract class BaseDatabaseBean extends BaseBean { @PersistenceContext private EntityManager entityManager; + /** + * Logger instance + */ + @Log + private LoggerBeanLocal loggerBeanLocal; + /** * This class' default protected constructor. Please call * super("jms/project-queue-factory", "jms/project-email-queue"); if you @@ -46,6 +60,17 @@ public abstract class BaseDatabaseBean extends BaseBean { protected BaseDatabaseBean () { // Call super constructor super(); + + 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 + } } /** @@ -68,4 +93,40 @@ public abstract class BaseDatabaseBean extends BaseBean { return this.entityManager; } + /** + * Getter for loggerBeanLocal + *

+ * @return Logger instance + */ + protected LoggerBeanLocal getLoggerBeanLocal () { + return this.loggerBeanLocal; + } + + /** + * 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 (this.getMessageProducer() == null) { + // 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 + } + } -- 2.39.5