FileDocCategorySizeDatePackage
ClientNotificationManager.javaAPI DocGlassfish v2 API9974Fri May 04 22:36:24 BST 2007com.sun.enterprise.admin.jmx.remote.notification

ClientNotificationManager

public final class ClientNotificationManager extends Object implements Runnable
A NotificationManager class used on the client-side to mainly call the NotificationListeners appropriately whenever a notification is received from the server-side. The ClientNotificationManager uses a NotificationReceiver running on a separate thread to listen for notifications. The ClientNotificationManager when constructed will make a connection to the server-side with an unique id identifying this ClientNotificationManager.

Fields Summary
private final Map
env
private final HashMap
listenerMap
private final String
mgrId
private final com.sun.enterprise.admin.jmx.remote.notification.SimpleQueue
que
private final NotificationReceiver
receiver
private final Thread
eventThread
private volatile boolean
exit
private static final Logger
logger
Constructors Summary
public ClientNotificationManager(com.sun.enterprise.admin.jmx.remote.comm.HttpConnectorAddress ad, Map envIn)

/*, 
        DefaultConfiguration.LOGGER_RESOURCE_BUNDLE_NAME );*/

        
              
        env = envIn;
        que = new SimpleQueue();
        listenerMap = new HashMap();
        
        String hname="null";            
        try { 
            hname = InetAddress.getLocalHost().getHostName(); 
        } catch (Exception ex) {
            /*ignore*/
            hname = "null";
        }        
        mgrId = (new java.rmi.server.UID()).toString() + ":" + hname;        

        eventThread = new Thread(this);
        eventThread.start();
        receiver = new NotificationReceiver(ad, this);
    
Methods Summary
public java.lang.StringaddNotificationListener(javax.management.ObjectName objname, javax.management.NotificationListener listener, javax.management.NotificationFilter filter, java.lang.Object handback)
Registers the notification listener so that they may be called in case a notification is received.

        ListenerInfo info = new ListenerInfo();

        info.listener   = listener;
        info.filter     = filter;
        info.handback   = handback;
        info.id         = info.computeId();

        ArrayList list = (ArrayList)listenerMap.get(objname);
        if (list == null) {
            list = new ArrayList();
        }
        list.add(info);

        listenerMap.put(objname, list);

        return info.id;
    
public voidclose()

        exit = true;
        try {
            receiver.exit();
        } finally {
            synchronized (que) {
                que.notify();
            }
            eventThread.join();
        }
    
public java.lang.StringgetId()

        return mgrId;
    
private booleanisExiting()

        return exit;
    
public voidraiseEvent(com.sun.enterprise.admin.jmx.remote.notification.NotificationWrapper wrapr)

        synchronized (que) {
            que.add(wrapr);
            que.notify();
        }
    
public booleanreinit()
A method to reinitialize the connection to the server-side in case the connection gets dropped. This method is called for every MBeanServerConnection method call.

        return receiver.reinit();
    
public java.lang.String[]removeNotificationListener(javax.management.ObjectName mbean, javax.management.NotificationListener listener)

        String[] strs = removeNotificationListener(mbean, listener, null, null, true);
        return strs;
    
public java.lang.String[]removeNotificationListener(javax.management.ObjectName mbean, javax.management.NotificationListener listener, javax.management.NotificationFilter filter, java.lang.Object handback)

        String[] strs = removeNotificationListener( mbean,
                                                    listener,
                                                    filter,
                                                    handback, false);
        return strs;
    
private java.lang.String[]removeNotificationListener(javax.management.ObjectName mbean, javax.management.NotificationListener listener, javax.management.NotificationFilter filter, java.lang.Object handback, boolean listenerOnly)
Unregisters the notification listeners as per the logic defined by MBeanServer.removeNotificationListener 1. -- all the registrations for the listener to the specified mbean are removed. 2. -- only that registration, for the listener, which was registered with this filter and handback will be removed.

        ArrayList idlist = new ArrayList();
        ArrayList list = (ArrayList) listenerMap.get(mbean);
        if (list == null) {
            return (new String[0]);
        }

        ListenerInfo info1 = new ListenerInfo();
        info1.listener = listener;
        info1.filter = filter;
        info1.handback = handback;
        info1.id = info1.computeId();

        Iterator itr = list.iterator();
        // Because updating the list when we are iterating the list throws an exception,
        // unless we return immediately
        ArrayList list1 = (ArrayList) list.clone();
        while (itr.hasNext()) {
            ListenerInfo info = (ListenerInfo) itr.next();
            if (!listenerOnly && info.id.equals(info1.id)) {
                list1.remove(list1.indexOf(info));
                idlist.add(info.id);
            } else if (listenerOnly && info.listener == listener) {
                list1.remove(list1.indexOf(info));
                idlist.add(info.id);
            }
        }

        listenerMap.put(mbean, list1);

        String[] ids = new String[idlist.size()];
        ids = (String[]) idlist.toArray(ids);
        return ids;
    
public voidrun()
This is the event dispatch thread, which calls the notification listeners as and when a notification is received for that listener.

        while (!isExiting()) {
            synchronized (que) {
                while (que.isEmpty() && !isExiting()) {
                    try {
                        que.wait();
                    } catch (InterruptedException intre) {
                    }
                }
            }
            if (isExiting())
                break;
            while (!que.isEmpty() && !isExiting()) {
                NotificationWrapper wrapr = (NotificationWrapper) que.remove();
                ObjectName source = wrapr.getSource();
                Notification notif = wrapr.getNotification();

                ArrayList listeners = (ArrayList) listenerMap.get(source);
                Iterator itr = listeners.iterator();
                while (itr.hasNext() && !isExiting()) {
                    ListenerInfo info = (ListenerInfo) itr.next();
                    boolean callListener = true;
                    if (info.filter != null)
                        callListener = info.filter.isNotificationEnabled(notif);
                    if (callListener)
                        info.listener.handleNotification(notif, info.handback);
                }
            }
        }