X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=src%2Forg%2Fmxchange%2Fjcoreee%2Fbean%2Fejb%2FBaseEnterpriseBean.java;fp=src%2Forg%2Fmxchange%2Fjcoreee%2Fbean%2Fejb%2FBaseEnterpriseBean.java;h=a08610c116cc03b03350393133e5bdf8796583a6;hb=b92af5ac83b41b05b1df42c8b364f796acb3e4c3;hp=0000000000000000000000000000000000000000;hpb=1f141c043a053621b86a5701420ca71f17a64e7b;p=jcore-utils.git
diff --git a/src/org/mxchange/jcoreee/bean/ejb/BaseEnterpriseBean.java b/src/org/mxchange/jcoreee/bean/ejb/BaseEnterpriseBean.java
new file mode 100644
index 0000000..a08610c
--- /dev/null
+++ b/src/org/mxchange/jcoreee/bean/ejb/BaseEnterpriseBean.java
@@ -0,0 +1,208 @@
+/*
+ * Copyright (C) 2016 - 2018 Free Software Foundation
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see
+ * @author Roland Häder
+ * @param factoryJndi JNDI name for queue factory
+ * @param queueJndi JNDI name for email queue
+ */
+ protected BaseEnterpriseBean (final String factoryJndi, final String queueJndi) {
+ // Call default constructor
+ this();
+
+ // Try it out
+ try {
+ // Get initial context
+ Context context = new InitialContext();
+
+ // Get factory from JMS resource
+ QueueConnectionFactory connectionFactory = (QueueConnectionFactory) context.lookup(factoryJndi);
+
+ // Lookup queue
+ this.queue = (Queue) context.lookup(queueJndi);
+
+ // Create connection
+ this.connection = connectionFactory.createConnection();
+
+ // Init session instance
+ this.session = this.connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
+
+ // And message producer
+ this.messageProducer = this.session.createProducer(this.queue);
+ } catch (final NamingException | JMSException e) {
+ // Continued to throw
+ throw new FacesException(e);
+ }
+ }
+
+ /**
+ * Initializes logger instance
+ */
+ private void initLoggerInstance () {
+ try {
+ // Get initial context
+ Context context = new InitialContext();
+
+ // Lookup logger
+ this.loggerBeanLocal = (LoggerBeanLocal) context.lookup("java:global/jcore-logger-ejb/logger!org.mxchange.jcoreeelogger.beans.local.logger.LoggerBeanLocal"); //NOI18N
+ } catch (final NamingException ex) {
+ // Continue to throw
+ throw new RuntimeException(MessageFormat.format("context.lookup() failed: {0}", ex.getMessage()), ex); //NOI18N
+ }
+ }
+
+ /**
+ * Getter for connection instance
+ *
+ * @return Connection instance
+ */
+ protected EntityManager getEntityManager () {
+ return this.entityManager;
+ }
+
+ /**
+ * Getter for loggerBeanLocal
+ *
+ * @return Logger instance
+ */
+ protected LoggerBeanLocal getLoggerBeanLocal () {
+ return this.loggerBeanLocal;
+ }
+
+ /**
+ * Getter for configured message producer instance
+ *
+ * @return Message producer
+ */
+ protected MessageProducer getMessageProducer () {
+ return this.messageProducer;
+ }
+
+ /**
+ * Getter for configured session instance
+ *
+ * @return Session
+ */
+ protected Session getSession () {
+ return this.session;
+ }
+
+ /**
+ * Sends given message to configured queue
+ *
+ * @param message Message to send
+ *
+ * @throws JMSException if something went wrong
+ */
+ protected void sendMessage (final ObjectMessage message) throws JMSException {
+ // Trace message
+ this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.sendMessage: message={1} - CALLED!", this.getClass().getSimpleName(), message)); //NOI18N
+
+ // The parameter should be valid
+ if (null == message) {
+ // Throw NPE
+ throw new NullPointerException("message is null"); //NOI18N
+ } else if (this.getMessageProducer() == null) {
+ // Throw NPE again
+ throw new NullPointerException("this.messageProvider is null"); //NOI18N
+ }
+
+ // Send it
+ this.getMessageProducer().send(message);
+
+ // Trace message
+ this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.sendMessage: EXIT!", this.getClass().getSimpleName())); //NOI18N
+ }
+
+}