2 * Copyright (C) 2016 Roland Haeder
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.io.Serializable;
20 import java.text.MessageFormat;
21 import javax.jms.JMSException;
22 import javax.jms.MessageProducer;
23 import javax.jms.ObjectMessage;
24 import javax.naming.Context;
25 import javax.naming.InitialContext;
26 import javax.naming.NamingException;
27 import javax.persistence.EntityManager;
28 import javax.persistence.PersistenceContext;
29 import org.mxchange.jcoreeelogger.beans.local.logger.Log;
30 import org.mxchange.jcoreeelogger.beans.local.logger.LoggerBeanLocal;
33 * A helper class for beans that access the database.
35 * @author Roland Haeder<roland@mxchange.org>
37 public abstract class BaseDatabaseBean implements Serializable {
42 private static final long serialVersionUID = 217_687_175_985_875L;
48 private EntityManager entityManager;
54 private LoggerBeanLocal loggerBeanLocal;
57 * Protected constructor
59 protected BaseDatabaseBean () {
61 // Get initial context
62 Context context = new InitialContext();
65 this.loggerBeanLocal = (LoggerBeanLocal) context.lookup("java:global/jcore-logger-ejb/logger!org.mxchange.jcoreeelogger.beans.local.logger.LoggerBeanLocal"); //NOI18N
66 } catch (final NamingException ex) {
68 throw new RuntimeException(MessageFormat.format("context.lookup() failed: {0}", ex.getMessage()), ex); //NOI18N
73 * Getter for connection instance
75 * @return Connection instance
77 protected EntityManager getEntityManager () {
78 return this.entityManager;
82 * Getter for loggerBeanLocal
84 * @return Logger instance
86 protected LoggerBeanLocal getLoggerBeanLocal () {
87 return this.loggerBeanLocal;
91 * Sends given message to configured queue
93 * @param message Message to send
94 * @param messageProducer Message producer
96 * @throws JMSException if something went wrong
98 protected void sendMessage (final ObjectMessage message, final MessageProducer messageProducer) throws JMSException {
100 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.sendMessage: message={1},messageProducer={2} - CALLED!", this.getClass().getSimpleName(), message, messageProducer)); //NOI18N
102 // The parameter should be valid
103 if (null == message) {
105 throw new NullPointerException("message is null"); //NOI18N
106 } else if (null == messageProducer) {
108 throw new NullPointerException("messageProvider is null"); //NOI18N
112 messageProducer.send(message);
115 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.sendMessage: EXIT!", this.getClass().getSimpleName())); //NOI18N