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