]> git.mxchange.org Git - addressbook-mailer-ejb.git/blob - src/java/org/mxchange/addressbook/mailer/model/delivery/AddressbookMailerSingletonBean.java
Please cherry-pick:
[addressbook-mailer-ejb.git] / src / java / org / mxchange / addressbook / mailer / model / delivery / AddressbookMailerSingletonBean.java
1 /*
2  * Copyright (C) 2016, 2017 Roland Häder
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.addressbook.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
39 public class AddressbookMailerSingletonBean extends BaseMailerBean implements DeliverableAddressbookEmailLocal {
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 = "addressbookSmtpSession", description = "A Java Mail session")
55         private Session mailSession;
56
57         /**
58          * Default constructor
59          * <p>
60          */
61         public AddressbookMailerSingletonBean () {
62         }
63
64         /**
65          * Post-construction
66          */
67         @PostConstruct
68         public void init () {
69                 // Trace message
70                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.init: CALLED!", this.getClass().getSimpleName())); //NOI18N
71
72                 // Try to load bundle
73                 ResourceBundle bundle = ResourceBundle.getBundle(this.configFile);
74
75                 // Debug message
76                 this.getLoggerBeanLocal().logDebug(MessageFormat.format("{0}.init: bundle={1}", this.getClass().getSimpleName(), bundle)); //NOI18N
77
78                 // The bunble should be valid
79                 if (null == bundle) {
80                         // Throw NPE
81                         throw new NullPointerException(MessageFormat.format("bundle is null, maybe file {0} does not exist?", this.configFile)); //NOI18N
82                 }
83
84                 // Init Properties
85                 Properties properties = new Properties();
86
87                 // Is the bundle not empty?
88                 if (!bundle.keySet().isEmpty()) {
89                         // Loop through all
90                         for (final String key : bundle.keySet()) {
91                                 // Log debug message
92                                 this.getLoggerBeanLocal().logDebug(MessageFormat.format("{0}.init: key={1}", this.getClass().getSimpleName(), key)); //NOI18N
93
94                                 // Get string from bundle and set it in properties
95                                 properties.put(key, bundle.getString(key));
96                         }
97                 }
98
99                 // Handle it over to the mailer
100                 this.setProperties(properties);
101
102                 // Trace message
103                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.init: EXIT!", this.getClass().getSimpleName())); //NOI18N
104         }
105
106         @Override
107         public void sendDeliverableMail (final WrapableEmailDelivery emailWrapper) throws MessagingException {
108                 // Log trace message
109                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.sendDeliverableMail: emailWrapper={1} - CALLED!", this.getClass().getSimpleName(), emailWrapper)); //NOI18N
110
111                 // The parameter must be valid
112                 if (null == emailWrapper) {
113                         // Throw NPE
114                         throw new NullPointerException("emailWrapper is null"); //NOI18N
115                 } else if (emailWrapper.getRecipientAddress() == null) {
116                         // Throw NPE again
117                         throw new NullPointerException("emailWrapper.recipientAddress is null"); //NOI18N
118                 } else if (emailWrapper.getSubjectLine() == null) {
119                         // ... and again
120                         throw new NullPointerException("emailWrapper.subjectLine is null"); //NOI18N
121                 } else if (emailWrapper.getSubjectLine().isEmpty()) {
122                         // Is empty
123                         throw new IllegalArgumentException("emailWrapper.subjectLine is empty"); //NOI18N
124                 } else if (emailWrapper.getTemplateName() == null) {
125                         // ... and again
126                         throw new NullPointerException("emailWrapper.templateName is null"); //NOI18N
127                 } else if (emailWrapper.getTemplateName().isEmpty()) {
128                         // Is empty
129                         throw new IllegalArgumentException("emailWrapper.templateName is empty"); //NOI18N
130                 } else if (emailWrapper.getLocale() == null) {
131                         // Throw NPE again
132                         throw new NullPointerException("emailWrapper.locale is null"); //NOI18N
133                 } else if (!emailWrapper.getTemplateVariables().containsKey("baseUrl")) { //NOI18N
134                         // Not set
135                         throw new IllegalArgumentException("emailWrapper.templateVariables.baseUrl is not set"); //NOI18N
136                 }
137
138                 // All required data is set, load template
139                 Template template = this.getTemplateEngine().getTemplate(String.format("templates/%s/%s.vm", emailWrapper.getLocale().getLanguage().toLowerCase(), emailWrapper.getTemplateName())); //NOI18N
140
141                 // Init context
142                 VelocityContext context = new VelocityContext();
143
144                 // Are some variables set?
145                 if ((emailWrapper.getTemplateVariables() != null) && (!emailWrapper.getTemplateVariables().isEmpty())) {
146                         // Add all variables
147                         for (Map.Entry<Object, Object> entry : emailWrapper.getTemplateVariables().entrySet()) {
148                                 // Get key/value
149                                 String key = (String) entry.getKey();
150                                 String value = (String) entry.getValue();
151
152                                 // Both should not be empty
153                                 if (null == key) {
154                                         // Throw NPE
155                                         throw new NullPointerException("key is null"); //NOI18N
156                                 } else if (null == value) {
157                                         // Throw NPE again
158                                         throw new NullPointerException(MessageFormat.format("value for key={0} is null", key)); //NOI18N
159                                 }
160
161                                 // Set it
162                                 context.put(key, value);
163                         }
164                 }
165
166                 // Send the email
167                 this.deliverMailWithTemplate(template, context, emailWrapper, this.mailSession);
168
169                 // Trace message
170                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.sendDeliverableMail: EXIT!", this.getClass().getSimpleName())); //NOI18N
171         }
172
173 }