1 /* 2 * #%L 3 * Bitrepository Core 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.service; 23 24 import javax.servlet.ServletContextEvent; 25 import javax.servlet.ServletContextListener; 26 27 import org.bitrepository.protocol.utils.LogbackConfigLoader; 28 import org.slf4j.Logger; 29 import org.slf4j.LoggerFactory; 30 31 /** 32 * The Listener has two intentions 33 * 1) Acquire necessary information at startup to locate configuration files and create the first instance 34 * of the basic client, so everything is setup before the first users start using the webservice. 35 * 2) In time shut the service down in a proper manner, so no threads will be orphaned. 36 */ 37 public abstract class AbstractBitrepositoryContextListener implements ServletContextListener { 38 /** The log.*/ 39 private final Logger log = LoggerFactory.getLogger(getClass()); 40 41 /** 42 * Return the path to the service's configuration directory 43 */ 44 public abstract String getSettingsParameter(); 45 46 /** 47 * Method to get an instance of the service for the context 48 */ 49 public abstract LifeCycledService getService(); 50 51 /** 52 * Sets up the configuration 53 */ 54 public abstract void initialize(String configurationDir); 55 56 /** 57 * Method called at servlet initialization 58 */ 59 public void contextInitialized(ServletContextEvent sce) { 60 String confDir = sce.getServletContext().getInitParameter(getSettingsParameter()); 61 if(confDir == null) { 62 throw new RuntimeException("No configuration directory specified!"); 63 } 64 log.debug("Configuration dir = " + confDir); 65 66 try { 67 new LogbackConfigLoader(confDir + "/logback.xml"); 68 } catch (Exception e) { 69 log.info("Failed to read log configuration file. Falling back to default."); 70 } 71 try { 72 initialize(confDir); 73 getService(); 74 } catch (RuntimeException e) { 75 // This is to ensure the message of what went wrong will go into the service's own logfile 76 // rather than just in Tomcat's localhost logfile where it can be hard to find. 77 // Rethrowing the exception makes the service shutdown again. 78 log.error(e.getMessage()); 79 throw e; 80 } 81 log.debug("Servlet context initialized"); 82 83 } 84 85 /** 86 * Method called at servlet shutdown. 87 */ 88 public void contextDestroyed(ServletContextEvent sce) { 89 getService().shutdown(); 90 log.debug("Servlet context destroyed"); 91 } 92 }