]> git.mxchange.org Git - jcore-utils.git/blob - src/org/mxchange/jcoreee/bean/BaseBean.java
ef7f11ddeac432bf1ddde1b63cd0e7d47a471157
[jcore-utils.git] / src / org / mxchange / jcoreee / bean / BaseBean.java
1 /*
2  * Copyright (C) 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.bean;
18
19 import java.io.Serializable;
20 import java.security.Principal;
21 import java.text.MessageFormat;
22 import java.util.Locale;
23 import java.util.MissingResourceException;
24 import java.util.ResourceBundle;
25 import javax.faces.FacesException;
26 import javax.faces.application.FacesMessage;
27 import javax.faces.context.FacesContext;
28 import javax.jms.Connection;
29 import javax.jms.JMSException;
30 import javax.jms.MessageProducer;
31 import javax.jms.ObjectMessage;
32 import javax.jms.Queue;
33 import javax.jms.QueueConnectionFactory;
34 import javax.jms.Session;
35 import javax.naming.Context;
36 import javax.naming.InitialContext;
37 import javax.naming.NamingException;
38 import org.mxchange.jcoreeelogger.beans.local.logger.Log;
39 import org.mxchange.jcoreeelogger.beans.local.logger.LoggerBeanLocal;
40
41 /**
42  * A generic bean class
43  * <p>
44  * @author Roland Häder<roland@mxchange.org>
45  */
46 public abstract class BaseBean implements Serializable {
47
48         /**
49          * Serial number
50          */
51         private static final long serialVersionUID = 18_305_698_567_265L;
52
53         /**
54          * Connection
55          */
56         private Connection connection;
57
58         /**
59          * Logger instance
60          */
61         @Log
62         private LoggerBeanLocal loggerBeanLocal;
63
64         /**
65          * Message producer
66          */
67         private MessageProducer messageProducer;
68
69         /**
70          * Mailer message queue
71          */
72         private Queue queue;
73
74         /**
75          * Session instance
76          */
77         private Session session;
78
79         /**
80          * This class' default protected constructor. Please call
81          * super("jms/project-queue-factory", "jms/project-email-queue"); if you
82          * need to send emails.
83          */
84         protected BaseBean () {
85                 try {
86                         // Get initial context
87                         Context context = new InitialContext();
88
89                         // Lookup logger
90                         this.loggerBeanLocal = (LoggerBeanLocal) context.lookup("java:global/jcore-logger-ejb/logger!org.mxchange.jcoreeelogger.beans.local.logger.LoggerBeanLocal"); //NOI18N
91                 } catch (final NamingException ex) {
92                         // Continue to throw
93                         throw new RuntimeException(MessageFormat.format("context.lookup() failed: {0}", ex.getMessage()), ex); //NOI18N
94                 }
95         }
96
97         /**
98          * Constructor with queue factory JNDI and queue JNDI names
99          * <p>
100          * @param factoryJndi    JNDI name for queue factory
101          * @param queueJndi JNDI name for email queue
102          */
103         protected BaseBean (final String factoryJndi, final String queueJndi) {
104                 // Call default constructor
105                 this();
106
107                 // Try it out
108                 try {
109                         // Get initial context
110                         Context context = new InitialContext();
111
112                         // Get factory from JMS resource
113                         QueueConnectionFactory connectionFactory = (QueueConnectionFactory) context.lookup(factoryJndi); //NOI18N
114
115                         // Lookup queue
116                         this.queue = (Queue) context.lookup(queueJndi); //NOI18N
117
118                         // Create connection
119                         this.connection = connectionFactory.createConnection();
120
121                         // Init session instance
122                         this.session = this.connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
123
124                         // And message producer
125                         this.messageProducer = this.session.createProducer(this.queue);
126                 } catch (final NamingException | JMSException e) {
127                         // Continued to throw
128                         throw new FacesException(e);
129                 }
130         }
131
132         /**
133          * Determines principal's name or returns null if no principal (security) is
134          * set.
135          * <p>
136          * @return Principal's name or null
137          */
138         protected String determinePrincipalName () {
139                 // Get principal
140                 Principal userPrincipal = FacesContext.getCurrentInstance().getExternalContext().getUserPrincipal();
141
142                 // Init with null
143                 String principalName = null;
144
145                 // Is the principal set?
146                 if (userPrincipal instanceof Principal) {
147                         // Get principal's name
148                         principalName = userPrincipal.getName();
149                 }
150
151                 // Return it
152                 return principalName;
153         }
154
155
156         /**
157          * Getter for loggerBeanLocal
158          * <p>
159          * @return Logger instance
160          */
161         protected LoggerBeanLocal getLoggerBeanLocal () {
162                 return this.loggerBeanLocal;
163         }
164
165         /**
166          * Getter for configured message producer instance
167          * <p>
168          * @return Message producer
169          */
170         protected MessageProducer getMessageProducer () {
171                 return this.messageProducer;
172         }
173
174         /**
175          * Getter for configured session instance
176          * <p>
177          * @return Session
178          */
179         protected Session getSession () {
180                 return this.session;
181         }
182
183
184         /**
185          * Sends given message to configured queue
186          * <p>
187          * @param message Message to send
188          * <p>
189          * @throws JMSException if something went wrong
190          */
191         protected void sendMessage (final ObjectMessage message) throws JMSException {
192                 // Trace message
193                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.sendMessage: message={1} - CALLED!", this.getClass().getSimpleName(), message)); //NOI18N
194
195                 // The parameter should be valid
196                 if (null == message) {
197                         // Throw NPE
198                         throw new NullPointerException("message is null"); //NOI18N
199                 } else if (null == this.getMessageProducer()) {
200                         // Throw NPE again
201                         throw new NullPointerException("this.messageProvider is null"); //NOI18N
202                 }
203
204                 // Send it
205                 this.getMessageProducer().send(message);
206
207                 // Trace message
208                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.sendMessage: EXIT!", this.getClass().getSimpleName())); //NOI18N
209         }
210
211
212 }