]> git.mxchange.org Git - jmailer-ee.git/blobdiff - src/org/mxchange/jmailee/model/delivery/BaseMailerBean.java
Updated copyright year
[jmailer-ee.git] / src / org / mxchange / jmailee / model / delivery / BaseMailerBean.java
index b77b97b4a03ae6470a148190e5a490629ad41c66..744658d028f94b910fee2a61b38bfc2a3992db3f 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2016, 2017 Roland Häder
+ * Copyright (C) 2016 - 2020 Free Software Foundation
  *
  * 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
@@ -20,21 +20,19 @@ import java.io.StringWriter;
 import java.text.MessageFormat;
 import java.util.Date;
 import java.util.Properties;
+import javax.mail.Address;
+import javax.mail.Message;
 import javax.mail.MessagingException;
 import javax.mail.Session;
 import javax.mail.Transport;
 import javax.mail.internet.InternetAddress;
 import javax.mail.internet.MimeMessage;
-import javax.naming.Context;
-import javax.naming.InitialContext;
-import javax.naming.NamingException;
 import org.apache.velocity.Template;
-import org.apache.velocity.VelocityContext;
 import org.apache.velocity.app.VelocityEngine;
+import org.apache.velocity.context.Context;
 import org.apache.velocity.runtime.RuntimeConstants;
 import org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader;
-import org.mxchange.jcoreeelogger.beans.local.logger.Log;
-import org.mxchange.jcoreeelogger.beans.local.logger.LoggerBeanLocal;
+import org.mxchange.jcoreee.bean.ejb.BaseEnterpriseBean;
 import org.mxchange.jmailee.model.delivery.wrapper.WrapableEmailDelivery;
 
 /**
@@ -42,19 +40,13 @@ import org.mxchange.jmailee.model.delivery.wrapper.WrapableEmailDelivery;
  * <p>
  * @author Roland Häder<roland@mxchange.org>
  */
