1 package org.apache.helix.monitoring.mbeans;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import java.io.IOException;
23 import java.lang.management.ManagementFactory;
24
25 import javax.management.AttributeNotFoundException;
26 import javax.management.InstanceNotFoundException;
27 import javax.management.IntrospectionException;
28 import javax.management.ListenerNotFoundException;
29 import javax.management.MBeanAttributeInfo;
30 import javax.management.MBeanException;
31 import javax.management.MBeanInfo;
32 import javax.management.MBeanServerConnection;
33 import javax.management.MBeanServerDelegate;
34 import javax.management.MBeanServerNotification;
35 import javax.management.MalformedObjectNameException;
36 import javax.management.Notification;
37 import javax.management.NotificationListener;
38 import javax.management.ReflectionException;
39 import javax.management.relation.MBeanServerNotificationFilter;
40
41 import org.apache.log4j.Logger;
42
43
44
45
46
47 public abstract class ClusterMBeanObserver implements NotificationListener
48 {
49 protected final String _domain;
50 protected MBeanServerConnection _server;
51 private static final Logger _logger = Logger.getLogger(ClusterMBeanObserver.class);
52
53 public ClusterMBeanObserver(String domain)
54 throws InstanceNotFoundException, IOException, MalformedObjectNameException, NullPointerException
55 {
56
57 _domain = domain;
58 _server = ManagementFactory.getPlatformMBeanServer();
59 MBeanServerNotificationFilter filter = new MBeanServerNotificationFilter();
60 filter.enableAllObjectNames();
61 _server.addNotificationListener(MBeanServerDelegate.DELEGATE_NAME, this, filter, null);
62 }
63
64 public void handleNotification(Notification notification, Object handback)
65 {
66 MBeanServerNotification mbs = (MBeanServerNotification) notification;
67 if(MBeanServerNotification.REGISTRATION_NOTIFICATION.equals(mbs.getType()))
68 {
69 if(mbs.getMBeanName().getDomain().equalsIgnoreCase(_domain))
70 {
71 _logger.info("MBean Registered, name :" + mbs.getMBeanName());
72 onMBeanRegistered(_server, mbs);
73 }
74 }
75 else if(MBeanServerNotification.UNREGISTRATION_NOTIFICATION.equals(mbs.getType()))
76 {
77 if(mbs.getMBeanName().getDomain().equalsIgnoreCase(_domain))
78 {
79 _logger.info("MBean Unregistered, name :" + mbs.getMBeanName());
80 onMBeanUnRegistered(_server, mbs);
81 }
82 }
83 }
84
85 public void disconnect()
86 {
87 MBeanServerNotificationFilter filter = new MBeanServerNotificationFilter();
88 try
89 {
90 _server.removeNotificationListener(MBeanServerDelegate.DELEGATE_NAME, this);
91 }
92 catch (Exception e)
93 {
94 _logger.error("", e);
95 }
96 }
97
98 public abstract void onMBeanRegistered(MBeanServerConnection server, MBeanServerNotification mbsNotification);
99
100 public abstract void onMBeanUnRegistered(MBeanServerConnection server, MBeanServerNotification mbsNotification);
101
102 }