1   /*
2    * $Id: GreenMailUtilities.java 11511 2008-03-26 03:22:01Z dirk.olmes $
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSource, Inc.  All rights reserved.  http://www.mulesource.com
5    *
6    * The software in this package is published under the terms of the CPAL v1.0
7    * license, a copy of which has been included with this distribution in the
8    * LICENSE.txt file.
9    */
10  
11  package org.mule.transport.email;
12  
13  import com.icegreen.greenmail.user.GreenMailUser;
14  import com.icegreen.greenmail.user.UserManager;
15  import com.icegreen.greenmail.util.GreenMail;
16  
17  import java.net.Socket;
18  import java.util.Properties;
19  
20  import javax.mail.Message;
21  import javax.mail.MessagingException;
22  import javax.mail.Session;
23  import javax.mail.internet.InternetAddress;
24  import javax.mail.internet.MimeMessage;
25  
26  import org.apache.commons.logging.Log;
27  import org.apache.commons.logging.LogFactory;
28  
29  public class GreenMailUtilities
30  {
31  
32      protected static Log logger = LogFactory.getLog(GreenMailUtilities.class);
33  
34      public static void storeEmail(UserManager userManager, String email, String user, String password,
35                                    MimeMessage message)
36              throws Exception
37      {
38          // note that with greenmail 1.1 the Servers object is unreliable
39          // and the approach taken in their examples will not work.
40          // the following does work, but may break in a later version
41          // (there is some confusion in the greenmail code about
42          // whether users are identified by email or name alone)
43          // in which case try retrieving by EMAIL rather than USER
44          userManager.createUser(email, user, password);
45          GreenMailUser gmUser = userManager.getUser(user);
46          assert null != gmUser;
47          gmUser.deliver(message);
48      }
49  
50      public static MimeMessage toMessage(String text, String email) throws MessagingException
51      {
52          MimeMessage message = new MimeMessage(Session.getDefaultInstance(new Properties()));
53          message.setContent(text, "text/plain");
54          message.setRecipient(Message.RecipientType.TO, new InternetAddress(email));
55          return message;
56      }
57  
58      public static void waitForStartup(String host, int port, int count, long wait) throws InterruptedException
59      {
60          for (int i = 0; i < count; ++i)
61          {
62              Thread.sleep(wait);
63              try {
64                  Socket socket = new Socket(host, port);
65                  socket.close();
66                  logger.info("Successful connection made to port " + port);
67                  return;
68              }
69              catch (Exception e)
70              {
71                  logger.warn("Could not connect to server on " + host + ":" + port + " - " + e.getMessage());
72              }
73          }
74          throw new RuntimeException("Server failed to start within " + (count * wait) + "ms");
75      }
76  
77      public static void robustStartup(GreenMail servers, String host, int port, int startMax, int testMax, long wait)
78              throws InterruptedException
79      {
80          for (int start = 0; start < startMax; ++start)
81          {
82              try
83              {
84                  servers.start();
85                  waitForStartup(host, port, testMax, wait);
86                  return;
87              }
88              catch (Exception e)
89              {
90                  try
91                  {
92                      servers.stop();
93                  }
94                  catch (Throwable t)
95                  {
96                      // ignore
97                  }
98              }
99              Thread.sleep(wait);
100         }
101     }
102 
103 }