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 |
2605 |
import com.jcabi.manifests.Manifests;
|
16 |
|
|
import com.jcabi.manifests.ServletMfs;
|
17 |
|
|
|
18 |
torben |
2346 |
import dk.daoas.daoadresseservice.admin.ServiceConfig;
|
19 |
torben |
2571 |
import dk.thoerup.circuitbreaker.CircuitBreaker;
|
20 |
torben |
2352 |
import dk.thoerup.circuitbreaker.CircuitBreakerManager;
|
21 |
torben |
2571 |
import dk.thoerup.circuitbreaker.statistics.AccountingStatistics;
|
22 |
torben |
2352 |
import dk.thoerup.circuitbreaker.web.CircuitBreakerServletBase;
|
23 |
torben |
2346 |
import dk.thoerup.webconfig.ConfigServlet;
|
24 |
torben |
2393 |
import dk.thoerup.webconfig.PropertiesConfigLoader;
|
25 |
torben |
2346 |
|
26 |
torben |
2274 |
/**
|
27 |
|
|
* Application Lifecycle Listener implementation class ContextListener
|
28 |
|
|
*
|
29 |
|
|
*/
|
30 |
|
|
@WebListener
|
31 |
|
|
public class ContextListener implements ServletContextListener {
|
32 |
torben |
2281 |
|
33 |
torben |
2393 |
|
34 |
torben |
2274 |
|
35 |
|
|
|
36 |
|
|
public ContextListener() {
|
37 |
|
|
// TODO Auto-generated constructor stub
|
38 |
|
|
}
|
39 |
|
|
|
40 |
|
|
public void contextDestroyed(ServletContextEvent ctxtEvt) {
|
41 |
|
|
// TODO Auto-generated method stub
|
42 |
|
|
}
|
43 |
|
|
|
44 |
torben |
2605 |
@SuppressWarnings("deprecation")
|
45 |
|
|
@Override
|
46 |
torben |
2274 |
public void contextInitialized(ServletContextEvent ctxtEvt) {
|
47 |
|
|
System.out.println("Starting DAO Context");
|
48 |
|
|
|
49 |
torben |
2605 |
|
50 |
|
|
|
51 |
torben |
2274 |
ServletContext ctxt = ctxtEvt.getServletContext();
|
52 |
torben |
2346 |
|
53 |
torben |
2393 |
ServiceConfig conf = null;
|
54 |
|
|
try {
|
55 |
|
|
conf = initConfig(ctxt);
|
56 |
|
|
} catch (IOException e) {
|
57 |
|
|
System.out.println("Error loading config " + e.getMessage());
|
58 |
|
|
throw new RuntimeException(e);
|
59 |
|
|
}
|
60 |
torben |
2346 |
|
61 |
torben |
2352 |
initDataLoader(ctxt, conf);
|
62 |
torben |
2351 |
|
63 |
torben |
2352 |
initCircuitBreakers(ctxt, conf);
|
64 |
torben |
2605 |
|
65 |
torben |
2351 |
|
66 |
torben |
2605 |
try {
|
67 |
|
|
Manifests.DEFAULT.append(new ServletMfs(ctxt));
|
68 |
|
|
} catch (Exception e) {
|
69 |
|
|
System.out.println("Error loading config " + e.getMessage());
|
70 |
|
|
}
|
71 |
torben |
2352 |
|
72 |
torben |
2346 |
System.out.println("DAO Context done");
|
73 |
|
|
}
|
74 |
|
|
|
75 |
torben |
2393 |
|
76 |
torben |
2346 |
|
77 |
|
|
|
78 |
torben |
2393 |
private ServiceConfig initConfig(ServletContext ctxt) throws IOException {
|
79 |
|
|
|
80 |
|
|
final String CONF_FILE_NAME = "daoadresseservice.conf";
|
81 |
|
|
|
82 |
|
|
|
83 |
torben |
2346 |
ServiceConfig conf = new ServiceConfig();
|
84 |
torben |
2274 |
|
85 |
torben |
2393 |
String file = null;
|
86 |
|
|
if (SystemUtils.IS_OS_WINDOWS) {
|
87 |
|
|
file = "C:\\" + CONF_FILE_NAME;
|
88 |
|
|
} else {
|
89 |
|
|
file = "/etc/" + CONF_FILE_NAME;
|
90 |
|
|
}
|
91 |
|
|
|
92 |
|
|
Properties propsFile = new Properties();
|
93 |
|
|
try (FileReader reader = new FileReader(file) ) {
|
94 |
|
|
propsFile.load(reader);
|
95 |
|
|
}
|
96 |
|
|
|
97 |
torben |
2274 |
|
98 |
torben |
2393 |
new PropertiesConfigLoader(propsFile).loadConfig(conf);
|
99 |
|
|
|
100 |
|
|
|
101 |
torben |
2346 |
ctxt.setAttribute("config", conf);
|
102 |
torben |
2337 |
|
103 |
torben |
2346 |
// /////////////////////////////////////
|
104 |
|
|
ConfigServlet servlet = new ConfigServlet();
|
105 |
|
|
servlet.setConfigObject( conf );
|
106 |
|
|
ServletRegistration.Dynamic dynconf = ctxt.addServlet("webconfig", servlet );
|
107 |
|
|
dynconf.addMapping("/ConfigServlet");
|
108 |
torben |
2351 |
|
109 |
torben |
2393 |
return conf;
|
110 |
torben |
2274 |
}
|
111 |
torben |
2346 |
|
112 |
torben |
2393 |
private void initDataLoader(ServletContext ctxt, ServiceConfig conf) {
|
113 |
|
|
DataLoader load = new DataLoader(ctxt,conf);
|
114 |
|
|
ctxt.setAttribute("dataloader", load);
|
115 |
|
|
load.doLoad();
|
116 |
|
|
}
|
117 |
|
|
|
118 |
torben |
2352 |
private void initCircuitBreakers(ServletContext ctxt, ServiceConfig conf) {
|
119 |
|
|
CircuitBreakerManager mgr = CircuitBreakerManager.getManager();
|
120 |
|
|
|
121 |
torben |
2571 |
CircuitBreaker osm = new CircuitBreaker("osm", conf);
|
122 |
|
|
osm.setStatistics( new AccountingStatistics() );
|
123 |
torben |
2352 |
mgr.addCircuitBreaker( osm );
|
124 |
|
|
|
125 |
torben |
2571 |
CircuitBreaker google = new CircuitBreaker("google", conf);
|
126 |
|
|
google.setStatistics( new AccountingStatistics() );
|
127 |
torben |
2352 |
mgr.addCircuitBreaker( google );
|
128 |
|
|
|
129 |
|
|
|
130 |
|
|
//
|
131 |
|
|
CircuitBreakerServletBase servlet = new CircuitBreakerServletBase();
|
132 |
|
|
ServletRegistration.Dynamic dynconf = ctxt.addServlet("circuitbreaker", servlet);
|
133 |
|
|
dynconf.addMapping("/CircuitBreakerServlet");
|
134 |
|
|
dynconf.setInitParameter("readonly", "1");
|
135 |
|
|
|
136 |
|
|
}
|
137 |
|
|
|
138 |
torben |
2274 |
|
139 |
|
|
}
|