/[projects]/dao/DaoAdresseVedligehold/src/main/java/dk/daoas/adressevedligehold/ContextListener.java
ViewVC logotype

Contents of /dao/DaoAdresseVedligehold/src/main/java/dk/daoas/adressevedligehold/ContextListener.java

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2903 - (show annotations) (download)
Wed Feb 3 18:45:33 2016 UTC (8 years, 3 months ago) by torben
File size: 2917 byte(s)
Implement logging system

Switch task to rest/jersey
1 package dk.daoas.adressevedligehold;
2
3 /*
4 * Denne Serivce bruger log4j og det kræver at tomcat leder efter log4j
5 * Tomcat 7 <7.0.43 ignores all JAR files named log4j*.jar, which prevents this feature from working.
6 * This has been fixed in Tomcat 7.0.43, Tomcat 8, and later.
7 * In Tomcat 7 <7.0.43 you will need to change catalina.properties and remove "log4j*.jar" from the jarsToSkip property.
8 * You may need to do something similar on other containers if they skip scanning Log4j JAR files.
9 *
10 */
11
12 import java.io.FileInputStream;
13 import java.io.IOException;
14 import java.io.InputStreamReader;
15 import java.io.Reader;
16 import java.nio.charset.Charset;
17 import java.util.Properties;
18
19 import javax.servlet.ServletContext;
20 import javax.servlet.ServletContextEvent;
21 import javax.servlet.ServletContextListener;
22 import javax.servlet.ServletRegistration;
23 import javax.servlet.annotation.WebListener;
24
25 import org.apache.commons.lang3.SystemUtils;
26
27 import dk.thoerup.webconfig.ConfigServlet;
28 import dk.thoerup.webconfig.PropertiesConfigLoader;
29
30 /**
31 * Application Lifecycle Listener implementation class ContextListener
32 *
33 */
34 @WebListener
35 public class ContextListener implements ServletContextListener {
36
37
38
39
40 public ContextListener() {
41 // TODO Auto-generated constructor stub
42 }
43
44 public void contextDestroyed(ServletContextEvent ctxtEvt) {
45 // TODO Auto-generated method stub
46 }
47
48 @Override
49 public void contextInitialized(ServletContextEvent ctxtEvt) {
50 System.out.println("Starting DAO Context");
51
52
53
54 ServletContext ctxt = ctxtEvt.getServletContext();
55
56 try {
57 initConfig(ctxt);
58 } catch (IOException e) {
59 System.out.println("Error loading config " + e.getMessage());
60 throw new RuntimeException(e);
61 }
62
63
64 System.out.println("DAO Context done");
65 }
66
67
68
69
70 private ServiceConfig initConfig(ServletContext ctxt) throws IOException {
71
72 final String CONF_FILE_NAME = "daoadressevedligehold.conf";
73
74
75 ServiceConfig conf = ServiceConfig.getInstance();
76
77 String file = null;
78 if (SystemUtils.IS_OS_WINDOWS) {
79 file = "C:\\" + CONF_FILE_NAME;
80 } else {
81 file = "/etc/" + CONF_FILE_NAME;
82 }
83
84 Properties propsFile = new Properties();
85 try ( Reader reader = new InputStreamReader(new FileInputStream(file), Charset.forName("UTF-8")) ) {
86 propsFile.load(reader);
87 }
88
89
90 new PropertiesConfigLoader(propsFile).loadConfig(conf);
91
92
93 ctxt.setAttribute("config", conf);
94
95 // /////////////////////////////////////
96 ConfigServlet servlet = new ConfigServlet();
97 servlet.setConfigObject( conf );
98 ServletRegistration.Dynamic dynconf = ctxt.addServlet("webconfig", servlet );
99 dynconf.addMapping("/ConfigServlet");
100
101 return conf;
102 }
103
104
105 }

  ViewVC Help
Powered by ViewVC 1.1.20