KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > management > support > NotificationBuffer


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23  
24 /*
25  * $Header: /cvs/glassfish/admin/mbeanapi-impl/src/java/com/sun/enterprise/management/support/NotificationBuffer.java,v 1.5 2006/03/09 20:30:48 llc Exp $
26  * $Revision: 1.5 $
27  * $Date: 2006/03/09 20:30:48 $
28  */

29
30 package com.sun.enterprise.management.support;
31
32 import java.util.Map JavaDoc;
33 import java.util.HashMap JavaDoc;
34
35 import javax.management.Notification JavaDoc;
36 import javax.management.NotificationFilter JavaDoc;
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 /**
45     A circular buffer for Notifications.
46  */

47 final class NotificationBuffer
48 {
49     private final CircularList<Notification JavaDoc> mNotifications;
50     private final NotificationFilter JavaDoc 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 JavaDoc filter,
72         final OverflowHandler handler )
73     {
74         mFilter = filter;
75         mBufferSize = bufferSize;
76         mNotifications = new CircularList<Notification JavaDoc>( Notification JavaDoc.class, bufferSize );
77         mNextSequenceNumber = 1;
78         mOverflowHandler = handler;
79     }
80
81     /**
82         Buffer the Notification if it matches the filter.
83      */

84         public void
85     bufferNotification( final Notification JavaDoc 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     /**
99         Get the number of Notifications available. Once the buffer is full, it will stay
100         full until cleared, so that getSize() will return the same number as getCapacity().
101      */

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 JavaDoc[] EMPTY_NOTIFS = new Notification JavaDoc[ 0 ];
119     private final Long JavaDoc LONG_ZERO = new Long JavaDoc( 0 );
120     
121     /**
122         Key within the Map returned by getNotifications() that yields the Long
123         for the next sequence number.
124      */

125     public static final String JavaDoc NEXT_SEQUENCE_NUMBER_KEY =
126         NotificationService.NEXT_SEQUENCE_NUMBER_KEY;
127     
128     /**
129         Key within the Map returned by getNotifications() that yields the
130         Notifications[].
131      */

132     public static final String JavaDoc NOTIFICATIONS_KEY =
133         NotificationService.NOTIFICATIONS_KEY;
134     
135     /**
136         Get all outstanding Notifications which have a sequence number
137         that is equal to or greater than the specified one. The sequence
138         number in this case is the overarching one maintained by this buffer,
139         and has nothing to do with the sequence number within any particular
140         Notification.
141         <p>
142         A sequence number of 0 means all Notifications.
143         <p>
144         The array returned contains the following:
145         <nl>
146         <li>result[ 0 ] is the next sequence number (for the next call)</li>
147         <li>result[ 1 ] a Notification[] of the available Notifications</li>
148         </nl>
149         
150         @return result[ 0 ] = next sequence number, result[ 1 ] = Notification[]
151      */

152         public Map JavaDoc<String JavaDoc,Object JavaDoc>
153     getNotifications( final long sequenceNumberIn )
154     {
155         if ( sequenceNumberIn < 0 )
156         {
157             throw new IllegalArgumentException JavaDoc( "" + sequenceNumberIn );
158         }
159         
160         final Map JavaDoc<String JavaDoc,Object JavaDoc> result = new HashMap JavaDoc<String JavaDoc,Object JavaDoc>();
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 JavaDoc( 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 JavaDoc[] notifs = new Notification JavaDoc[ numMatches ];
187                     
188                     final int startIndex = (int)
189                         (requestedSequenceNumber - firstAvailSequenceNumber);
190                     for( int i = 0 ; i < numMatches; ++i )
191                     {
192                         notifs[ i ] = (Notification JavaDoc)
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 JavaDoc[] );
202         assert( result.get( NEXT_SEQUENCE_NUMBER_KEY ) instanceof Long JavaDoc );
203         
204         return( result );
205     }
206 }
207
208
209
210
211
212
213
214
215
216
217
218
Popular Tags