Author: woonsan Date: Thu Sep 15 18:08:54 2011 New Revision: 1171200 URL: http://svn.apache.org/viewvc?rev=1171200&view=rev Log: JS2-1256: Fixing cross contexts classloader issue when using portalAdmin service to send emails
Modified: portals/jetspeed-2/portal/trunk/components/jetspeed-portal/src/main/java/org/apache/jetspeed/administration/PortalAdministrationImpl.java portals/jetspeed-2/portal/trunk/components/jetspeed-portal/src/test/java/org/apache/jetspeed/administration/TestPortalAdministrationImpl.java Modified: portals/jetspeed-2/portal/trunk/components/jetspeed-portal/src/main/java/org/apache/jetspeed/administration/PortalAdministrationImpl.java URL: http://svn.apache.org/viewvc/portals/jetspeed-2/portal/trunk/components/jetspeed-portal/src/main/java/org/apache/jetspeed/administration/PortalAdministrationImpl.java?rev=1171200&r1=1171199&r2=1171200&view=diff ============================================================================== --- portals/jetspeed-2/portal/trunk/components/jetspeed-portal/src/main/java/org/apache/jetspeed/administration/PortalAdministrationImpl.java (original) +++ portals/jetspeed-2/portal/trunk/components/jetspeed-portal/src/main/java/org/apache/jetspeed/administration/PortalAdministrationImpl.java Thu Sep 15 18:08:54 2011 @@ -472,8 +472,13 @@ public class PortalAdministrationImpl im msg.setSubject(subject); msg.setTo(to); msg.setText(text); + + ClassLoader currentCL = Thread.currentThread().getContextClassLoader(); + try { + // JS2-1256: Needs to set context classloader to null to let geronimo-javamail find provider class properly. + Thread.currentThread().setContextClassLoader(null); mailSender.send(msg); } catch (MailException ex) @@ -482,6 +487,10 @@ public class PortalAdministrationImpl im "Failed to send forgotten password email to user with email address because "+ex.getMessage(), ex ); //+ user.getEmail()); } + finally + { + Thread.currentThread().setContextClassLoader(currentCL); + } } public String mergeEmailTemplate(PortletConfig portletConfig, Map attributes, String attributesName, String template) Modified: portals/jetspeed-2/portal/trunk/components/jetspeed-portal/src/test/java/org/apache/jetspeed/administration/TestPortalAdministrationImpl.java URL: http://svn.apache.org/viewvc/portals/jetspeed-2/portal/trunk/components/jetspeed-portal/src/test/java/org/apache/jetspeed/administration/TestPortalAdministrationImpl.java?rev=1171200&r1=1171199&r2=1171200&view=diff ============================================================================== --- portals/jetspeed-2/portal/trunk/components/jetspeed-portal/src/test/java/org/apache/jetspeed/administration/TestPortalAdministrationImpl.java (original) +++ portals/jetspeed-2/portal/trunk/components/jetspeed-portal/src/test/java/org/apache/jetspeed/administration/TestPortalAdministrationImpl.java Thu Sep 15 18:08:54 2011 @@ -17,6 +17,8 @@ package org.apache.jetspeed.administration; import java.io.ByteArrayOutputStream; +import java.net.URL; +import java.net.URLClassLoader; import java.util.ArrayList; import java.util.Arrays; import java.util.Date; @@ -49,6 +51,8 @@ import org.springframework.mail.javamail public class TestPortalAdministrationImpl extends TestCase { private String smtpHost; + private String adminEmail = "admin@localhost"; + private String userEmail = "user@localhost"; public static void main(String args[]) { @@ -59,11 +63,25 @@ public class TestPortalAdministrationImp { super.setUp(); - // If the following sys property is provided (e.g. '-DTestPortalAdministrationImpl.smtp.host=localhost') + // If the following sys properties are provided (e.g. '-DTestPortalAdministrationImpl.smtp.host=localhost -DTestPortalAdministrationImpl.user.email=jdoe@localhost') // and if the destination smtp server is provided, then - // this test case will send message to the target server. + // this test case will send message to the recipient and the target server. // Otherwise, by default, this test case uses a mock object. - smtpHost = System.getProperty("TestPortalAdministrationImpl.smtp.host"); + + String prop = System.getProperty("TestPortalAdministrationImpl.smtp.host"); + if (prop != null) { + smtpHost = prop; + } + + prop = System.getProperty("TestPortalAdministrationImpl.admin.email"); + if (prop != null) { + adminEmail = prop; + } + + prop = System.getProperty("TestPortalAdministrationImpl.user.email"); + if (prop != null) { + userEmail = prop; + } } public static Test suite() @@ -97,7 +115,7 @@ public class TestPortalAdministrationImp } PortalAdministrationImpl pai = new PortalAdministrationImpl(null,null,null,null,null,null,javaMailSender,null); - pai.sendEmail("ch...@bluesunrise.com","this is a unittest","da...@bluesunrise.com","this is the content of the message"); + pai.sendEmail(adminEmail,"this is a unittest",userEmail,"this is the content of the message"); if (javaMailSender instanceof MockJavaMailSender) { @@ -111,11 +129,11 @@ public class TestPortalAdministrationImp List<Address> froms = Arrays.asList(sentMessage.getFrom()); assertEquals(1, froms.size()); - assertEquals("ch...@bluesunrise.com", ((InternetAddress) froms.get(0)).getAddress()); + assertEquals(adminEmail, ((InternetAddress) froms.get(0)).getAddress()); List<Address> tos = Arrays.asList(sentMessage.getRecipients(Message.RecipientType.TO)); assertEquals(1, tos.size()); - assertEquals("da...@bluesunrise.com", ((InternetAddress) tos.get(0)).getAddress()); + assertEquals(userEmail, ((InternetAddress) tos.get(0)).getAddress()); assertEquals("this is a unittest", sentMessage.getSubject()); @@ -129,6 +147,31 @@ public class TestPortalAdministrationImp } } + public void testSendMailFromAnotherThread() throws Exception { + final List<Exception> exceptions = new ArrayList<Exception>(); + + Thread t = new Thread(new Runnable() { + public void run() { + ClassLoader cl = Thread.currentThread().getContextClassLoader(); + try { + Thread.currentThread().setContextClassLoader(new URLClassLoader(new URL[0])); + testSendEmail(); + } catch (Exception e) { + exceptions.add(e); + throw new RuntimeException(e); + } finally { + Thread.currentThread().setContextClassLoader(cl); + } + } + }); + t.start(); + t.join(); + + if (!exceptions.isEmpty()) { + fail("testSendMail was not successful when the service is invoked from another thread having a different context classloader. " + exceptions.get(0)); + } + } + // this needs too much init to test easily right now public void xtestRegUser() throws Exception { --------------------------------------------------------------------- To unsubscribe, e-mail: jetspeed-dev-unsubscr...@portals.apache.org For additional commands, e-mail: jetspeed-dev-h...@portals.apache.org