KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > appserv > management > util > jmx > NotificationListenerTracking


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-core/mbeanapi/src/java/com/sun/appserv/management/util/jmx/NotificationListenerTracking.java,v 1.3 2005/12/25 03:51:29 tcfujii Exp $
26  * $Revision: 1.3 $
27  * $Date: 2005/12/25 03:51:29 $
28  */

29 package com.sun.appserv.management.util.jmx;
30
31 import java.util.List JavaDoc;
32 import java.util.ArrayList JavaDoc;
33 import java.util.Iterator JavaDoc;
34 import java.util.Collections JavaDoc;
35
36 import javax.management.NotificationListener JavaDoc;
37 import javax.management.NotificationFilter JavaDoc;
38
39
40 /**
41  */

42 public class NotificationListenerTracking
43 {
44     // NotificationListeners are not unique, so we can't use a Map
45
private final List JavaDoc<NotificationListenerInfo> mInfos;
46     
47         public
48     NotificationListenerTracking( boolean synchronize )
49     {
50         final List JavaDoc<NotificationListenerInfo> infos =
51             new ArrayList JavaDoc<NotificationListenerInfo>();
52         
53         mInfos = synchronize ? Collections.synchronizedList( infos ) : infos;
54     }
55     
56         public void
57     addNotificationListener(
58         NotificationListener JavaDoc listener,
59         NotificationFilter JavaDoc filter,
60         Object JavaDoc handback )
61     {
62         final NotificationListenerInfo info =
63             new NotificationListenerInfo( listener, filter, handback );
64             
65         mInfos.add( info );
66     }
67     
68     
69         public int
70     getListenerCount()
71     {
72         return mInfos.size();
73     }
74     
75         private final boolean
76     listenersEqual(
77         final NotificationListener JavaDoc listener1,
78         final NotificationListener JavaDoc listener2)
79     {
80         return( listener1 == listener2 );
81     }
82     
83         private final boolean
84     handbacksEqual(
85         final Object JavaDoc handback1,
86         final Object JavaDoc handback2)
87     {
88         return( handback1 == handback2 );
89     }
90     
91     /**
92         Remove <b>all instances</b> of the specified listener and return
93         their corresponding NotificationListenerInfo.
94         This behavior matches the behavior of
95         {@link javax.management.NotificationEmitter}.
96         
97         @return list of NotificationListenerInfo
98      */

99         public List JavaDoc<NotificationListenerInfo>
100     removeNotificationListener( final NotificationListener JavaDoc listener )
101     {
102         final Iterator JavaDoc iter = mInfos.iterator();
103         
104         final List JavaDoc<NotificationListenerInfo> results = new ArrayList JavaDoc<NotificationListenerInfo>();
105         
106         while( iter.hasNext() )
107         {
108             final NotificationListenerInfo info =
109                 (NotificationListenerInfo)iter.next();
110             
111             if ( listenersEqual( listener, info.getListener() ) )
112             {
113                 iter.remove();
114                 results.add( info );
115             }
116         }
117         
118         return( results );
119     }
120     
121     /**
122         Remove <b>the first instance</b> of the specified listener/filter/handback
123         combination and return its corresponding NotificationListenerInfo.
124         This behavior matches the behavior of
125         {@link javax.management.NotificationEmitter}.
126         
127         @return list of NotificationListenerInfo
128      */

129         public NotificationListenerInfo
130     removeNotificationListener(
131         final NotificationListener JavaDoc listener,
132         final NotificationFilter JavaDoc filter,
133         final Object JavaDoc handback )
134     {
135         final Iterator JavaDoc iter = mInfos.iterator();
136         NotificationListenerInfo result = null;
137         
138         while( iter.hasNext() )
139         {
140             final NotificationListenerInfo info =
141                 (NotificationListenerInfo)iter.next();
142             
143             if ( listenersEqual( listener, info.getListener() ) &&
144                 handbacksEqual( handback, info.getHandback() ) )
145             {
146                 iter.remove();
147                 result = info;
148                 break;
149             }
150         }
151         
152         return( result );
153     }
154 }
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
Popular Tags