2 * Copyright (C) 2016 - 2018 Free Software Foundation
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (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 General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 package org.mxchange.jjobs.mailer.model.delivery;
19 import java.text.MessageFormat;
21 import java.util.Properties;
22 import java.util.ResourceBundle;
23 import javax.annotation.PostConstruct;
24 import javax.annotation.Resource;
25 import javax.ejb.Singleton;
26 import javax.mail.MessagingException;
27 import javax.mail.Session;
28 import org.apache.velocity.Template;
29 import org.apache.velocity.VelocityContext;
30 import org.mxchange.jmailee.model.delivery.BaseMailerBean;
31 import org.mxchange.jmailee.model.delivery.wrapper.WrapableEmailDelivery;
34 * A singleton mailer EJB
36 * @author Roland Häder<roland@mxchange.org>
38 @Singleton (name = "mailerBean", description = "A singleton mailer bean, usually called by jfinancialsEmailDelivery queue bean.")
39 public class JobsMailerSingletonBean extends BaseMailerBean implements DeliverableJobsEmailRemote {
44 private static final long serialVersionUID = 17_857_816_596_030_918L;
49 private final String configFile = "org.mxchange.jmailer.config"; //NOI18N//NOI18N
54 @Resource (name = "jjobsSmtpSession", description = "A Java Mail session")
55 private Session mailSession;
60 public JobsMailerSingletonBean () {
61 // Invoke super constructor
71 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.init: CALLED!", this.getClass().getSimpleName())); //NOI18N
74 ResourceBundle bundle = ResourceBundle.getBundle(this.configFile);
77 this.getLoggerBeanLocal().logDebug(MessageFormat.format("{0}.init: bundle={1}", this.getClass().getSimpleName(), bundle)); //NOI18N
79 // The bunble should be valid
82 throw new NullPointerException(MessageFormat.format("bundle is null, maybe file {0} does not exist?", this.configFile)); //NOI18N
86 Properties properties = new Properties();
88 // Is the bundle not empty?
89 if (!bundle.keySet().isEmpty()) {
91 for (final String key : bundle.keySet()) {
93 this.getLoggerBeanLocal().logDebug(MessageFormat.format("{0}.init: key={1}", this.getClass().getSimpleName(), key)); //NOI18N
95 // Get string from bundle and set it in properties
96 properties.put(key, bundle.getString(key));
100 // Handle it over to the mailer
101 this.setProperties(properties);
104 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.init: EXIT!", this.getClass().getSimpleName())); //NOI18N
108 public void sendDeliverableMail (final WrapableEmailDelivery emailWrapper) throws MessagingException {
110 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.sendDeliverableMail: emailWrapper={1} - CALLED!", this.getClass().getSimpleName(), emailWrapper)); //NOI18N
112 // The parameter must be valid
113 if (null == emailWrapper) {
115 throw new NullPointerException("emailWrapper is null"); //NOI18N
116 } else if (emailWrapper.getRecipientAddress() == null) {
118 throw new NullPointerException("emailWrapper.recipientAddress is null"); //NOI18N
119 } else if (emailWrapper.getSubjectLine() == null) {
121 throw new NullPointerException("emailWrapper.subjectLine is null"); //NOI18N
122 } else if (emailWrapper.getSubjectLine().isEmpty()) {
124 throw new IllegalArgumentException("emailWrapper.subjectLine is empty"); //NOI18N
125 } else if (emailWrapper.getTemplateName() == null) {
127 throw new NullPointerException("emailWrapper.templateName is null"); //NOI18N
128 } else if (emailWrapper.getTemplateName().isEmpty()) {
130 throw new IllegalArgumentException("emailWrapper.templateName is empty"); //NOI18N
131 } else if (emailWrapper.getLocale() == null) {
133 throw new NullPointerException("emailWrapper.locale is null"); //NOI18N
134 } else if (!emailWrapper.getTemplateVariables().containsKey("baseUrl")) { //NOI18N
136 throw new IllegalArgumentException("emailWrapper.templateVariables.baseUrl is not set"); //NOI18N
139 // All required data is set, load template
140 Template template = this.getTemplateEngine().getTemplate(String.format("templates/%s/%s.vm", emailWrapper.getLocale().getLanguage().toLowerCase(), emailWrapper.getTemplateName())); //NOI18N
143 VelocityContext context = new VelocityContext();
145 // Are some variables set?
146 if ((emailWrapper.getTemplateVariables() != null) && (!emailWrapper.getTemplateVariables().isEmpty())) {
148 for (Map.Entry<Object, Object> entry : emailWrapper.getTemplateVariables().entrySet()) {
150 String key = (String) entry.getKey();
151 String value = (String) entry.getValue();
153 // Both should not be empty
156 throw new NullPointerException("key is null"); //NOI18N
157 } else if (null == value) {
159 throw new NullPointerException(MessageFormat.format("value for key={0} is null", key)); //NOI18N
163 context.put(key, value);
168 this.deliverMailWithTemplate(template, context, emailWrapper, this.mailSession);
171 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.sendDeliverableMail: EXIT!", this.getClass().getSimpleName())); //NOI18N