/[projects]/dao/DaoAdresseService/src/main/java/dk/daoas/daoadresseservice/ContextListener.java
ViewVC logotype

Annotation of /dao/DaoAdresseService/src/main/java/dk/daoas/daoadresseservice/ContextListener.java

Parent Directory Parent Directory | Revision Log Revision Log


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

  ViewVC Help
Powered by ViewVC 1.1.20