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