View Javadoc
1   package fr.ifremer.coser.web;
2   
3   /*
4    * #%L
5    * Coser :: Web
6    * %%
7    * Copyright (C) 2010 - 2014 Ifremer, Codelutin, Chemit Tony
8    * %%
9    * This program is free software: you can redistribute it and/or modify
10   * it under the terms of the GNU Affero General Public License as published by
11   * the Free Software Foundation, either version 3 of the License, or
12   * (at your option) any later version.
13   * 
14   * This program is distributed in the hope that it will be useful,
15   * but WITHOUT ANY WARRANTY; without even the implied warranty of
16   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17   * GNU General Public License for more details.
18   * 
19   * You should have received a copy of the GNU Affero General Public License
20   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
21   * #L%
22   */
23  
24  import fr.ifremer.coser.bean.ZoneMap;
25  import org.apache.commons.lang3.StringUtils;
26  import org.apache.commons.logging.Log;
27  import org.apache.commons.logging.LogFactory;
28  
29  import javax.servlet.ServletContextEvent;
30  import javax.servlet.ServletContextListener;
31  import java.util.Date;
32  import java.util.Map;
33  
34  /**
35   * To listen start and end of the application.
36   * <p/>
37   * On start we will init the application context ({@link CoserWebApplicationContext#init()}).
38   * <p/>
39   * On stop, just release close the application context ({@link CoserWebApplicationContext#close()}).
40   *
41   * @author tchemit <chemit@codelutin.com>
42   * @since 1.5
43   */
44  public class CoserApplicationListener implements ServletContextListener {
45  
46      /** Logger. */
47      protected static final Log log =
48              LogFactory.getLog(CoserApplicationListener.class);
49  
50      @Override
51      public void contextInitialized(ServletContextEvent sce) {
52  
53          if (log.isInfoEnabled()) {
54              log.info("Application starting at " + new Date() + "...");
55          }
56          CoserWebApplicationContext.init();
57  
58          CoserWebApplicationContext coserWebApplicationContext = CoserWebApplicationContext.get();
59  
60          checkConfiguration(coserWebApplicationContext);
61      }
62  
63      @Override
64      public void contextDestroyed(ServletContextEvent sce) {
65          if (log.isInfoEnabled()) {
66              log.info("Application is ending at " + new Date() + "...");
67          }
68          CoserWebApplicationContext.close();
69      }
70  
71      protected void checkConfiguration(CoserWebApplicationContext coserWebApplicationContext) {
72  
73  
74          // check that zone pictures are sane
75          ZoneMap zoneMap = coserWebApplicationContext.getZoneMap();
76  
77          Map<String, String> zonePictures = zoneMap.getZonePictures();
78          for (Map.Entry<String, String> entry : zonePictures.entrySet()) {
79  
80              if (StringUtils.isBlank(entry.getValue())) {
81  
82                  if (log.isErrorEnabled()) {
83                      log.error(String.format("Zone with no picture: %s", entry.getKey()));
84                  }
85              }
86          }
87  
88      }
89  }