KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > snmp4j > agent > mo > jmx > JMXNotificationSupport


1 /*_############################################################################
2   _##
3   _## SNMP4J-AgentJMX - JMXNotificationSupport.java
4   _##
5   _## Copyright (C) 2006-2007 Frank Fock (SNMP4J.org)
6   _##
7   _## This program is free software; you can redistribute it and/or modify
8   _## it under the terms of the GNU General Public License version 2 as
9   _## published by the Free Software Foundation.
10   _##
11   _## This program is distributed in the hope that it will be useful,
12   _## but WITHOUT ANY WARRANTY; without even the implied warranty of
13   _## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14   _## GNU General Public License for more details.
15   _##
16   _## You should have received a copy of the GNU General Public License
17   _## along with this program; if not, write to the Free Software
18   _## Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19   _## MA 02110-1301 USA
20   _##
21   _##########################################################################*/

22
23 package org.snmp4j.agent.mo.jmx;
24
25 import javax.management.Notification JavaDoc;
26 import javax.management.NotificationListener JavaDoc;
27 import java.util.HashMap JavaDoc;
28 import java.util.Map JavaDoc;
29 import org.snmp4j.smi.OID;
30 import org.snmp4j.smi.VariableBinding;
31 import org.snmp4j.agent.NotificationOriginator;
32
33 /**
34  * The <code>JMXNotificationSupport</code> receives JMX notifications and
35  * forwards them to a {@link NotificationOriginator}.
36  *
37  * @author Frank Fock
38  * @version 1.0
39  */

40 public class JMXNotificationSupport implements NotificationListener JavaDoc {
41
42   private Map JavaDoc<OID,MBeanNotificationInfo> notificationInfos;
43   private NotificationOriginator notificationOriginator;
44
45   /**
46    * Creates a <code>JMXNotificationSupport</code> to will use the supplied
47    * <code>NotificationOriginator</code> to send SNMP notifications on behalf
48    * of JMX notifications.
49    *
50    * @param notificationOriginator
51    * a <code>NotificationOriginator</code> instance
52    */

53   public JMXNotificationSupport(NotificationOriginator notificationOriginator) {
54     this.notificationOriginator = notificationOriginator;
55     notificationInfos = new HashMap JavaDoc<OID,MBeanNotificationInfo>();
56   }
57
58   /**
59    * Adds a notification type to this support instance.
60    * @param notificationID
61    * a notification or trap OID.
62    * @param notificationInfo
63    * a <code>MBeanNotificationInfo</code> instance describing the mapping
64    * between JMX and SNMP notification.
65    */

66   public void add(OID notificationID, MBeanNotificationInfo notificationInfo) {
67     notificationInfos.put(notificationID, notificationInfo);
68   }
69
70   /**
71    * Adds all supplied notification types to this support instance.
72    * @param notificationDefinitions
73    * an array containing zero or more arrays with two elements where the
74    * first is a notification or trap OID and the second element is
75    * a <code>MBeanNotificationInfo</code> instance describing the mapping
76    * between JMX and SNMP notification.
77    */

78   public void addAll(Object JavaDoc[][] notificationDefinitions) {
79     for (Object JavaDoc[] tableDescr : notificationDefinitions) {
80       MBeanNotificationInfo mBeanInfo =
81           (MBeanNotificationInfo)tableDescr[1];
82       notificationInfos.put((OID)tableDescr[0], mBeanInfo);
83     }
84   }
85
86   /**
87    * Invoked when a JMX notification occurs, this method sends a SNMP
88    * notification
89    *
90    * @param notification
91    * The notification.
92    * @param handback
93    * An opaque object which helps the listener to associate
94    * information regarding the MBean emitter. This object is passed to the
95    * MBean during the addListener call and resent, without modification, to
96    * the listener. The MBean object should not use or modify the object.
97    */

98   public void handleNotification(Notification JavaDoc notification, Object JavaDoc handback) {
99     MBeanNotificationInfo info = notificationInfos.get(handback);
100     if (info != null) {
101       VariableBinding[] vbs =
102           info.getNotificationPayload(notification.getUserData());
103       notificationOriginator.notify(info.getContext(), (OID)handback, vbs);
104     }
105   }
106 }
107
Popular Tags