]> git.mxchange.org Git - jfinancials-mailer-ejb.git/blob - src/java/org/mxchange/addressbook/mailer/model/delivery/AddressbookEmailDeliveryMessageBean.java
ea99d1656cacc862421037d8a07319b6649de071
[jfinancials-mailer-ejb.git] / src / java / org / mxchange / addressbook / mailer / model / delivery / AddressbookEmailDeliveryMessageBean.java
1 /*
2  * Copyright (C) 2016 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                 // Init mailer instance
65                 this.mailer = new AddressbookMailer();
66         }
67
68         /**
69          * Post-construction
70          */
71         @PostConstruct
72         public void init () {
73                 // Trace message
74                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.init: CALLED!", this.getClass().getSimpleName())); //NOI18N
75
76                 // Try to load bundle
77                 ResourceBundle bundle = ResourceBundle.getBundle(this.configFile);
78
79                 // Debug message
80                 this.getLoggerBeanLocal().logDebug(MessageFormat.format("{0}.init: bundle={1}", this.getClass().getSimpleName(), bundle)); //NOI18N
81
82                 // The bunble should be valid
83                 if (null == bundle) {
84                         // Throw NPE
85                         throw new NullPointerException(MessageFormat.format("bundle is null, maybe file {0} does not exist?", this.configFile)); //NOI18N
86                 }
87
88                 // Init Properties
89                 Properties properties = new Properties();
90
91                 // Is the bundle not empty?
92                 if (!bundle.keySet().isEmpty()) {
93                         // Loop through all
94                         for (final String key : bundle.keySet()) {
95                                 // Log debug message
96                                 this.getLoggerBeanLocal().logDebug(MessageFormat.format("{0}.init: key={1}", this.getClass().getSimpleName(), key)); //NOI18N
97
98                                 // Get string from bundle and set it in properties
99                                 properties.put(key, bundle.getString(key));
100                         }
101                 }
102
103                 // Handle it over to the mailer
104                 this.mailer.init(properties);
105
106                 // Trace message
107                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.init: EXIT!", this.getClass().getSimpleName())); //NOI18N
108         }
109
110         @Override
111         public void onMessage (final Message message) {
112                 // Trace message
113                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.onMessage: message={1} - CALLED!", this.getClass().getSimpleName(), message)); //NOI18N
114
115                 // The parameter should be valid
116                 if (null == message) {
117                         // Throw NPE
118                         throw new NullPointerException("message is null"); //NOI18N
119                 } else if (!(message instanceof ObjectMessage)) {
120                         // Not implementing right interface
121                         throw new IllegalArgumentException(MessageFormat.format("message={0} does not implemented ObjectMessage", message)); //NOI18N
122                 }
123
124                 // Securely cast it
125                 ObjectMessage objectMessage = (ObjectMessage) message;
126
127                 // Init variable
128                 Serializable serializable;
129
130                 try {
131                         // Get object from message
132                         serializable = objectMessage.getObject();
133                 } catch (final JMSException ex) {
134                         // Log it and don't continue any further
135                         this.getLoggerBeanLocal().logException(ex);
136                         return;
137                 }
138
139                 // Debug message
140                 this.getLoggerBeanLocal().logDebug(MessageFormat.format("{0}.onMessage: serializable={1}", this.getClass().getSimpleName(), serializable)); //NOI18N
141
142                 // Okay, is it the right interface?
143                 if (null == serializable) {
144                         // Throw NPE
145                         throw new NullPointerException("serializable is null"); //NOI18N
146                 } else if (!(serializable instanceof WrapableEmailDelivery)) {
147                         // Not correct object send
148                         throw new IllegalArgumentException(MessageFormat.format("serializable={0} does not implement WrapableEmailDelivery", serializable)); //NOI18N
149                 }
150
151                 // Securely cast it
152                 WrapableEmailDelivery wrapper = (WrapableEmailDelivery) serializable;
153
154                 // Is all required set?
155                 if (wrapper.getLocale() == null) {
156                         // Throw NPE
157                         throw new NullPointerException("wrapper.locale is null"); //NOI18N
158                 } else if (wrapper.getRecipient() == null) {
159                         // Throw again ...
160                         throw new NullPointerException("wrapper.recipient is null"); //NOI18N
161                 } else if (wrapper.getSubjectLine() == null) {
162                         // ... and again
163                         throw new NullPointerException("wrapper.subjectLine is null"); //NOI18N
164                 } else if (wrapper.getSubjectLine().isEmpty()) {
165                         // Is empty
166                         throw new IllegalArgumentException("wrapper.subjectLine is empty"); //NOI18N
167                 } else if (wrapper.getTemplateName() == null) {
168                         // Throw NPE again
169                         throw new NullPointerException("wrapper.templateName is null"); //NOI18N
170                 } else if (wrapper.getTemplateName().isEmpty()) {
171                         // Is empty
172                         throw new IllegalArgumentException("wrapper.templateName is empty"); //NOI18N
173                 }
174
175                 try {
176                         // Send email out
177                         this.mailer.sendDeliverableMail(wrapper);
178                 } catch (final MessagingException ex) {
179                         // Opps, something went wrong
180                         this.getLoggerBeanLocal().logException(ex);
181                         return;
182                 }
183
184                 // Trace message
185                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.onMessage - EXIT!", this.getClass().getSimpleName())); //NOI18N
186         }
187
188 }