]> git.mxchange.org Git - addressbook-ejb.git/blob - src/java/org/mxchange/addressbook/mailer/model/delivery/EmailDeliveryMessageBean.java
1fead75b47f28c9f3cbd5e75a557753b04880020
[addressbook-ejb.git] / src / java / org / mxchange / addressbook / mailer / model / delivery / EmailDeliveryMessageBean.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.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 org.mxchange.addressbook.database.BaseAddressbookDatabaseBean;
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 EmailDeliveryMessageBean extends BaseAddressbookDatabaseBean implements MessageListener {
42
43         /**
44          * Serial number
45          */
46         private static final long serialVersionUID = 75_638_176_619_024L;
47
48         /**
49          * Mailer instance
50          */
51         private final DeliverableAddressbookEmail mailer;
52
53         /**
54          * Configuration file
55          */
56         private final String configFile = "org.mxchange.jmailer.config"; //NOI18N
57
58         /**
59          * Default constructor
60          */
61         public EmailDeliveryMessageBean () {
62                 // Init mailer instance
63                 this.mailer = new AddressbookMailer();
64         }
65
66         @Override
67         public void onMessage (final Message message) {
68                 // Trace message
69                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("onMessage: message={0} - CALLED!", message)); //NOI18N
70
71                 // The parameter should be valid
72                 if (null == message) {
73                         // Throw NPE
74                         throw new NullPointerException("message is null"); //NOI18N
75                 } else if (!(message instanceof ObjectMessage)) {
76                         // Not implementing right interface
77                         throw new IllegalArgumentException(MessageFormat.format("message={0} does not implemented ObjectMessage", message)); //NOI18N
78                 }
79
80                 // Securely cast it
81                 ObjectMessage objectMessage = (ObjectMessage) message;
82
83                 // Init variable
84                 Serializable serializable;
85
86                 try {
87                         // Get object from message
88                         serializable = objectMessage.getObject();
89                 } catch (final JMSException ex) {
90                         // Log it and don't continue any further
91                         this.getLoggerBeanLocal().logException(ex);
92                         return;
93                 }
94
95                 // Debug message
96                 this.getLoggerBeanLocal().logDebug(MessageFormat.format("onMessage: serializable={0}", serializable)); //NOI18N
97
98                 // Trace message
99                 this.getLoggerBeanLocal().logTrace("onMessage - EXIT!"); //NOI18N
100         }
101
102         /**
103          * Post-construction
104          */
105         @PostConstruct
106         public void init () {
107                 // Trace message
108                 this.getLoggerBeanLocal().logTrace("init: CALLED!"); //NOI18N
109
110                 // Try to load bundle
111                 ResourceBundle bundle = ResourceBundle.getBundle(this.configFile);
112
113                 // Debug message
114                 this.getLoggerBeanLocal().logDebug(MessageFormat.format("init: bundle={0}", bundle)); //NOI18N
115
116                 // The bunble should be valid
117                 if (null == bundle) {
118                         // Throw NPE
119                         throw new NullPointerException(MessageFormat.format("bundle is null, maybe file {0} does not exist?", this.configFile)); //NOI18N
120                 }
121
122                 // Init Properties
123                 Properties properties = new Properties();
124
125                 // Is the bundle not empty?
126                 if (!bundle.keySet().isEmpty()) {
127                         // Loop through all
128                         for (final String key : bundle.keySet()) {
129                                 // Log debug message
130                                 this.getLoggerBeanLocal().logDebug(MessageFormat.format("init: key={0}", key)); //NOI18N
131
132                                 // Get string from bundle and set it in properties
133                                 properties.put(key, bundle.getString(key));
134                         }
135                 }
136
137                 // Handle it over to the mailer
138                 this.mailer.init(properties);
139
140                 // Trace message
141                 this.getLoggerBeanLocal().logTrace("init: EXIT!"); //NOI18N
142         }
143 }