1 |
torben |
2274 |
package dk.daoas.daoadresseservice;
|
2 |
|
|
|
3 |
|
|
import javax.servlet.ServletContext;
|
4 |
|
|
import javax.servlet.ServletContextEvent;
|
5 |
|
|
import javax.servlet.ServletContextListener;
|
6 |
torben |
2346 |
import javax.servlet.ServletRegistration;
|
7 |
torben |
2274 |
import javax.servlet.annotation.WebListener;
|
8 |
|
|
|
9 |
torben |
2346 |
import dk.daoas.daoadresseservice.admin.ServiceConfig;
|
10 |
torben |
2352 |
import dk.thoerup.circuitbreaker.AccountingCircuitBreaker;
|
11 |
|
|
import dk.thoerup.circuitbreaker.CircuitBreakerManager;
|
12 |
|
|
import dk.thoerup.circuitbreaker.web.CircuitBreakerServletBase;
|
13 |
torben |
2346 |
import dk.thoerup.webconfig.ConfigServlet;
|
14 |
|
|
import dk.thoerup.webconfig.ContextConfigLoader;
|
15 |
|
|
|
16 |
torben |
2274 |
/**
|
17 |
|
|
* Application Lifecycle Listener implementation class ContextListener
|
18 |
|
|
*
|
19 |
|
|
*/
|
20 |
|
|
@WebListener
|
21 |
|
|
public class ContextListener implements ServletContextListener {
|
22 |
torben |
2281 |
|
23 |
torben |
2274 |
|
24 |
|
|
|
25 |
torben |
2281 |
|
26 |
torben |
2274 |
public ContextListener() {
|
27 |
|
|
// TODO Auto-generated constructor stub
|
28 |
|
|
}
|
29 |
|
|
|
30 |
|
|
public void contextDestroyed(ServletContextEvent ctxtEvt) {
|
31 |
|
|
// TODO Auto-generated method stub
|
32 |
|
|
}
|
33 |
|
|
|
34 |
torben |
2346 |
@Override
|
35 |
torben |
2274 |
public void contextInitialized(ServletContextEvent ctxtEvt) {
|
36 |
|
|
System.out.println("Starting DAO Context");
|
37 |
|
|
|
38 |
|
|
ServletContext ctxt = ctxtEvt.getServletContext();
|
39 |
torben |
2346 |
|
40 |
torben |
2351 |
ServiceConfig conf = initConfig(ctxt);
|
41 |
torben |
2346 |
|
42 |
torben |
2352 |
initDataLoader(ctxt, conf);
|
43 |
torben |
2351 |
|
44 |
torben |
2352 |
initCircuitBreakers(ctxt, conf);
|
45 |
torben |
2351 |
|
46 |
torben |
2352 |
|
47 |
torben |
2346 |
System.out.println("DAO Context done");
|
48 |
|
|
}
|
49 |
|
|
|
50 |
torben |
2351 |
private void initDataLoader(ServletContext ctxt, ServiceConfig conf) {
|
51 |
|
|
DataLoader load = new DataLoader(ctxt,conf);
|
52 |
torben |
2337 |
ctxt.setAttribute("dataloader", load);
|
53 |
|
|
load.doLoad();
|
54 |
torben |
2346 |
}
|
55 |
|
|
|
56 |
|
|
|
57 |
torben |
2351 |
private ServiceConfig initConfig(ServletContext ctxt) {
|
58 |
torben |
2346 |
ServiceConfig conf = new ServiceConfig();
|
59 |
torben |
2274 |
|
60 |
torben |
2346 |
new ContextConfigLoader(ctxt).loadConfig(conf);
|
61 |
torben |
2274 |
|
62 |
torben |
2346 |
ctxt.setAttribute("config", conf);
|
63 |
torben |
2337 |
|
64 |
torben |
2346 |
// /////////////////////////////////////
|
65 |
|
|
ConfigServlet servlet = new ConfigServlet();
|
66 |
|
|
servlet.setConfigObject( conf );
|
67 |
|
|
ServletRegistration.Dynamic dynconf = ctxt.addServlet("webconfig", servlet );
|
68 |
|
|
dynconf.addMapping("/ConfigServlet");
|
69 |
torben |
2351 |
|
70 |
|
|
return conf;
|
71 |
|
|
|
72 |
torben |
2274 |
}
|
73 |
torben |
2346 |
|
74 |
torben |
2352 |
private void initCircuitBreakers(ServletContext ctxt, ServiceConfig conf) {
|
75 |
|
|
CircuitBreakerManager mgr = CircuitBreakerManager.getManager();
|
76 |
|
|
|
77 |
|
|
AccountingCircuitBreaker osm = new AccountingCircuitBreaker("osm", conf);
|
78 |
|
|
mgr.addCircuitBreaker( osm );
|
79 |
|
|
|
80 |
|
|
AccountingCircuitBreaker google = new AccountingCircuitBreaker("google", conf);
|
81 |
|
|
mgr.addCircuitBreaker( google );
|
82 |
|
|
|
83 |
|
|
|
84 |
|
|
//
|
85 |
|
|
CircuitBreakerServletBase servlet = new CircuitBreakerServletBase();
|
86 |
|
|
ServletRegistration.Dynamic dynconf = ctxt.addServlet("circuitbreaker", servlet);
|
87 |
|
|
dynconf.addMapping("/CircuitBreakerServlet");
|
88 |
|
|
dynconf.setInitParameter("readonly", "1");
|
89 |
|
|
|
90 |
|
|
}
|
91 |
|
|
|
92 |
torben |
2274 |
|
93 |
|
|
}
|