]> git.mxchange.org Git - jjobs-ejb.git/blobdiff - src/java/org/mxchange/jmailee/model/delivery/JobsEmailDeliveryMessageBean.java
Continued a bit:
[jjobs-ejb.git] / src / java / org / mxchange / jmailee / model / delivery / JobsEmailDeliveryMessageBean.java
index 1f68cc86148b03c1e399dbbd1e835c7d36483610..898d6c6e7943aa8356c257a3b12274078477f8ce 100644 (file)
 /*
- * Copyright (C) 2016 Roland Haeder
+ * Copyright (C) 2016, 2017 Roland Häder
  *
  * This program is free software: you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation, either version 3 of the License, or
- * (at your option) any later version.
+ * it under the terms of the GNU Affero General Public License as
+ * published by the Free Software Foundation, either version 3 of the
+ * License, or (at your option) any later version.
  *
  * This program is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- * GNU General Public License for more details.
+ * GNU Affero General Public License for more details.
  *
- * You should have received a copy of the GNU General Public License
+ * You should have received a copy of the GNU Affero General Public License
  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  */
 package org.mxchange.jmailee.model.delivery;
 
 import java.io.Serializable;
 import java.text.MessageFormat;
+import java.util.Properties;
+import java.util.ResourceBundle;
+import javax.annotation.PostConstruct;
 import javax.ejb.ActivationConfigProperty;
 import javax.ejb.MessageDriven;
 import javax.jms.JMSException;
 import javax.jms.Message;
 import javax.jms.MessageListener;
 import javax.jms.ObjectMessage;
-import javax.naming.Context;
-import javax.naming.InitialContext;
-import javax.naming.NamingException;
-import org.mxchange.jcoreeelogger.beans.local.logger.Log;
-import org.mxchange.jcoreeelogger.beans.local.logger.LoggerBeanLocal;
+import javax.mail.MessagingException;
+import org.mxchange.jjobs.database.BaseJobsDatabaseBean;
+import org.mxchange.jjobsmailer.model.delivery.DeliverableJobsEmail;
+import org.mxchange.jjobsmailer.model.delivery.JobsMailer;
+import org.mxchange.jmailee.model.delivery.wrapper.WrapableEmailDelivery;
 
 /**
  * A message queue for sending out emails
  * <p>
- * @author Roland Haeder<roland@mxchange.org>
+ * @author Roland Häder<roland@mxchange.org>
  */
