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 dk.daoas.daoadresseservice.admin.ServiceConfig; |
import dk.daoas.daoadresseservice.admin.ServiceConfig; |
16 |
|
import dk.thoerup.circuitbreaker.AccountingCircuitBreaker; |
17 |
|
import dk.thoerup.circuitbreaker.CircuitBreakerManager; |
18 |
|
import dk.thoerup.circuitbreaker.web.CircuitBreakerServletBase; |
19 |
import dk.thoerup.webconfig.ConfigServlet; |
import dk.thoerup.webconfig.ConfigServlet; |
20 |
import dk.thoerup.webconfig.ContextConfigLoader; |
import dk.thoerup.webconfig.PropertiesConfigLoader; |
21 |
|
|
22 |
/** |
/** |
23 |
* Application Lifecycle Listener implementation class ContextListener |
* Application Lifecycle Listener implementation class ContextListener |
26 |
@WebListener |
@WebListener |
27 |
public class ContextListener implements ServletContextListener { |
public class ContextListener implements ServletContextListener { |
28 |
|
|
29 |
|
|
30 |
|
|
31 |
|
|
32 |
public ContextListener() { |
public ContextListener() { |
43 |
|
|
44 |
ServletContext ctxt = ctxtEvt.getServletContext(); |
ServletContext ctxt = ctxtEvt.getServletContext(); |
45 |
|
|
46 |
initDataLoader(ctxt); |
ServiceConfig conf = null; |
47 |
initConfig(ctxt); |
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 |
|
|
54 |
|
initDataLoader(ctxt, conf); |
55 |
|
|
56 |
|
initCircuitBreakers(ctxt, conf); |
57 |
|
|
58 |
|
|
59 |
System.out.println("DAO Context done"); |
System.out.println("DAO Context done"); |
60 |
} |
} |
61 |
|
|
62 |
private void initDataLoader(ServletContext ctxt) { |
|
|
DataLoader load = new DataLoader(ctxt); |
|
|
ctxt.setAttribute("dataloader", load); |
|
|
load.doLoad(); |
|
|
} |
|
63 |
|
|
64 |
|
|
65 |
private void initConfig(ServletContext ctxt) { |
private ServiceConfig initConfig(ServletContext ctxt) throws IOException { |
66 |
|
|
67 |
|
final String CONF_FILE_NAME = "daoadresseservice.conf"; |
68 |
|
|
69 |
|
|
70 |
ServiceConfig conf = new ServiceConfig(); |
ServiceConfig conf = new ServiceConfig(); |
71 |
|
|
72 |
new ContextConfigLoader(ctxt).loadConfig(conf); |
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 |
|
|
85 |
|
new PropertiesConfigLoader(propsFile).loadConfig(conf); |
86 |
|
|
87 |
|
|
88 |
ctxt.setAttribute("config", conf); |
ctxt.setAttribute("config", conf); |
89 |
|
|
92 |
servlet.setConfigObject( conf ); |
servlet.setConfigObject( conf ); |
93 |
ServletRegistration.Dynamic dynconf = ctxt.addServlet("webconfig", servlet ); |
ServletRegistration.Dynamic dynconf = ctxt.addServlet("webconfig", servlet ); |
94 |
dynconf.addMapping("/ConfigServlet"); |
dynconf.addMapping("/ConfigServlet"); |
95 |
|
|
96 |
|
return conf; |
97 |
|
} |
98 |
|
|
99 |
|
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 |
|
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 |
|
|
124 |
} |
} |