2 * Copyright (C) 2016, 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.database;
19 import java.text.MessageFormat;
20 import javax.jms.JMSException;
21 import javax.jms.ObjectMessage;
22 import javax.naming.Context;
23 import javax.naming.InitialContext;
24 import javax.naming.NamingException;
25 import javax.persistence.EntityManager;
26 import javax.persistence.PersistenceContext;
27 import org.mxchange.jcoreee.bean.BaseBean;
28 import org.mxchange.jcoreeelogger.beans.local.logger.Log;
29 import org.mxchange.jcoreeelogger.beans.local.logger.LoggerBeanLocal;
32 * A helper class for beans that access the database.
34 * @author Roland Häder<roland@mxchange.org>
36 public abstract class BaseDatabaseBean extends BaseBean {
41 private static final long serialVersionUID = 217_687_175_985_875L;
47 private EntityManager entityManager;
53 private LoggerBeanLocal loggerBeanLocal;
56 * This class' default protected constructor. Please call
57 * super("jms/project-queue-factory", "jms/project-email-queue"); if you
58 * need to send emails.
60 protected BaseDatabaseBean () {
61 // Call super constructor
65 // Get initial context
66 Context context = new InitialContext();
69 this.loggerBeanLocal = (LoggerBeanLocal) context.lookup("java:global/jcore-logger-ejb/logger!org.mxchange.jcoreeelogger.beans.local.logger.LoggerBeanLocal"); //NOI18N
70 } catch (final NamingException ex) {
72 throw new RuntimeException(MessageFormat.format("context.lookup() failed: {0}", ex.getMessage()), ex); //NOI18N
77 * Constructor with queue factory JNDI and queue JNDI names
79 * @param factoryJndi JNDI name for queue factory
80 * @param queueJndi JNDI name for email queue
82 protected BaseDatabaseBean (final String factoryJndi, final String queueJndi) {
83 // Call super constructor
84 super(factoryJndi, queueJndi);
88 * Getter for connection instance
90 * @return Connection instance
92 protected EntityManager getEntityManager () {
93 return this.entityManager;
97 * Getter for loggerBeanLocal
99 * @return Logger instance
101 protected LoggerBeanLocal getLoggerBeanLocal () {
102 return this.loggerBeanLocal;
106 * Sends given message to configured queue
108 * @param message Message to send
110 * @throws JMSException if something went wrong
112 protected void sendMessage (final ObjectMessage message) throws JMSException {
114 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.sendMessage: message={1} - CALLED!", this.getClass().getSimpleName(), message)); //NOI18N
116 // The parameter should be valid
117 if (null == message) {
119 throw new NullPointerException("message is null"); //NOI18N
120 } else if (this.getMessageProducer() == null) {
122 throw new NullPointerException("this.messageProvider is null"); //NOI18N
126 this.getMessageProducer().send(message);
129 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.sendMessage: EXIT!", this.getClass().getSimpleName())); //NOI18N