1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.syncope.fit.buildtools;
20
21 import com.icegreen.greenmail.util.InterruptableGreenMail;
22 import com.icegreen.greenmail.util.ServerSetup;
23 import javax.servlet.ServletContext;
24 import javax.servlet.ServletContextEvent;
25 import javax.servlet.ServletContextListener;
26 import javax.servlet.annotation.WebListener;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
29 import org.springframework.web.context.WebApplicationContext;
30 import org.springframework.web.context.support.WebApplicationContextUtils;
31
32
33
34
35 @WebListener
36 public class GreenMailStartStopListener implements ServletContextListener {
37
38 private static final Logger LOG = LoggerFactory.getLogger(GreenMailStartStopListener.class);
39
40 public static final String GREENMAIL = "greenMail";
41
42 @Override
43 public void contextInitialized(final ServletContextEvent sce) {
44 ServletContext sc = sce.getServletContext();
45 WebApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(sc);
46
47 InterruptableGreenMail greenMail = (InterruptableGreenMail) sc.getAttribute(GREENMAIL);
48 if (greenMail == null) {
49 ServerSetup[] config = new ServerSetup[2];
50 config[0] = new ServerSetup(
51 ctx.getEnvironment().getProperty("testmail.smtpport", Integer.class),
52 "localhost", ServerSetup.PROTOCOL_SMTP);
53 config[1] = new ServerSetup(
54 ctx.getEnvironment().getProperty("testmail.pop3port", Integer.class),
55 "localhost", ServerSetup.PROTOCOL_POP3);
56 greenMail = new InterruptableGreenMail(config);
57 greenMail.start();
58
59 sc.setAttribute(GREENMAIL, greenMail);
60 }
61
62 LOG.info("SMTP and POP3 servers successfully (re)started");
63 }
64
65 @Override
66 public void contextDestroyed(final ServletContextEvent sce) {
67 ServletContext sc = sce.getServletContext();
68
69 InterruptableGreenMail greenMail = (InterruptableGreenMail) sc.getAttribute(GREENMAIL);
70 if (greenMail != null) {
71 greenMail.stop();
72
73 LOG.info("SMTP and POP3 servers successfully stopped");
74 }
75 }
76 }