]> git.mxchange.org Git - jcore-utils.git/blob - src/org/mxchange/jcoreee/bean/BaseBean.java
The local logger EJB is only available to EJBs, not web applications. Therefore
[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 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;
32
33 /**
34  * A generic bean class
35  * <p>
36  * @author Roland Häder<roland@mxchange.org>
37  */
38 public abstract class BaseBean implements Serializable {
39
40         /**
41          * Serial number
42          */
43         private static final long serialVersionUID = 18_305_698_567_265L;
44
45         /**
46          * Connection
47          */
48         private Connection connection;
49
50         /**
51          * Message producer
52          */
53         private MessageProducer messageProducer;
54
55         /**
56          * Mailer message queue
57          */
58         private Queue queue;
59
60         /**
61          * Session instance
62          */
63         private Session session;
64
65         /**
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.
69          */
70         protected BaseBean () {
71         }
72
73         /**
74          * Constructor with queue factory JNDI and queue JNDI names
75          * <p>
76          * @param factoryJndi    JNDI name for queue factory
77          * @param queueJndi JNDI name for email queue
78          */
79         protected BaseBean (final String factoryJndi, final String queueJndi) {
80                 // Call default constructor
81                 this();
82
83                 // Try it out
84                 try {
85                         // Get initial context
86                         Context context = new InitialContext();
87
88                         // Get factory from JMS resource
89                         QueueConnectionFactory connectionFactory = (QueueConnectionFactory) context.lookup(factoryJndi); //NOI18N
90
91                         // Lookup queue
92                         this.queue = (Queue) context.lookup(queueJndi); //NOI18N
93
94                         // Create connection
95                         this.connection = connectionFactory.createConnection();
96
97                         // Init session instance
98                         this.session = this.connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
99
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);
105                 }
106         }
107
108         /**
109          * Determines principal's name or returns null if no principal (security) is
110          * set.
111          * <p>
112          * @return Principal's name or null
113          */
114         protected String determinePrincipalName () {
115                 // Get principal
116                 Principal userPrincipal = FacesContext.getCurrentInstance().getExternalContext().getUserPrincipal();
117
118                 // Init with null
119                 String principalName = null;
120
121                 // Is the principal set?
122                 if (userPrincipal instanceof Principal) {
123                         // Get principal's name
124                         principalName = userPrincipal.getName();
125                 }
126
127                 // Return it
128                 return principalName;
129         }
130
131         /**
132          * Getter for configured message producer instance
133          * <p>
134          * @return Message producer
135          */
136         protected MessageProducer getMessageProducer () {
137                 return this.messageProducer;
138         }
139
140         /**
141          * Getter for configured session instance
142          * <p>
143          * @return Session
144          */
145         protected Session getSession () {
146                 return this.session;
147         }
148
149 }