]> git.mxchange.org Git - jcore-utils.git/blob - src/org/mxchange/jcoreee/database/BaseDatabaseBean.java
log also class name
[jcore-utils.git] / src / org / mxchange / jcoreee / database / BaseDatabaseBean.java
1 /*
2  * Copyright (C) 2016 Roland Haeder
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.io.Serializable;
20 import java.text.MessageFormat;
21 import javax.jms.JMSException;
22 import javax.jms.MessageProducer;
23 import javax.jms.ObjectMessage;
24 import javax.naming.Context;
25 import javax.naming.InitialContext;
26 import javax.naming.NamingException;
27 import javax.persistence.EntityManager;
28 import javax.persistence.PersistenceContext;
29 import org.mxchange.jcoreeelogger.beans.local.logger.Log;
30 import org.mxchange.jcoreeelogger.beans.local.logger.LoggerBeanLocal;
31
32 /**
33  * A helper class for beans that access the database.
34  * <p>
35  * @author Roland Haeder<roland@mxchange.org>
36  */
37 public abstract class BaseDatabaseBean implements Serializable {
38
39         /**
40          * Serial number
41          */
42         private static final long serialVersionUID = 217_687_175_985_875L;
43
44         /**
45          * Entity manager
46          */
47         @PersistenceContext
48         private EntityManager entityManager;
49
50         /**
51          * Logger instance
52          */
53         @Log
54         private LoggerBeanLocal loggerBeanLocal;
55
56         /**
57          * Protected constructor
58          */
59         protected BaseDatabaseBean () {
60                 try {
61                         // Get initial context
62                         Context context = new InitialContext();
63
64                         // Lookup logger
65                         this.loggerBeanLocal = (LoggerBeanLocal) context.lookup("java:global/jcore-logger-ejb/logger!org.mxchange.jcoreeelogger.beans.local.logger.LoggerBeanLocal"); //NOI18N
66                 } catch (final NamingException ex) {
67                         // Continue to throw
68                         throw new RuntimeException(MessageFormat.format("context.lookup() failed: {0}", ex.getMessage()), ex); //NOI18N
69                 }
70         }
71
72         /**
73          * Getter for connection instance
74          * <p>
75          * @return Connection instance
76          */
77         protected EntityManager getEntityManager () {
78                 return this.entityManager;
79         }
80
81         /**
82          * Getter for loggerBeanLocal
83          * <p>
84          * @return Logger instance
85          */
86         protected LoggerBeanLocal getLoggerBeanLocal () {
87                 return this.loggerBeanLocal;
88         }
89
90         /**
91          * Sends given message to configured queue
92          * <p>
93          * @param message Message to send
94          * @param messageProducer Message producer
95          * <p>
96          * @throws JMSException if something went wrong
97          */
98         protected void sendMessage (final ObjectMessage message, final MessageProducer messageProducer) throws JMSException {
99                 // Trace message
100                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.sendMessage: message={1},messageProducer={2} - CALLED!", this.getClass().getSimpleName(), message, messageProducer)); //NOI18N
101
102                 // The parameter should be valid
103                 if (null == message) {
104                         // Throw NPE
105                         throw new NullPointerException("message is null"); //NOI18N
106                 } else if (null == messageProducer) {
107                         // Throw NPE again
108                         throw new NullPointerException("messageProvider is null"); //NOI18N
109                 }
110
111                 // Send it
112                 messageProducer.send(message);
113
114                 // Trace message
115                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.sendMessage: EXIT!", this.getClass().getSimpleName())); //NOI18N
116         }
117
118 }