1 |
package dk.daoas.daoadresseservice; |
package dk.daoas.daoadresseservice; |
2 |
|
|
3 |
|
import java.io.FileReader; |
4 |
|
import java.io.IOException; |
5 |
|
import java.util.Properties; |
6 |
|
|
7 |
import javax.servlet.ServletContext; |
import javax.servlet.ServletContext; |
8 |
import javax.servlet.ServletContextEvent; |
import javax.servlet.ServletContextEvent; |
9 |
import javax.servlet.ServletContextListener; |
import javax.servlet.ServletContextListener; |
10 |
import javax.servlet.ServletRegistration; |
import javax.servlet.ServletRegistration; |
11 |
import javax.servlet.annotation.WebListener; |
import javax.servlet.annotation.WebListener; |
12 |
|
|
13 |
|
import org.apache.commons.lang3.SystemUtils; |
14 |
|
|
15 |
|
import com.jcabi.manifests.Manifests; |
16 |
|
import com.jcabi.manifests.ServletMfs; |
17 |
|
|
18 |
import dk.daoas.daoadresseservice.admin.ServiceConfig; |
import dk.daoas.daoadresseservice.admin.ServiceConfig; |
19 |
import dk.thoerup.circuitbreaker.AccountingCircuitBreaker; |
import dk.thoerup.circuitbreaker.CircuitBreaker; |
20 |
import dk.thoerup.circuitbreaker.CircuitBreakerManager; |
import dk.thoerup.circuitbreaker.CircuitBreakerManager; |
21 |
|
import dk.thoerup.circuitbreaker.statistics.AccountingStatistics; |
22 |
import dk.thoerup.circuitbreaker.web.CircuitBreakerServletBase; |
import dk.thoerup.circuitbreaker.web.CircuitBreakerServletBase; |
23 |
import dk.thoerup.webconfig.ConfigServlet; |
import dk.thoerup.webconfig.ConfigServlet; |
24 |
import dk.thoerup.webconfig.ContextConfigLoader; |
import dk.thoerup.webconfig.PropertiesConfigLoader; |
25 |
|
|
26 |
/** |
/** |
27 |
* Application Lifecycle Listener implementation class ContextListener |
* Application Lifecycle Listener implementation class ContextListener |
30 |
@WebListener |
@WebListener |
31 |
public class ContextListener implements ServletContextListener { |
public class ContextListener implements ServletContextListener { |
32 |
|
|
33 |
|
|
34 |
|
|
35 |
|
|
36 |
public ContextListener() { |
public ContextListener() { |
45 |
public void contextInitialized(ServletContextEvent ctxtEvt) { |
public void contextInitialized(ServletContextEvent ctxtEvt) { |
46 |
System.out.println("Starting DAO Context"); |
System.out.println("Starting DAO Context"); |
47 |
|
|
48 |
|
|
49 |
|
|
50 |
ServletContext ctxt = ctxtEvt.getServletContext(); |
ServletContext ctxt = ctxtEvt.getServletContext(); |
51 |
|
|
52 |
ServiceConfig conf = initConfig(ctxt); |
ServiceConfig conf = null; |
53 |
|
try { |
54 |
|
conf = initConfig(ctxt); |
55 |
|
} catch (IOException e) { |
56 |
|
System.out.println("Error loading config " + e.getMessage()); |
57 |
|
throw new RuntimeException(e); |
58 |
|
} |
59 |
|
|
60 |
initDataLoader(ctxt, conf); |
initDataLoader(ctxt, conf); |
61 |
|
|
62 |
initCircuitBreakers(ctxt, conf); |
initCircuitBreakers(ctxt, conf); |
63 |
|
|
64 |
|
|
65 |
|
try { |
66 |
|
Manifests.DEFAULT.append(new ServletMfs(ctxt)); |
67 |
|
} catch (Exception e) { |
68 |
|
System.out.println("Error loading config " + e.getMessage()); |
69 |
|
} |
70 |
|
|
71 |
System.out.println("DAO Context done"); |
System.out.println("DAO Context done"); |
72 |
} |
} |
73 |
|
|
74 |
private void initDataLoader(ServletContext ctxt, ServiceConfig conf) { |
|
|
DataLoader load = new DataLoader(ctxt,conf); |
|
|
ctxt.setAttribute("dataloader", load); |
|
|
load.doLoad(); |
|
|
} |
|
75 |
|
|
76 |
|
|
77 |
private ServiceConfig initConfig(ServletContext ctxt) { |
private ServiceConfig initConfig(ServletContext ctxt) throws IOException { |
78 |
|
|
79 |
|
final String CONF_FILE_NAME = "daoadresseservice.conf"; |
80 |
|
|
81 |
|
|
82 |
ServiceConfig conf = new ServiceConfig(); |
ServiceConfig conf = new ServiceConfig(); |
83 |
|
|
84 |
new ContextConfigLoader(ctxt).loadConfig(conf); |
String file = null; |
85 |
|
if (SystemUtils.IS_OS_WINDOWS) { |
86 |
|
file = "C:\\" + CONF_FILE_NAME; |
87 |
|
} else { |
88 |
|
file = "/etc/" + CONF_FILE_NAME; |
89 |
|
} |
90 |
|
|
91 |
|
Properties propsFile = new Properties(); |
92 |
|
try (FileReader reader = new FileReader(file) ) { |
93 |
|
propsFile.load(reader); |
94 |
|
} |
95 |
|
|
96 |
|
|
97 |
|
new PropertiesConfigLoader(propsFile).loadConfig(conf); |
98 |
|
|
99 |
|
|
100 |
ctxt.setAttribute("config", conf); |
ctxt.setAttribute("config", conf); |
101 |
|
|
105 |
ServletRegistration.Dynamic dynconf = ctxt.addServlet("webconfig", servlet ); |
ServletRegistration.Dynamic dynconf = ctxt.addServlet("webconfig", servlet ); |
106 |
dynconf.addMapping("/ConfigServlet"); |
dynconf.addMapping("/ConfigServlet"); |
107 |
|
|
108 |
return conf; |
return conf; |
109 |
|
} |
110 |
|
|
111 |
|
private void initDataLoader(ServletContext ctxt, ServiceConfig conf) { |
112 |
|
DataLoader load = new DataLoader(ctxt,conf); |
113 |
|
ctxt.setAttribute("dataloader", load); |
114 |
|
load.doLoad(); |
115 |
} |
} |
116 |
|
|
117 |
private void initCircuitBreakers(ServletContext ctxt, ServiceConfig conf) { |
private void initCircuitBreakers(ServletContext ctxt, ServiceConfig conf) { |
118 |
CircuitBreakerManager mgr = CircuitBreakerManager.getManager(); |
CircuitBreakerManager mgr = CircuitBreakerManager.getManager(); |
119 |
|
|
120 |
AccountingCircuitBreaker osm = new AccountingCircuitBreaker("osm", conf); |
CircuitBreaker osm = new CircuitBreaker("osm", conf); |
121 |
|
osm.setStatistics( new AccountingStatistics() ); |
122 |
mgr.addCircuitBreaker( osm ); |
mgr.addCircuitBreaker( osm ); |
123 |
|
|
124 |
AccountingCircuitBreaker google = new AccountingCircuitBreaker("google", conf); |
CircuitBreaker google = new CircuitBreaker("google", conf); |
125 |
|
google.setStatistics( new AccountingStatistics() ); |
126 |
mgr.addCircuitBreaker( google ); |
mgr.addCircuitBreaker( google ); |
127 |
|
|
128 |
|
|