KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > javax > management > Notification


1 /*
2  * @(#)Notification.java 4.40 03/12/19
3  *
4  * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
5  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6  */

7
8 package javax.management;
9
10 import java.io.IOException JavaDoc;
11 import java.io.ObjectInputStream JavaDoc;
12 import java.io.ObjectOutputStream JavaDoc;
13 import java.io.ObjectStreamField JavaDoc;
14
15 import java.util.Date JavaDoc;
16 import java.util.EventObject JavaDoc;
17
18 import java.security.AccessController JavaDoc;
19 import java.security.PrivilegedAction JavaDoc;
20
21 import com.sun.jmx.mbeanserver.GetPropertyAction;
22
23 /**
24  * <p>The Notification class represents a notification emitted by an
25  * MBean. It contains a reference to the source MBean: if the
26  * notification has been forwarded through the MBean server, and the
27  * original source of the notification was a reference to the emitting
28  * MBean object, then the MBean server replaces it by the MBean's
29  * ObjectName. If the listener has registered directly with the
30  * MBean, this is either the object name or a direct reference to the
31  * MBean.</p>
32  *
33  * <p>It is strongly recommended that notification senders use the
34  * object name rather than a reference to the MBean object as the
35  * source.</p>
36  *
37  * @since 1.5
38  */

