001: /**
002: * The XMOJO Project 5
003: * Copyright © 2003 XMOJO.org. All rights reserved.
004:
005: * NO WARRANTY
006:
007: * BECAUSE THE LIBRARY IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY FOR
008: * THE LIBRARY, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN
009: * OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES
010: * PROVIDE THE LIBRARY "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED
011: * OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
012: * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS
013: * TO THE QUALITY AND PERFORMANCE OF THE LIBRARY IS WITH YOU. SHOULD THE
014: * LIBRARY PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING,
015: * REPAIR OR CORRECTION.
016:
017: * IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING WILL
018: * ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR REDISTRIBUTE
019: * THE LIBRARY AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY
020: * GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE
021: * USE OR INABILITY TO USE THE LIBRARY (INCLUDING BUT NOT LIMITED TO LOSS OF
022: * DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD
023: * PARTIES OR A FAILURE OF THE LIBRARY TO OPERATE WITH ANY OTHER SOFTWARE),
024: * EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF
025: * SUCH DAMAGES.
026: **/package javax.management;
027:
028: import java.lang.reflect.Array;
029: import java.util.ArrayList;
030: import java.util.Enumeration;
031: import java.util.Hashtable;
032: import java.util.Vector;
033:
034: import com.adventnet.agent.utilities.scheduler.*;
035:
036: import com.adventnet.jmx.NotificationHandler;
037: import com.adventnet.jmx.NotificationThread;
038: import com.adventnet.agent.logging.Log;
039: import com.adventnet.agent.logging.LogFactory;
040:
041: /**
042: * This class provides an implementation of NotificationBroadcaster. It could
043: * be used as a super class of MBean to deal with notification. If inheritance
044: * can't be used, the following code can be used as an example for
045: * NotificationBroadcaster implementation.
046: */
047: public class NotificationBroadcasterSupport implements
048: NotificationBroadcaster {
049: /**
050: * Hastable containing the Listener and hand-back objects.
051: */
052: private Hashtable notifTable = null;
053:
054: private Hashtable info = null;
055:
056: private Log log = null;
057:
058: private Vector notifList = null;
059:
060: private static Scheduler scheduler = null;
061:
062: private static int MAX_THREADS = 4;
063:
064: /**
065: * Creates a NotificationBraoadCasterSupport. Default constructor.
066: */
067: public NotificationBroadcasterSupport() {
068: createLogger();
069: notifTable = new Hashtable();
070: info = new Hashtable();
071: notifList = new Vector();
072:
073: if (scheduler == null) {
074: scheduler = Scheduler.createScheduler(
075: "NOTIFICATIONBROADCASTERSUPPORT", MAX_THREADS);
076: scheduler.start();
077: log.trace("JMX BROADCASTER Scheduler instantiated");
078: }
079: }
080:
081: /**
082: * Enables a couple (listener,handback) for a registered MBean to be added.
083: *
084: * @param listener The listener object which will handles notifications
085: * emitted by the registered MBean.
086: *
087: * @param filter The filter object. If not specified, no filtering will
088: * be performed before handling notifications.
089: *
090: * @param handback The context to be sent to the listener when a
091: * notification is emitted.
092: *
093: * @throws java.lang.IllegalArgumentException - Listener parameter is null.
094: */
095: public void addNotificationListener(NotificationListener listener,
096: NotificationFilter filter, Object handback) {
097: if (listener == null)
098: throw new RuntimeOperationsException(
099: new IllegalArgumentException(
100: "Listener cannot be null!!!"));
101:
102: ArrayList[] obj = (ArrayList[]) notifTable.get(listener);
103:
104: if (obj == null) {
105: obj = new ArrayList[2];
106: obj[0] = new ArrayList();
107: obj[1] = new ArrayList();
108: obj[0].add(filter);
109: obj[1].add(handback);
110:
111: notifTable.put(listener, obj);
112: } else {
113: boolean flag = false;
114: Object[] handbacks = obj[1].toArray();
115:
116: for (int i = 0; i < handbacks.length; i++) {
117: if (handbacks[i].equals(handback)) {
118: flag = true;
119: break;
120: }
121: }
122:
123: if (!flag) {
124: obj[0].add(filter);
125: obj[1].add(handback);
126: }
127: }
128: }
129:
130: /**
131: * Enables a listener for an MBean to be removed.
132: * All couple (listener, handback) are removed.
133: *
134: * @param listener The listener object which will handles notifications
135: * emitted by the registered MBean.
136: *
137: * @throws ListenerNotFoundException The listener is not registered
138: * in the MBean.
139: */
140: public void removeNotificationListener(NotificationListener listener)
141: throws ListenerNotFoundException {
142: Object list = notifTable.get(listener);
143:
144: if (list == null) {
145: throw new ListenerNotFoundException(
146: "Unknown Listener is being passed !!!");
147: }
148:
149: notifTable.remove(listener);
150: }
151:
152: /**
153: * Returns a NotificationInfo object contaning the name of the
154: * Java class of the notification and the notification types sent.
155: *
156: * @return This returns a array of MBeanNotificationInfo which
157: * contains the notification information.
158: */
159: public MBeanNotificationInfo[] getNotificationInfo() {
160: MBeanNotificationInfo[] toRet = new MBeanNotificationInfo[info
161: .size()];
162: int i = 0;
163: for (Enumeration e = info.keys(); e.hasMoreElements();) {
164: String key = (String) e.nextElement();
165: ArrayList list = (ArrayList) info.get(key);
166: String[] strArray = (String[]) list.toArray(new String[0]);
167:
168: toRet[i++] = new MBeanNotificationInfo(strArray, key,
169: "Notification description");
170: }
171: return toRet;
172: }
173:
174: /**
175: * Enables a MBean to send a notification.
176: *
177: * @param notif - The notification to send.
178: */
179: public void sendNotification(Notification notif) {
180: for (Enumeration e = notifTable.keys(); e.hasMoreElements();) {
181: NotificationListener nl = (NotificationListener) e
182: .nextElement();
183:
184: ArrayList[] list = (ArrayList[]) notifTable.get(nl);
185:
186: Object[] filters = list[0].toArray();
187: Object[] handbacks = list[1].toArray();
188:
189: try {
190: for (int i = 0; i < filters.length; i++) {
191: NotificationHandler hdlr = new NotificationHandler(
192: (NotificationFilter) filters[i], nl,
193: handbacks[i], notif, info);
194:
195: Vector myVect = new Vector();
196:
197: myVect.addElement(hdlr);
198:
199: Runnable runnable = new NotificationThread(myVect,
200: this .info);
201:
202: scheduler.scheduleTask(runnable, null);
203: log.debug("Task Scheduled");
204: runnable = null;
205: }
206: } catch (Exception ex) {
207: log.error("Exception in SendNotification" + ex);
208: }
209: }
210: }
211:
212: //------------------------- Private methods ---------------------------//
213:
214: private void createLogger() {
215: try {
216: log = LogFactory.getInstance("JMX");
217: } catch (Exception e) {
218: e.printStackTrace();
219: }
220: }
221: }
|