]> git.mxchange.org Git - addressbook-mailer-ejb.git/blob - src/java/org/mxchange/addressbook/mailer/model/delivery/AddressbookEmailDeliveryMessageBean.java
4566c9cbf3b69b17279cf363424026843c26c9ea
[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 javax.ejb.ActivationConfigProperty;
22 import javax.ejb.EJB;
23 import javax.ejb.MessageDriven;
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 javax.naming.NamingException;
30 import org.mxchange.jcoreee.database.BaseDatabaseBean;
31 import org.mxchange.jmailee.model.delivery.wrapper.WrapableEmailDelivery;
32
33 /**
34  * A message-driven bean for sending out mails
35  * <p>
36  * @author Roland Häder<roland@mxchange.org>
37  */
38 @MessageDriven (
39                 name = "addressbookEmailDelivery",
40                 description = "A message bean for email delivery",
41                 activationConfig = {
42                         @ActivationConfigProperty (propertyName = "destinationLookup", propertyValue = "jms/addressbook-email-queue"),
43                         @ActivationConfigProperty (propertyName = "destinationType", propertyValue = "javax.jms.Queue")
44                 })
45 public class AddressbookEmailDeliveryMessageBean extends BaseDatabaseBean implements MessageListener {
46
47         /**
48          * Serial number
49          */
50         private static final long serialVersionUID = 75_638_176_619_024L;
51
52         /**
53          * Mailer instance
54          */
55         @EJB
56         private DeliverableAddressbookEmailRemote mailerBean;
57
58         /**
59          * Default constructor
60          * <p>
61          * @throws javax.naming.NamingException If a JNDI name could not be found
62          */
63         public AddressbookEmailDeliveryMessageBean () throws NamingException {
64                 // Call super constructor
65                 super();
66         }
67
68         @Override
69         public void onMessage (final Message message) {
70                 // Trace message
71                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.onMessage: message={1} - CALLED!", this.getClass().getSimpleName(), message)); //NOI18N
72
73                 // The parameter should be valid
74                 if (null == message) {
75                         // Throw NPE
76                         throw new NullPointerException("message is null"); //NOI18N
77                 } else if (!(message instanceof ObjectMessage)) {
78                         // Not implementing right interface
79                         throw new IllegalArgumentException(MessageFormat.format("message={0} does not implemented ObjectMessage", message)); //NOI18N
80                 }
81
82                 // Securely cast it
83                 ObjectMessage objectMessage = (ObjectMessage) message;
84
85                 // Init variable
86                 Serializable serializable;
87
88                 try {
89                         // Get object from message
90                         serializable = objectMessage.getObject();
91                 } catch (final JMSException ex) {
92                         // Log it and don't continue any further
93                         this.getLoggerBeanLocal().logException(ex);
94                         return;
95                 }
96
97                 // Debug message
98                 this.getLoggerBeanLocal().logDebug(MessageFormat.format("{0}.onMessage: serializable={1}", this.getClass().getSimpleName(), serializable)); //NOI18N
99
100                 // Okay, is it the right interface?
101                 if (null == serializable) {
102                         // Throw NPE
103                         throw new NullPointerException("serializable is null"); //NOI18N
104                 } else if (!(serializable instanceof WrapableEmailDelivery)) {
105                         // Not correct object send
106                         throw new IllegalArgumentException(MessageFormat.format("serializable={0} does not implement WrapableEmailDelivery", serializable)); //NOI18N
107                 }
108
109                 // Securely cast it
110                 WrapableEmailDelivery wrapper = (WrapableEmailDelivery) serializable;
111
112                 // Is all required set?
113                 if (wrapper.getLocale() == null) {
114                         // Throw NPE
115                         throw new NullPointerException("wrapper.locale is null"); //NOI18N
116                 } else if (wrapper.getRecipientAddress() == null) {
117                         // Throw again ...
118                         throw new NullPointerException("wrapper.recipientAddress is null"); //NOI18N
119                 } else if (wrapper.getSubjectLine() == null) {
120                         // ... and again
121                         throw new NullPointerException("wrapper.subjectLine is null"); //NOI18N
122                 } else if (wrapper.getSubjectLine().isEmpty()) {
123                         // Is empty
124                         throw new IllegalArgumentException("wrapper.subjectLine is empty"); //NOI18N
125                 } else if (wrapper.getTemplateName() == null) {
126                         // Throw NPE again
127                         throw new NullPointerException("wrapper.templateName is null"); //NOI18N
128                 } else if (wrapper.getTemplateName().isEmpty()) {
129                         // Is empty
130                         throw new IllegalArgumentException("wrapper.templateName is empty"); //NOI18N
131                 }
132
133                 try {
134                         // Send email out
135                         this.mailerBean.sendDeliverableMail(wrapper);
136                 } catch (final MessagingException ex) {
137                         // Opps, something went wrong
138                         this.getLoggerBeanLocal().logException(ex);
139                         return;
140                 }
141
142                 // Trace message
143                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.onMessage - EXIT!", this.getClass().getSimpleName())); //NOI18N
144         }
145
146 }