39 public class Notification extends EventObject JavaDoc {
40
41     // Serialization compatibility stuff:
42
// Two serial forms are supported in this class. The selected form depends
43
// on system property "jmx.serial.form":
44
// - "1.0" for JMX 1.0
45
// - any other value for JMX 1.1 and higher
46
//
47
// Serial version for old serial form
48
private static final long oldSerialVersionUID = 1716977971058914352L;
49     //
50
// Serial version for new serial form
51
private static final long newSerialVersionUID = -7516092053498031989L;
52     //
53
// Serializable fields in old serial form
54
private static final ObjectStreamField JavaDoc[] oldSerialPersistentFields =
55     {
56     new ObjectStreamField JavaDoc("message", String JavaDoc.class),
57         new ObjectStreamField JavaDoc("sequenceNumber", Long.TYPE),
58         new ObjectStreamField JavaDoc("source", Object JavaDoc.class),
59         new ObjectStreamField JavaDoc("sourceObjectName", ObjectName JavaDoc.class),
60         new ObjectStreamField JavaDoc("timeStamp", Long.TYPE),
61         new ObjectStreamField JavaDoc("type", String JavaDoc.class),
62         new ObjectStreamField JavaDoc("userData", Object JavaDoc.class)
63     };
64     //
65
// Serializable fields in new serial form
66
private static final ObjectStreamField JavaDoc[] newSerialPersistentFields =
67     {
68     new ObjectStreamField JavaDoc("message", String JavaDoc.class),
69         new ObjectStreamField JavaDoc("sequenceNumber", Long.TYPE),
70         new ObjectStreamField JavaDoc("source", Object JavaDoc.class),
71         new ObjectStreamField JavaDoc("timeStamp", Long.TYPE),
72         new ObjectStreamField JavaDoc("type", String JavaDoc.class),
73         new ObjectStreamField JavaDoc("userData", Object JavaDoc.class)
74     };
75     //
76
// Actual serial version and serial form
77
private static final long serialVersionUID;
78     /**
79      * @serialField type String The notification type.
80      * A string expressed in a dot notation similar to Java properties.
81      * An example of a notification type is network.alarm.router
82      * @serialField sequenceNumber long The notification sequence number.
83      * A serial number which identify particular instance
84      * of notification in the context of the notification source.
85      * @serialField timeStamp long The notification timestamp.
86      * Indicating when the notification was generated
87      * @serialField userData Object The notification user data.
88      * Used for whatever other data the notification
89      * source wishes to communicate to its consumers
90      * @serialField message String The notification message.
91      * @serialField source Object The object on which the notification initially occurred.
92      */

93     private static final ObjectStreamField JavaDoc[] serialPersistentFields;
94     private static boolean compat = false;
95     static {
96     try {
97         PrivilegedAction JavaDoc act = new GetPropertyAction("jmx.serial.form");
98         String JavaDoc form = (String JavaDoc) AccessController.doPrivileged(act);
99         compat = (form != null && form.equals("1.0"));
100     } catch (Exception JavaDoc e) {
101         // OK: exception means no compat with 1.0, too bad
102
}
103     if (compat) {
104         serialPersistentFields = oldSerialPersistentFields;
105         serialVersionUID = oldSerialVersionUID;
106     } else {
107         serialPersistentFields = newSerialPersistentFields;
108         serialVersionUID = newSerialVersionUID;
109     }
110     }
111     //
112
// END Serialization compatibility stuff
113

114     /**
115      * @serial The notification type.
116      * A string expressed in a dot notation similar to Java properties.
117      * An example of a notification type is network.alarm.router
118      */

119     private String JavaDoc type;
120     
121     /**
122      * @serial The notification sequence number.
123      * A serial number which identify particular instance
124      * of notification in the context of the notification source.
125      */

126     private long sequenceNumber;
127     
128     /**
129      * @serial The notification timestamp.
130      * Indicating when the notification was generated
131      */

132     private long timeStamp;
133
134     /**
135      * @serial The notification user data.
136      * Used for whatever other data the notification
137      * source wishes to communicate to its consumers
138      */

139     private Object JavaDoc userData = null;
140
141     /**
142      * @serial The notification message.
143      */

144     private String JavaDoc message = "";
145     
146     /**
147      * <p>This field hides the {@link EventObject#source} field in the
148      * parent class to make it non-transient and therefore part of the
149      * serialized form.</p>
150      *
151      * @serial The object on which the notification initially occurred.
152      */

153     protected Object JavaDoc source = null;
154     
155     
156     /**
157      * Creates a Notification object.
158      * The notification timeStamp is set to the current date.
159      *
160      * @param type The notification type.
161      * @param source The notification source.
162      * @param sequenceNumber The notification sequence number within the source object.
163      *
164      */

165     public Notification(String JavaDoc type, Object JavaDoc source, long sequenceNumber) {
166         super (source) ;
167     this.source = source;
168         this.type = type;
169         this.sequenceNumber = sequenceNumber ;
170         this.timeStamp = (new java.util.Date JavaDoc()).getTime() ;
171     }
172
173     /**
174      * Creates a Notification object.
175      * The notification timeStamp is set to the current date.
176      *
177      * @param type The notification type.
178      * @param source The notification source.
179      * @param sequenceNumber The notification sequence number within the source object.
180      * @param message The detailed message.
181      *
182      */

183     public Notification(String JavaDoc type, Object JavaDoc source, long sequenceNumber, String JavaDoc message) {
184         super (source) ;
185     this.source = source;
186         this.type = type;
187         this.sequenceNumber = sequenceNumber ;
188         this.timeStamp = (new java.util.Date JavaDoc()).getTime() ;
189         this.message = message ;
190     }
191     
192     /**
193      * Creates a Notification object.
194      *
195      * @param type The notification type.
196      * @param source The notification source.
197      * @param sequenceNumber The notification sequence number within the source object.
198      * @param timeStamp The notification emission date.
199      *
200      */

201     public Notification(String JavaDoc type, Object JavaDoc source, long sequenceNumber, long timeStamp) {
202         super (source) ;
203     this.source = source;
204         this.type = type ;
205         this.sequenceNumber = sequenceNumber ;
206         this.timeStamp = timeStamp ;
207     }
208     
209     /**
210      * Creates a Notification object.
211      *
212      * @param type The notification type.
213      * @param source The notification source.
214      * @param sequenceNumber The notification sequence number within the source object.
215      * @param timeStamp The notification emission date.
216      * @param message The detailed message.
217      *
218      */

219     public Notification(String JavaDoc type, Object JavaDoc source, long sequenceNumber, long timeStamp, String JavaDoc message) {
220         super (source) ;
221     this.source = source;
222         this.type = type ;
223         this.sequenceNumber = sequenceNumber ;
224         this.timeStamp = timeStamp ;
225         this.message = message ;
226     }
227
228     /**
229      * Sets the source.
230      *
231      * @param source the new source for this object.
232      *
233      * @see EventObject#getSource
234      */

235     public void setSource(Object JavaDoc source) {
236     super.source = source;
237     this.source = source;
238     }
239
240     /**
241      * Get the notification sequence number.
242      *
243      * @return The notification sequence number within the source object. It's a serial number
244      * identifying a particular instance of notification in the context of the notification source.
245      * The notification model does not assume that notifications will be received in the same order
246      * that they are sent. The sequence number helps listeners to sort received notifications.
247      *
248      * @see #setSequenceNumber
249      */

250     public long getSequenceNumber() {
251         return sequenceNumber ;
252     }
253     
254     /**
255      * Set the notification sequence number.
256      *
257      * @param sequenceNumber The notification sequence number within the source object. It is
258      * a serial number identifying a particular instance of notification in the
259      * context of the notification source.
260      *
261      * @see #getSequenceNumber
262      */

263     public void setSequenceNumber(long sequenceNumber) {
264         this.sequenceNumber = sequenceNumber;
265     }
266     
267     /**
268      * Get the notification type.
269      *
270      * @return The notification type. It's a string expressed in a dot notation similar
271      * to Java properties. An example of a notification type is network.alarm.router .
272      */

273     public String JavaDoc getType() {
274         return type ;
275     }
276     
277     /**
278      * Get the notification timestamp.
279      *
280      * @return The notification timestamp.
281      *
282      * @see #setTimeStamp
283      */

284     public long getTimeStamp() {
285         return timeStamp ;
286     }
287     
288     /**
289      * Set the notification timestamp.
290      *
291      * @param timeStamp The notification timestamp. It indicates when the notification was generated.
292      *
293      * @see #getTimeStamp
294      */

295     public void setTimeStamp(long timeStamp) {
296         this.timeStamp = timeStamp;
297     }
298     
299     /**
300      * Get the notification message.
301      *
302      * @return The message string of this notification object. It contains in a string,
303      * which could be the explanation of the notification for displaying to a user
304      *
305      */

306     public String JavaDoc getMessage() {
307         return message ;
308     }
309     
310     /**
311      * Get the user data.
312      *
313      * @return The user data object. It is used for whatever data
314      * the notification source wishes to communicate to its consumers.
315      *
316      * @see #setUserData
317      */

318     public Object JavaDoc getUserData() {
319         return userData ;
320     }
321     
322     /**
323      * Set the user data.
324      *
325      * @param userData The user data object. It is used for whatever data
326      * the notification source wishes to communicate to its consumers.
327      *
328      * @see #getUserData
329      */

330     public void setUserData(Object JavaDoc userData) {
331
332         this.userData = userData ;
333     }
334
335     /**
336      * Returns a String representation of this notification.
337      *
338      * @return A String representation of this notification.
339      */

340     public String JavaDoc toString() {
341     return super.toString()+"[type="+type+"][message="+message+"]";
342     }
343
344     /**
345      * Deserializes a {@link Notification} from an {@link ObjectInputStream}.
346      */

347     private void readObject(ObjectInputStream JavaDoc in)
348         throws IOException JavaDoc, ClassNotFoundException JavaDoc {
349       // New serial form ignores extra field "sourceObjectName"
350
in.defaultReadObject();
351       super.source = source;
352     }
353
354
355     /**
356      * Serializes a {@link Notification} to an {@link ObjectOutputStream}.
357      */

358     private void writeObject(ObjectOutputStream JavaDoc out)
359         throws IOException JavaDoc {
360     if (compat) {
361         // Serializes this instance in the old serial form
362
//
363
ObjectOutputStream.PutField JavaDoc fields = out.putFields();
364         fields.put("type", type);
365         fields.put("sequenceNumber", sequenceNumber);
366         fields.put("timeStamp", timeStamp);
367         fields.put("userData", userData);
368         fields.put("message", message);
369         fields.put("source", source);
370         out.writeFields();
371     } else {
372         // Serializes this instance in the new serial form
373
//
374
out.defaultWriteObject();
375     }
376     }
377 }
378
Popular Tags