FileDocCategorySizeDatePackage
NotificationBuffer.javaAPI DocGlassfish v2 API6980Fri May 04 22:23:42 BST 2007com.sun.enterprise.management.support

NotificationBuffer

public final class NotificationBuffer extends Object
A circular buffer for Notifications.

Fields Summary
private final com.sun.appserv.management.util.misc.CircularList
mNotifications
private final NotificationFilter
mFilter
private final int
mBufferSize
long
mNextSequenceNumber
private com.sun.appserv.management.util.misc.OverflowHandler
mOverflowHandler
private final Notification[]
EMPTY_NOTIFS
private final Long
LONG_ZERO
public static final String
NEXT_SEQUENCE_NUMBER_KEY
Key within the Map returned by getNotifications() that yields the Long for the next sequence number.
public static final String
NOTIFICATIONS_KEY
Key within the Map returned by getNotifications() that yields the Notifications[].
Constructors Summary
public NotificationBuffer(int bufferSize, NotificationFilter filter, com.sun.appserv.management.util.misc.OverflowHandler handler)

		mFilter					= filter;
		mBufferSize				= bufferSize;
		mNotifications			= new CircularList<Notification>( Notification.class, bufferSize );
		mNextSequenceNumber		= 1;
		mOverflowHandler		= handler;
	
Methods Summary
public voidbufferNotification(javax.management.Notification notif)
Buffer the Notification if it matches the filter.

		if ( mFilter == null || mFilter.isNotificationEnabled( notif ) )
		{
			synchronized( mNotifications )
			{
				mNotifications.add( notif );
				nextSequenceNumber();
			}
		}
	
public voidclear()

		synchronized( mNotifications )
		{
			mNotifications.clear();
		}
	
public intgetBufferSize()
Get the number of Notifications available. Once the buffer is full, it will stay full until cleared, so that getSize() will return the same number as getCapacity().

		return( mBufferSize );
	
protected synchronized longgetNextSequenceNumber()

		return( mNextSequenceNumber );
	
public java.util.MapgetNotifications(long sequenceNumberIn)
Get all outstanding Notifications which have a sequence number that is equal to or greater than the specified one. The sequence number in this case is the overarching one maintained by this buffer, and has nothing to do with the sequence number within any particular Notification.

A sequence number of 0 means all Notifications.

The array returned contains the following:

  • result[ 0 ] is the next sequence number (for the next call)
  • result[ 1 ] a Notification[] of the available Notifications
  • return
    result[ 0 ] = next sequence number, result[ 1 ] = Notification[]

    	
    			        		            		           		           						       				     				           		        						            	 
    		 
    	     
    	
    		if ( sequenceNumberIn < 0 )
    		{
    			throw new IllegalArgumentException( "" + sequenceNumberIn );
    		}
    		
    		final Map<String,Object>	result	= new HashMap<String,Object>();
    		result.put( NEXT_SEQUENCE_NUMBER_KEY, LONG_ZERO );
    		result.put( NOTIFICATIONS_KEY, EMPTY_NOTIFS );
    		
    		synchronized( mNotifications )
    		{
    			final int	numNotifsAvailable	= mNotifications.size();
    			final long	nextAvailSequenceNumber	= getNextSequenceNumber();
    			result.put(NEXT_SEQUENCE_NUMBER_KEY,
                                        Long.valueOf(nextAvailSequenceNumber));
    			
    			if ( numNotifsAvailable != 0 )
    			{
    				final long	lastAvailSequenceNumber		= nextAvailSequenceNumber - 1;
    				final long	firstAvailSequenceNumber	= 1 + (lastAvailSequenceNumber - numNotifsAvailable);
    				
    				assert( firstAvailSequenceNumber >= 1 );
    				
    				final long	requestedSequenceNumber	= sequenceNumberIn == 0 ?
    					firstAvailSequenceNumber : sequenceNumberIn;
    					
    				if ( requestedSequenceNumber >= firstAvailSequenceNumber &&
    					requestedSequenceNumber <= lastAvailSequenceNumber)
    				{
    					final int	numMatches	= 1 +
    						(int)(lastAvailSequenceNumber - requestedSequenceNumber);
    					
    					final Notification[]	notifs	= new Notification[ numMatches ];
    					
    					final int	startIndex	= (int)
    						(requestedSequenceNumber - firstAvailSequenceNumber);
    					for( int i = 0 ; i < numMatches; ++i )
    					{
    						notifs[ i ]	= (Notification)
    							mNotifications.get( startIndex + i );
    					}
    					
    					result.put( NOTIFICATIONS_KEY, notifs );
    				}
    			}
    		}
    		
    		assert( result.get( NOTIFICATIONS_KEY ) instanceof Notification[] );
    		assert( result.get( NEXT_SEQUENCE_NUMBER_KEY ) instanceof Long );
    		
    		return( result );
    	
    protected synchronized longnextSequenceNumber()

    		return( mNextSequenceNumber++ );