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