View Javadoc

1   /*
2    * #%L
3    * Bitrepository Webclient
4    * %%
5    * Copyright (C) 2010 - 2012 The State and University Library, The Royal Library and The State Archives, Denmark
6    * %%
7    * This program is free software: you can redistribute it and/or modify
8    * it under the terms of the GNU Lesser General Public License as 
9    * published by the Free Software Foundation, either version 2.1 of the 
10   * License, or (at your option) any later version.
11   * 
12   * This program is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15   * GNU General Lesser Public License for more details.
16   * 
17   * You should have received a copy of the GNU General Lesser Public 
18   * License along with this program.  If not, see
19   * <http://www.gnu.org/licenses/lgpl-2.1.html>.
20   * #L%
21   */
22  package org.bitrepository.webservice;
23  
24  import javax.servlet.ServletContextEvent;
25  import javax.servlet.ServletContextListener;
26  
27  import org.bitrepository.BasicClient;
28  import org.bitrepository.BasicClientFactory;
29  import org.bitrepository.utils.LogbackConfigLoader;
30  import org.slf4j.Logger;
31  import org.slf4j.LoggerFactory;
32  
33  
34  /**
35   * The Listener has two intentions
36   * 1) Acquire necessary information at startup to locate configuration files and create the first instance 
37   *     of the basic client, so everything is setup before the first users start using the webservice. 
38   * 2) In time shut the service down in a proper manner, so no threads will be orphaned.   
39   */
40  public class WebclientContextListener implements ServletContextListener {
41      private final Logger log = LoggerFactory.getLogger(getClass());
42  
43      /**
44       * Do initialization work  
45       */
46      @SuppressWarnings("unused")
47      @Override
48      public void contextInitialized(ServletContextEvent sce) {
49          String confDir = sce.getServletContext().getInitParameter("configurationDir");
50          if(confDir == null) {
51              throw new RuntimeException("No configuration directory specified!");
52          }
53          log.debug("Configuration dir = " + confDir);
54          try {
55              new LogbackConfigLoader(confDir + "/logback.xml");
56          } catch (Exception e) {
57              log.info("Failed to read log configuration file. Falling back to default.");
58          } 
59          BasicClientFactory.init(confDir);
60          BasicClient client = BasicClientFactory.getInstance();
61          log.debug("Servlet context initialized");
62      }
63  
64      /**
65       * Does work of shutting the webclient down in a graceful manner. 
66       * This is done by calling BasicClient's shutdown method.  
67       */
68      @Override
69      public void contextDestroyed(ServletContextEvent sce) {
70          BasicClient client = BasicClientFactory.getInstance();
71          client.shutdown(); 
72          log.debug("Servlet context destroyed");
73      }
74  }