package dk.daoas.adressevedligehold; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStreamReader; import java.io.Reader; import java.nio.charset.Charset; import java.util.Properties; import javax.servlet.ServletContext; import javax.servlet.ServletContextEvent; import javax.servlet.ServletContextListener; import javax.servlet.ServletRegistration; import javax.servlet.annotation.WebListener; import org.apache.commons.lang3.SystemUtils; import dk.thoerup.webconfig.ConfigServlet; import dk.thoerup.webconfig.PropertiesConfigLoader; /** * Application Lifecycle Listener implementation class ContextListener * */ @WebListener public class ContextListener implements ServletContextListener { public ContextListener() { // TODO Auto-generated constructor stub } public void contextDestroyed(ServletContextEvent ctxtEvt) { // TODO Auto-generated method stub } @Override public void contextInitialized(ServletContextEvent ctxtEvt) { System.out.println("Starting DAO Context"); ServletContext ctxt = ctxtEvt.getServletContext(); try { initConfig(ctxt); } catch (IOException e) { System.out.println("Error loading config " + e.getMessage()); throw new RuntimeException(e); } System.out.println("DAO Context done"); } private ServiceConfig initConfig(ServletContext ctxt) throws IOException { final String CONF_FILE_NAME = "daoadressevedligehold.conf"; ServiceConfig conf = ServiceConfig.getInstance(); String file = null; if (SystemUtils.IS_OS_WINDOWS) { file = "C:\\" + CONF_FILE_NAME; } else { file = "/etc/" + CONF_FILE_NAME; } Properties propsFile = new Properties(); try ( Reader reader = new InputStreamReader(new FileInputStream(file), Charset.forName("UTF-8")) ) { propsFile.load(reader); } new PropertiesConfigLoader(propsFile).loadConfig(conf); ctxt.setAttribute("config", conf); // ///////////////////////////////////// ConfigServlet servlet = new ConfigServlet(); servlet.setConfigObject( conf ); ServletRegistration.Dynamic dynconf = ctxt.addServlet("webconfig", servlet ); dynconf.addMapping("/ConfigServlet"); return conf; } }