2 * Copyright (C) 2016, 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 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.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.addressbook.database.BaseAddressbookDatabaseBean;
32 import org.mxchange.jmailee.model.delivery.wrapper.WrapableEmailDelivery;
35 * A message-driven bean for sending out emails
37 * @author Roland Häder<roland@mxchange.org>
40 name = "addressbookEmailDelivery",
41 description = "A message bean for email delivery",
43 @ActivationConfigProperty (propertyName = "destinationLookup", propertyValue = "jms/addressbook-email-queue"),
44 @ActivationConfigProperty (propertyName = "destinationType", propertyValue = "javax.jms.Queue")
46 public class AddressbookEmailDeliveryMessageBean extends BaseAddressbookDatabaseBean implements MessageListener {
51 private static final long serialVersionUID = 75_638_176_619_024L;
56 private final String configFile = "org.mxchange.jmailer.config"; //NOI18N//NOI18N
61 private final DeliverableAddressbookEmail mailer;
66 public AddressbookEmailDeliveryMessageBean () {
67 // Call super constructor
70 // Init mailer instance
71 this.mailer = new AddressbookMailer();
80 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.init: CALLED!", this.getClass().getSimpleName())); //NOI18N
83 ResourceBundle bundle = ResourceBundle.getBundle(this.configFile);
86 this.getLoggerBeanLocal().logDebug(MessageFormat.format("{0}.init: bundle={1}", this.getClass().getSimpleName(), bundle)); //NOI18N
88 // The bunble should be valid
91 throw new NullPointerException(MessageFormat.format("bundle is null, maybe file {0} does not exist?", this.configFile)); //NOI18N
95 Properties properties = new Properties();
97 // Is the bundle not empty?
98 if (!bundle.keySet().isEmpty()) {
100 for (final String key : bundle.keySet()) {
102 this.getLoggerBeanLocal().logDebug(MessageFormat.format("{0}.init: key={1}", this.getClass().getSimpleName(), key)); //NOI18N
104 // Get string from bundle and set it in properties
105 properties.put(key, bundle.getString(key));
109 // Handle it over to the mailer
110 this.mailer.init(properties);
113 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.init: EXIT!", this.getClass().getSimpleName())); //NOI18N
117 public void onMessage (final Message message) {
119 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.onMessage: message={1} - CALLED!", this.getClass().getSimpleName(), message)); //NOI18N
121 // The parameter should be valid
122 if (null == message) {
124 throw new NullPointerException("message is null"); //NOI18N
125 } else if (!(message instanceof ObjectMessage)) {
126 // Not implementing right interface
127 throw new IllegalArgumentException(MessageFormat.format("message={0} does not implemented ObjectMessage", message)); //NOI18N
131 ObjectMessage objectMessage = (ObjectMessage) message;
134 Serializable serializable;
137 // Get object from message
138 serializable = objectMessage.getObject();
139 } catch (final JMSException ex) {
140 // Log it and don't continue any further
141 this.getLoggerBeanLocal().logException(ex);
146 this.getLoggerBeanLocal().logDebug(MessageFormat.format("{0}.onMessage: serializable={1}", this.getClass().getSimpleName(), serializable)); //NOI18N
148 // Okay, is it the right interface?
149 if (null == serializable) {
151 throw new NullPointerException("serializable is null"); //NOI18N
152 } else if (!(serializable instanceof WrapableEmailDelivery)) {
153 // Not correct object send
154 throw new IllegalArgumentException(MessageFormat.format("serializable={0} does not implement WrapableEmailDelivery", serializable)); //NOI18N
158 WrapableEmailDelivery wrapper = (WrapableEmailDelivery) serializable;
160 // Is all required set?
161 if (wrapper.getLocale() == null) {
163 throw new NullPointerException("wrapper.locale is null"); //NOI18N
164 } else if (wrapper.getRecipient() == null) {
166 throw new NullPointerException("wrapper.recipient is null"); //NOI18N
167 } else if (wrapper.getSubjectLine() == null) {
169 throw new NullPointerException("wrapper.subjectLine is null"); //NOI18N
170 } else if (wrapper.getSubjectLine().isEmpty()) {
172 throw new IllegalArgumentException("wrapper.subjectLine is empty"); //NOI18N
173 } else if (wrapper.getTemplateName() == null) {
175 throw new NullPointerException("wrapper.templateName is null"); //NOI18N
176 } else if (wrapper.getTemplateName().isEmpty()) {
178 throw new IllegalArgumentException("wrapper.templateName is empty"); //NOI18N
183 this.mailer.sendDeliverableMail(wrapper);
184 } catch (final MessagingException ex) {
185 // Opps, something went wrong
186 this.getLoggerBeanLocal().logException(ex);
191 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.onMessage - EXIT!", this.getClass().getSimpleName())); //NOI18N