2 * Copyright (C) 2016 - 2024 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 // Invoke default constructor
101 // Validate all parameter
102 if (null == factoryJndi) {
104 throw new NullPointerException("factoryJndi is null"); //NOI18N
105 } else if (factoryJndi.isEmpty()) {
107 throw new IllegalArgumentException("factoryJndi is empty"); //NOI18N
108 } else if (null == queueJndi) {
110 throw new NullPointerException("queueJndi is null"); //NOI18N
111 } else if (queueJndi.isEmpty()) {
113 throw new IllegalArgumentException("queueJndi is empty"); //NOI18N
118 // Get initial context
119 final Context context = new InitialContext();
121 // Get factory from JMS resource
122 final QueueConnectionFactory connectionFactory = (QueueConnectionFactory) context.lookup(factoryJndi);
125 this.queue = (Queue) context.lookup(queueJndi);
128 this.connection = connectionFactory.createConnection();
130 // Init session instance
131 this.session = this.connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
133 // And message producer
134 this.messageProducer = this.session.createProducer(this.queue);
135 } catch (final NamingException | JMSException e) {
136 // Continued to throw
137 throw new FacesException(e);
142 * Initializes logger instance
144 private void initLoggerInstance () {
146 // Get initial context
147 final Context context = new InitialContext();
150 this.loggerBeanLocal = (LoggerBeanLocal) context.lookup("java:global/jcore-logger-ejb/logger!org.mxchange.jcoreeelogger.beans.local.logger.LoggerBeanLocal"); //NOI18N
151 } catch (final NamingException ex) {
153 throw new RuntimeException(MessageFormat.format("context.lookup() failed: {0}", ex.getMessage()), ex); //NOI18N
158 * Getter for connection instance
160 * @return Connection instance
162 protected EntityManager getEntityManager () {
163 return this.entityManager;
167 * Getter for loggerBeanLocal
169 * @return Logger instance
171 protected LoggerBeanLocal getLoggerBeanLocal () {
172 return this.loggerBeanLocal;
176 * Getter for configured message producer instance
178 * @return Message producer
180 protected MessageProducer getMessageProducer () {
181 return this.messageProducer;
185 * Getter for configured session instance
189 protected Session getSession () {
194 * Sends given message to configured queue
196 * @param message Message to send
198 * @throws JMSException if something went wrong
200 protected void sendMessage (final ObjectMessage message) throws JMSException {
202 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.sendMessage: message={1} - CALLED!", this.getClass().getSimpleName(), message)); //NOI18N
204 // The parameter should be valid
205 if (null == message) {
207 throw new NullPointerException("message is null"); //NOI18N
208 } else if (this.getMessageProducer() == null) {
210 throw new NullPointerException("this.messageProvider is null"); //NOI18N
214 this.getMessageProducer().send(message);
217 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.sendMessage: EXIT!", this.getClass().getSimpleName())); //NOI18N