2 * Copyright (C) 2016 - 2018 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.bean.ejb;
19 import java.io.Serializable;
20 import java.text.MessageFormat;
21 import javax.faces.FacesException;
22 import javax.jms.Connection;
23 import javax.jms.JMSException;
24 import javax.jms.MessageProducer;
25 import javax.jms.ObjectMessage;
26 import javax.jms.Queue;
27 import javax.jms.QueueConnectionFactory;
28 import javax.jms.Session;
29 import javax.naming.Context;
30 import javax.naming.InitialContext;
31 import javax.naming.NamingException;
32 import javax.persistence.EntityManager;
33 import javax.persistence.PersistenceContext;
34 import org.mxchange.jcoreeelogger.beans.local.logger.Log;
35 import org.mxchange.jcoreeelogger.beans.local.logger.LoggerBeanLocal;
38 * A helper class for beans that access the database.
40 * @author Roland Häder<roland@mxchange.org>
42 public abstract class BaseEnterpriseBean implements Serializable {
47 private static final long serialVersionUID = 217_687_175_985_875L;
52 private Connection connection;
58 private EntityManager entityManager;
64 private LoggerBeanLocal loggerBeanLocal;
69 private MessageProducer messageProducer;
72 * Mailer message queue
79 private Session session;
82 * This class' default protected constructor. Please invoke
83 * super("jms/project-queue-factory", "jms/project-email-queue"); if you
84 * need to send emails.
86 protected BaseEnterpriseBean () {
87 // Init logger instance
88 this.initLoggerInstance();
92 * Constructor with queue factory JNDI and queue JNDI names
94 * @param factoryJndi JNDI name for queue factory
95 * @param queueJndi JNDI name for email queue
97 protected BaseEnterpriseBean (final String factoryJndi, final String queueJndi) {
98 // Call default constructor
103 // Get initial context
104 Context context = new InitialContext();
106 // Get factory from JMS resource
107 QueueConnectionFactory connectionFactory = (QueueConnectionFactory) context.lookup(factoryJndi);
110 this.queue = (Queue) context.lookup(queueJndi);
113 this.connection = connectionFactory.createConnection();
115 // Init session instance
116 this.session = this.connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
118 // And message producer
119 this.messageProducer = this.session.createProducer(this.queue);
120 } catch (final NamingException | JMSException e) {
121 // Continued to throw
122 throw new FacesException(e);
127 * Initializes logger instance
129 private void initLoggerInstance () {
131 // Get initial context
132 Context context = new InitialContext();
135 this.loggerBeanLocal = (LoggerBeanLocal) context.lookup("java:global/jcore-logger-ejb/logger!org.mxchange.jcoreeelogger.beans.local.logger.LoggerBeanLocal"); //NOI18N
136 } catch (final NamingException ex) {
138 throw new RuntimeException(MessageFormat.format("context.lookup() failed: {0}", ex.getMessage()), ex); //NOI18N
143 * Getter for connection instance
145 * @return Connection instance
147 protected EntityManager getEntityManager () {
148 return this.entityManager;
152 * Getter for loggerBeanLocal
154 * @return Logger instance
156 protected LoggerBeanLocal getLoggerBeanLocal () {
157 return this.loggerBeanLocal;
161 * Getter for configured message producer instance
163 * @return Message producer
165 protected MessageProducer getMessageProducer () {
166 return this.messageProducer;
170 * Getter for configured session instance
174 protected Session getSession () {
179 * Sends given message to configured queue
181 * @param message Message to send
183 * @throws JMSException if something went wrong
185 protected void sendMessage (final ObjectMessage message) throws JMSException {
187 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.sendMessage: message={1} - CALLED!", this.getClass().getSimpleName(), message)); //NOI18N
189 // The parameter should be valid
190 if (null == message) {
192 throw new NullPointerException("message is null"); //NOI18N
193 } else if (this.getMessageProducer() == null) {
195 throw new NullPointerException("this.messageProvider is null"); //NOI18N
199 this.getMessageProducer().send(message);
202 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.sendMessage: EXIT!", this.getClass().getSimpleName())); //NOI18N