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