]> git.mxchange.org Git - jcore-utils.git/blob - src/org/mxchange/jcoreee/database/BaseDatabaseBean.java
Introduced initLoggerInstance() which every contructor needs to call, surely
[jcore-utils.git] / src / org / mxchange / jcoreee / database / BaseDatabaseBean.java
1 /*
2  * Copyright (C) 2016, 2017 Roland Häder
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.database;
18
19 import java.text.MessageFormat;
20 import javax.jms.JMSException;
21 import javax.jms.ObjectMessage;
22 import javax.naming.Context;
23 import javax.naming.InitialContext;
24 import javax.naming.NamingException;
25 import javax.persistence.EntityManager;
26 import javax.persistence.PersistenceContext;
27 import org.mxchange.jcoreee.bean.BaseBean;
28 import org.mxchange.jcoreeelogger.beans.local.logger.Log;
29 import org.mxchange.jcoreeelogger.beans.local.logger.LoggerBeanLocal;
30
31 /**
32  * A helper class for beans that access the database.
33  * <p>
34  * @author Roland Häder<roland@mxchange.org>
35  */
36 public abstract class BaseDatabaseBean extends BaseBean {
37
38         /**
39          * Serial number
40          */
41         private static final long serialVersionUID = 217_687_175_985_875L;
42
43         /**
44          * Entity manager
45          */
46         @PersistenceContext
47         private EntityManager entityManager;
48
49         /**
50          * Logger instance
51          */
52         @Log
53         private LoggerBeanLocal loggerBeanLocal;
54
55         /**
56          * This class' default protected constructor. Please call
57          * super("jms/project-queue-factory", "jms/project-email-queue"); if you
58          * need to send emails.
59          */
60         protected BaseDatabaseBean () {
61                 // Call super constructor
62                 super();
63
64                 // Init logger instance
65                 this.initLoggerInstance();
66         }
67
68         /**
69          * Constructor with queue factory JNDI and queue JNDI names
70          * <p>
71          * @param factoryJndi JNDI name for queue factory
72          * @param queueJndi   JNDI name for email queue
73          */
74         protected BaseDatabaseBean (final String factoryJndi, final String queueJndi) {
75                 // Call super constructor
76                 super(factoryJndi, queueJndi);
77
78                 // Init logger instance
79                 this.initLoggerInstance();
80         }
81
82         /**
83          * Initializes logger instance
84          */
85         private void initLoggerInstance () {
86                 try {
87                         // Get initial context
88                         Context context = new InitialContext();
89
90                         // Lookup logger
91                         this.loggerBeanLocal = (LoggerBeanLocal) context.lookup("java:global/jcore-logger-ejb/logger!org.mxchange.jcoreeelogger.beans.local.logger.LoggerBeanLocal"); //NOI18N
92                 } catch (final NamingException ex) {
93                         // Continue to throw
94                         throw new RuntimeException(MessageFormat.format("context.lookup() failed: {0}", ex.getMessage()), ex); //NOI18N
95                 }
96         }
97
98         /**
99          * Getter for connection instance
100          * <p>
101          * @return Connection instance
102          */
103         protected EntityManager getEntityManager () {
104                 return this.entityManager;
105         }
106
107         /**
108          * Getter for loggerBeanLocal
109          * <p>
110          * @return Logger instance
111          */
112         protected LoggerBeanLocal getLoggerBeanLocal () {
113                 return this.loggerBeanLocal;
114         }
115
116         /**
117          * Sends given message to configured queue
118          * <p>
119          * @param message Message to send
120          * <p>
121          * @throws JMSException if something went wrong
122          */
123         protected void sendMessage (final ObjectMessage message) throws JMSException {
124                 // Trace message
125                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.sendMessage: message={1} - CALLED!", this.getClass().getSimpleName(), message)); //NOI18N
126
127                 // The parameter should be valid
128                 if (null == message) {
129                         // Throw NPE
130                         throw new NullPointerException("message is null"); //NOI18N
131                 } else if (this.getMessageProducer() == null) {
132                         // Throw NPE again
133                         throw new NullPointerException("this.messageProvider is null"); //NOI18N
134                 }
135
136                 // Send it
137                 this.getMessageProducer().send(message);
138
139                 // Trace message
140                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.sendMessage: EXIT!", this.getClass().getSimpleName())); //NOI18N
141         }
142
143 }