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.jmailee.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.jjobs.database.BaseJobsDatabaseBean;
32 import org.mxchange.jjobsmailer.model.delivery.DeliverableJobsEmail;
33 import org.mxchange.jjobsmailer.model.delivery.JobsMailer;
34 import org.mxchange.jmailee.model.delivery.wrapper.WrapableEmailDelivery;
37 * A message queue for sending out emails
39 * @author Roland Haeder<roland@mxchange.org>
43 description = "A message bean for email delivery",
45 @ActivationConfigProperty (propertyName = "destinationLookup", propertyValue = "jms/jjobs-email-queue"),
46 @ActivationConfigProperty (propertyName = "destinationType", propertyValue = "javax.jms.Queue")
48 public class JobsEmailDeliveryMessageBean extends BaseJobsDatabaseBean implements MessageListener {
53 private static final long serialVersionUID = 75_638_176_619_024L;
58 private final String configFile = "org.mxchange.jmailer.config"; //NOI18N
63 private final DeliverableJobsEmail mailer;
68 public JobsEmailDeliveryMessageBean () {
69 // Init mailer instance
70 this.mailer = new JobsMailer();
79 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.init: CALLED!", this.getClass().getSimpleName())); //NOI18N
82 ResourceBundle bundle = ResourceBundle.getBundle(this.configFile);
85 this.getLoggerBeanLocal().logDebug(MessageFormat.format("{0}.init: bundle={1}", this.getClass().getSimpleName(), bundle)); //NOI18N
87 // The bunble should be valid
90 throw new NullPointerException(MessageFormat.format("bundle is null, maybe file {0} does not exist?", this.configFile)); //NOI18N
94 Properties properties = new Properties();
96 // Is the bundle not empty?
97 if (!bundle.keySet().isEmpty()) {
99 for (final String key : bundle.keySet()) {
101 this.getLoggerBeanLocal().logDebug(MessageFormat.format("{0}.init: key={1}", this.getClass().getSimpleName(), key)); //NOI18N
103 // Get string from bundle and set it in properties
104 properties.put(key, bundle.getString(key));
108 // Handle it over to the mailer
109 this.mailer.init(properties);
112 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.init: EXIT!", this.getClass().getSimpleName())); //NOI18N
116 public void onMessage (final Message message) {
118 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.onMessage: message={1} - CALLED!", this.getClass().getSimpleName(), message)); //NOI18N
120 // The parameter should be valid
121 if (null == message) {
123 throw new NullPointerException("message is null"); //NOI18N
124 } else if (!(message instanceof ObjectMessage)) {
125 // Not implementing right interface
126 throw new IllegalArgumentException(MessageFormat.format("message={0} does not implemented ObjectMessage", message)); //NOI18N
130 ObjectMessage objectMessage = (ObjectMessage) message;
133 Serializable serializable;
136 // Get object from message
137 serializable = objectMessage.getObject();
138 } catch (final JMSException ex) {
139 // Log it and don't continue any further
140 this.getLoggerBeanLocal().logException(ex);
145 this.getLoggerBeanLocal().logDebug(MessageFormat.format("{0}.onMessage: serializable={1}", this.getClass().getSimpleName(), serializable)); //NOI18N
147 // Okay, is it the right interface?
148 if (null == serializable) {
150 throw new NullPointerException("serializable is null"); //NOI18N
151 } else if (!(serializable instanceof WrapableEmailDelivery)) {
152 // Not correct object send
153 throw new IllegalArgumentException(MessageFormat.format("serializable={0} does not implement WrapableEmailDelivery", serializable)); //NOI18N
157 WrapableEmailDelivery wrapper = (WrapableEmailDelivery) serializable;
159 // Is all required set?
160 if (wrapper.getLocale() == null) {
162 throw new NullPointerException("wrapper.locale is null"); //NOI18N
163 } else if (wrapper.getRecipient() == null) {
165 throw new NullPointerException("wrapper.recipient is null"); //NOI18N
166 } else if (wrapper.getSubjectLine() == null) {
168 throw new NullPointerException("wrapper.subjectLine is null"); //NOI18N
169 } else if (wrapper.getSubjectLine().isEmpty()) {
171 throw new IllegalArgumentException("wrapper.subjectLine is empty"); //NOI18N
172 } else if (wrapper.getTemplateName() == null) {
174 throw new NullPointerException("wrapper.templateName is null"); //NOI18N
175 } else if (wrapper.getTemplateName().isEmpty()) {
177 throw new IllegalArgumentException("wrapper.templateName is empty"); //NOI18N
182 this.mailer.sendDeliverableMail(wrapper);
183 } catch (final MessagingException ex) {
184 // Opps, something went wrong
185 this.getLoggerBeanLocal().logException(ex);
190 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.onMessage - EXIT!", this.getClass().getSimpleName())); //NOI18N