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.addressbook.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.jms.JMSException;
25 import javax.jms.Message;
26 import javax.jms.MessageListener;
27 import javax.jms.ObjectMessage;
28 import javax.mail.MessagingException;
29 import org.mxchange.addressbook.database.BaseAddressbookDatabaseBean;
30 import org.mxchange.jmailee.model.delivery.wrapper.WrapableEmailDelivery;
33 * A message-driven bean for sending out emails
35 * @author Roland Haeder<roland@mxchange.org>
37 @MessageDriven (activationConfig = {
38 @ActivationConfigProperty (propertyName = "destinationLookup", propertyValue = "jms/jlandingpage-email-queue"),
39 @ActivationConfigProperty (propertyName = "destinationType", propertyValue = "javax.jms.Queue")
41 public class AddressbookEmailDeliveryMessageBean extends BaseAddressbookDatabaseBean implements MessageListener {
46 private static final long serialVersionUID = 75_638_176_619_024L;
51 private final String configFile = "org.mxchange.jmailer.config"; //NOI18N//NOI18N
56 private final DeliverableAddressbookEmail mailer;
61 public AddressbookEmailDeliveryMessageBean () {
62 // Init mailer instance
63 this.mailer = new AddressbookMailer();
72 this.getLoggerBeanLocal().logTrace("init: CALLED!"); //NOI18N
75 ResourceBundle bundle = ResourceBundle.getBundle(this.configFile);
78 this.getLoggerBeanLocal().logDebug(MessageFormat.format("init: bundle={0}", bundle)); //NOI18N
80 // The bunble should be valid
83 throw new NullPointerException(MessageFormat.format("bundle is null, maybe file {0} does not exist?", this.configFile)); //NOI18N
87 Properties properties = new Properties();
89 // Is the bundle not empty?
90 if (!bundle.keySet().isEmpty()) {
92 for (final String key : bundle.keySet()) {
94 this.getLoggerBeanLocal().logDebug(MessageFormat.format("init: key={0}", key)); //NOI18N
96 // Get string from bundle and set it in properties
97 properties.put(key, bundle.getString(key));
101 // Handle it over to the mailer
102 this.mailer.init(properties);
105 this.getLoggerBeanLocal().logTrace("init: EXIT!"); //NOI18N
109 public void onMessage (final Message message) {
111 this.getLoggerBeanLocal().logTrace(MessageFormat.format("onMessage: message={0} - CALLED!", message)); //NOI18N
113 // The parameter should be valid
114 if (null == message) {
116 throw new NullPointerException("message is null"); //NOI18N
117 } else if (!(message instanceof ObjectMessage)) {
118 // Not implementing right interface
119 throw new IllegalArgumentException(MessageFormat.format("message={0} does not implemented ObjectMessage", message)); //NOI18N
123 ObjectMessage objectMessage = (ObjectMessage) message;
126 Serializable serializable;
129 // Get object from message
130 serializable = objectMessage.getObject();
131 } catch (final JMSException ex) {
132 // Log it and don't continue any further
133 this.getLoggerBeanLocal().logException(ex);
138 this.getLoggerBeanLocal().logDebug(MessageFormat.format("onMessage: serializable={0}", serializable)); //NOI18N
140 // Okay, is it the right interface?
141 if (null == serializable) {
143 throw new NullPointerException("serializable is null"); //NOI18N
144 } else if (!(serializable instanceof WrapableEmailDelivery)) {
145 // Not correct object send
146 throw new IllegalArgumentException(MessageFormat.format("serializable={0} does not implement WrapableEmailDelivery", serializable)); //NOI18N
150 WrapableEmailDelivery wrapper = (WrapableEmailDelivery) serializable;
152 // Is all required set?
153 if (wrapper.getLocale() == null) {
155 throw new NullPointerException("wrapper.locale is null"); //NOI18N
156 } else if (wrapper.getRecipient() == null) {
158 throw new NullPointerException("wrapper.recipient is null"); //NOI18N
159 } else if (wrapper.getSubjectLine() == null) {
161 throw new NullPointerException("wrapper.subjectLine is null"); //NOI18N
162 } else if (wrapper.getSubjectLine().isEmpty()) {
164 throw new IllegalArgumentException("wrapper.subjectLine is empty"); //NOI18N
165 } else if (wrapper.getTemplateName() == null) {
167 throw new NullPointerException("wrapper.templateName is null"); //NOI18N
168 } else if (wrapper.getTemplateName().isEmpty()) {
170 throw new IllegalArgumentException("wrapper.templateName is empty"); //NOI18N
175 this.mailer.sendDeliverableMail(wrapper);
176 } catch (final MessagingException ex) {
177 // Opps, something went wrong
178 this.getLoggerBeanLocal().logException(ex);
183 this.getLoggerBeanLocal().logTrace("onMessage - EXIT!"); //NOI18N