2 * Copyright (C) 2016 Roland Haeder
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU Affero General Public License as
6 * published by the Free Software Foundation, either version 3 of the
7 * License, or (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 Affero General Public License for more details.
14 * You should have received a copy of the GNU Affero General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 package org.mxchange.pizzaapplication.mailer.model.delivery;
19 import java.io.Serializable;
20 import java.text.MessageFormat;
21 import java.util.Properties;
22 import java.util.ResourceBundle;
23 import javax.annotation.PostConstruct;
24 import javax.ejb.ActivationConfigProperty;
25 import javax.ejb.MessageDriven;
26 import javax.jms.JMSException;
27 import javax.jms.Message;
28 import javax.jms.MessageListener;
29 import javax.jms.ObjectMessage;
30 import javax.mail.MessagingException;
31 import org.mxchange.jmailee.model.delivery.wrapper.WrapableEmailDelivery;
32 import org.mxchange.pizzaaplication.database.BasePizzaDatabaseBean;
35 * A message-driven bean for sending out emails
37 * @author Roland Haeder<roland@mxchange.org>
40 name = "emailDelivery",
41 description = "A message-driven bean for email delivery (some kind of 'queue')",
43 @ActivationConfigProperty (
44 propertyName = "destinationLookup",
45 propertyValue = "jms/jreports-email-queue"
47 @ActivationConfigProperty (
48 propertyName = "destinationType",
49 propertyValue = "javax.jms.Queue"
53 public class PizzaEmailDeliveryMessageBean extends BasePizzaDatabaseBean implements MessageListener {
58 private static final long serialVersionUID = 75_638_176_619_024L;
63 private final String configFile = "org.mxchange.jmailer.config"; //NOI18N
68 private final DeliverablePizzaEmail mailer;
73 public PizzaEmailDeliveryMessageBean () {
74 // Init mailer instance
75 this.mailer = new PizzaMailer();
84 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.init: CALLED!", this.getClass().getSimpleName())); //NOI18N
87 ResourceBundle bundle = ResourceBundle.getBundle(this.configFile);
90 this.getLoggerBeanLocal().logDebug(MessageFormat.format("{0}.init: bundle={1}", this.getClass().getSimpleName(), bundle)); //NOI18N
92 // The bunble should be valid
95 throw new NullPointerException(MessageFormat.format("bundle is null, maybe file {0} does not exist?", this.configFile)); //NOI18N
99 Properties properties = new Properties();
101 // Is the bundle not empty?
102 if (!bundle.keySet().isEmpty()) {
104 for (final String key : bundle.keySet()) {
106 this.getLoggerBeanLocal().logDebug(MessageFormat.format("{0}.init: key={1}", this.getClass().getSimpleName(), key)); //NOI18N
108 // Get string from bundle and set it in properties
109 properties.put(key, bundle.getString(key));
113 // Handle it over to the mailer
114 this.mailer.init(properties);
117 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.init: EXIT!", this.getClass().getSimpleName())); //NOI18N
121 public void onMessage (final Message message) {
123 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.onMessage: message={1} - CALLED!", this.getClass().getSimpleName(), message)); //NOI18N
125 // The parameter should be valid
126 if (null == message) {
128 throw new NullPointerException("message is null"); //NOI18N
129 } else if (!(message instanceof ObjectMessage)) {
130 // Not implementing right interface
131 throw new IllegalArgumentException(MessageFormat.format("message={0} does not implemented ObjectMessage", message)); //NOI18N
135 ObjectMessage objectMessage = (ObjectMessage) message;
138 Serializable serializable;
141 // Get object from message
142 serializable = objectMessage.getObject();
143 } catch (final JMSException ex) {
144 // Log it and don't continue any further
145 this.getLoggerBeanLocal().logException(ex);
150 this.getLoggerBeanLocal().logDebug(MessageFormat.format("{0}.onMessage: serializable={1}", this.getClass().getSimpleName(), serializable)); //NOI18N
152 // Okay, is it the right interface?
153 if (null == serializable) {
155 throw new NullPointerException("serializable is null"); //NOI18N
156 } else if (!(serializable instanceof WrapableEmailDelivery)) {
157 // Not correct object send
158 throw new IllegalArgumentException(MessageFormat.format("serializable={0} does not implement WrapableEmailDelivery", serializable)); //NOI18N
162 WrapableEmailDelivery wrapper = (WrapableEmailDelivery) serializable;
164 // Is all required set?
165 if (wrapper.getLocale() == null) {
167 throw new NullPointerException("wrapper.locale is null"); //NOI18N
168 } else if (wrapper.getRecipient() == null) {
170 throw new NullPointerException("wrapper.recipient is null"); //NOI18N
171 } else if (wrapper.getSubjectLine() == null) {
173 throw new NullPointerException("wrapper.subjectLine is null"); //NOI18N
174 } else if (wrapper.getSubjectLine().isEmpty()) {
176 throw new IllegalArgumentException("wrapper.subjectLine is empty"); //NOI18N
177 } else if (wrapper.getTemplateName() == null) {
179 throw new NullPointerException("wrapper.templateName is null"); //NOI18N
180 } else if (wrapper.getTemplateName().isEmpty()) {
182 throw new IllegalArgumentException("wrapper.templateName is empty"); //NOI18N
183 } else if (wrapper.getTemplateVariables() == null) {
184 // No template variables set, should not happen
185 throw new NullPointerException("wrapper.templateVariables is null"); //NOI18N
190 this.mailer.sendDeliverableMail(wrapper);
191 } catch (final MessagingException ex) {
192 // Opps, something went wrong
193 this.getLoggerBeanLocal().logException(ex);
198 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.onMessage - EXIT!", this.getClass().getSimpleName())); //NOI18N