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