]> 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 - 2018 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                 // Call super constructor
88                 super();
89
90                 // Init logger instance
91                 this.initLoggerInstance();
92         }
93
94         /**
95          * Constructor with queue factory JNDI and queue JNDI names
96          * <p>
97          * @param factoryJndi JNDI name for queue factory
98          * @param queueJndi   JNDI name for email queue
99          */
100         protected BaseEnterpriseBean (final String factoryJndi, final String queueJndi) {
101                 // Call default constructor
102                 this();
103
104                 // Try it out
105                 try {
106                         // Get initial context
107                         Context context = new InitialContext();
108
109                         // Get factory from JMS resource
110                         QueueConnectionFactory connectionFactory = (QueueConnectionFactory) context.lookup(factoryJndi);
111
112                         // Lookup queue
113                         this.queue = (Queue) context.lookup(queueJndi);
114
115                         // Create connection
116                         this.connection = connectionFactory.createConnection();
117
118                         // Init session instance
119                         this.session = this.connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
120
121                         // And message producer
122                         this.messageProducer = this.session.createProducer(this.queue);
123                 } catch (final NamingException | JMSException e) {
124                         // Continued to throw
125                         throw new FacesException(e);
126                 }
127         }
128
129         /**
130          * Initializes logger instance
131          */
132         private void initLoggerInstance () {
133                 try {
134                         // Get initial context
135                         Context context = new InitialContext();
136
137                         // Lookup logger
138                         this.loggerBeanLocal = (LoggerBeanLocal) context.lookup("java:global/jcore-logger-ejb/logger!org.mxchange.jcoreeelogger.beans.local.logger.LoggerBeanLocal"); //NOI18N
139                 } catch (final NamingException ex) {
140                         // Continue to throw
141                         throw new RuntimeException(MessageFormat.format("context.lookup() failed: {0}", ex.getMessage()), ex); //NOI18N
142                 }
143         }
144
145         /**
146          * Getter for connection instance
147          * <p>
148          * @return Connection instance
149          */
150         protected EntityManager getEntityManager () {
151                 return this.entityManager;
152         }
153
154         /**
155          * Getter for loggerBeanLocal
156          * <p>
157          * @return Logger instance
158          */
159         protected LoggerBeanLocal getLoggerBeanLocal () {
160                 return this.loggerBeanLocal;
161         }
162
163         /**
164          * Getter for configured message producer instance
165          * <p>
166          * @return Message producer
167          */
168         protected MessageProducer getMessageProducer () {
169                 return this.messageProducer;
170         }
171
172         /**
173          * Getter for configured session instance
174          * <p>
175          * @return Session
176          */
177         protected Session getSession () {
178                 return this.session;
179         }
180
181         /**
182          * Sends given message to configured queue
183          * <p>
184          * @param message Message to send
185          * <p>
186          * @throws JMSException if something went wrong
187          */
188         protected void sendMessage (final ObjectMessage message) throws JMSException {
189                 // Trace message
190                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.sendMessage: message={1} - CALLED!", this.getClass().getSimpleName(), message)); //NOI18N
191
192                 // The parameter should be valid
193                 if (null == message) {
194                         // Throw NPE
195                         throw new NullPointerException("message is null"); //NOI18N
196                 } else if (this.getMessageProducer() == null) {
197                         // Throw NPE again
198                         throw new NullPointerException("this.messageProvider is null"); //NOI18N
199                 }
200
201                 // Send it
202                 this.getMessageProducer().send(message);
203
204                 // Trace message
205                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.sendMessage: EXIT!", this.getClass().getSimpleName())); //NOI18N
206         }
207
208 }