-@MessageDriven (activationConfig = {
-       @ActivationConfigProperty (propertyName = "destinationLookup", propertyValue = "jms/jjobs-email-queue"),
-       @ActivationConfigProperty (propertyName = "destinationType", propertyValue = "javax.jms.Queue")
-})
-public class JobsEmailDeliveryMessageBean implements MessageListener {
+@MessageDriven (
+               name = "jjobsEmail",
+               description = "A message bean for email delivery",
+               activationConfig = {
+                       @ActivationConfigProperty (propertyName = "destinationLookup", propertyValue = "jms/jjobs-email-queue"),
+                       @ActivationConfigProperty (propertyName = "destinationType", propertyValue = "javax.jms.Queue")
+               })
+public class JobsEmailDeliveryMessageBean extends BaseJobsDatabaseBean implements MessageListener {
 
        /**
-        * Logger bean
+        * Serial number
         */
-       @Log
-       private LoggerBeanLocal loggerBeanLocal;
+       private static final long serialVersionUID = 75_638_176_619_024L;
+
+       /**
+        * Configuration file
+        */
+       private final String configFile = "org.mxchange.jmailer.config"; //NOI18N
+
+       /**
+        * Mailer instance
+        */
+       private final DeliverableJobsEmail mailer;
 
        /**
         * Default constructor
         */
        public JobsEmailDeliveryMessageBean () {
-               try {
-                       // Get initial context
-                       Context context = new InitialContext();
-
-                       // Lookup logger
-                       this.loggerBeanLocal = (LoggerBeanLocal) context.lookup("java:global/jcore-logger-ejb/logger!org.mxchange.jcoreeelogger.beans.local.logger.LoggerBeanLocal"); //NOI18N
-               } catch (final NamingException ex) {
-                       // Continue to throw
-                       throw new RuntimeException("context.lookup() failed.", ex); //NOI18N
+               // Init mailer instance
+               this.mailer = new JobsMailer();
+       }
+
+       /**
+        * Post-construction
+        */
+       @PostConstruct
+       public void init () {
+               // Trace message
+               this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.init: CALLED!", this.getClass().getSimpleName())); //NOI18N
+
+               // Try to load bundle
+               ResourceBundle bundle = ResourceBundle.getBundle(this.configFile);
+
+               // Debug message
+               this.getLoggerBeanLocal().logDebug(MessageFormat.format("{0}.init: bundle={1}", this.getClass().getSimpleName(), bundle)); //NOI18N
+
+               // The bunble should be valid
+               if (null == bundle) {
+                       // Throw NPE
+                       throw new NullPointerException(MessageFormat.format("bundle is null, maybe file {0} does not exist?", this.configFile)); //NOI18N
+               }
+
+               // Init Properties
+               Properties properties = new Properties();
+
+               // Is the bundle not empty?
+               if (!bundle.keySet().isEmpty()) {
+                       // Loop through all
+                       for (final String key : bundle.keySet()) {
+                               // Log debug message
+                               this.getLoggerBeanLocal().logDebug(MessageFormat.format("{0}.init: key={1}", this.getClass().getSimpleName(), key)); //NOI18N
+
+                               // Get string from bundle and set it in properties
+                               properties.put(key, bundle.getString(key));
+                       }
                }
+
+               // Handle it over to the mailer
+               this.mailer.init(properties);
+
+               // Trace message
+               this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.init: EXIT!", this.getClass().getSimpleName())); //NOI18N
        }
 
        @Override
        public void onMessage (final Message message) {
                // Trace message
-               this.loggerBeanLocal.logTrace(MessageFormat.format("onMessage: message={0} - CALLED!", message)); //NOI18N
+               this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.onMessage: message={1} - CALLED!", this.getClass().getSimpleName(), message)); //NOI18N
 
-               // Is the message castable to ObjectMessage?
+               // The parameter should be valid
                if (null == message) {
-                       // message is null
+                       // Throw NPE
                        throw new NullPointerException("message is null"); //NOI18N
                } else if (!(message instanceof ObjectMessage)) {
-                       // Not castable
-                       throw new ClassCastException(MessageFormat.format("message cannot be casted to ObjectMessage: {0}", message)); //NOI18N
+                       // Not implementing right interface
+                       throw new IllegalArgumentException(MessageFormat.format("message={0} does not implemented ObjectMessage", message)); //NOI18N
                }
 
                // Securely cast it
                ObjectMessage objectMessage = (ObjectMessage) message;
 
-               // Init instance
-               Serializable object;
+               // Init variable
+               Serializable serializable;
 
                try {
-                       // Get object from it
-                       object = objectMessage.getObject();
+                       // Get object from message
+                       serializable = objectMessage.getObject();
                } catch (final JMSException ex) {
-                       // Log exception ...
-                       this.loggerBeanLocal.logException(ex);
-
-                       // ... and don't continue
+                       // Log it and don't continue any further
+                       this.getLoggerBeanLocal().logException(ex);
                        return;
                }
 
                // Debug message
-               this.loggerBeanLocal.logDebug(MessageFormat.format("onMessage: object={0}", object)); //NOI18N
-
-               // Does this object implement WrapableCheckout ?
-               if (null == object) {
-                       // object cannot be null
-                       throw new NullPointerException("object is null"); //NOI18N
-               } else if (!(object instanceof WrapableEmailDelivery)) {
-                       // Not proper interface used
-                       throw new ClassCastException(MessageFormat.format("object does not implement WrapableEmailDelivery: {0}", object)); //NOI18N
+               this.getLoggerBeanLocal().logDebug(MessageFormat.format("{0}.onMessage: serializable={1}", this.getClass().getSimpleName(), serializable)); //NOI18N
+
+               // Okay, is it the right interface?
+               if (null == serializable) {
+                       // Throw NPE
+                       throw new NullPointerException("serializable is null"); //NOI18N
+               } else if (!(serializable instanceof WrapableEmailDelivery)) {
+                       // Not correct object send
+                       throw new IllegalArgumentException(MessageFormat.format("serializable={0} does not implement WrapableEmailDelivery", serializable)); //NOI18N
+               }
+
+               // Securely cast it
+               WrapableEmailDelivery wrapper = (WrapableEmailDelivery) serializable;
+
+               // Is all required set?
+               if (wrapper.getLocale() == null) {
+                       // Throw NPE
+                       throw new NullPointerException("wrapper.locale is null"); //NOI18N
+               } else if (wrapper.getRecipient() == null) {
+                       // Throw again ...
+                       throw new NullPointerException("wrapper.recipient is null"); //NOI18N
+               } else if (wrapper.getSubjectLine() == null) {
+                       // ... and again
+                       throw new NullPointerException("wrapper.subjectLine is null"); //NOI18N
+               } else if (wrapper.getSubjectLine().isEmpty()) {
+                       // Is empty
+                       throw new IllegalArgumentException("wrapper.subjectLine is empty"); //NOI18N
+               } else if (wrapper.getTemplateName() == null) {
+                       // Throw NPE again
+                       throw new NullPointerException("wrapper.templateName is null"); //NOI18N
+               } else if (wrapper.getTemplateName().isEmpty()) {
+                       // Is empty
+                       throw new IllegalArgumentException("wrapper.templateName is empty"); //NOI18N
+               } else if (wrapper.getTemplateVariables() == null) {
+                       // No template variables set, should not happen
+                       throw new NullPointerException("wrapper.templateVariables is null"); //NOI18N
                }
 
-               // Cast the object to the wrapper interface
-               WrapableEmailDelivery emailDelivery = (WrapableEmailDelivery) object;
+               try {
+                       // Send email out
+                       this.mailer.sendDeliverableMail(wrapper);
+               } catch (final MessagingException ex) {
+                       // Opps, something went wrong
+                       this.getLoggerBeanLocal().logException(ex);
+                       return;
+               }
 
                // Trace message
-               this.loggerBeanLocal.logTrace("onMessage: EXIT!"); //NOI18N
+               this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.onMessage - EXIT!", this.getClass().getSimpleName())); //NOI18N
        }
 
 }