FileDocCategorySizeDatePackage
NotificationEmitterSupport.javaAPI DocGlassfish v2 API10954Fri May 04 22:31:04 BST 2007com.sun.appserv.management.util.jmx

NotificationEmitterSupport

public class NotificationEmitterSupport extends NotificationBroadcasterSupport
Features:
  • Maintains information on all NotificationListeners so that queries can be made on the number of listeners, and the number of listeners of each type
  • optionally sends all Notifications asynchronously via a separate Thread

Fields Summary
private final boolean
mAsyncDelivery
private SenderThread
mSenderThread
private final Map
mListenerTypeCounts
private final NotificationListenerTracking
mListeners
private static final String[]
NO_TYPES
private static final String
WILDCARD_TYPE
private static final String[]
ALL_TYPES
private static final String[]
ATTRIBUTE_CHANGE_TYPES
private static final String[]
MBEAN_SERVER_NOTIFICATION_TYPES
private final Integer
COUNT_1
Constructors Summary
public NotificationEmitterSupport(boolean asyncDelivery)

		mAsyncDelivery	= asyncDelivery;
		// don't create a thread until needed
		mSenderThread	= null;
		
		mListenerTypeCounts = Collections.synchronizedMap( new HashMap<String,Integer>() );
		
		mListeners = new NotificationListenerTracking( true );
	
Methods Summary
private voidaddFilterTypeCounts(javax.management.NotificationFilter filter)

	    String[]  types  = getTypes( filter );
    	    
	    for( String type : types )
	    {
	        incrementListenerCountForType( type );
	    }
	
public voidaddNotificationListener(javax.management.NotificationListener listener, javax.management.NotificationFilter filter, java.lang.Object handback)

		super.addNotificationListener( listener, filter, handback );
		
		mListeners.addNotificationListener( listener, filter, handback );
		addFilterTypeCounts( filter );
	
public synchronized voidcleanup()

		if ( mSenderThread != null )
		{
			mSenderThread.quit();
			mSenderThread	= null;
		}
	
private voiddecrementListenerCountForType(java.lang.String type)

	    synchronized( mListenerTypeCounts )
	    {
	        final Integer count   = mListenerTypeCounts.get( type );
	        if ( count == null )
	        {
	            throw new IllegalArgumentException( type );
	        }
	        
	        final int   oldValue    = count.intValue();
	        if ( oldValue == 1 )
	        {
	            mListenerTypeCounts.remove( type );
	        }
	        else
	        {
	            mListenerTypeCounts.put( type, new Integer( oldValue - 1 ) );
	        }
	    }
	
public intgetListenerCount()

		return( mListeners.getListenerCount() );
	
public intgetNotificationTypeListenerCount(java.lang.String type)

	    final Integer   count   = mListenerTypeCounts.get( type );
	    
	    int resultCount = 0;
	    
	    if ( count == null )
	    {
	        final Integer allCount  = mListenerTypeCounts.get( WILDCARD_TYPE );
	        if ( allCount != null )
	        {
	            resultCount = allCount;
	        }
	        else
	        {
	            // no wildcards are in use
	        }
	    }
	    
		return( resultCount );
	
private synchronized com.sun.appserv.management.util.jmx.NotificationEmitterSupport$SenderThreadgetSenderThread()

		if ( mSenderThread == null )
		{
			mSenderThread	= mAsyncDelivery ? new SenderThread() : null;
			if ( mSenderThread != null )
			{
				mSenderThread.start();
			}
		}
		
		return( mSenderThread );
	
private java.lang.String[]getTypes(javax.management.NotificationFilter filter)

	    String[]    types   = NO_TYPES;
	    
	    if ( filter instanceof NotificationFilterSupport )
	    {
	        final NotificationFilterSupport fs  = (NotificationFilterSupport)filter;
	        
	        types   = ListUtil.toStringArray( fs.getEnabledTypes() );
	    }
	    else if ( filter instanceof AttributeChangeNotificationFilter )
	    {
	        types   = ATTRIBUTE_CHANGE_TYPES;
	    }
	    else if ( filter instanceof MBeanServerNotificationFilter )
	    {
	        types   = MBEAN_SERVER_NOTIFICATION_TYPES;
	    }
	    else
	    {
	        // no filter, or non-standard one, have to assume all types
	        types   = ALL_TYPES;
	    }
	    
	    return types;
	
private voidincrementListenerCountForType(java.lang.String type)

	
	     
	    
	
	    synchronized( mListenerTypeCounts )
	    {
	        final Integer count   = mListenerTypeCounts.get( type );
	        
	        final Integer newCount  = (count == null ) ?
	                                    COUNT_1 : new Integer( count.intValue() + 1 );
	        
	        mListenerTypeCounts.put( type, newCount );
	    }
	
protected voidinternalSendNotification(javax.management.Notification notif)

		super.sendNotification( notif );
	
private voidremoveFilterTypeCounts(javax.management.NotificationFilter filter)

    	final String[]  types   = getTypes( filter );
    	    
	    for( String type : types )
	    {
	        decrementListenerCountForType( type );
	    }
	
private voidremoveFilterTypeCounts(java.util.List infos)

	    for( NotificationListenerInfo info : infos )
	    {
	        removeFilterTypeCounts( info.getFilter() );
	    }
	
public voidremoveNotificationListener(javax.management.NotificationListener listener)

		super.removeNotificationListener( listener );
		
		final List<NotificationListenerInfo>    infos =
		    mListeners.removeNotificationListener( listener );
		removeFilterTypeCounts( infos );
	
public voidremoveNotificationListener(javax.management.NotificationListener listener, javax.management.NotificationFilter filter, java.lang.Object handback)

		super.removeNotificationListener( listener, filter, handback );
		
		mListeners.removeNotificationListener( listener );
		if ( filter != null )
		{
		    removeFilterTypeCounts( filter );
		}
		
	
public voidsendAll()
Synchronously (on current thread), ensure that all Notifications have been delivered.

		if ( mSenderThread != null )
		{
			mSenderThread.sendAll();
		}
	
public voidsendNotification(javax.management.Notification notif)
Send the Notification. If created with async=true, then this routine returns immediately and the Notification is sent on a separate Thread.

		if ( getListenerCount() != 0 )
		{
			if ( getSenderThread() != null )
			{
				mSenderThread.enqueue( notif );
			}
			else
			{
				internalSendNotification( notif );
			}
		}