2 * Copyright (C) 2017 Roland Häder
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.
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.
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/>.
17 package org.mxchange.jcoreee.bean;
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;
42 * A generic bean class
44 * @author Roland Häder<roland@mxchange.org>
46 public abstract class BaseBean implements Serializable {
51 private static final long serialVersionUID = 18_305_698_567_265L;
56 private Connection connection;
62 private LoggerBeanLocal loggerBeanLocal;
67 private MessageProducer messageProducer;
70 * Mailer message queue
77 private Session session;
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.
84 protected BaseBean () {
86 // Get initial context
87 Context context = new InitialContext();
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) {
93 throw new RuntimeException(MessageFormat.format("context.lookup() failed: {0}", ex.getMessage()), ex); //NOI18N
98 * Constructor with queue factory JNDI and queue JNDI names
100 * @param factoryJndi JNDI name for queue factory
101 * @param queueJndi JNDI name for email queue
103 protected BaseBean (final String factoryJndi, final String queueJndi) {
104 // Call default constructor
109 // Get initial context
110 Context context = new InitialContext();
112 // Get factory from JMS resource
113 QueueConnectionFactory connectionFactory = (QueueConnectionFactory) context.lookup(factoryJndi); //NOI18N
116 this.queue = (Queue) context.lookup(queueJndi); //NOI18N
119 this.connection = connectionFactory.createConnection();
121 // Init session instance
122 this.session = this.connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
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);
133 * Determines principal's name or returns null if no principal (security) is
136 * @return Principal's name or null
138 protected String determinePrincipalName () {
140 Principal userPrincipal = FacesContext.getCurrentInstance().getExternalContext().getUserPrincipal();
143 String principalName = null;
145 // Is the principal set?
146 if (userPrincipal instanceof Principal) {
147 // Get principal's name
148 principalName = userPrincipal.getName();
152 return principalName;
157 * Getter for loggerBeanLocal
159 * @return Logger instance
161 protected LoggerBeanLocal getLoggerBeanLocal () {
162 return this.loggerBeanLocal;
166 * Getter for configured message producer instance
168 * @return Message producer
170 protected MessageProducer getMessageProducer () {
171 return this.messageProducer;
175 * Getter for configured session instance
179 protected Session getSession () {
185 * Sends given message to configured queue
187 * @param message Message to send
189 * @throws JMSException if something went wrong
191 protected void sendMessage (final ObjectMessage message) throws JMSException {
193 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.sendMessage: message={1} - CALLED!", this.getClass().getSimpleName(), message)); //NOI18N
195 // The parameter should be valid
196 if (null == message) {
198 throw new NullPointerException("message is null"); //NOI18N
199 } else if (null == this.getMessageProducer()) {
201 throw new NullPointerException("this.messageProvider is null"); //NOI18N
205 this.getMessageProducer().send(message);
208 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.sendMessage: EXIT!", this.getClass().getSimpleName())); //NOI18N