]> git.mxchange.org Git - pizzaservice-mailer-ejb.git/blob - src/java/org/mxchange/addressbook/mailer/model/delivery/AddressbookEmailDeliveryMessageBean.java
Please cherry-pick:
[pizzaservice-mailer-ejb.git] / src / java / org / mxchange / addressbook / mailer / model / delivery / AddressbookEmailDeliveryMessageBean.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 Affero General Public License as
6  * published by the Free Software Foundation, either version 3 of the
7  * License, or (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 Affero General Public License for more details.
13  *
14  * You should have received a copy of the GNU Affero 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.io.Serializable;
20 import java.text.MessageFormat;
21 import java.util.Properties;
22 import java.util.ResourceBundle;
23 import javax.annotation.PostConstruct;
24 import javax.ejb.ActivationConfigProperty;
25 import javax.ejb.MessageDriven;
26 import javax.jms.JMSException;
27 import javax.jms.Message;
28 import javax.jms.MessageListener;
29 import javax.jms.ObjectMessage;
30 import javax.mail.MessagingException;
31 import javax.naming.NamingException;
32 import org.mxchange.addressbook.database.BaseAddressbookDatabaseBean;
33 import org.mxchange.jmailee.model.delivery.wrapper.WrapableEmailDelivery;
34
35 /**
36  * A message-driven bean for sending out emails
37  * <p>
38  * @author Roland Häder<roland@mxchange.org>
39  */
40 @MessageDriven (
41                 name = "addressbookEmailDelivery",
42                 description = "A message bean for email delivery",
43                 activationConfig = {
44                         @ActivationConfigProperty (propertyName = "destinationLookup", propertyValue = "jms/addressbook-email-queue"),
45                         @ActivationConfigProperty (propertyName = "destinationType", propertyValue = "javax.jms.Queue")
46                 })
47 public class AddressbookEmailDeliveryMessageBean extends BaseAddressbookDatabaseBean implements MessageListener {
48
49         /**
50          * Serial number
51          */
52         private static final long serialVersionUID = 75_638_176_619_024L;
53
54         /**
55          * Configuration file
56          */
57         private final String configFile = "org.mxchange.jmailer.config"; //NOI18N//NOI18N
58
59         /**
60          * Mailer instance
61          */
62         private final DeliverableAddressbookEmail mailer;
63
64         /**
65          * Default constructor
66          * <p>
67          * @throws javax.naming.NamingException If a JNDI name could not be found
68          */
69         public AddressbookEmailDeliveryMessageBean () throws NamingException {
70                 // Call super constructor
71                 super();
72
73                 // Init mailer instance
74                 this.mailer = new AddressbookMailer();
75         }
76
77         /**
78          * Post-construction
79          */
80         @PostConstruct
81         public void init () {
82                 // Trace message
83                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.init: CALLED!", this.getClass().getSimpleName())); //NOI18N
84
85                 // Try to load bundle
86                 ResourceBundle bundle = ResourceBundle.getBundle(this.configFile);
87
88                 // Debug message
89                 this.getLoggerBeanLocal().logDebug(MessageFormat.format("{0}.init: bundle={1}", this.getClass().getSimpleName(), bundle)); //NOI18N
90
91                 // The bunble should be valid
92                 if (null == bundle) {
93                         // Throw NPE
94                         throw new NullPointerException(MessageFormat.format("bundle is null, maybe file {0} does not exist?", this.configFile)); //NOI18N
95                 }
96
97                 // Init Properties
98                 Properties properties = new Properties();
99
100                 // Is the bundle not empty?
101                 if (!bundle.keySet().isEmpty()) {
102                         // Loop through all
103                         for (final String key : bundle.keySet()) {
104                                 // Log debug message
105                                 this.getLoggerBeanLocal().logDebug(MessageFormat.format("{0}.init: key={1}", this.getClass().getSimpleName(), key)); //NOI18N
106
107                                 // Get string from bundle and set it in properties
108                                 properties.put(key, bundle.getString(key));
109                         }
110                 }
111
112                 // Handle it over to the mailer
113                 this.mailer.init(properties);
114
115                 // Trace message
116                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.init: EXIT!", this.getClass().getSimpleName())); //NOI18N
117         }
118
119         @Override
120         public void onMessage (final Message message) {
121                 // Trace message
122                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.onMessage: message={1} - CALLED!", this.getClass().getSimpleName(), message)); //NOI18N
123
124                 // The parameter should be valid
125                 if (null == message) {
126                         // Throw NPE
127                         throw new NullPointerException("message is null"); //NOI18N
128                 } else if (!(message instanceof ObjectMessage)) {
129                         // Not implementing right interface
130                         throw new IllegalArgumentException(MessageFormat.format("message={0} does not implemented ObjectMessage", message)); //NOI18N
131                 }
132
133                 // Securely cast it
134                 ObjectMessage objectMessage = (ObjectMessage) message;
135
136                 // Init variable
137                 Serializable serializable;
138
139                 try {
140                         // Get object from message
141                         serializable = objectMessage.getObject();
142                 } catch (final JMSException ex) {
143                         // Log it and don't continue any further
144                         this.getLoggerBeanLocal().logException(ex);
145                         return;
146                 }
147
148                 // Debug message
149                 this.getLoggerBeanLocal().logDebug(MessageFormat.format("{0}.onMessage: serializable={1}", this.getClass().getSimpleName(), serializable)); //NOI18N
150
151                 // Okay, is it the right interface?
152                 if (null == serializable) {
153                         // Throw NPE
154                         throw new NullPointerException("serializable is null"); //NOI18N
155                 } else if (!(serializable instanceof WrapableEmailDelivery)) {
156                         // Not correct object send
157                         throw new IllegalArgumentException(MessageFormat.format("serializable={0} does not implement WrapableEmailDelivery", serializable)); //NOI18N
158                 }
159
160                 // Securely cast it
161                 WrapableEmailDelivery wrapper = (WrapableEmailDelivery) serializable;
162
163                 // Is all required set?
164                 if (wrapper.getLocale() == null) {
165                         // Throw NPE
166                         throw new NullPointerException("wrapper.locale is null"); //NOI18N
167                 } else if (wrapper.getRecipientAddress()== null) {
168                         // Throw again ...
169                         throw new NullPointerException("wrapper.recipientAddress is null"); //NOI18N
170                 } else if (wrapper.getSubjectLine() == null) {
171                         // ... and again
172                         throw new NullPointerException("wrapper.subjectLine is null"); //NOI18N
173                 } else if (wrapper.getSubjectLine().isEmpty()) {
174                         // Is empty
175                         throw new IllegalArgumentException("wrapper.subjectLine is empty"); //NOI18N
176                 } else if (wrapper.getTemplateName() == null) {
177                         // Throw NPE again
178                         throw new NullPointerException("wrapper.templateName is null"); //NOI18N
179                 } else if (wrapper.getTemplateName().isEmpty()) {
180                         // Is empty
181                         throw new IllegalArgumentException("wrapper.templateName is empty"); //NOI18N
182                 }
183
184                 try {
185                         // Send email out
186                         this.mailer.sendDeliverableMail(wrapper);
187                 } catch (final MessagingException ex) {
188                         // Opps, something went wrong
189                         this.getLoggerBeanLocal().logException(ex);
190                         return;
191                 }
192
193                 // Trace message
194                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.onMessage - EXIT!", this.getClass().getSimpleName())); //NOI18N
195         }
196
197 }