--- dao/DaoAdresseService/src/dk/daoas/daoadresseservice/ContextListener.java 2015/02/26 16:11:50 2392 +++ dao/DaoAdresseService/src/dk/daoas/daoadresseservice/ContextListener.java 2015/02/27 07:34:08 2393 @@ -1,17 +1,23 @@ package dk.daoas.daoadresseservice; +import java.io.FileReader; +import java.io.IOException; +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.daoas.daoadresseservice.admin.ServiceConfig; import dk.thoerup.circuitbreaker.AccountingCircuitBreaker; import dk.thoerup.circuitbreaker.CircuitBreakerManager; import dk.thoerup.circuitbreaker.web.CircuitBreakerServletBase; import dk.thoerup.webconfig.ConfigServlet; -import dk.thoerup.webconfig.ContextConfigLoader; +import dk.thoerup.webconfig.PropertiesConfigLoader; /** * Application Lifecycle Listener implementation class ContextListener @@ -20,7 +26,7 @@ @WebListener public class ContextListener implements ServletContextListener { - + public ContextListener() { @@ -37,7 +43,13 @@ ServletContext ctxt = ctxtEvt.getServletContext(); - ServiceConfig conf = initConfig(ctxt); + ServiceConfig conf = null; + try { + conf = initConfig(ctxt); + } catch (IOException e) { + System.out.println("Error loading config " + e.getMessage()); + throw new RuntimeException(e); + } initDataLoader(ctxt, conf); @@ -47,17 +59,31 @@ System.out.println("DAO Context done"); } - private void initDataLoader(ServletContext ctxt, ServiceConfig conf) { - DataLoader load = new DataLoader(ctxt,conf); - ctxt.setAttribute("dataloader", load); - load.doLoad(); - } + - private ServiceConfig initConfig(ServletContext ctxt) { + private ServiceConfig initConfig(ServletContext ctxt) throws IOException { + + final String CONF_FILE_NAME = "daoadresseservice.conf"; + + ServiceConfig conf = new ServiceConfig(); - new ContextConfigLoader(ctxt).loadConfig(conf); + String file = null; + if (SystemUtils.IS_OS_WINDOWS) { + file = "C:\\" + CONF_FILE_NAME; + } else { + file = "/etc/" + CONF_FILE_NAME; + } + + Properties propsFile = new Properties(); + try (FileReader reader = new FileReader(file) ) { + propsFile.load(reader); + } + + + new PropertiesConfigLoader(propsFile).loadConfig(conf); + ctxt.setAttribute("config", conf); @@ -67,8 +93,13 @@ ServletRegistration.Dynamic dynconf = ctxt.addServlet("webconfig", servlet ); dynconf.addMapping("/ConfigServlet"); - return conf; - + return conf; + } + + private void initDataLoader(ServletContext ctxt, ServiceConfig conf) { + DataLoader load = new DataLoader(ctxt,conf); + ctxt.setAttribute("dataloader", load); + load.doLoad(); } private void initCircuitBreakers(ServletContext ctxt, ServiceConfig conf) {