FileDocCategorySizeDatePackage
ServerNotifForwarder.javaAPI DocJava SE 5 API9088Fri Aug 26 14:55:00 BST 2005com.sun.jmx.remote.internal

ServerNotifForwarder

public class ServerNotifForwarder extends Object

Fields Summary
private MBeanServer
mbeanServer
private final long
connectionTimeout
private static int
listenerCounter
private static final int[]
listenerCounterLock
private NotificationBuffer
notifBuffer
private Set
listenerList
private boolean
terminated
private final int[]
terminationLock
static final String
broadcasterClass
private static final ClassLogger
logger
Constructors Summary
public ServerNotifForwarder(MBeanServer mbeanServer, Map env, NotificationBuffer notifBuffer)

	this.mbeanServer = mbeanServer;

	this.notifBuffer = notifBuffer;


	connectionTimeout = EnvHelp.getServerConnectionTimeout(env);
    
Methods Summary
public java.lang.IntegeraddNotificationListener(javax.management.ObjectName name, javax.management.NotificationFilter filter)

         
        if (logger.traceOn()) {
            logger.trace("addNotificationListener",
                         "Add a listener at " + name);
        }

        checkState();

        // Explicitly check MBeanPermission for addNotificationListener
        //
        checkMBeanPermission(name, "addNotificationListener");

	try {
	    Boolean instanceOf = (Boolean)
		AccessController.doPrivileged(new PrivilegedExceptionAction() {
			public Object run() throws InstanceNotFoundException {
			    return new Boolean(
					       mbeanServer.isInstanceOf(name,
									broadcasterClass));
			}
                    });
	    if (!instanceOf.booleanValue()) {
		throw new IllegalArgumentException("The specified MBean [" +
						   name + "] is not a " +
						   "NotificationBroadcaster " +
						   "object.");
	    }
	} catch (PrivilegedActionException e) {
	    throw (InstanceNotFoundException) extractException(e);
	}

        final Integer id = getListenerID();
        synchronized(listenerList) {
            listenerList.add(new ListenerInfo(id, name, filter));
        }

        return id;
    
private voidcheckMBeanPermission(javax.management.ObjectName name, java.lang.String actions)
Explicitly check the MBeanPermission for the current access control context.

        SecurityManager sm = System.getSecurityManager();
        if (sm != null) {
            AccessControlContext acc = AccessController.getContext();
            ObjectInstance oi = null;
            try {
                oi = (ObjectInstance) AccessController.doPrivileged(
                    new PrivilegedExceptionAction() {
                            public Object run()
                                throws InstanceNotFoundException {
                                return mbeanServer.getObjectInstance(name);
                            }
                        });
            } catch (PrivilegedActionException e) {
                throw (InstanceNotFoundException) extractException(e);
            }
            String classname = oi.getClassName();
            MBeanPermission perm = new MBeanPermission(classname,
                                                       null,
                                                       name,
                                                       actions);
            sm.checkPermission(perm, acc);
        }
    
private voidcheckState()

        synchronized(terminationLock) {
            if (terminated) {
                throw new IOException("The connection has been terminated.");
            }
        }
    
private static java.lang.ExceptionextractException(java.lang.Exception e)
Iterate until we extract the real exception from a stack of PrivilegedActionExceptions.

        while (e instanceof PrivilegedActionException) {
            e = ((PrivilegedActionException)e).getException(); 
        }
        return e;
    
public javax.management.remote.NotificationResultfetchNotifs(long startSequenceNumber, long timeout, int maxNotifications)

        if (logger.traceOn()) {
            logger.trace("fetchNotifs", "Fetching notifications, the " +
                         "startSequenceNumber is " + startSequenceNumber +
                         ", the timeout is " + timeout +
                         ", the maxNotifications is " + maxNotifications);
        }

        NotificationResult nr = null;
	final long t = Math.min(connectionTimeout, timeout);
        try {
            nr = notifBuffer.fetchNotifications(listenerList,
                                                startSequenceNumber,
                                                t, maxNotifications);
        } catch (InterruptedException ire) {
            nr = new NotificationResult(0L, 0L, new TargetedNotification[0]);
        }

        if (logger.traceOn()) {
            logger.trace("fetchNotifs", "Forwarding the notifs: "+nr);
        }

        return nr;
    
private java.lang.IntegergetListenerID()

        synchronized(listenerCounterLock) {
            return new Integer(listenerCounter++);
        }
    
public voidremoveNotificationListener(javax.management.ObjectName name, java.lang.Integer[] listenerIDs)


        if (logger.traceOn()) {
            logger.trace("removeNotificationListener",
                         "Remove some listeners from " + name);
        }

        checkState();

        // Explicitly check MBeanPermission for removeNotificationListener
        //
        checkMBeanPermission(name, "removeNotificationListener");

        Exception re = null;
        for (int i = 0 ; i < listenerIDs.length ; i++) {
            try {
                removeNotificationListener(name, listenerIDs[i]);
            } catch (Exception e) {
                // Give back the first exception
                //
                if (re != null) {
                    re = e;
                }
            }
        }
        if (re != null) {
            throw re;
        }
    
public voidremoveNotificationListener(javax.management.ObjectName name, java.lang.Integer listenerID)


        if (logger.traceOn()) {
            logger.trace("removeNotificationListener",
                         "Remove the listener " + listenerID + " from " + name);
        }

        checkState();

	if (name != null && !name.isPattern()) {
	    if (!mbeanServer.isRegistered(name)) {
		throw new InstanceNotFoundException("The MBean " + name +
						    " is not registered.");
	    }
	}

        synchronized(listenerList) {
            if (!listenerList.remove(new ListenerInfo(listenerID,name,null))) {
                throw new ListenerNotFoundException("Listener not found!");
            }
        }
    
public voidterminate()

        if (logger.traceOn()) {
            logger.trace("terminate", "Be called.");
        }

        synchronized(terminationLock) {
            if (terminated) {
                return;
            }

            terminated = true;

            synchronized(listenerList) {
                listenerList.clear();
            }
        }

        if (logger.traceOn()) {
            logger.trace("terminate", "Terminated.");
        }