/**
* The XMOJO Project 5
* Copyright 2003 XMOJO.org. All rights reserved.
* NO WARRANTY
* BECAUSE THE LIBRARY IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY FOR
* THE LIBRARY, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN
* OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES
* PROVIDE THE LIBRARY "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED
* OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS
* TO THE QUALITY AND PERFORMANCE OF THE LIBRARY IS WITH YOU. SHOULD THE
* LIBRARY PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING,
* REPAIR OR CORRECTION.
* IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING WILL
* ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR REDISTRIBUTE
* THE LIBRARY AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY
* GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE
* USE OR INABILITY TO USE THE LIBRARY (INCLUDING BUT NOT LIMITED TO LOSS OF
* DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD
* PARTIES OR A FAILURE OF THE LIBRARY TO OPERATE WITH ANY OTHER SOFTWARE),
* EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGES.
**/
package javax.management;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.Vector;
import com.adventnet.agent.utilities.scheduler.*;
import com.adventnet.jmx.NotificationHandler;
import com.adventnet.jmx.NotificationThread;
import com.adventnet.agent.logging.Log;
import com.adventnet.agent.logging.LogFactory;
/**
* This class provides an implementation of NotificationBroadcaster. It could
* be used as a super class of MBean to deal with notification. If inheritance
* can't be used, the following code can be used as an example for
* NotificationBroadcaster implementation.
*/
public class NotificationBroadcasterSupport implements NotificationBroadcaster
{
/**
* Hastable containing the Listener and hand-back objects.
*/
private Hashtable notifTable = null;
private Hashtable info = null;
private Log log = null;
private Vector notifList = null;
private static Scheduler scheduler = null;
private static int MAX_THREADS = 4;
/**
* Creates a NotificationBraoadCasterSupport. Default constructor.
*/
public NotificationBroadcasterSupport()
{
createLogger();
notifTable = new Hashtable();
info = new Hashtable();
notifList = new Vector();
if(scheduler == null)
{
scheduler = Scheduler.createScheduler(
"NOTIFICATIONBROADCASTERSUPPORT", MAX_THREADS);
scheduler.start();
log.trace("JMX BROADCASTER Scheduler instantiated");
}
}
/**
* Enables a couple (listener,handback) for a registered MBean to be added.
*
* @param listener The listener object which will handles notifications
* emitted by the registered MBean.
*
* @param filter The filter object. If not specified, no filtering will
* be performed before handling notifications.
*
* @param handback The context to be sent to the listener when a
* notification is emitted.
*
* @throws java.lang.IllegalArgumentException - Listener parameter is null.
*/
public void addNotificationListener(NotificationListener listener,
NotificationFilter filter,
Object handback)
{
if(listener == null )
throw new RuntimeOperationsException(new IllegalArgumentException(
"Listener cannot be null!!!"));
ArrayList[] obj = (ArrayList[])notifTable.get(listener);
if(obj == null)
{
obj = new ArrayList[2];
obj[0] = new ArrayList();
obj[1] = new ArrayList();
obj[0].add(filter);
obj[1].add(handback);
notifTable.put(listener, obj);
}
else
{
boolean flag = false;
Object[] handbacks = obj[1].toArray();
for(int i=0; i<handbacks.length;i++)
{
if(handbacks[i].equals(handback))
{
flag = true;
break;
}
}
if(!flag)
{
obj[0].add(filter);
obj[1].add(handback);
}
}
}
/**
* Enables a listener for an MBean to be removed.
* All couple (listener, handback) are removed.
*
* @param listener The listener object which will handles notifications
* emitted by the registered MBean.
*
* @throws ListenerNotFoundException The listener is not registered
* in the MBean.
*/
public void removeNotificationListener(NotificationListener listener)
throws ListenerNotFoundException
{
Object list = notifTable.get(listener);
if(list == null)
{
throw new ListenerNotFoundException("Unknown Listener is being passed !!!");
}
notifTable.remove(listener);
}
/**
* Returns a NotificationInfo object contaning the name of the
* Java class of the notification and the notification types sent.
*
* @return This returns a array of MBeanNotificationInfo which
* contains the notification information.
*/
public MBeanNotificationInfo[] getNotificationInfo()
{
MBeanNotificationInfo[] toRet = new MBeanNotificationInfo[info.size()];
int i = 0;
for(Enumeration e = info.keys();e.hasMoreElements();)
{
String key = (String)e.nextElement();
ArrayList list = (ArrayList)info.get(key);
String [] strArray = (String[])list.toArray(new String[0]);
toRet[i++] = new MBeanNotificationInfo(strArray,key,"Notification description");
}
return toRet;
}
/**
* Enables a MBean to send a notification.
*
* @param notif - The notification to send.
*/
public void sendNotification(Notification notif)
{
for(Enumeration e = notifTable.keys();e.hasMoreElements();)
{
NotificationListener nl = (NotificationListener)e.nextElement();
ArrayList[] list = (ArrayList[])notifTable.get(nl);
Object[] filters = list[0].toArray();
Object[] handbacks = list[1].toArray();
try
{
for(int i=0;i<filters.length;i++)
{
NotificationHandler hdlr = new NotificationHandler(
(NotificationFilter)filters[i],
nl,
handbacks[i],
notif,
info);
Vector myVect = new Vector();
myVect.addElement(hdlr);
Runnable runnable = new NotificationThread(myVect, this.info);
scheduler.scheduleTask(runnable, null);
log.debug("Task Scheduled");
runnable = null;
}
}
catch(Exception ex)
{
log.error("Exception in SendNotification" + ex);
}
}
}
//------------------------- Private methods ---------------------------//
private void createLogger()
{
try
{
log=LogFactory.getInstance("JMX");
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
|