View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
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   * Utility servlet context listener managing GreenMail test server instance.
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  }