diff options
author | Simon Brandhof <simon.brandhof@gmail.com> | 2011-08-03 11:07:52 +0200 |
---|---|---|
committer | Simon Brandhof <simon.brandhof@gmail.com> | 2011-08-03 11:07:52 +0200 |
commit | 44d7cafb871b95a6148a068976e421bcfa502983 (patch) | |
tree | 7a722a06677a9b3fd475e6f4a17699e35a9e0c1e /plugins/sonar-email-notifications-plugin | |
parent | 21e80bbc49b8168e94b5c0cdcb74d6393c7b9cfc (diff) | |
download | sonarqube-44d7cafb871b95a6148a068976e421bcfa502983.tar.gz sonarqube-44d7cafb871b95a6148a068976e421bcfa502983.zip |
Fix loading of javax.mail library
Diffstat (limited to 'plugins/sonar-email-notifications-plugin')
-rw-r--r-- | plugins/sonar-email-notifications-plugin/src/main/java/org/sonar/plugins/emailnotifications/EmailNotificationChannel.java | 118 |
1 files changed, 66 insertions, 52 deletions
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: * <ul> @@ -42,7 +42,7 @@ import org.sonar.plugins.emailnotifications.api.EmailTemplate; * <li><a href="http://tools.ietf.org/html/rfc2919">List-Id: A Structured Field and Namespace for the Identification of Mailing Lists</a></li> * <li><a href="https://github.com/blog/798-threaded-email-notifications">GitHub: Threaded Email Notifications</a></li> * </ul> - * + * * @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 <sonar." + host + ">"); + email.addHeader(LIST_ARCHIVE_HEADER, configuration.getServerBaseURL()); } - // Set headers for proper filtering - email.addHeader(LIST_ID_HEADER, "Sonar <sonar." + host + ">"); - 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; + } } } |