]> git.mxchange.org Git - jcoreee.git/blob - src/org/mxchange/jcoreee/bean/ejb/BaseEnterpriseBean.java
Continued:
[jcoreee.git] / src / org / mxchange / jcoreee / bean / ejb / BaseEnterpriseBean.java
1 /*
2  * Copyright (C) 2016 - 2022 Free Software Foundation
3  *
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.
8  *
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.
13  *
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/>.
16  */
17 package org.mxchange.jcoreee.bean.ejb;
18
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;
36
37 /**
38  * A helper class for beans that access the database.
39  * <p>
40  * @author Roland Häder<roland@mxchange.org>
41  */
42 public abstract class BaseEnterpriseBean implements Serializable {
43
44         /**
45          * Serial number
46          */
47         private static final long serialVersionUID = 217_687_175_985_875L;
48
49         /**
50          * Connection
51          */
52         private Connection connection;
53
54         /**
55          * Entity manager
56          */
57         @PersistenceContext
58         private EntityManager entityManager;
59
60         /**
61          * Logger instance
62          */
63         @Log
64         private LoggerBeanLocal loggerBeanLocal;
65
66         /**
67          * Message producer
68          */
69         private MessageProducer messageProducer;
70
71         /**
72          * Mailer message queue
73          */
74         private Queue queue;
75
76         /**
77          * Session instance
78          */
79         private Session session;
80
81         /**
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.
85          */
86         protected BaseEnterpriseBean () {
87                 // Init logger instance
88                 this.initLoggerInstance();
89         }
90
91         /**
92          * Constructor with queue factory JNDI and queue JNDI names
93          * <p>
94          * @param factoryJndi JNDI name for queue factory
95          * @param queueJndi   JNDI name for email queue
96          */
97         protected BaseEnterpriseBean (final String factoryJndi, final String queueJndi) {
98                 // Invoke default constructor
99                 this();
100
101                 // Validate all parameter
102                 if (null == factoryJndi) {
103                         // Throw IAE
104                         throw new NullPointerException("factoryJndi is null"); //NOI18N
105                 } else if (factoryJndi.isEmpty()) {
106                         // Throw IAE
107                         throw new IllegalArgumentException("factoryJndi is empty"); //NOI18N
108                 } else if (null == queueJndi) {
109                         // Throw IAE
110                         throw new NullPointerException("queueJndi is null"); //NOI18N
111                 } else if (queueJndi.isEmpty()) {
112                         // Throw IAE
113                         throw new IllegalArgumentException("queueJndi is empty"); //NOI18N
114                 }
115
116                 // Try it out
117                 try {
118                         // Get initial context
119                         Context context = new InitialContext();
120
121                         // Get factory from JMS resource
122                         QueueConnectionFactory connectionFactory = (QueueConnectionFactory) context.lookup(factoryJndi);
123
124                         // Lookup queue
125                         this.queue = (Queue) context.lookup(queueJndi);
126
127                         // Create connection
128                         this.connection = connectionFactory.createConnection();
129
130                         // Init session instance
131                         this.session = this.connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
132
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);
138                 }
139         }
140
141         /**
142          * Initializes logger instance
143          */
144         private void initLoggerInstance () {
145                 try {
146                         // Get initial context
147                         Context context = new InitialContext();
148
149                         // Lookup logger
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) {
152                         // Continue to throw
153                         throw new RuntimeException(MessageFormat.format("context.lookup() failed: {0}", ex.getMessage()), ex); //NOI18N
154                 }
155         }
156
157         /**
158          * Getter for connection instance
159          * <p>
160          * @return Connection instance
161          */
162         protected EntityManager getEntityManager () {
163                 return this.entityManager;
164         }
165
166         /**
167          * Getter for loggerBeanLocal
168          * <p>
169          * @return Logger instance
170          */
171         protected LoggerBeanLocal getLoggerBeanLocal () {
172                 return this.loggerBeanLocal;
173         }
174
175         /**
176          * Getter for configured message producer instance
177          * <p>
178          * @return Message producer
179          */
180         protected MessageProducer getMessageProducer () {
181                 return this.messageProducer;
182         }
183
184         /**
185          * Getter for configured session instance
186          * <p>
187          * @return Session
188          */
189         protected Session getSession () {
190                 return this.session;
191         }
192
193         /**
194          * Sends given message to configured queue
195          * <p>
196          * @param message Message to send
197          * <p>
198          * @throws JMSException if something went wrong
199          */
200         protected void sendMessage (final ObjectMessage message) throws JMSException {
201                 // Trace message
202                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.sendMessage: message={1} - CALLED!", this.getClass().getSimpleName(), message)); //NOI18N
203
204                 // The parameter should be valid
205                 if (null == message) {
206                         // Throw NPE
207                         throw new NullPointerException("message is null"); //NOI18N
208                 } else if (this.getMessageProducer() == null) {
209                         // Throw NPE again
210                         throw new NullPointerException("this.messageProvider is null"); //NOI18N
211                 }
212
213                 // Send it
214                 this.getMessageProducer().send(message);
215
216                 // Trace message
217                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.sendMessage: EXIT!", this.getClass().getSimpleName())); //NOI18N
218         }
219
220 }