Skip to content

Commit

Permalink
Use Jakarta EE
Browse files Browse the repository at this point in the history
  • Loading branch information
twogee committed Sep 30, 2021
1 parent f77817b commit 7b26711
Show file tree
Hide file tree
Showing 6 changed files with 408 additions and 26 deletions.
10 changes: 10 additions & 0 deletions build.xml
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,12 @@
</or>
</selector>

<selector id="needs.jakartamail">
<or>
<filename name="${ant.package}/taskdefs/email/JakartaMimeMailer*"/>
</or>
</selector>

<selector id="needs.netrexx">
<filename name="${optional.package}/NetRexxC*"/>
</selector>
Expand Down Expand Up @@ -507,6 +513,9 @@
<available property="javamail.present"
classname="javax.mail.Transport"
classpathref="classpath" ignoresystemclasses="${ignoresystemclasses}"/>
<available property="jakartamail.present"
classname="jakarta.mail.Transport"
classpathref="classpath" ignoresystemclasses="${ignoresystemclasses}"/>
<available property="graaljs.present"
classname="com.oracle.truffle.js.scriptengine.GraalJSScriptEngine"
classpathref="classpath" ignoresystemclasses="${ignoresystemclasses}"/>
Expand Down Expand Up @@ -704,6 +713,7 @@
<selector refid="needs.commons-logging" unless="commons.logging.present"/>
<selector refid="needs.apache-bsf" unless="bsf.present"/>
<selector refid="needs.javamail" unless="javamail.present"/>
<selector refid="needs.jakartamail" unless="jakartamail.present"/>
<selector refid="needs.netrexx" unless="netrexx.present"/>
<selector refid="needs.commons-net" unless="commons.net.present"/>
<selector refid="needs.antlr" unless="antlr.present"/>
Expand Down
6 changes: 6 additions & 0 deletions fetch.xml
Original file line number Diff line number Diff line change
Expand Up @@ -335,6 +335,12 @@ Set -Ddest=LOCATION on the command line
<f2 project="which"/>
</target>

<target name="javamail"
description="load Java Mail"
depends="init">
<f2 project="com.sun.mail" archive="javax.mail"/>
</target>

<target name="jakartamail"
description="load Jakarta Mail"
depends="init">
Expand Down
4 changes: 3 additions & 1 deletion lib/libraries.properties
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,9 @@ hamcrest-core.version=1.3
hamcrest-library.version=${hamcrest-core.version}
jai-core.version=1.1.3
jai-codec.version=1.1.3
jakarta.mail.version=1.6.4
# Later 1.6 versions call themselves "jakarta.mail" but do not use the namespace yet
javax.mail.version=1.6.2
jakarta.mail.version=2.0.1
jakarta-regexp.version=1.4
# Later versions of Tomcat provide a jspc task
jasper-compiler.version=4.1.36
Expand Down
65 changes: 42 additions & 23 deletions src/main/org/apache/tools/ant/taskdefs/email/EmailTask.java
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ public void addMessage(Message message) throws BuildException {
}

/**
* Add a from address element.
* Add a "from" address element.
*
* @param address The address to send from.
*/
Expand All @@ -251,7 +251,7 @@ public void addFrom(EmailAddress address) {
}

/**
* Shorthand to set the from address element.
* Shorthand to set the "from" address element.
*
* @param address The address to send mail from.
*/
Expand All @@ -263,7 +263,7 @@ public void setFrom(String address) {
}

/**
* Add a replyto address element.
* Add a "replyto" address element.
*
* @param address The address to reply to.
* @since Ant 1.6
Expand All @@ -273,7 +273,7 @@ public void addReplyTo(EmailAddress address) {
}

/**
* Shorthand to set the replyto address element.
* Shorthand to set the "replyto" address element.
*
* @param address The address to which replies should be directed.
* @since Ant 1.6
Expand All @@ -283,7 +283,7 @@ public void setReplyTo(String address) {
}

/**
* Add a to address element.
* Add a "to" address element.
*
* @param address An email address.
*/
Expand Down Expand Up @@ -449,16 +449,9 @@ public void execute() {
// prepare for the auto select mechanism
boolean autoFound = false;
// try MIME format
if (MIME.equals(encoding)
|| (AUTO.equals(encoding) && !autoFound)) {
if (MIME.equals(encoding) || AUTO.equals(encoding)) {
try {
//check to make sure that activation.jar
//and mail.jar are available - see bug 31969
Class.forName("javax.activation.DataHandler");
Class.forName("javax.mail.internet.MimeMessage");

mailer = ClasspathUtils.newInstance(
"org.apache.tools.ant.taskdefs.email.MimeMailer",
mailer = ClasspathUtils.newInstance(getMailerImplementation(),
EmailTask.class.getClassLoader(), Mailer.class);
autoFound = true;

Expand All @@ -467,16 +460,16 @@ public void execute() {
logBuildException("Failed to initialise MIME mail: ", e);
}
}
// SMTP auth only allowed with MIME mail
if (!autoFound && ((user != null) || (password != null))
&& (UU.equals(encoding) || PLAIN.equals(encoding))) {
throw new BuildException("SMTP auth only possible with MIME mail");
if ((UU.equals(encoding) || PLAIN.equals(encoding))
&& !autoFound) {
// SMTP auth only allowed with MIME mail
if (user != null || password != null) {
throw new BuildException("SMTP auth only possible with MIME mail");
}
// SSL only allowed with MIME mail
if (ssl || starttls) {
throw new BuildException("SSL and STARTTLS only possible with MIME mail");
}
// SSL only allowed with MIME mail
if (!autoFound && (ssl || starttls)
&& (UU.equals(encoding) || PLAIN.equals(encoding))) {
throw new BuildException(
"SSL and STARTTLS only possible with MIME mail");
}
// try UU format
if (UU.equals(encoding)
Expand Down Expand Up @@ -600,6 +593,32 @@ public void execute() {
}
}

private String getMailerImplementation() {
//check to make sure that activation.jar
//and mail.jar are available - see bug 31969
try {
Class.forName("jakarta.activation.DataHandler");
Class.forName("jakarta.mail.internet.MimeMessage");

return "org.apache.tools.ant.taskdefs.email.JakartaMimeMailer";
} catch (ClassNotFoundException cnfe) {
logBuildException("Could not find Jakarta MIME mail: ",
new BuildException(cnfe));
}

try {
Class.forName("javax.activation.DataHandler");
Class.forName("javax.mail.internet.MimeMessage");

return "org.apache.tools.ant.taskdefs.email.MimeMailer";
} catch (ClassNotFoundException cnfe) {
logBuildException("Could not find MIME mail: ",
new BuildException(cnfe));
}

return "org.apache.tools.ant.taskdefs.email.Mailer";
}

private void logBuildException(String reason, BuildException e) {
Throwable t = e.getCause() == null ? e : e.getCause();
log(reason + t.getMessage(), Project.MSG_WARN);
Expand Down
Loading

0 comments on commit 7b26711

Please sign in to comment.