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 javax.management.InstanceAlreadyExistsException; |
25 |
|
import javax.management.InstanceNotFoundException; |
26 |
|
import javax.management.MBeanRegistrationException; |
27 |
|
import javax.management.MBeanServer; |
28 |
|
import javax.management.MalformedObjectNameException; |
29 |
|
import javax.management.NotCompliantMBeanException; |
30 |
|
import javax.management.ObjectName; |
31 |
|
|
32 |
|
import net.sf.infrared.agent.MonitorConfig; |
33 |
|
import net.sf.infrared.agent.MonitorConfigImpl; |
34 |
|
import net.sf.infrared.agent.MonitorFacade; |
35 |
|
import net.sf.infrared.agent.MonitorFacadeImpl; |
36 |
|
import net.sf.infrared.agent.MonitorFactory; |
37 |
|
import net.sf.infrared.agent.MultipleEntryGuard; |
38 |
|
import net.sf.infrared.agent.configmgmt.InfraredProperties; |
39 |
|
import net.sf.infrared.base.configmgmt.AbstractMBeanServerFactory; |
40 |
|
import net.sf.infrared.base.util.LoggingFactory; |
41 |
|
|
42 |
|
import org.apache.log4j.Logger; |
43 |
|
|
44 |
|
|
45 |
|
|
46 |
|
|
47 |
|
public class InfraREDLifeCycleListener { |
48 |
|
|
49 |
4 |
private static final Logger log = LoggingFactory.getLogger(InfraREDLifeCycleListener.class); |
50 |
|
|
51 |
|
private static final String KEY_MBEAN_SERVER_PROVIDER = "mbean-server-provider"; |
52 |
|
|
53 |
3 |
private MBeanServer mbeanServer = null; |
54 |
|
|
55 |
|
private ObjectName propertiesObjectName; |
56 |
|
|
57 |
3 |
private MonitorFacade facade = null; |
58 |
|
|
59 |
|
|
60 |
|
|
61 |
|
|
62 |
3 |
public InfraREDLifeCycleListener() { |
63 |
3 |
} |
64 |
|
|
65 |
|
|
66 |
|
|
67 |
|
|
68 |
|
|
69 |
|
|
70 |
|
|
71 |
|
|
72 |
|
|
73 |
|
|
74 |
|
|
75 |
|
public void initialized(String applicationName, String instanceId, String configProvider) { |
76 |
1 |
MonitorConfig config = new MonitorConfigImpl(configProvider); |
77 |
|
|
78 |
1 |
facade = new MonitorFacadeImpl(applicationName, instanceId, config, true); |
79 |
1 |
facade = new MultipleEntryGuard(facade); |
80 |
|
|
81 |
1 |
MonitorFactory.registerFacadeImpl(facade); |
82 |
1 |
if (log.isDebugEnabled()) { |
83 |
0 |
log.debug("Initializing application : " + applicationName + " InstanceId : " |
84 |
|
+ instanceId); |
85 |
|
} |
86 |
|
|
87 |
|
|
88 |
1 |
setMBeanServer(config, applicationName, instanceId); |
89 |
1 |
registerMBeans(config); |
90 |
1 |
} |
91 |
|
|
92 |
|
|
93 |
|
|
94 |
|
|
95 |
|
|
96 |
|
|
97 |
|
public void destroyed(String applicationName) { |
98 |
|
|
99 |
1 |
unregisterMBeans(); |
100 |
|
|
101 |
|
|
102 |
|
|
103 |
1 |
MonitorFacade facade = MonitorFactory.unregisterFacadeImpl(); |
104 |
1 |
if (facade != null) { |
105 |
1 |
facade.destroy(); |
106 |
|
} |
107 |
1 |
if (log.isDebugEnabled()) { |
108 |
0 |
log.debug("Destroying application " + applicationName); |
109 |
|
} |
110 |
1 |
} |
111 |
|
|
112 |
|
|
113 |
|
|
114 |
|
|
115 |
|
|
116 |
|
public void registerMBeans(MonitorConfig config) { |
117 |
1 |
if (mbeanServer != null) { |
118 |
0 |
InfraredProperties infraredProperties = new InfraredProperties(config); |
119 |
|
try { |
120 |
0 |
String objectName = "InfraredProperties:name=Infrared Properties," |
121 |
|
+ "type=InfraredPropertiesMBean"; |
122 |
0 |
propertiesObjectName = new ObjectName(objectName); |
123 |
0 |
mbeanServer.registerMBean(infraredProperties, propertiesObjectName); |
124 |
0 |
log.debug("The MBean has been successfully registered !"); |
125 |
0 |
} catch (MalformedObjectNameException e) { |
126 |
0 |
log.error("The MBean Object name is Malformed", e); |
127 |
0 |
} catch (InstanceAlreadyExistsException e) { |
128 |
0 |
log.error("The MBean instance already exists", e); |
129 |
0 |
} catch (MBeanRegistrationException e) { |
130 |
0 |
log.error("The MBean Registration failed", e); |
131 |
0 |
} catch (NotCompliantMBeanException e) { |
132 |
0 |
log.error("The MBean is not Compliant & Registration failed", e); |
133 |
0 |
} |
134 |
|
} |
135 |
1 |
} |
136 |
|
|
137 |
|
public void unregisterMBeans() { |
138 |
1 |
if (mbeanServer == null) { |
139 |
1 |
return; |
140 |
|
} |
141 |
|
|
142 |
|
try { |
143 |
0 |
mbeanServer.unregisterMBean(propertiesObjectName); |
144 |
0 |
} catch (MBeanRegistrationException e) { |
145 |
0 |
log.error("Unable to unregister the MBean", e); |
146 |
0 |
} catch (InstanceNotFoundException e) { |
147 |
0 |
log.error("The MBean instance is not registered in the MBeanServer", e); |
148 |
0 |
} |
149 |
0 |
} |
150 |
|
|
151 |
|
public MonitorFacade getFacade() { |
152 |
0 |
return facade; |
153 |
|
} |
154 |
|
|
155 |
|
private void setMBeanServer(MonitorConfig config, String appName, String instanceId) { |
156 |
1 |
String mbeanServerClass = config.getProperty(KEY_MBEAN_SERVER_PROVIDER, (String) null); |
157 |
1 |
if (mbeanServerClass == null) { |
158 |
1 |
log.warn("No MBean Server Class set for application " + appName |
159 |
|
+ ", instance " + instanceId); |
160 |
1 |
return; |
161 |
|
} |
162 |
|
try { |
163 |
0 |
AbstractMBeanServerFactory mbeanServerProvider = |
164 |
|
(AbstractMBeanServerFactory) Class.forName(mbeanServerClass).newInstance(); |
165 |
0 |
mbeanServer = mbeanServerProvider.getMBeanServer(); |
166 |
0 |
} catch (Exception e) { |
167 |
0 |
log.error("Unable to load MBeanServer provider", e); |
168 |
0 |
} |
169 |
0 |
} |
170 |
|
} |