KickJava   Java API By Example, From Geeks To Geeks.

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


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.appserv.management.util.jmx;
24
25 import java.util.Map JavaDoc;
26 import java.util.HashMap JavaDoc;
27 import java.io.Serializable JavaDoc;
28
29 import javax.management.Notification JavaDoc;
30 import javax.management.ObjectName JavaDoc;
31
32 /**
33     Base class for building AMX Notifications. AMX Notifications
34     all place a Map in the userData field. This class takes care
35     of building Notifications with correct time stamp, sequence number,
36     etc. It also enforces certain conventions.
37     <p>
38     A convenience routine is provided for adding additional fields to
39     the Map--putMapData().
40  */

41 public class NotificationBuilder
42 {
43     private long mSequenceNumber = 0;
44     private final String JavaDoc mNotificationType;
45     private final Object JavaDoc mSource;
46     
47         protected synchronized long
48     nextSequenceNumber( )
49     {
50         return( mSequenceNumber++ );
51     }
52     
53         public
54     NotificationBuilder(
55         final String JavaDoc notificationType,
56         final Object JavaDoc source )
57     {
58         mNotificationType = notificationType;
59         mSource = source;
60     }
61     
62         public final String JavaDoc
63     getNotificationType()
64     {
65         return( mNotificationType );
66     }
67     
68         public final Object JavaDoc
69     getSource()
70     {
71         return( mSource );
72     }
73     
74     
75         protected final long
76     now()
77     {
78         return( System.currentTimeMillis() );
79     }
80     
81     /**
82         Build a new Notification with an existing Map.
83      */

84         public Notification JavaDoc
85     buildNewWithMap(
86         final String JavaDoc message,
87         final Map JavaDoc<String JavaDoc,Serializable JavaDoc> userDataMap )
88     {
89         final Notification JavaDoc notif = new Notification JavaDoc(
90             mNotificationType,
91             mSource,
92             nextSequenceNumber(),
93             now(),
94             message);
95     
96         if ( userDataMap != null )
97         {
98             notif.setUserData( userDataMap );
99         }
100         else
101         {
102             notif.setUserData( new HashMap JavaDoc<String JavaDoc,Serializable JavaDoc>() );
103         }
104         
105         return( notif );
106     }
107     
108     
109     
110     /**
111         Build a new Notification without any values in its Map
112         and no message.
113      */

114         public Notification JavaDoc
115     buildNew()
116     {
117         return buildNew( mNotificationType );
118     }
119     
120     
121     /**
122         Build a new Notification without any values in its Map.
123         @param message
124      */

125         public Notification JavaDoc
126     buildNew( final String JavaDoc message )
127     {
128         return buildNewWithMap( message, null );
129     }
130     
131     /**
132         Build a new Notification with one key/value for the Map.
133         public Notification
134     buildNew(
135         final String key,
136         final Serializable value )
137     {
138         if ( value instanceof Map )
139         {
140             throw new IllegalArgumentException("use buildNewWithMap" );
141         }
142
143         final Notification notif = buildNew();
144         
145         if ( key != null )
146         {
147             putMapData( notif, key, value );
148         }
149         
150         return( notif );
151     }
152      */

153     
154     /**
155         Build a new Notification with one key/value for the Map.
156         public Notification
157     buildNew(
158         final String key,
159         final Serializable value,
160         final String message )
161     {
162         final Notification notif = buildNew( message );
163         
164         if ( key != null )
165         {
166             putMapData( notif, key, value );
167         }
168         
169         return( notif );
170     }
171      */

172     
173     
174     
175     
176     
177     /**
178         Put a single key/value pair into the user data Map.
179      */

180         public static final void
181     putMapData(
182         final Notification JavaDoc notif,
183         final String JavaDoc keyToInsert,
184         final Serializable JavaDoc valueToInsert )
185     {
186         final Map JavaDoc<String JavaDoc,Serializable JavaDoc> userData =
187             JMXUtil.getUserDataMapString_Serializable( notif );
188         
189         userData.put( keyToInsert, valueToInsert );
190     }
191     
192     /**
193         Put all key/value pairs into the user data Map.
194      */

195         public static final <T extends Serializable JavaDoc> void
196     putAllMapData(
197         final Notification JavaDoc notif,
198         final Map JavaDoc<String JavaDoc,T> additionalUserData )
199     {
200         final Map JavaDoc<String JavaDoc,Serializable JavaDoc> userData =
201             JMXUtil.getUserDataMapString_Serializable( notif );
202             
203         userData.putAll( additionalUserData );
204     }
205     
206     
207 }
208
209
210
211
212
213
Popular Tags