1 23 24 29 30 package com.sun.enterprise.management.support; 31 32 import java.util.Map ; 33 import java.util.HashMap ; 34 35 import javax.management.Notification ; 36 import javax.management.NotificationFilter ; 37 38 import com.sun.appserv.management.base.NotificationService; 39 import com.sun.appserv.management.util.misc.CircularList; 40 import com.sun.appserv.management.util.misc.OverflowHandler; 41 42 43 44 47 final class NotificationBuffer 48 { 49 private final CircularList<Notification > mNotifications; 50 private final NotificationFilter mFilter; 51 private final int mBufferSize; 52 long mNextSequenceNumber; 53 private OverflowHandler mOverflowHandler; 54 55 56 protected synchronized long 57 nextSequenceNumber( ) 58 { 59 return( mNextSequenceNumber++ ); 60 } 61 62 protected synchronized long 63 getNextSequenceNumber( ) 64 { 65 return( mNextSequenceNumber ); 66 } 67 68 public 69 NotificationBuffer( 70 final int bufferSize, 71 final NotificationFilter filter, 72 final OverflowHandler handler ) 73 { 74 mFilter = filter; 75 mBufferSize = bufferSize; 76 mNotifications = new CircularList<Notification >( Notification .class, bufferSize ); 77 mNextSequenceNumber = 1; 78 mOverflowHandler = handler; 79 } 80 81 84 public void 85 bufferNotification( final Notification notif ) 86 { 87 if ( mFilter == null || mFilter.isNotificationEnabled( notif ) ) 88 { 89 synchronized( mNotifications ) 90 { 91 mNotifications.add( notif ); 92 nextSequenceNumber(); 93 } 94 } 95 } 96 97 98 102 public int 103 getBufferSize() 104 { 105 return( mBufferSize ); 106 } 107 108 public void 109 clear() 110 { 111 synchronized( mNotifications ) 112 { 113 mNotifications.clear(); 114 } 115 } 116 117 118 private final Notification [] EMPTY_NOTIFS = new Notification [ 0 ]; 119 private final Long LONG_ZERO = new Long ( 0 ); 120 121 125 public static final String NEXT_SEQUENCE_NUMBER_KEY = 126 NotificationService.NEXT_SEQUENCE_NUMBER_KEY; 127 128 132 public static final String NOTIFICATIONS_KEY = 133 NotificationService.NOTIFICATIONS_KEY; 134 135 152 public Map <String ,Object > 153 getNotifications( final long sequenceNumberIn ) 154 { 155 if ( sequenceNumberIn < 0 ) 156 { 157 throw new IllegalArgumentException ( "" + sequenceNumberIn ); 158 } 159 160 final Map <String ,Object > result = new HashMap <String ,Object >(); 161 result.put( NEXT_SEQUENCE_NUMBER_KEY, LONG_ZERO ); 162 result.put( NOTIFICATIONS_KEY, EMPTY_NOTIFS ); 163 164 synchronized( mNotifications ) 165 { 166 final int numNotifsAvailable = mNotifications.size(); 167 final long nextAvailSequenceNumber = getNextSequenceNumber(); 168 result.put( NEXT_SEQUENCE_NUMBER_KEY, new Long ( nextAvailSequenceNumber ) ); 169 170 if ( numNotifsAvailable != 0 ) 171 { 172 final long lastAvailSequenceNumber = nextAvailSequenceNumber - 1; 173 final long firstAvailSequenceNumber = 1 + (lastAvailSequenceNumber - numNotifsAvailable); 174 175 assert( firstAvailSequenceNumber >= 1 ); 176 177 final long requestedSequenceNumber = sequenceNumberIn == 0 ? 178 firstAvailSequenceNumber : sequenceNumberIn; 179 180 if ( requestedSequenceNumber >= firstAvailSequenceNumber && 181 requestedSequenceNumber <= lastAvailSequenceNumber) 182 { 183 final int numMatches = 1 + 184 (int)(lastAvailSequenceNumber - requestedSequenceNumber); 185 186 final Notification [] notifs = new Notification [ numMatches ]; 187 188 final int startIndex = (int) 189 (requestedSequenceNumber - firstAvailSequenceNumber); 190 for( int i = 0 ; i < numMatches; ++i ) 191 { 192 notifs[ i ] = (Notification ) 193 mNotifications.get( startIndex + i ); 194 } 195 196 result.put( NOTIFICATIONS_KEY, notifs ); 197 } 198 } 199 } 200 201 assert( result.get( NOTIFICATIONS_KEY ) instanceof Notification [] ); 202 assert( result.get( NEXT_SEQUENCE_NUMBER_KEY ) instanceof Long ); 203 204 return( result ); 205 } 206 } 207 208 209 210 211 212 213 214 215 216 217 218 | Popular Tags |