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 javax.naming.NamingException;
32 import org.mxchange.addressbook.database.BaseAddressbookDatabaseBean;
33 import org.mxchange.jmailee.model.delivery.wrapper.WrapableEmailDelivery;
36 * A message-driven bean for sending out emails
38 * @author Roland Häder<roland@mxchange.org>
41 name = "addressbookEmailDelivery",
42 description = "A message bean for email delivery",
44 @ActivationConfigProperty (propertyName = "destinationLookup", propertyValue = "jms/addressbook-email-queue"),
45 @ActivationConfigProperty (propertyName = "destinationType", propertyValue = "javax.jms.Queue")
47 public class AddressbookEmailDeliveryMessageBean extends BaseAddressbookDatabaseBean implements MessageListener {
52 private static final long serialVersionUID = 75_638_176_619_024L;
57 private final String configFile = "org.mxchange.jmailer.config"; //NOI18N//NOI18N
62 private final DeliverableAddressbookEmail mailer;
67 * @throws javax.naming.NamingException If a JNDI name could not be found
69 public AddressbookEmailDeliveryMessageBean () throws NamingException {
70 // Call super constructor
73 // Init mailer instance
74 this.mailer = new AddressbookMailer();
83 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.init: CALLED!", this.getClass().getSimpleName())); //NOI18N
86 ResourceBundle bundle = ResourceBundle.getBundle(this.configFile);
89 this.getLoggerBeanLocal().logDebug(MessageFormat.format("{0}.init: bundle={1}", this.getClass().getSimpleName(), bundle)); //NOI18N
91 // The bunble should be valid
94 throw new NullPointerException(MessageFormat.format("bundle is null, maybe file {0} does not exist?", this.configFile)); //NOI18N
98 Properties properties = new Properties();
100 // Is the bundle not empty?
101 if (!bundle.keySet().isEmpty()) {
103 for (final String key : bundle.keySet()) {
105 this.getLoggerBeanLocal().logDebug(MessageFormat.format("{0}.init: key={1}", this.getClass().getSimpleName(), key)); //NOI18N
107 // Get string from bundle and set it in properties
108 properties.put(key, bundle.getString(key));
112 // Handle it over to the mailer
113 this.mailer.init(properties);
116 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.init: EXIT!", this.getClass().getSimpleName())); //NOI18N
120 public void onMessage (final Message message) {
122 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.onMessage: message={1} - CALLED!", this.getClass().getSimpleName(), message)); //NOI18N
124 // The parameter should be valid
125 if (null == message) {
127 throw new NullPointerException("message is null"); //NOI18N
128 } else if (!(message instanceof ObjectMessage)) {
129 // Not implementing right interface
130 throw new IllegalArgumentException(MessageFormat.format("message={0} does not implemented ObjectMessage", message)); //NOI18N
134 ObjectMessage objectMessage = (ObjectMessage) message;
137 Serializable serializable;
140 // Get object from message
141 serializable = objectMessage.getObject();
142 } catch (final JMSException ex) {
143 // Log it and don't continue any further
144 this.getLoggerBeanLocal().logException(ex);
149 this.getLoggerBeanLocal().logDebug(MessageFormat.format("{0}.onMessage: serializable={1}", this.getClass().getSimpleName(), serializable)); //NOI18N
151 // Okay, is it the right interface?
152 if (null == serializable) {
154 throw new NullPointerException("serializable is null"); //NOI18N
155 } else if (!(serializable instanceof WrapableEmailDelivery)) {
156 // Not correct object send
157 throw new IllegalArgumentException(MessageFormat.format("serializable={0} does not implement WrapableEmailDelivery", serializable)); //NOI18N
161 WrapableEmailDelivery wrapper = (WrapableEmailDelivery) serializable;
163 // Is all required set?
164 if (wrapper.getLocale() == null) {
166 throw new NullPointerException("wrapper.locale is null"); //NOI18N
167 } else if (wrapper.getRecipientAddress()== null) {
169 throw new NullPointerException("wrapper.recipientAddress is null"); //NOI18N
170 } else if (wrapper.getSubjectLine() == null) {
172 throw new NullPointerException("wrapper.subjectLine is null"); //NOI18N
173 } else if (wrapper.getSubjectLine().isEmpty()) {
175 throw new IllegalArgumentException("wrapper.subjectLine is empty"); //NOI18N
176 } else if (wrapper.getTemplateName() == null) {
178 throw new NullPointerException("wrapper.templateName is null"); //NOI18N
179 } else if (wrapper.getTemplateName().isEmpty()) {
181 throw new IllegalArgumentException("wrapper.templateName is empty"); //NOI18N
186 this.mailer.sendDeliverableMail(wrapper);
187 } catch (final MessagingException ex) {
188 // Opps, something went wrong
189 this.getLoggerBeanLocal().logException(ex);
194 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.onMessage - EXIT!", this.getClass().getSimpleName())); //NOI18N