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