1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
19 |
|
|
20 |
|
|
21 |
|
|
22 |
|
package net.sf.infrared.agent.setup; |
23 |
|
|
24 |
|
import java.net.InetAddress; |
25 |
|
import java.net.UnknownHostException; |
26 |
|
|
27 |
|
import javax.servlet.ServletContext; |
28 |
|
import javax.servlet.ServletContextEvent; |
29 |
|
import javax.servlet.ServletContextListener; |
30 |
|
|
31 |
|
import net.sf.infrared.agent.MonitorConfigImpl; |
32 |
|
import net.sf.infrared.base.util.LoggingFactory; |
33 |
|
|
34 |
|
import org.apache.log4j.Logger; |
35 |
|
|
36 |
|
|
37 |
|
|
38 |
|
|
39 |
|
public class InfraREDServletContextListener implements ServletContextListener { |
40 |
|
public static final String KEY_CONFIGURATIONPROVIDER = "net.sf.infrared.configurationprovider"; |
41 |
|
|
42 |
1 |
private static final Logger log = |
43 |
1 |
LoggingFactory.getLogger(InfraREDServletContextListener.class); |
44 |
|
|
45 |
|
private InfraREDLifeCycleListener lifeCycleListener; |
46 |
|
|
47 |
1 |
public InfraREDServletContextListener() { |
48 |
1 |
lifeCycleListener = new InfraREDLifeCycleListener(); |
49 |
1 |
} |
50 |
|
|
51 |
|
public void contextInitialized(ServletContextEvent event) { |
52 |
0 |
ServletContext context = event.getServletContext(); |
53 |
0 |
initialized(context); |
54 |
0 |
} |
55 |
|
|
56 |
|
public void contextDestroyed(ServletContextEvent event) { |
57 |
0 |
destroyed(event.getServletContext()); |
58 |
0 |
} |
59 |
|
|
60 |
|
public void initialized(ServletContext context) { |
61 |
1 |
String configProvider = |
62 |
|
(String) context.getAttribute(KEY_CONFIGURATIONPROVIDER); |
63 |
1 |
if (configProvider == null) { |
64 |
1 |
configProvider = MonitorConfigImpl.DEFAULT_CONFIG_LOCATION; |
65 |
|
} |
66 |
|
|
67 |
1 |
String appName = context.getServletContextName(); |
68 |
1 |
if (appName == null) { |
69 |
1 |
appName = "unknown"; |
70 |
1 |
log.info("Application name is not set in the war"); |
71 |
|
} |
72 |
1 |
getLifeCycleListener().initialized(appName, getInstanceId(), configProvider); |
73 |
1 |
} |
74 |
|
|
75 |
|
public void destroyed(ServletContext context) { |
76 |
0 |
getLifeCycleListener().destroyed(context.getServletContextName()); |
77 |
0 |
} |
78 |
|
|
79 |
|
public String getInstanceId() { |
80 |
1 |
String instanceId = "unknown"; |
81 |
|
try { |
82 |
1 |
instanceId = InetAddress.getLocalHost().getHostName(); |
83 |
0 |
} catch (UnknownHostException e) { |
84 |
0 |
log.error(e); |
85 |
1 |
} |
86 |
1 |
return instanceId; |
87 |
|
} |
88 |
|
|
89 |
|
InfraREDLifeCycleListener getLifeCycleListener() { |
90 |
0 |
return this.lifeCycleListener; |
91 |
|
} |
92 |
|
} |