From 4db28100b35e25eb7f4f37f47f86dae7c5da866e Mon Sep 17 00:00:00 2001 From: =?utf8?q?Roland=20H=C3=A4der?= Date: Sun, 23 Jul 2017 10:36:02 +0200 Subject: [PATCH] Continued: - let BaseDatabaseBean initialize the logger for us - added sender email address which is taken from property "mailer.from" - new dependency 'jcoree' added MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Roland Häder --- nbproject/project.properties | 3 + .../model/delivery/BaseMailerBean.java | 63 +++++++------------ .../wrapper/WrapableEmailDelivery.java | 4 +- 3 files changed, 29 insertions(+), 41 deletions(-) diff --git a/nbproject/project.properties b/nbproject/project.properties index 93ff525..d1bf3a8 100644 --- a/nbproject/project.properties +++ b/nbproject/project.properties @@ -31,6 +31,7 @@ endorsed.classpath= excludes= file.reference.jcontacts-core.jar=lib/jcontacts-core.jar file.reference.jcore-logger-lib.jar=lib/jcore-logger-lib.jar +file.reference.jcoreee.jar=lib/jcoreee.jar file.reference.juser-core.jar=lib/juser-core.jar includes=** jar.archive.disabled=${jnlp.enabled} @@ -40,6 +41,7 @@ javac.classpath=\ ${file.reference.juser-core.jar}:\ ${file.reference.jcontacts-core.jar}:\ ${file.reference.jcore-logger-lib.jar}:\ + ${file.reference.jcoreee.jar}:\ ${libs.Apache_Velocity_1.7.classpath}:\ ${libs.javaee-api-7.0.classpath} # Space-separated list of extra javac options @@ -100,6 +102,7 @@ run.test.classpath=\ source.encoding=UTF-8 source.reference.jcontacts-core.jar=../jcontacts-core/src/ source.reference.jcore-logger-lib.jar=../jcore-logger-lib/src/ +source.reference.jcoreee.jar=../jcoreee/src/ source.reference.juser-core.jar=../juser-core/src/ src.dir=src test.src.dir=test diff --git a/src/org/mxchange/jmailee/model/delivery/BaseMailerBean.java b/src/org/mxchange/jmailee/model/delivery/BaseMailerBean.java index b77b97b..5fb6303 100644 --- a/src/org/mxchange/jmailee/model/delivery/BaseMailerBean.java +++ b/src/org/mxchange/jmailee/model/delivery/BaseMailerBean.java @@ -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.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.database.BaseDatabaseBean; import org.mxchange.jmailee.model.delivery.wrapper.WrapableEmailDelivery; /** @@ -42,19 +40,13 @@ import org.mxchange.jmailee.model.delivery.wrapper.WrapableEmailDelivery; *

* @author Roland Häder */ -public abstract class BaseMailerBean implements DeliverableEmailLocal { +public abstract class BaseMailerBean extends BaseDatabaseBean implements DeliverableEmailLocal { /** * Serial number */ private static final long serialVersionUID = 14_598_912_753_106L; - /** - * Logger bean - */ - @Log - private LoggerBeanLocal loggerBeanLocal; - /** * Properties for this mailer *

@@ -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 } /** @@ -194,7 +185,7 @@ public abstract class BaseMailerBean implements DeliverableEmailLocal { */ 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 + 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 - *

- * @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 } diff --git a/src/org/mxchange/jmailee/model/delivery/wrapper/WrapableEmailDelivery.java b/src/org/mxchange/jmailee/model/delivery/wrapper/WrapableEmailDelivery.java index ef1e6c0..7f501f1 100644 --- a/src/org/mxchange/jmailee/model/delivery/wrapper/WrapableEmailDelivery.java +++ b/src/org/mxchange/jmailee/model/delivery/wrapper/WrapableEmailDelivery.java @@ -52,9 +52,9 @@ public interface WrapableEmailDelivery extends Serializable { /** * Setter for recipient email address *

- * @param recipient Email address recipient + * @param recipientAddress Email address recipient */ - void setRecipientAddress (final Address recipient); + void setRecipientAddress (final Address recipientAddress); /** * Getter for optional properties -- 2.39.5