-public abstract class BaseMailerBean implements DeliverableEmailLocal {
+public abstract class BaseMailerBean extends BaseEnterpriseBean implements DeliverableEmailRemote {
 
        /**
         * Serial number
         */
        private static final long serialVersionUID = 14_598_912_753_106L;
 
-       /**
-        * Logger bean
-        */
-       @Log
-       private LoggerBeanLocal loggerBeanLocal;
-
        /**
         * Properties for this mailer
         * <p>
@@ -74,16 +66,8 @@ public abstract class BaseMailerBean implements DeliverableEmailLocal {
         * Default constructor
         */
        protected BaseMailerBean () {
-               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(MessageFormat.format("context.lookup() failed: {0}", ex.getMessage()), ex); //NOI18N
-               }
+               // Call super constructor
+               super();
 
                // Init template engine
                this.templateEngine = new VelocityEngine();
@@ -104,12 +88,15 @@ public abstract class BaseMailerBean implements DeliverableEmailLocal {
         */
        private void deliverMail (final WrapableEmailDelivery emailWrapper, final StringWriter writer, final Session mailSession) throws MessagingException {
                // Trace message
-               this.getLoggerBeanLocal().logTrace(MessageFormat.format("sendMail: emailWrapper={0},body={1},mailSession={2} - CALLED!", emailWrapper, writer, mailSession)); //NOI18N
+               this.getLoggerBeanLocal().logTrace(MessageFormat.format("deliverMail: emailWrapper={0},body={1},mailSession={2} - CALLED!", emailWrapper, writer, mailSession)); //NOI18N
 
                // Are the additional properties and all parameter set?
                if (null == this.properties) {
                        // Nothing set
                        throw new NullPointerException("this.properties is null"); //NOI18N
+               } else if (!this.properties.containsKey("mailer.from")) { //NOI18N
+                       // Throw exception
+                       throw new IllegalStateException("this.properties.mailer.from is not set"); //NOI18N
                } else if (null == emailWrapper) {
                        // Throw NPE
                        throw new NullPointerException("emailWrapper is null"); //NOI18N
@@ -118,7 +105,7 @@ public abstract class BaseMailerBean implements DeliverableEmailLocal {
                        throw new NullPointerException("emailWrapper.locale is null"); //NOI18N
                } else if (emailWrapper.getRecipientAddress() == null) {
                        // Throw it again
-                       throw new NullPointerException("emailWrapper.recipient is null"); //NOI18N
+                       throw new NullPointerException("emailWrapper.recipientAddress is null"); //NOI18N
                } else if (emailWrapper.getTemplateName() == null) {
                        // Throw it again
                        throw new NullPointerException("emailWrapper.templateName is null"); //NOI18N
@@ -145,12 +132,16 @@ public abstract class BaseMailerBean implements DeliverableEmailLocal {
                        throw new NullPointerException("mailSession is null"); //NOI18N
                }
 
+               // Parse from address
+               Address senderAddress = new InternetAddress(this.properties.getProperty("mailer.from"));
+
                // Get MIME message instance
                MimeMessage message = new MimeMessage(mailSession);
 
                // Set subject, recipients and body
+               message.setFrom(senderAddress);
                message.setSubject(emailWrapper.getSubjectLine());
-               message.setRecipients(javax.mail.Message.RecipientType.TO, InternetAddress.parse(emailWrapper.getRecipientAddress().toString(), true));
+               message.setRecipient(Message.RecipientType.TO, emailWrapper.getRecipientAddress());
                message.setSentDate(new Date());
                message.setText(writer.toString());
                message.setHeader("MIME-Version", "1.0"); //NOI18N
@@ -179,7 +170,7 @@ public abstract class BaseMailerBean implements DeliverableEmailLocal {
                Transport.send(message);
 
                // Trace message
-               this.getLoggerBeanLocal().logTrace("init: EXIT!"); //NOI18N
+               this.getLoggerBeanLocal().logTrace("deliverMail: EXIT!"); //NOI18N
        }
 
        /**
@@ -192,9 +183,9 @@ public abstract class BaseMailerBean implements DeliverableEmailLocal {
         * <p>
         * @throws MessagingException If something happened on message delivery
         */
-       protected void deliverMailWithTemplate (final Template template, final VelocityContext context, final WrapableEmailDelivery emailWrapper, final Session mailSession) throws MessagingException {
+       protected void deliverMailWithTemplate (final Template template, final Context context, final WrapableEmailDelivery emailWrapper, final Session mailSession) throws MessagingException {
                // Log trace message
-               this.getLoggerBeanLocal().logTrace(MessageFormat.format("sendMailTemplate: template={0},emailWrapper={1},mailSession={2} - CALLED!", template, emailWrapper, mailSession)); //NOI18N
+               this.getLoggerBeanLocal().logTrace(MessageFormat.format("deliverMailWithTemplate: template={0},emailWrapper={1},mailSession={2} - CALLED!", template, emailWrapper, mailSession)); //NOI18N
 
                // The parameters must be valid
                if (null == template) {
@@ -208,7 +199,7 @@ public abstract class BaseMailerBean implements DeliverableEmailLocal {
                        throw new NullPointerException("emailWrapper.locale is null"); //NOI18N
                } else if (emailWrapper.getRecipientAddress() == null) {
                        // Throw it again
-                       throw new NullPointerException("emailWrapper.recipient is null"); //NOI18N
+                       throw new NullPointerException("emailWrapper.recipientAddress is null"); //NOI18N
                } else if (emailWrapper.getTemplateName() == null) {
                        // Throw it again
                        throw new NullPointerException("emailWrapper.templateName is null"); //NOI18N
@@ -239,16 +230,7 @@ public abstract class BaseMailerBean implements DeliverableEmailLocal {
                this.deliverMail(emailWrapper, writer, mailSession);
 
                // Trace message
-               this.getLoggerBeanLocal().logTrace("init: EXIT!"); //NOI18N
-       }
-
-       /**
-        * Getter for logger bean
-        * <p>
-        * @return Local logger bean
-        */
-       protected LoggerBeanLocal getLoggerBeanLocal () {
-               return this.loggerBeanLocal;
+               this.getLoggerBeanLocal().logTrace("deliverMailWithTemplate: EXIT!"); //NOI18N
        }
 
        /**
@@ -265,11 +247,14 @@ public abstract class BaseMailerBean implements DeliverableEmailLocal {
                if (null == properties) {
                        // Is null
                        throw new NullPointerException("properties is null"); //NOI18N
+               } else if (!properties.containsKey("mailer.from")) { //NOI18N
+                       // From not set
+                       throw new IllegalArgumentException("properties.mailer.from is not set"); //NOI18N
                } else if (!properties.containsKey("mailer.errorsto")) { //NOI18N
                        // Errors-To not set
                        throw new IllegalArgumentException("properties.mailer.errorsto is not set"); //NOI18N
                } else if (!properties.containsKey("mailer.bouncesto")) { //NOI18N
-                       // Errors-To not set
+                       // Bounces-To not set
                        throw new IllegalArgumentException("properties.mailer.bouncesto is not set"); //NOI18N
                }