KickJava   Java API By Example, From Geeks To Geeks.

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


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 package com.sun.enterprise.management.support;
24
25 import java.util.Set JavaDoc;
26 import java.util.HashSet JavaDoc;
27 import java.util.Iterator JavaDoc;
28 import java.util.Map JavaDoc;
29 import java.util.HashMap JavaDoc;
30 import java.util.Collections JavaDoc;
31
32 import javax.management.Notification JavaDoc;
33 import javax.management.MBeanNotificationInfo JavaDoc;
34
35
36 import com.sun.appserv.management.base.AMX;
37 import com.sun.appserv.management.base.NotificationEmitterService;
38
39 import com.sun.appserv.management.util.misc.GSetUtil;
40 import com.sun.appserv.management.util.jmx.JMXUtil;
41
42
43
44 /**
45     A key aspect of the implementation is that it maintains a list of all types
46     of Notifications it has seen emitted, paired with the implementing class
47     so that getNotificationInfo() can return infos that accurately reflect
48     what it is likely to emit. This approach is used instead of statically
49     coding Notification classes/types because it cannot be known in advance
50     which type(s) will actually be emitted.
51  */

52 public final class NotificationEmitterServiceImpl extends AMXImplBase
53     implements NotificationEmitterService
54 {
55     /**
56         Each class of Notification may have one or more types associated with it,
57         stored in a Set&lt;String&gt;:<br>
58         Map&ltnotif classname, Set&ltString of notif type&gt;&gt;
59      */

60     private final Map JavaDoc<String JavaDoc,Set JavaDoc<String JavaDoc>> mEmittedTypes;
61     
62         public
63     NotificationEmitterServiceImpl( )
64     {
65         mEmittedTypes = Collections.synchronizedMap( new HashMap JavaDoc<String JavaDoc,Set JavaDoc<String JavaDoc>>() );
66     }
67
68         public String JavaDoc
69     getGroup()
70     {
71         return( AMX.GROUP_UTILITY );
72     }
73     
74         public boolean
75     getMBeanInfoIsInvariant()
76     {
77         return( false ); // MBeanNotificationInfo may grow as we learn of new types
78
}
79     
80     
81         public MBeanNotificationInfo JavaDoc[]
82     getNotificationInfo()
83     {
84         final MBeanNotificationInfo JavaDoc[] superTypes = super.getNotificationInfo();
85         
86         final Set JavaDoc<MBeanNotificationInfo JavaDoc> dynamicInfos =
87             new HashSet JavaDoc<MBeanNotificationInfo JavaDoc>();
88         
89         /*
90             Go through our list of class/types we've seen emitted so far
91             and generate MBeanNotificationInfo for each class of Notification
92          */

93         for( final String JavaDoc notifClass : mEmittedTypes.keySet() )
94         {
95             final Set JavaDoc<String JavaDoc> types = mEmittedTypes.get( notifClass );
96                 
97             final MBeanNotificationInfo JavaDoc info = new MBeanNotificationInfo JavaDoc(
98                 GSetUtil.toStringArray( types ), notifClass, "" );
99             
100             dynamicInfos.add( info );
101         }
102         
103         final MBeanNotificationInfo JavaDoc[] dynamicInfosArray = (MBeanNotificationInfo JavaDoc[])
104             dynamicInfos.toArray( new MBeanNotificationInfo JavaDoc[ dynamicInfos.size() ] );
105         
106         // now merge what we've found with super's types
107

108         final MBeanNotificationInfo JavaDoc[] allInfos =
109             JMXUtil.mergeMBeanNotificationInfos( superTypes, dynamicInfosArray );
110         
111         return allInfos;
112     }
113     
114     
115     /**
116         Cumulatively update our Map of Notification classes and the types corresponding
117         to each class. A few of the standard ones are:
118
119 <code>
120         javax.management.AttributeChangeNotification =>
121             { AttributeChangeNotification.ATTRIBUTE_CHANGE }
122             
123         javax.management.MBeanServerNotification =>
124             { MBeanServerNotification.REGISTRATION_NOTIFICATION,
125                 MBeanServerNotification.UNREGISTRATION_NOTIFICATION }
126         
127         Various others are likely to be just generic Notification:
128         javax.management.Notification =>
129             {
130                 x.y.z,
131                 a.b.c,
132                 ...
133             }
134 </code>
135      */

136         private void
137     cumulateTypes( final Notification JavaDoc notif )
138     {
139         final String JavaDoc notifClass = notif.getClass().getName();
140         final String JavaDoc type = notif.getType();
141         
142         if ( ! mEmittedTypes.containsKey( notifClass ) )
143         {
144             final Set JavaDoc<String JavaDoc> types = GSetUtil.newStringSet( type );
145             mEmittedTypes.put( notifClass, types );
146         }
147         else
148         {
149             final Set JavaDoc<String JavaDoc> types = mEmittedTypes.get( notifClass );
150             if ( ! types.contains( type ) )
151             {
152                 types.add( type );
153             }
154         }
155     }
156
157     /**
158         Note that this method may be used to test the functionality of this service
159         by asking it to emit any desired Notification.
160         <p>
161         Note that this method is likely to be called from many different threads.
162      */

163         public void
164     emitNotification( final Notification JavaDoc notif )
165     {
166         cumulateTypes( notif );
167         
168         if ( getListenerCount() != 0 )
169         {
170             sendNotification( notif );
171         }
172     }
173 }
174
175
176
177
178
179
180
181
182
183
184
185
Popular Tags