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