From 44d7cafb871b95a6148a068976e421bcfa502983 Mon Sep 17 00:00:00 2001 From: Simon Brandhof Date: Wed, 3 Aug 2011 11:07:52 +0200 Subject: Fix loading of javax.mail library --- .../EmailNotificationChannel.java | 118 ++++++++++++--------- 1 file changed, 66 insertions(+), 52 deletions(-) (limited to 'plugins/sonar-email-notifications-plugin') diff --git a/plugins/sonar-email-notifications-plugin/src/main/java/org/sonar/plugins/emailnotifications/EmailNotificationChannel.java b/plugins/sonar-email-notifications-plugin/src/main/java/org/sonar/plugins/emailnotifications/EmailNotificationChannel.java index 1942d0fa215..a988fe52a4e 100644 --- a/plugins/sonar-email-notifications-plugin/src/main/java/org/sonar/plugins/emailnotifications/EmailNotificationChannel.java +++ b/plugins/sonar-email-notifications-plugin/src/main/java/org/sonar/plugins/emailnotifications/EmailNotificationChannel.java @@ -19,9 +19,6 @@ */ package org.sonar.plugins.emailnotifications; -import java.net.MalformedURLException; -import java.net.URL; - import org.apache.commons.lang.StringUtils; import org.apache.commons.mail.EmailException; import org.apache.commons.mail.SimpleEmail; @@ -35,6 +32,9 @@ import org.sonar.api.utils.SonarException; import org.sonar.plugins.emailnotifications.api.EmailMessage; import org.sonar.plugins.emailnotifications.api.EmailTemplate; +import java.net.MalformedURLException; +import java.net.URL; + /** * References: * - * + * * @since 2.10 */ public class EmailNotificationChannel extends NotificationChannel { @@ -133,65 +133,79 @@ public class EmailNotificationChannel extends NotificationChannel { } private void send(EmailMessage emailMessage) throws EmailException { - LOG.info("Sending email: {}", emailMessage); - String host = null; + // Trick to correctly initilize javax.mail library + ClassLoader classloader = Thread.currentThread().getContextClassLoader(); + Thread.currentThread().setContextClassLoader(getClass().getClassLoader()); + try { - host = new URL(configuration.getServerBaseURL()).getHost(); - } catch (MalformedURLException e) { - // ignore - } + LOG.info("Sending email: {}", emailMessage); + String host = null; + try { + host = new URL(configuration.getServerBaseURL()).getHost(); + } catch (MalformedURLException e) { + // ignore + } - SimpleEmail email = new SimpleEmail(); - if (StringUtils.isNotBlank(host)) { - /* - * Set headers for proper threading: GMail will not group messages, even if they have same subject, but don't have "In-Reply-To" and - * "References" headers. TODO investigate threading in other clients like KMail, Thunderbird, Outlook - */ - if (StringUtils.isNotEmpty(emailMessage.getMessageId())) { - String messageId = "<" + emailMessage.getMessageId() + "@" + host + ">"; - email.addHeader(IN_REPLY_TO_HEADER, messageId); - email.addHeader(REFERENCES_HEADER, messageId); + SimpleEmail email = new SimpleEmail(); + if (StringUtils.isNotBlank(host)) { + /* + * Set headers for proper threading: GMail will not group messages, even if they have same subject, but don't have "In-Reply-To" and + * "References" headers. TODO investigate threading in other clients like KMail, Thunderbird, Outlook + */ + if (StringUtils.isNotEmpty(emailMessage.getMessageId())) { + String messageId = "<" + emailMessage.getMessageId() + "@" + host + ">"; + email.addHeader(IN_REPLY_TO_HEADER, messageId); + email.addHeader(REFERENCES_HEADER, messageId); + } + // Set headers for proper filtering + email.addHeader(LIST_ID_HEADER, "Sonar "); + email.addHeader(LIST_ARCHIVE_HEADER, configuration.getServerBaseURL()); } - // Set headers for proper filtering - email.addHeader(LIST_ID_HEADER, "Sonar "); - email.addHeader(LIST_ARCHIVE_HEADER, configuration.getServerBaseURL()); - } - // Set general information - email.setFrom(configuration.getFrom(), StringUtils.defaultIfBlank(emailMessage.getFrom(), FROM_NAME_DEFAULT)); - email.addTo(emailMessage.getTo(), " "); - String subject = StringUtils.defaultIfBlank(StringUtils.trimToEmpty(configuration.getPrefix()) + " ", "") - + StringUtils.defaultString(emailMessage.getSubject(), SUBJECT_DEFAULT); - email.setSubject(subject); - email.setMsg(emailMessage.getMessage()); - // Send - email.setHostName(configuration.getSmtpHost()); - if (StringUtils.equalsIgnoreCase(configuration.getSecureConnection(), "SSL")) { - email.setSSL(true); - email.setSslSmtpPort(configuration.getSmtpPort()); - } else if (StringUtils.isBlank(configuration.getSecureConnection())) { - email.setSmtpPort(Integer.parseInt(configuration.getSmtpPort())); - } else { - throw new SonarException("Unknown type of SMTP secure connection: " + configuration.getSecureConnection()); - } - if (StringUtils.isNotBlank(configuration.getSmtpUsername()) || StringUtils.isNotBlank(configuration.getSmtpPassword())) { - email.setAuthentication(configuration.getSmtpUsername(), configuration.getSmtpPassword()); + // Set general information + email.setFrom(configuration.getFrom(), StringUtils.defaultIfBlank(emailMessage.getFrom(), FROM_NAME_DEFAULT)); + email.addTo(emailMessage.getTo(), " "); + String subject = StringUtils.defaultIfBlank(StringUtils.trimToEmpty(configuration.getPrefix()) + " ", "") + + StringUtils.defaultString(emailMessage.getSubject(), SUBJECT_DEFAULT); + email.setSubject(subject); + email.setMsg(emailMessage.getMessage()); + // Send + email.setHostName(configuration.getSmtpHost()); + if (StringUtils.equalsIgnoreCase(configuration.getSecureConnection(), "SSL")) { + email.setSSL(true); + email.setSslSmtpPort(configuration.getSmtpPort()); + } else if (StringUtils.isBlank(configuration.getSecureConnection())) { + email.setSmtpPort(Integer.parseInt(configuration.getSmtpPort())); + } else { + throw new SonarException("Unknown type of SMTP secure connection: " + configuration.getSecureConnection()); + } + if (StringUtils.isNotBlank(configuration.getSmtpUsername()) || StringUtils.isNotBlank(configuration.getSmtpPassword())) { + email.setAuthentication(configuration.getSmtpUsername(), configuration.getSmtpPassword()); + } + email.setSocketConnectionTimeout(SOCKET_TIMEOUT); + email.setSocketTimeout(SOCKET_TIMEOUT); + email.send(); + + } finally { + Thread.currentThread().setContextClassLoader(classloader); } - email.setSocketConnectionTimeout(SOCKET_TIMEOUT); - email.setSocketTimeout(SOCKET_TIMEOUT); - email.send(); } /** * Send test email. This method called from Ruby. - * + * * @throws EmailException when unable to send */ public void sendTestEmail(String toAddress, String subject, String message) throws EmailException { - EmailMessage emailMessage = new EmailMessage(); - emailMessage.setTo(toAddress); - emailMessage.setSubject(subject); - emailMessage.setMessage(message); - send(emailMessage); + try { + EmailMessage emailMessage = new EmailMessage(); + emailMessage.setTo(toAddress); + emailMessage.setSubject(subject); + emailMessage.setMessage(message); + send(emailMessage); + } catch (EmailException e) { + LOG.error("Fail to send test email to: " + toAddress, e); + throw e; + } } } -- cgit v1.2.3