1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package ch.elca.el4j.services.monitoring.jmx;
19
20 import java.lang.management.ManagementFactory;
21 import java.util.ArrayList;
22 import java.util.HashMap;
23 import java.util.List;
24 import java.util.Map;
25 import java.util.Set;
26
27 import javax.management.InstanceAlreadyExistsException;
28 import javax.management.MBeanRegistrationException;
29 import javax.management.MBeanServer;
30 import javax.management.MalformedObjectNameException;
31 import javax.management.NotCompliantMBeanException;
32 import javax.management.ObjectInstance;
33 import javax.management.ObjectName;
34
35 import org.slf4j.Logger;
36 import org.slf4j.LoggerFactory;
37 import org.springframework.beans.factory.InitializingBean;
38 import org.springframework.context.ApplicationContext;
39 import org.springframework.context.ApplicationContextAware;
40 import org.springframework.context.ApplicationEvent;
41 import org.springframework.context.ApplicationListener;
42 import org.springframework.context.ConfigurableApplicationContext;
43 import org.springframework.context.event.ContextRefreshedEvent;
44
45 import ch.elca.el4j.core.exceptions.BaseException;
46 import ch.elca.el4j.core.exceptions.BaseRTException;
47 import ch.elca.el4j.services.monitoring.notification.CoreNotificationHelper;
48 import ch.elca.el4j.util.codingsupport.Reject;
49
50
51
52
53
54
55
56
57
58
59
60 public class Loader implements ApplicationContextAware, InitializingBean,
61 ApplicationListener {
62
63
64
65
66 protected static final Map<MBeanServer, JvmMBMBean> s_jvmMBs
67 = new HashMap<MBeanServer, JvmMBMBean>();
68
69
70
71
72 private static Logger s_logger = LoggerFactory.getLogger(Loader.class);
73
74
75
76
77
78 protected ApplicationContextMB m_acMB;
79
80
81
82
83 protected JvmMB m_jvmmb;
84
85
86
87
88 private MBeanServer m_server;
89
90
91
92
93 private ConfigurableApplicationContext m_applicationContext;
94
95
96
97
98
99 private boolean m_initAfterPropertiesSet = false;
100
101
102
103
104
105
106 public MBeanServer getServer() {
107 return m_server;
108 }
109
110
111
112
113
114
115
116 public void setServer(MBeanServer server) {
117 this.m_server = server;
118 }
119
120
121
122
123
124
125
126
127
128
129 protected void setJvmMB() throws BaseException {
130 String jreVersion = System.getProperty("java.version");
131
132
133
134 synchronized (Loader.class) {
135
136
137 ObjectName[] jvms = getObjectNames(this.m_server, JvmMB.JVM_DOMAIN,
138 null, null);
139
140
141
142
143
144
145
146 int numberOfMBeans = 0;
147
148 for (int i = 0; i < jvms.length; i++) {
149 if (!jvms[i].getCanonicalName().contains("log4jConfig")) {
150 numberOfMBeans++;
151 }
152 }
153
154
155
156 if (numberOfMBeans == 0) {
157
158
159
160 if (jreVersion.startsWith("1.5") || jreVersion.startsWith("1.6")
161 || jreVersion.startsWith("1.7")) {
162 s_logger.info("Registering Jdk 1.5 Mbean");
163 registerJdk15Mbean(m_server);
164 }
165
166 m_jvmmb = new JvmMB();
167 s_jvmMBs.put(m_server, m_jvmmb);
168 try {
169 m_server.registerMBean(m_jvmmb, m_jvmmb.getObjectName());
170 } catch (InstanceAlreadyExistsException e) {
171 String message = "The MBean is already under the "
172 + "control of the MBean server.";
173 s_logger.error(message);
174 throw new BaseException(message, e);
175 } catch (MBeanRegistrationException e) {
176 String message = "The MBean will not be registered.";
177 s_logger.error(message);
178 throw new BaseException(message, e);
179 } catch (NotCompliantMBeanException e) {
180 String message = "This object is not a JMX compliant"
181 + " MBean.";
182 s_logger.error(message);
183 throw new BaseException(message, e);
184 }
185 } else if (numberOfMBeans == 1) {
186
187
188 m_jvmmb = (JvmMB) s_jvmMBs.get(m_server);
189 if (m_jvmmb == null) {
190 CoreNotificationHelper
191 .notifyMisconfiguration("Error in Mapping: "
192 + "No JVM defined on this MBean Server.");
193 }
194 } else {
195
196
197 String message = "There is more than 1 JVM defined on this"
198 + " MBean Server.";
199 s_logger.error(message);
200 throw new BaseRTException(message, (Object[]) null);
201 }
202 }
203 }
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219 public ObjectName[] getObjectNames(MBeanServer server, String domain,
220 String keyProperty, String value) {
221
222
223 Set mBeansSet = server.queryMBeans(null, null);
224
225 Reject.ifNull(mBeansSet, "The 'queryMBeans(ObjectName, QueryExp)' "
226 + "method on the MBeanServer returned null.");
227
228 List<ObjectName> relatedBeans = new ArrayList<ObjectName>();
229
230 for (Object oi : mBeansSet) {
231 ObjectInstance objectInstance = (ObjectInstance) oi;
232 ObjectName objectName = objectInstance.getObjectName();
233
234
235
236 if (objectName.getDomain().equals(domain)) {
237 if (keyProperty != null) {
238 if (objectName.getKeyProperty(keyProperty).equals(value)) {
239 relatedBeans.add(objectName);
240 }
241 } else {
242 relatedBeans.add(objectName);
243 }
244 }
245 }
246
247 return relatedBeans.toArray(new ObjectName[relatedBeans.size()]);
248 }
249
250
251
252
253
254
255
256
257 public void setApplicationContext(ApplicationContext applicationContext) {
258 Reject.ifNotAssignableTo(applicationContext,
259 ConfigurableApplicationContext.class);
260 m_applicationContext
261 = (ConfigurableApplicationContext) applicationContext;
262
263 }
264
265
266
267
268
269
270 public ConfigurableApplicationContext getApplicationContext() {
271 return m_applicationContext;
272 }
273
274
275
276
277 public final boolean isInitAfterPropertiesSet() {
278 return m_initAfterPropertiesSet;
279 }
280
281
282
283
284 public final void setInitAfterPropertiesSet(
285 boolean initAfterPropertiesSet) {
286 m_initAfterPropertiesSet = initAfterPropertiesSet;
287 }
288
289
290
291
292
293
294 public void afterPropertiesSet() throws BaseException {
295 if (isInitAfterPropertiesSet()) {
296 init();
297 }
298 }
299
300
301
302
303
304
305 public void onApplicationEvent(ApplicationEvent event) {
306 if (event instanceof ContextRefreshedEvent
307 && ((ContextRefreshedEvent) event).getSource()
308 == getApplicationContext()) {
309 try {
310 init();
311 } catch (BaseException e) {
312 throw new RuntimeException(e);
313 }
314 }
315 }
316
317
318
319
320
321
322
323 protected void init() throws BaseException {
324
325 setJvmMB();
326
327
328 m_acMB = new ApplicationContextMB(m_applicationContext,
329 m_applicationContext.getBeanFactory(),
330 m_server, m_jvmmb);
331
332
333 m_acMB.init();
334 }
335
336
337
338
339 protected static void registerJdk15Mbean(MBeanServer ms) {
340 try {
341 ms.registerMBean(ManagementFactory.getClassLoadingMXBean(),
342 new ObjectName(ManagementFactory.CLASS_LOADING_MXBEAN_NAME));
343
344 ms.registerMBean(ManagementFactory.getThreadMXBean(),
345 new ObjectName(ManagementFactory.THREAD_MXBEAN_NAME));
346
347 ms.registerMBean(ManagementFactory.getRuntimeMXBean(),
348 new ObjectName(ManagementFactory.RUNTIME_MXBEAN_NAME));
349 ms.registerMBean(ManagementFactory.getOperatingSystemMXBean(),
350 new ObjectName(ManagementFactory.OPERATING_SYSTEM_MXBEAN_NAME));
351 ms.registerMBean(ManagementFactory.getMemoryMXBean(),
352 new ObjectName(ManagementFactory.MEMORY_MXBEAN_NAME));
353 ms.registerMBean(ManagementFactory.getCompilationMXBean(),
354 new ObjectName(ManagementFactory.COMPILATION_MXBEAN_NAME));
355
356 int i = 0;
357 for (Object o : ManagementFactory.getGarbageCollectorMXBeans()) {
358 ms.registerMBean(o, new ObjectName(
359 ManagementFactory.GARBAGE_COLLECTOR_MXBEAN_DOMAIN_TYPE
360 + ",num=" + (i++)));
361 }
362
363 i = 0;
364 for (Object o : ManagementFactory.getMemoryManagerMXBeans()) {
365 ms.registerMBean(o, new ObjectName(
366 ManagementFactory.MEMORY_MANAGER_MXBEAN_DOMAIN_TYPE
367 + ",num=" + (i++)));
368 }
369
370 i = 0;
371 for (Object o : ManagementFactory.getMemoryPoolMXBeans()) {
372 ms.registerMBean(o, new ObjectName(
373 ManagementFactory.MEMORY_POOL_MXBEAN_DOMAIN_TYPE + ",num="
374 + (i++)));
375 }
376
377 } catch (InstanceAlreadyExistsException e1) {
378
379
380
381 s_logger.debug("Bean " + e1.getMessage() + "already exists.");
382
383 } catch (MBeanRegistrationException e1) {
384 e1.printStackTrace();
385 } catch (NotCompliantMBeanException e1) {
386 e1.printStackTrace();
387 } catch (MalformedObjectNameException e1) {
388 e1.printStackTrace();
389 } catch (NullPointerException e1) {
390 e1.printStackTrace();
391 }
392 }
393 }