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 javax.faces.FacesException;
22 import javax.faces.context.FacesContext;
23 import javax.jms.Connection;
24 import javax.jms.JMSException;
25 import javax.jms.MessageProducer;
26 import javax.jms.Queue;
27 import javax.jms.QueueConnectionFactory;
28 import javax.jms.Session;
29 import javax.naming.Context;
30 import javax.naming.InitialContext;
31 import javax.naming.NamingException;
34 * A generic bean class
36 * @author Roland Häder<roland@mxchange.org>
38 public abstract class BaseBean implements Serializable {
43 private static final long serialVersionUID = 18_305_698_567_265L;
48 private Connection connection;
53 private MessageProducer messageProducer;
56 * Mailer message queue
63 private Session session;
66 * This class' default protected constructor. Please call
67 * super("jms/project-queue-factory", "jms/project-email-queue"); if you
68 * need to send emails.
70 protected BaseBean () {
74 * Constructor with queue factory JNDI and queue JNDI names
76 * @param factoryJndi JNDI name for queue factory
77 * @param queueJndi JNDI name for email queue
79 protected BaseBean (final String factoryJndi, final String queueJndi) {
80 // Call default constructor
85 // Get initial context
86 Context context = new InitialContext();
88 // Get factory from JMS resource
89 QueueConnectionFactory connectionFactory = (QueueConnectionFactory) context.lookup(factoryJndi); //NOI18N
92 this.queue = (Queue) context.lookup(queueJndi); //NOI18N
95 this.connection = connectionFactory.createConnection();
97 // Init session instance
98 this.session = this.connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
100 // And message producer
101 this.messageProducer = this.session.createProducer(this.queue);
102 } catch (final NamingException | JMSException e) {
103 // Continued to throw
104 throw new FacesException(e);
109 * Determines principal's name or returns null if no principal (security) is
112 * @return Principal's name or null
114 protected String determinePrincipalName () {
116 Principal userPrincipal = FacesContext.getCurrentInstance().getExternalContext().getUserPrincipal();
119 String principalName = null;
121 // Is the principal set?
122 if (userPrincipal instanceof Principal) {
123 // Get principal's name
124 principalName = userPrincipal.getName();
128 return principalName;
132 * Getter for configured message producer instance
134 * @return Message producer
136 protected MessageProducer getMessageProducer () {
137 return this.messageProducer;
141 * Getter for configured session instance
145 protected Session getSession () {