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 |
public void contextDestroyed(ServletContextEvent ctxtEvt) { |
40 |
} |
41 |
|
42 |
@Override |
43 |
public void contextInitialized(ServletContextEvent ctxtEvt) { |
44 |
System.out.println("Starting DAO Context"); |
45 |
|
46 |
|
47 |
|
48 |
ServletContext ctxt = ctxtEvt.getServletContext(); |
49 |
|
50 |
try { |
51 |
initConfig(ctxt); |
52 |
} catch (IOException e) { |
53 |
System.out.println("Error loading config " + e.getMessage()); |
54 |
throw new RuntimeException(e); |
55 |
} |
56 |
|
57 |
|
58 |
System.out.println("DAO Context done"); |
59 |
} |
60 |
|
61 |
|
62 |
|
63 |
|
64 |
private ServiceConfig initConfig(ServletContext ctxt) throws IOException { |
65 |
|
66 |
final String CONF_FILE_NAME = "daoadressevedligehold.conf"; |
67 |
|
68 |
|
69 |
ServiceConfig conf = ServiceConfig.getInstance(); |
70 |
|
71 |
String file = null; |
72 |
if (SystemUtils.IS_OS_WINDOWS) { |
73 |
file = "C:\\" + CONF_FILE_NAME; |
74 |
} else { |
75 |
file = "/etc/" + CONF_FILE_NAME; |
76 |
} |
77 |
|
78 |
Properties propsFile = new Properties(); |
79 |
try ( Reader reader = new InputStreamReader(new FileInputStream(file), Charset.forName("UTF-8")) ) { |
80 |
propsFile.load(reader); |
81 |
} |
82 |
|
83 |
|
84 |
new PropertiesConfigLoader(propsFile).loadConfig(conf); |
85 |
|
86 |
|
87 |
ctxt.setAttribute("config", conf); |
88 |
|
89 |
// ///////////////////////////////////// |
90 |
ConfigServlet servlet = new ConfigServlet(); |
91 |
servlet.setConfigObject( conf ); |
92 |
ServletRegistration.Dynamic dynconf = ctxt.addServlet("webconfig", servlet ); |
93 |
dynconf.addMapping("/ConfigServlet"); |
94 |
|
95 |
return conf; |
96 |
} |
97 |
|
98 |
|
99 |
} |