]> git.mxchange.org Git - jjobs-mailer-ejb.git/blob - src/java/org/mxchange/jjobs/mailer/model/delivery/JobsMailerSingletonBean.java
f819f07c9f4093156b256669a04e2d69607e8c83
[jjobs-mailer-ejb.git] / src / java / org / mxchange / jjobs / mailer / model / delivery / JobsMailerSingletonBean.java
1 /*
2  * Copyright (C) 2016 - 2018 Free Software Foundation
3  *
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.
8  *
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.
13  *
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/>.
16  */
17 package org.mxchange.jjobs.mailer.model.delivery;
18
19 import java.text.MessageFormat;
20 import java.util.Map;
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;
32
33 /**
34  * A singleton mailer EJB
35  * <p>
36  * @author Roland Häder<roland@mxchange.org>
37  */
38 @Singleton (name = "mailerBean", description = "A singleton mailer bean, usually called by jfinancialsEmailDelivery queue bean.")
39 public class JobsMailerSingletonBean extends BaseMailerBean implements DeliverableJobsEmailRemote {
40
41         /**
42          * Serial number
43          */
44         private static final long serialVersionUID = 17_857_816_596_030_918L;
45
46         /**
47          * Configuration file
48          */
49         private final String configFile = "org.mxchange.jmailer.config"; //NOI18N//NOI18N
50
51         /**
52          * Email session
53          */
54         @Resource (name = "jjobsSmtpSession", description = "A Java Mail session")
55         private Session mailSession;
56
57         /**
58          * Default constructor
59          */
60         public JobsMailerSingletonBean () {
61                 // Invoke super constructor
62                 super();
63         }
64
65         /**
66          * Post-construction
67          */
68         @PostConstruct
69         public void init () {
70                 // Trace message
71                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.init: CALLED!", this.getClass().getSimpleName())); //NOI18N
72
73                 // Try to load bundle
74                 final ResourceBundle bundle = ResourceBundle.getBundle(this.configFile);
75
76                 // Debug message
77                 this.getLoggerBeanLocal().logDebug(MessageFormat.format("{0}.init: bundle={1}", this.getClass().getSimpleName(), bundle)); //NOI18N
78
79                 // The bunble should be valid
80                 if (null == bundle) {
81                         // Throw NPE
82                         throw new NullPointerException(MessageFormat.format("bundle is null, maybe file {0} does not exist?", this.configFile)); //NOI18N
83                 }
84
85                 // Init Properties
86                 final Properties properties = new Properties();
87
88                 // Is the bundle not empty?
89                 if (!bundle.keySet().isEmpty()) {
90                         // Loop through all
91                         for (final String key : bundle.keySet()) {
92                                 // Log debug message
93                                 this.getLoggerBeanLocal().logDebug(MessageFormat.format("{0}.init: key={1}", this.getClass().getSimpleName(), key)); //NOI18N
94
95                                 // Get string from bundle and set it in properties
96                                 properties.put(key, bundle.getString(key));
97                         }
98                 }
99
100                 // Handle it over to the mailer
101                 this.setProperties(properties);
102
103                 // Trace message
104                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.init: EXIT!", this.getClass().getSimpleName())); //NOI18N
105         }
106
107         @Override
108         public void sendDeliverableMail (final WrapableEmailDelivery emailWrapper) throws MessagingException {
109                 // Log trace message
110                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.sendDeliverableMail: emailWrapper={1} - CALLED!", this.getClass().getSimpleName(), emailWrapper)); //NOI18N
111
112                 // The parameter must be valid
113                 if (null == emailWrapper) {
114                         // Throw NPE
115                         throw new NullPointerException("emailWrapper is null"); //NOI18N
116                 } else if (emailWrapper.getRecipientAddress() == null) {
117                         // Throw NPE again
118                         throw new NullPointerException("emailWrapper.recipientAddress is null"); //NOI18N
119                 } else if (emailWrapper.getSubjectLine() == null) {
120                         // ... and again
121                         throw new NullPointerException("emailWrapper.subjectLine is null"); //NOI18N
122                 } else if (emailWrapper.getSubjectLine().isEmpty()) {
123                         // Is empty
124                         throw new IllegalArgumentException("emailWrapper.subjectLine is empty"); //NOI18N
125                 } else if (emailWrapper.getTemplateName() == null) {
126                         // ... and again
127                         throw new NullPointerException("emailWrapper.templateName is null"); //NOI18N
128                 } else if (emailWrapper.getTemplateName().isEmpty()) {
129                         // Is empty
130                         throw new IllegalArgumentException("emailWrapper.templateName is empty"); //NOI18N
131                 } else if (emailWrapper.getLocale() == null) {
132                         // Throw NPE again
133                         throw new NullPointerException("emailWrapper.locale is null"); //NOI18N
134                 } else if (!emailWrapper.getTemplateVariables().containsKey("baseUrl")) { //NOI18N
135                         // Not set
136                         throw new IllegalArgumentException("emailWrapper.templateVariables.baseUrl is not set"); //NOI18N
137                 }
138
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
141
142                 // Init context
143                 VelocityContext context = new VelocityContext();
144
145                 // Are some variables set?
146                 if ((emailWrapper.getTemplateVariables() != null) && (!emailWrapper.getTemplateVariables().isEmpty())) {
147                         // Add all variables
148                         for (Map.Entry<Object, Object> entry : emailWrapper.getTemplateVariables().entrySet()) {
149                                 // Get key/value
150                                 String key = (String) entry.getKey();
151                                 String value = (String) entry.getValue();
152
153                                 // Both should not be empty
154                                 if (null == key) {
155                                         // Throw NPE
156                                         throw new NullPointerException("key is null"); //NOI18N
157                                 } else if (null == value) {
158                                         // Throw NPE again
159                                         throw new NullPointerException(MessageFormat.format("value for key={0} is null", key)); //NOI18N
160                                 }
161
162                                 // Set it
163                                 context.put(key, value);
164                         }
165                 }
166
167                 // Send the email
168                 this.deliverMailWithTemplate(template, context, emailWrapper, this.mailSession);
169
170                 // Trace message
171                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.sendDeliverableMail: EXIT!", this.getClass().getSimpleName())); //NOI18N
172         }
173
174 }