2 * Copyright (C) 2016, 2017 Free Software Foundation
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
64 // Init logger instance
65 this.initLoggerInstance();
69 * Constructor with queue factory JNDI and queue JNDI names
71 * @param factoryJndi JNDI name for queue factory
72 * @param queueJndi JNDI name for email queue
74 protected BaseDatabaseBean (final String factoryJndi, final String queueJndi) {
75 // Call super constructor
76 super(factoryJndi, queueJndi);
78 // Init logger instance
79 this.initLoggerInstance();
83 * Initializes logger instance
85 private void initLoggerInstance () {
87 // Get initial context
88 Context context = new InitialContext();
91 this.loggerBeanLocal = (LoggerBeanLocal) context.lookup("java:global/jcore-logger-ejb/logger!org.mxchange.jcoreeelogger.beans.local.logger.LoggerBeanLocal"); //NOI18N
92 } catch (final NamingException ex) {
94 throw new RuntimeException(MessageFormat.format("context.lookup() failed: {0}", ex.getMessage()), ex); //NOI18N
99 * Getter for connection instance
101 * @return Connection instance
103 protected EntityManager getEntityManager () {
104 return this.entityManager;
108 * Getter for loggerBeanLocal
110 * @return Logger instance
112 protected LoggerBeanLocal getLoggerBeanLocal () {
113 return this.loggerBeanLocal;
117 * Sends given message to configured queue
119 * @param message Message to send
121 * @throws JMSException if something went wrong
123 protected void sendMessage (final ObjectMessage message) throws JMSException {
125 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.sendMessage: message={1} - CALLED!", this.getClass().getSimpleName(), message)); //NOI18N
127 // The parameter should be valid
128 if (null == message) {
130 throw new NullPointerException("message is null"); //NOI18N
131 } else if (this.getMessageProducer() == null) {
133 throw new NullPointerException("this.messageProvider is null"); //NOI18N
137 this.getMessageProducer().send(message);
140 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.sendMessage: EXIT!", this.getClass().getSimpleName())); //NOI18N