1 |
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;
|
8 |
import javax.servlet.ServletContextEvent;
|
9 |
import javax.servlet.ServletContextListener;
|
10 |
import javax.servlet.ServletRegistration;
|
11 |
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;
|
19 |
import dk.thoerup.circuitbreaker.CircuitBreaker;
|
20 |
import dk.thoerup.circuitbreaker.CircuitBreakerManager;
|
21 |
import dk.thoerup.circuitbreaker.statistics.AccountingStatistics;
|
22 |
import dk.thoerup.circuitbreaker.web.CircuitBreakerServletBase;
|
23 |
import dk.thoerup.webconfig.ConfigServlet;
|
24 |
import dk.thoerup.webconfig.PropertiesConfigLoader;
|
25 |
|
26 |
/**
|
27 |
* Application Lifecycle Listener implementation class ContextListener
|
28 |
*
|
29 |
*/
|
30 |
@WebListener
|
31 |
public class ContextListener implements ServletContextListener {
|
32 |
|
33 |
|
34 |
|
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 |
@SuppressWarnings("deprecation")
|
45 |
@Override
|
46 |
public void contextInitialized(ServletContextEvent ctxtEvt) {
|
47 |
System.out.println("Starting DAO Context");
|
48 |
|
49 |
|
50 |
|
51 |
ServletContext ctxt = ctxtEvt.getServletContext();
|
52 |
|
53 |
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 |
|
61 |
initDataLoader(ctxt, conf);
|
62 |
|
63 |
initCircuitBreakers(ctxt, conf);
|
64 |
|
65 |
|
66 |
try {
|
67 |
Manifests.DEFAULT.append(new ServletMfs(ctxt));
|
68 |
} catch (Exception e) {
|
69 |
System.out.println("Error loading config " + e.getMessage());
|
70 |
}
|
71 |
|
72 |
System.out.println("DAO Context done");
|
73 |
}
|
74 |
|
75 |
|
76 |
|
77 |
|
78 |
private ServiceConfig initConfig(ServletContext ctxt) throws IOException {
|
79 |
|
80 |
final String CONF_FILE_NAME = "daoadresseservice.conf";
|
81 |
|
82 |
|
83 |
ServiceConfig conf = new ServiceConfig();
|
84 |
|
85 |
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 |
|
98 |
new PropertiesConfigLoader(propsFile).loadConfig(conf);
|
99 |
|
100 |
|
101 |
ctxt.setAttribute("config", conf);
|
102 |
|
103 |
// /////////////////////////////////////
|
104 |
ConfigServlet servlet = new ConfigServlet();
|
105 |
servlet.setConfigObject( conf );
|
106 |
ServletRegistration.Dynamic dynconf = ctxt.addServlet("webconfig", servlet );
|
107 |
dynconf.addMapping("/ConfigServlet");
|
108 |
|
109 |
return conf;
|
110 |
}
|
111 |
|
112 |
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 |
private void initCircuitBreakers(ServletContext ctxt, ServiceConfig conf) {
|
119 |
CircuitBreakerManager mgr = CircuitBreakerManager.getManager();
|
120 |
|
121 |
CircuitBreaker osm = new CircuitBreaker("osm", conf);
|
122 |
osm.setStatistics( new AccountingStatistics() );
|
123 |
mgr.addCircuitBreaker( osm );
|
124 |
|
125 |
CircuitBreaker google = new CircuitBreaker("google", conf);
|
126 |
google.setStatistics( new AccountingStatistics() );
|
127 |
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 |
|
139 |
}
|