]> git.mxchange.org Git - jcore-utils.git/blobdiff - src/org/mxchange/jcoreee/database/BaseDatabaseBean.java
Introduced initLoggerInstance() which every contructor needs to call, surely
[jcore-utils.git] / src / org / mxchange / jcoreee / database / BaseDatabaseBean.java
index d5e07624c9387c26bcd052f23c540e49e803e807..8e3f9f31e760cc6138d34352169c8d9ff0853f42 100644 (file)
  */
 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.jcoreee.bean.BaseBean;
 import org.mxchange.jcoreeelogger.beans.local.logger.Log;
 import org.mxchange.jcoreeelogger.beans.local.logger.LoggerBeanLocal;
 
@@ -39,18 +33,13 @@ import org.mxchange.jcoreeelogger.beans.local.logger.LoggerBeanLocal;
  * <p>
  * @author Roland Häder<roland@mxchange.org>
  */
-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
         */
@@ -64,26 +53,36 @@ public abstract class BaseDatabaseBean implements Serializable {
        private LoggerBeanLocal loggerBeanLocal;
 
        /**
-        * Message producer
+        * This class' default protected constructor. Please call
+        * super("jms/project-queue-factory", "jms/project-email-queue"); if you
+        * need to send emails.
         */
-       private MessageProducer messageProducer;
+       protected BaseDatabaseBean () {
+               // Call super constructor
+               super();
 
-       /**
-        * Mailer message queue
-        */
-       private Queue queue;
+               // Init logger instance
+               this.initLoggerInstance();
+       }
 
        /**
-        * Session instance
+        * Constructor with queue factory JNDI and queue JNDI names
+        * <p>
+        * @param factoryJndi JNDI name for queue factory
+        * @param queueJndi   JNDI name for email queue
         */
-       private Session session;
+       protected BaseDatabaseBean (final String factoryJndi, final String queueJndi) {
+               // Call super constructor
+               super(factoryJndi, queueJndi);
+
+               // Init logger instance
+               this.initLoggerInstance();
+       }
 
        /**
-        * This class' default protected constructor. Please call
-        * super("jms/project-queue-factory", "jms/project-email-queue"); if you
-        * need to send emails.
+        * Initializes logger instance
         */
-       protected BaseDatabaseBean () {
+       private void initLoggerInstance () {
                try {
                        // Get initial context
                        Context context = new InitialContext();
@@ -96,40 +95,6 @@ public abstract class BaseDatabaseBean implements Serializable {
                }
        }
 
-       /**
-        * Constructor with queue factory JNDI and email-queue JNDI names
-        * <p>
-        * @param factoryJndi    JNDI name for queue factory
-        * @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);
-               }
-       }
-
        /**
         * Getter for connection instance
         * <p>
@@ -148,24 +113,6 @@ public abstract class BaseDatabaseBean implements Serializable {
                return this.loggerBeanLocal;
        }
 
-       /**
-        * Getter for configured message producer instance
-        * <p>
-        * @return Message producer
-        */
-       protected MessageProducer getMessageProducer () {
-               return this.messageProducer;
-       }
-
-       /**
-        * Getter for configured session instance
-        * <p>
-        * @return Session
-        */
-       protected Session getSession () {
-               return this.session;
-       }
-
        /**
         * Sends given message to configured queue
         * <p>
@@ -181,7 +128,7 @@ public abstract class BaseDatabaseBean implements Serializable {
                if (null == message) {
                        // Throw NPE
                        throw new NullPointerException("message is null"); //NOI18N
-               } else if (null == this.getMessageProducer()) {
+               } else if (this.getMessageProducer() == null) {
                        // Throw NPE again
                        throw new NullPointerException("this.messageProvider is null"); //NOI18N
                }