]> git.mxchange.org Git - jcoreee.git/blobdiff - src/org/mxchange/jcoreee/database/BaseDatabaseBean.java
The local logger EJB is only available to EJBs, not web applications. Therefore
[jcoreee.git] / src / org / mxchange / jcoreee / database / BaseDatabaseBean.java
index 69d3b9bda0332be521a656777c509f0bebb47ba8..71a92feef35163668c10a0149d38bdba5d795abb 100644 (file)
  */
 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
+        * <p>
+        * @return Logger instance
+        */
+       protected LoggerBeanLocal getLoggerBeanLocal () {
+               return this.loggerBeanLocal;
+       }
+
+       /**
+        * Sends given message to configured queue
+        * <p>
+        * @param message Message to send
+        * <p>
+        * @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
+       }
+
 }