2 * Copyright (C) 2016 - 2022 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.apache.velocity.context.Context;
31 import org.mxchange.jmailee.model.delivery.BaseMailerBean;
32 import org.mxchange.jmailee.model.delivery.wrapper.WrapableEmailDelivery;
35 * A singleton mailer EJB
37 * @author Roland Häder<roland@mxchange.org>
39 @Singleton (name = "mailerBean", description = "A singleton mailer bean, usually called by jfinancialsEmailDelivery queue bean.")
40 public class JobsMailerSingletonBean extends BaseMailerBean implements DeliverableJobsEmailRemote {
45 private static final long serialVersionUID = 17_857_816_596_030_918L;
50 private final String configFile = "org.mxchange.jmailer.config"; //NOI18N
55 @Resource (name = "jjobsSmtpSession", description = "A Java Mail session")
56 private Session mailSession;
61 public JobsMailerSingletonBean () {
62 // Invoke super constructor
72 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.init: CALLED!", this.getClass().getSimpleName())); //NOI18N
75 final ResourceBundle bundle = ResourceBundle.getBundle(this.configFile);
78 this.getLoggerBeanLocal().logDebug(MessageFormat.format("{0}.init: bundle={1}", this.getClass().getSimpleName(), 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 final 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("{0}.init: key={1}", this.getClass().getSimpleName(), 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.setProperties(properties);
105 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.init: EXIT!", this.getClass().getSimpleName())); //NOI18N
109 public void sendDeliverableMail (final WrapableEmailDelivery emailWrapper) throws MessagingException {
111 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.sendDeliverableMail: emailWrapper={1} - CALLED!", this.getClass().getSimpleName(), emailWrapper)); //NOI18N
113 // The parameter must be valid
114 if (null == emailWrapper) {
116 throw new NullPointerException("emailWrapper is null"); //NOI18N
117 } else if (emailWrapper.getRecipientAddress() == null) {
119 throw new NullPointerException("emailWrapper.recipientAddress is null"); //NOI18N
120 } else if (emailWrapper.getSubjectLine() == null) {
122 throw new NullPointerException("emailWrapper.subjectLine is null"); //NOI18N
123 } else if (emailWrapper.getSubjectLine().isEmpty()) {
125 throw new IllegalArgumentException("emailWrapper.subjectLine is empty"); //NOI18N
126 } else if (emailWrapper.getTemplateName() == null) {
128 throw new NullPointerException("emailWrapper.templateName is null"); //NOI18N
129 } else if (emailWrapper.getTemplateName().isEmpty()) {
131 throw new IllegalArgumentException("emailWrapper.templateName is empty"); //NOI18N
132 } else if (emailWrapper.getLocale() == null) {
134 throw new NullPointerException("emailWrapper.locale is null"); //NOI18N
135 } else if (!emailWrapper.getTemplateVariables().containsKey("baseUrl")) { //NOI18N
137 throw new IllegalArgumentException("emailWrapper.templateVariables.baseUrl is not set"); //NOI18N
140 // All required data is set, load template
141 final Template template = this.getTemplateEngine().getTemplate(String.format("templates/%s/%s.vm", emailWrapper.getLocale().getLanguage().toLowerCase(), emailWrapper.getTemplateName())); //NOI18N
144 final Context context = new VelocityContext();
146 // Are some variables set?
147 if ((emailWrapper.getTemplateVariables() != null) && (!emailWrapper.getTemplateVariables().isEmpty())) {
149 for (Map.Entry<Object, Object> entry : emailWrapper.getTemplateVariables().entrySet()) {
151 String key = (String) entry.getKey();
152 String value = (String) entry.getValue();
154 // Both should not be empty
157 throw new NullPointerException("key is null"); //NOI18N
158 } else if (null == value) {
160 throw new NullPointerException(MessageFormat.format("value for key={0} is null", key)); //NOI18N
164 context.put(key, value);
169 this.deliverMailWithTemplate(template, context, emailWrapper, this.mailSession);
172 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.sendDeliverableMail: EXIT!", this.getClass().getSimpleName())); //NOI18N