]> git.mxchange.org Git - jcore-utils.git/blob - src/org/mxchange/jcoreee/database/BaseDatabaseBean.java
71a92feef35163668c10a0149d38bdba5d795abb
[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                 try {
65                         // Get initial context
66                         Context context = new InitialContext();
67
68                         // Lookup logger
69                         this.loggerBeanLocal = (LoggerBeanLocal) context.lookup("java:global/jcore-logger-ejb/logger!org.mxchange.jcoreeelogger.beans.local.logger.LoggerBeanLocal"); //NOI18N
70                 } catch (final NamingException ex) {
71                         // Continue to throw
72                         throw new RuntimeException(MessageFormat.format("context.lookup() failed: {0}", ex.getMessage()), ex); //NOI18N
73                 }
74         }
75
76         /**
77          * Constructor with queue factory JNDI and queue JNDI names
78          * <p>
79          * @param factoryJndi    JNDI name for queue factory
80          * @param queueJndi JNDI name for email queue
81          */
82         protected BaseDatabaseBean (final String factoryJndi, final String queueJndi) {
83                 // Call super constructor
84                 super(factoryJndi, queueJndi);
85         }
86
87         /**
88          * Getter for connection instance
89          * <p>
90          * @return Connection instance
91          */
92         protected EntityManager getEntityManager () {
93                 return this.entityManager;
94         }
95
96         /**
97          * Getter for loggerBeanLocal
98          * <p>
99          * @return Logger instance
100          */
101         protected LoggerBeanLocal getLoggerBeanLocal () {
102                 return this.loggerBeanLocal;
103         }
104
105         /**
106          * Sends given message to configured queue
107          * <p>
108          * @param message Message to send
109          * <p>
110          * @throws JMSException if something went wrong
111          */
112         protected void sendMessage (final ObjectMessage message) throws JMSException {
113                 // Trace message
114                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.sendMessage: message={1} - CALLED!", this.getClass().getSimpleName(), message)); //NOI18N
115
116                 // The parameter should be valid
117                 if (null == message) {
118                         // Throw NPE
119                         throw new NullPointerException("message is null"); //NOI18N
120                 } else if (this.getMessageProducer() == null) {
121                         // Throw NPE again
122                         throw new NullPointerException("this.messageProvider is null"); //NOI18N
123                 }
124
125                 // Send it
126                 this.getMessageProducer().send(message);
127
128                 // Trace message
129                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.sendMessage: EXIT!", this.getClass().getSimpleName())); //NOI18N
130         }
131
132 }