1 |
package dk.daoas.daoadresseservice; |
package dk.daoas.daoadresseservice; |
2 |
|
|
3 |
import java.sql.SQLException; |
import java.io.FileInputStream; |
4 |
|
import java.io.IOException; |
5 |
|
import java.io.InputStreamReader; |
6 |
|
import java.io.Reader; |
7 |
|
import java.nio.charset.Charset; |
8 |
|
import java.util.Properties; |
9 |
|
|
10 |
import javax.servlet.ServletContext; |
import javax.servlet.ServletContext; |
11 |
import javax.servlet.ServletContextEvent; |
import javax.servlet.ServletContextEvent; |
12 |
import javax.servlet.ServletContextListener; |
import javax.servlet.ServletContextListener; |
13 |
|
import javax.servlet.ServletRegistration; |
14 |
import javax.servlet.annotation.WebListener; |
import javax.servlet.annotation.WebListener; |
15 |
|
|
16 |
|
import org.apache.commons.lang3.SystemUtils; |
17 |
|
|
18 |
|
import com.jcabi.manifests.Manifests; |
19 |
|
import com.jcabi.manifests.ServletMfs; |
20 |
|
|
21 |
|
import dk.daoas.daoadresseservice.admin.ServiceConfig; |
22 |
|
import dk.thoerup.circuitbreaker.CircuitBreaker; |
23 |
|
import dk.thoerup.circuitbreaker.CircuitBreakerManager; |
24 |
|
import dk.thoerup.circuitbreaker.statistics.AccountingStatistics; |
25 |
|
import dk.thoerup.circuitbreaker.web.CircuitBreakerServletBase; |
26 |
|
import dk.thoerup.webconfig.ConfigServlet; |
27 |
|
import dk.thoerup.webconfig.PropertiesConfigLoader; |
28 |
|
|
29 |
/** |
/** |
30 |
* Application Lifecycle Listener implementation class ContextListener |
* Application Lifecycle Listener implementation class ContextListener |
31 |
* |
* |
33 |
@WebListener |
@WebListener |
34 |
public class ContextListener implements ServletContextListener { |
public class ContextListener implements ServletContextListener { |
35 |
|
|
36 |
public static class DataLoader implements Runnable { |
|
|
ServletContext ctxt; |
|
|
public DataLoader(ServletContext ctxt) { |
|
|
this.ctxt = ctxt; |
|
|
} |
|
|
|
|
|
|
|
|
@Override |
|
|
public void run() { |
|
|
|
|
|
AdressSearch search = new AdressSearch(); |
|
|
try { |
|
|
search.buildSearchStructures(); |
|
|
ctxt.setAttribute("search", search); |
|
|
} catch (SQLException e) { |
|
|
System.out.println("Error loading data: " + e.getMessage()); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
} |
|
37 |
|
|
38 |
|
|
39 |
public ContextListener() { |
public ContextListener() { |
44 |
// TODO Auto-generated method stub |
// TODO Auto-generated method stub |
45 |
} |
} |
46 |
|
|
47 |
|
@Override |
48 |
public void contextInitialized(ServletContextEvent ctxtEvt) { |
public void contextInitialized(ServletContextEvent ctxtEvt) { |
49 |
System.out.println("Starting DAO Context"); |
System.out.println("Starting DAO Context"); |
50 |
|
|
51 |
|
|
52 |
|
|
53 |
ServletContext ctxt = ctxtEvt.getServletContext(); |
ServletContext ctxt = ctxtEvt.getServletContext(); |
54 |
|
|
55 |
Thread t = new Thread( new DataLoader(ctxt), "SearchDataLoader"); |
ServiceConfig conf = null; |
56 |
t.start(); |
try { |
57 |
|
conf = initConfig(ctxt); |
58 |
|
} catch (IOException e) { |
59 |
|
System.out.println("Error loading config " + e.getMessage()); |
60 |
|
throw new RuntimeException(e); |
61 |
|
} |
62 |
|
|
63 |
|
initDataLoader(ctxt, conf); |
64 |
|
|
65 |
|
initCircuitBreakers(ctxt, conf); |
66 |
|
|
67 |
|
|
68 |
|
try { |
69 |
|
Manifests.DEFAULT.append(new ServletMfs(ctxt)); |
70 |
|
} catch (Exception e) { |
71 |
|
System.out.println("Error loading config " + e.getMessage()); |
72 |
|
} |
73 |
|
|
74 |
System.out.println("DAO Context done"); |
System.out.println("DAO Context done"); |
75 |
} |
} |
76 |
|
|
77 |
|
|
78 |
|
|
79 |
|
|
80 |
|
private ServiceConfig initConfig(ServletContext ctxt) throws IOException { |
81 |
|
|
82 |
|
final String CONF_FILE_NAME = "daoadresseservice.conf"; |
83 |
|
|
84 |
|
|
85 |
|
ServiceConfig conf = new ServiceConfig(); |
86 |
|
|
87 |
|
String file = null; |
88 |
|
if (SystemUtils.IS_OS_WINDOWS) { |
89 |
|
file = "C:\\" + CONF_FILE_NAME; |
90 |
|
} else { |
91 |
|
file = "/etc/" + CONF_FILE_NAME; |
92 |
|
} |
93 |
|
|
94 |
|
Properties propsFile = new Properties(); |
95 |
|
try ( Reader reader = new InputStreamReader(new FileInputStream(file), Charset.forName("UTF-8")) ) { |
96 |
|
propsFile.load(reader); |
97 |
|
} |
98 |
|
|
99 |
|
|
100 |
|
new PropertiesConfigLoader(propsFile).loadConfig(conf); |
101 |
|
|
102 |
|
|
103 |
|
ctxt.setAttribute("config", conf); |
104 |
|
|
105 |
|
// ///////////////////////////////////// |
106 |
|
ConfigServlet servlet = new ConfigServlet(); |
107 |
|
servlet.setConfigObject( conf ); |
108 |
|
ServletRegistration.Dynamic dynconf = ctxt.addServlet("webconfig", servlet ); |
109 |
|
dynconf.addMapping("/ConfigServlet"); |
110 |
|
|
111 |
|
return conf; |
112 |
|
} |
113 |
|
|
114 |
|
private void initDataLoader(ServletContext ctxt, ServiceConfig conf) { |
115 |
|
DataLoader load = new DataLoader(ctxt,conf); |
116 |
|
ctxt.setAttribute("dataloader", load); |
117 |
|
load.doLoad( "(re) deploy" ); |
118 |
|
} |
119 |
|
|
120 |
|
private void initCircuitBreakers(ServletContext ctxt, ServiceConfig conf) { |
121 |
|
CircuitBreakerManager mgr = CircuitBreakerManager.getManager(); |
122 |
|
|
123 |
|
CircuitBreaker osm = new CircuitBreaker("osm", conf); |
124 |
|
osm.setStatistics( new AccountingStatistics() ); |
125 |
|
mgr.addCircuitBreaker( osm ); |
126 |
|
|
127 |
|
CircuitBreaker google = new CircuitBreaker("google", conf); |
128 |
|
google.setStatistics( new AccountingStatistics() ); |
129 |
|
mgr.addCircuitBreaker( google ); |
130 |
|
|
131 |
|
|
132 |
|
// |
133 |
|
CircuitBreakerServletBase servlet = new CircuitBreakerServletBase(); |
134 |
|
ServletRegistration.Dynamic dynconf = ctxt.addServlet("circuitbreaker", servlet); |
135 |
|
dynconf.addMapping("/CircuitBreakerServlet"); |
136 |
|
dynconf.setInitParameter("readonly", "1"); |
137 |
|
|
138 |
|
} |
139 |
|
|
140 |
|
|
141 |
} |
} |