]> git.mxchange.org Git - pizzaservice-ejb.git/blob - src/java/org/mxchange/pizzaapplication/mailer/model/delivery/PizzaEmailDeliveryMessageBean.java
Continued a bit:
[pizzaservice-ejb.git] / src / java / org / mxchange / pizzaapplication / mailer / model / delivery / PizzaEmailDeliveryMessageBean.java
1 /*
2  * Copyright (C) 2016 Cho-Time GmbH
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 de.chotime.landingpage.database.BaseLandingDatabaseBean;
20 import de.chotime.landingpage.mailer.model.delivery.DeliverableLandingEmail;
21 import de.chotime.landingpage.mailer.model.delivery.LandingMailer;
22 import java.io.Serializable;
23 import java.text.MessageFormat;
24 import java.util.Properties;
25 import java.util.ResourceBundle;
26 import javax.annotation.PostConstruct;
27 import javax.ejb.ActivationConfigProperty;
28 import javax.ejb.MessageDriven;
29 import javax.jms.JMSException;
30 import javax.jms.Message;
31 import javax.jms.MessageListener;
32 import javax.jms.ObjectMessage;
33
34 /**
35  * A message-driven bean for sending out emails
36  * <p>
37  * @author Roland Haeder<rhaeder@cho-time.de>
38  */
39 @MessageDriven (activationConfig = {
40         @ActivationConfigProperty (propertyName = "destinationLookup", propertyValue = "jms/jlandingpage-email-queue"),
41         @ActivationConfigProperty (propertyName = "destinationType", propertyValue = "javax.jms.Queue")
42 })
43 public class PizzaEmailDeliveryMessageBean extends BaseLandingDatabaseBean implements MessageListener {
44
45         /**
46          * Serial number
47          */
48         private static final long serialVersionUID = 75_638_176_619_024L;
49
50         /**
51          * Mailer instance
52          */
53         private final DeliverableLandingEmail mailer;
54
55         /**
56          * Configuration file
57          */
58         private final String configFile = "org.mxchange.jmailer.config"; //NOI18N
59
60         /**
61          * Default constructor
62          */
63         public PizzaEmailDeliveryMessageBean () {
64                 // Init mailer instance
65                 this.mailer = new LandingMailer();
66         }
67
68         @Override
69         public void onMessage (final Message message) {
70                 // Trace message
71                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("onMessage: message={0} - CALLED!", message)); //NOI18N
72                 // The parameter should be valid
73                 if (null == message) {
74                         // Throw NPE
75                         throw new NullPointerException("message is null"); //NOI18N
76                 } else if (!(message instanceof ObjectMessage)) {
77                         // Not implementing right interface
78                         throw new IllegalArgumentException(MessageFormat.format("message={0} does not implemented ObjectMessage", message)); //NOI18N
79                 }
80
81                 // Securely cast it
82                 ObjectMessage objectMessage = (ObjectMessage) message;
83
84                 // Init variable
85                 Serializable serializable;
86
87                 try {
88                         // Get object from message
89                         serializable = objectMessage.getObject();
90                 } catch (final JMSException ex) {
91                         // Log it and don't continue any further
92                         this.getLoggerBeanLocal().logException(ex);
93                         return;
94                 }
95
96                 // Debug message
97                 this.getLoggerBeanLocal().logDebug(MessageFormat.format("onMessage: serializable={0}", serializable)); //NOI18N
98
99                 // Trace message
100                 this.getLoggerBeanLocal().logTrace("onMessage - EXIT!"); //NOI18N
101         }
102
103         /**
104          * Post-construction
105          */
106         @PostConstruct
107         public void init () {
108                 // Trace message
109                 this.getLoggerBeanLocal().logTrace("init: CALLED!"); //NOI18N
110
111                 // Try to load bundle
112                 ResourceBundle bundle = ResourceBundle.getBundle(this.configFile);
113
114                 // Debug message
115                 this.getLoggerBeanLocal().logDebug(MessageFormat.format("init: bundle={0}", bundle)); //NOI18N
116
117                 // The bunble should be valid
118                 if (null == bundle) {
119                         // Throw NPE
120                         throw new NullPointerException(MessageFormat.format("bundle is null, maybe file {0} does not exist?", this.configFile)); //NOI18N
121                 }
122
123                 // Init Properties
124                 Properties properties = new Properties();
125
126                 // Is the bundle not empty?
127                 if (!bundle.keySet().isEmpty()) {
128                         // Loop through all
129                         for (final String key : bundle.keySet()) {
130                                 // Log debug message
131                                 this.getLoggerBeanLocal().logDebug(MessageFormat.format("init: key={0}", key)); //NOI18N
132
133                                 // Get string from bundle and set it in properties
134                                 properties.put(key, bundle.getString(key));
135                         }
136                 }
137
138                 // Handle it over to the mailer
139                 this.mailer.init(properties);
140
141                 // Trace message
142                 this.getLoggerBeanLocal().logTrace("init: EXIT!"); //NOI18N
143         }
144 }