From 33644439acf79034b3e070f6a015513c2a14daea Mon Sep 17 00:00:00 2001 From: =?utf8?q?Roland=20H=C3=A4der?= Date: Sat, 8 Jul 2017 18:56:32 +0200 Subject: [PATCH] Rewrite: - having a mailer class is nice but only as a singleton (EJB) bean MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Roland Häder --- .../{BaseMailer.java => BaseMailerBean.java} | 110 ++++++++++-------- ...eEmail.java => DeliverableEmailLocal.java} | 23 ++-- 2 files changed, 68 insertions(+), 65 deletions(-) rename src/org/mxchange/jmailee/model/delivery/{BaseMailer.java => BaseMailerBean.java} (88%) rename src/org/mxchange/jmailee/model/delivery/{DeliverableEmail.java => DeliverableEmailLocal.java} (64%) diff --git a/src/org/mxchange/jmailee/model/delivery/BaseMailer.java b/src/org/mxchange/jmailee/model/delivery/BaseMailerBean.java similarity index 88% rename from src/org/mxchange/jmailee/model/delivery/BaseMailer.java rename to src/org/mxchange/jmailee/model/delivery/BaseMailerBean.java index 88571f3..b77b97b 100644 --- a/src/org/mxchange/jmailee/model/delivery/BaseMailer.java +++ b/src/org/mxchange/jmailee/model/delivery/BaseMailerBean.java @@ -38,11 +38,11 @@ import org.mxchange.jcoreeelogger.beans.local.logger.LoggerBeanLocal; import org.mxchange.jmailee.model.delivery.wrapper.WrapableEmailDelivery; /** - * An email class for sending out mails from templates + * A general email bean for sending out mails from templates *

* @author Roland Häder */ -public abstract class BaseMailer implements DeliverableEmail { +public abstract class BaseMailerBean implements DeliverableEmailLocal { /** * Serial number @@ -73,7 +73,7 @@ public abstract class BaseMailer implements DeliverableEmail { /** * Default constructor */ - protected BaseMailer () { + protected BaseMailerBean () { try { // Get initial context Context context = new InitialContext(); @@ -92,47 +92,17 @@ public abstract class BaseMailer implements DeliverableEmail { this.templateEngine.init(); } - @Override - public VelocityEngine getTemplateEngine () { - return this.templateEngine; - } - - @Override - @SuppressWarnings ("AssignmentToCollectionOrArrayFieldFromParameter") - public void init (final Properties properties) { - // Trace message - this.getLoggerBeanLocal().logTrace(MessageFormat.format("init: properties={0} - CALLED!", properties)); //NOI18N - - // Are all required properties set? - if (null == properties) { - // Is null - throw new NullPointerException("properties is null"); //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 - throw new IllegalArgumentException("properties.mailer.bouncesto is not set"); //NOI18N - } - - // Set it here - this.properties = properties; - - // Trace message - this.getLoggerBeanLocal().logTrace("init: EXIT!"); //NOI18N - } - /** * Sends an email to given email address with subject line. *

* @param emailAddress Email address for recipient - * @param subjectLine Subject line - * @param writer Body part - * @param mailSession Corresponding mail session to use + * @param subjectLine Subject line + * @param writer Body part + * @param mailSession Corresponding mail session to use *

* @throws MessagingException If something happened on message delivery */ - private void sendMail (final WrapableEmailDelivery emailWrapper, final StringWriter writer, final Session mailSession) throws MessagingException { + 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 @@ -212,26 +182,17 @@ public abstract class BaseMailer implements DeliverableEmail { this.getLoggerBeanLocal().logTrace("init: EXIT!"); //NOI18N } - /** - * Getter for logger bean - *

- * @return Local logger bean - */ - protected LoggerBeanLocal getLoggerBeanLocal () { - return this.loggerBeanLocal; - } - /** * Sends given mail template to all addresses found in email wrapper *

- * @param template Template to send - * @param context Velocity context + * @param template Template to send + * @param context Velocity context * @param emailWrapper Email wrapper containing recipient and such - * @param mailSession Mail session + * @param mailSession Mail session *

* @throws MessagingException If something happened on message delivery */ - protected void sendMailTemplate (final Template template, final VelocityContext context, final WrapableEmailDelivery emailWrapper, final Session mailSession) throws MessagingException { + protected void deliverMailWithTemplate (final Template template, final VelocityContext 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 @@ -275,10 +236,57 @@ public abstract class BaseMailer implements DeliverableEmail { template.merge(context, writer); // Get all out and send it - this.sendMail(emailWrapper, writer, mailSession); + this.deliverMail(emailWrapper, writer, mailSession); // Trace message this.getLoggerBeanLocal().logTrace("init: EXIT!"); //NOI18N } + /** + * Getter for logger bean + *

+ * @return Local logger bean + */ + protected LoggerBeanLocal getLoggerBeanLocal () { + return this.loggerBeanLocal; + } + + /** + * Setter for initial properties + *

+ * @param properties Initial properties + */ + @SuppressWarnings ("AssignmentToCollectionOrArrayFieldFromParameter") + protected void setProperties (final Properties properties) { + // Trace message + this.getLoggerBeanLocal().logTrace(MessageFormat.format("setProperties: properties={0} - CALLED!", properties)); //NOI18N + + // Are all required properties set? + if (null == properties) { + // Is null + throw new NullPointerException("properties is null"); //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 + throw new IllegalArgumentException("properties.mailer.bouncesto is not set"); //NOI18N + } + + // Set it here + this.properties = properties; + + // Trace message + this.getLoggerBeanLocal().logTrace("setProperties: EXIT!"); //NOI18N + } + + /** + * Getter for template engine instance + *

+ * @return Template engine instance + */ + protected VelocityEngine getTemplateEngine () { + return this.templateEngine; + } + } diff --git a/src/org/mxchange/jmailee/model/delivery/DeliverableEmail.java b/src/org/mxchange/jmailee/model/delivery/DeliverableEmailLocal.java similarity index 64% rename from src/org/mxchange/jmailee/model/delivery/DeliverableEmail.java rename to src/org/mxchange/jmailee/model/delivery/DeliverableEmailLocal.java index d845c1a..42b5148 100644 --- a/src/org/mxchange/jmailee/model/delivery/DeliverableEmail.java +++ b/src/org/mxchange/jmailee/model/delivery/DeliverableEmailLocal.java @@ -17,9 +17,9 @@ package org.mxchange.jmailee.model.delivery; import java.io.Serializable; -import java.util.Properties; import javax.ejb.Local; -import org.apache.velocity.app.VelocityEngine; +import javax.mail.MessagingException; +import org.mxchange.jmailee.model.delivery.wrapper.WrapableEmailDelivery; /** * An interface for email delivery classes @@ -27,21 +27,16 @@ import org.apache.velocity.app.VelocityEngine; * @author Roland Häder */ @Local -public interface DeliverableEmail extends Serializable { +public interface DeliverableEmailLocal extends Serializable { /** - * Getter for template engine + * Sends given deliverable mail instance. This must have set a template + * name, a recipient address and a subject line set. *

- * @return Template engine + * @param emailWrapper Deliverable mail wrapper + * + * @throws MessagingException If something bad happened */ - VelocityEngine getTemplateEngine (); - - /** - * Initializes the mailer with given properties. Please see BaseMailer for - * all supported and required properties. - *

- * @param properties Properties instance - */ - void init (final Properties properties); + void sendDeliverableMail (final WrapableEmailDelivery emailWrapper) throws MessagingException; } -- 2.39.5