View Javadoc

1   package cz.cuni.amis.pogamut.base.utils.jmx.flag;
2   
3   import java.io.Serializable;
4   
5   import javax.management.InstanceAlreadyExistsException;
6   import javax.management.ListenerNotFoundException;
7   import javax.management.MBeanNotificationInfo;
8   import javax.management.MBeanRegistrationException;
9   import javax.management.MBeanServer;
10  import javax.management.MalformedObjectNameException;
11  import javax.management.NotCompliantMBeanException;
12  import javax.management.Notification;
13  import javax.management.NotificationBroadcasterSupport;
14  import javax.management.NotificationEmitter;
15  import javax.management.NotificationFilter;
16  import javax.management.NotificationListener;
17  import javax.management.ObjectName;
18  
19  import cz.cuni.amis.pogamut.base.utils.jmx.PogamutJMX;
20  import cz.cuni.amis.utils.flag.Flag;
21  import cz.cuni.amis.utils.flag.FlagListener;
22  
23  /**
24   * Translates Flag events to JMX events. Adds a listener on the flag and resends 
25   * the events to supplied broadcaster support.
26   * @author Ik
27   */
28  public class JMXFlagDecorator<T extends Serializable> implements JMXFlagDecoratorMBean, NotificationEmitter {
29  
30      protected Flag<T> flag = null;
31      protected ObjectName source;
32      protected int eventCounter = 0;
33      protected String flagName = null;
34  
35      protected FlagListener<T> listener = new FlagListener<T>() {
36  
37          @Override
38          public void flagChanged(T changedValue) {
39              Notification notification = new Notification(
40                      flagName,
41                      source,
42                      eventCounter++,
43                      changedValue.toString());
44              notification.setUserData(flag.getFlag());
45              nbs.sendNotification(notification);
46          }
47      };
48  
49      /**
50       * 
51       * @param flag Flag to be exposed through JMX.
52       * @param source MBean or ObjectName of the object where the flag resides.
53       * @param nbs NotificationBroadcasterSupport through which the events will be send.
54       */
55      public JMXFlagDecorator(Flag<T> flag, ObjectName source, MBeanServer mbs, String flagName) throws MalformedObjectNameException, InstanceAlreadyExistsException, MBeanRegistrationException, NotCompliantMBeanException {
56          this.flag = flag;
57          this.source = source;
58          this.flagName = flagName;
59          ObjectName name = PogamutJMX.getObjectName(source, flagName, PogamutJMX.FLAGS_SUBTYPE);
60          mbs.registerMBean(this, name);
61          flag.addListener(listener);
62      }
63  
64      /**
65       * Stops listening for the flag events.
66       */
67      public void stop() {
68          flag.removeListener(listener);
69      }
70  
71      /**
72       * 
73       * @return Notification info about this events possibly raised by this flag.
74       */
75      public MBeanNotificationInfo getMBeanNotificationInfo() {
76          return new MBeanNotificationInfo(new String[]{flagName},
77                  Notification.class.getName(), "The flag has changed it's value.");
78      }
79  
80      @Override
81      public Serializable getFlag() {
82          return flag.getFlag();
83      }
84  
85      
86          /**
87       * Support object for sending notifications.
88       */
89      protected NotificationBroadcasterSupport nbs = new NotificationBroadcasterSupport();
90      @Override
91      public void removeNotificationListener(NotificationListener listener, NotificationFilter filter, Object handback) throws ListenerNotFoundException {
92          nbs.removeNotificationListener(listener, filter, handback);
93      }
94  
95      @Override
96      public void addNotificationListener(NotificationListener listener, NotificationFilter filter, Object handback) throws IllegalArgumentException {
97          nbs.addNotificationListener(listener, filter, handback);
98      }
99  
100     @Override
101     public void removeNotificationListener(NotificationListener listener) throws ListenerNotFoundException {
102         nbs.removeNotificationListener(listener);
103     }
104 
105     @Override
106     public MBeanNotificationInfo[] getNotificationInfo() {
107         return new MBeanNotificationInfo[]{
108                     getMBeanNotificationInfo()
109                 };
110     }
111 }