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. package javax.management;
  8. import java.io.IOException;
  9. import java.io.ObjectInputStream;
  10. import java.io.ObjectOutputStream;
  11. import java.io.ObjectStreamField;
  12. import java.util.Date;
  13. import java.util.EventObject;
  14. import java.security.AccessController;
  15. import java.security.PrivilegedAction;
  16. import com.sun.jmx.mbeanserver.GetPropertyAction;
  17. /**
  18. * <p>The Notification class represents a notification emitted by an
  19. * MBean. It contains a reference to the source MBean: if the
  20. * notification has been forwarded through the MBean server, and the
  21. * original source of the notification was a reference to the emitting
  22. * MBean object, then the MBean server replaces it by the MBean's
  23. * ObjectName. If the listener has registered directly with the
  24. * MBean, this is either the object name or a direct reference to the
  25. * MBean.</p>
  26. *
  27. * <p>It is strongly recommended that notification senders use the
  28. * object name rather than a reference to the MBean object as the
  29. * source.</p>
  30. *
  31. * @since 1.5
  32. */
  33. public class Notification extends EventObject {
  34. // Serialization compatibility stuff:
  35. // Two serial forms are supported in this class. The selected form depends
  36. // on system property "jmx.serial.form":
  37. // - "1.0" for JMX 1.0
  38. // - any other value for JMX 1.1 and higher
  39. //
  40. // Serial version for old serial form
  41. private static final long oldSerialVersionUID = 1716977971058914352L;
  42. //
  43. // Serial version for new serial form
  44. private static final long newSerialVersionUID = -7516092053498031989L;
  45. //
  46. // Serializable fields in old serial form
  47. private static final ObjectStreamField[] oldSerialPersistentFields =
  48. {
  49. new ObjectStreamField("message", String.class),
  50. new ObjectStreamField("sequenceNumber", Long.TYPE),
  51. new ObjectStreamField("source", Object.class),
  52. new ObjectStreamField("sourceObjectName", ObjectName.class),
  53. new ObjectStreamField("timeStamp", Long.TYPE),
  54. new ObjectStreamField("type", String.class),
  55. new ObjectStreamField("userData", Object.class)
  56. };
  57. //
  58. // Serializable fields in new serial form
  59. private static final ObjectStreamField[] newSerialPersistentFields =
  60. {
  61. new ObjectStreamField("message", String.class),
  62. new ObjectStreamField("sequenceNumber", Long.TYPE),
  63. new ObjectStreamField("source", Object.class),
  64. new ObjectStreamField("timeStamp", Long.TYPE),
  65. new ObjectStreamField("type", String.class),
  66. new ObjectStreamField("userData", Object.class)
  67. };
  68. //
  69. // Actual serial version and serial form
  70. private static final long serialVersionUID;
  71. /**
  72. * @serialField type String The notification type.
  73. * A string expressed in a dot notation similar to Java properties.
  74. * An example of a notification type is network.alarm.router
  75. * @serialField sequenceNumber long The notification sequence number.
  76. * A serial number which identify particular instance
  77. * of notification in the context of the notification source.
  78. * @serialField timeStamp long The notification timestamp.
  79. * Indicating when the notification was generated
  80. * @serialField userData Object The notification user data.
  81. * Used for whatever other data the notification
  82. * source wishes to communicate to its consumers
  83. * @serialField message String The notification message.
  84. * @serialField source Object The object on which the notification initially occurred.
  85. */
  86. private static final ObjectStreamField[] serialPersistentFields;
  87. private static boolean compat = false;
  88. static {
  89. try {
  90. PrivilegedAction act = new GetPropertyAction("jmx.serial.form");
  91. String form = (String) AccessController.doPrivileged(act);
  92. compat = (form != null && form.equals("1.0"));
  93. } catch (Exception e) {
  94. // OK: exception means no compat with 1.0, too bad
  95. }
  96. if (compat) {
  97. serialPersistentFields = oldSerialPersistentFields;
  98. serialVersionUID = oldSerialVersionUID;
  99. } else {
  100. serialPersistentFields = newSerialPersistentFields;
  101. serialVersionUID = newSerialVersionUID;
  102. }
  103. }
  104. //
  105. // END Serialization compatibility stuff
  106. /**
  107. * @serial The notification type.
  108. * A string expressed in a dot notation similar to Java properties.
  109. * An example of a notification type is network.alarm.router
  110. */
  111. private String type;
  112. /**
  113. * @serial The notification sequence number.
  114. * A serial number which identify particular instance
  115. * of notification in the context of the notification source.
  116. */
  117. private long sequenceNumber;
  118. /**
  119. * @serial The notification timestamp.
  120. * Indicating when the notification was generated
  121. */
  122. private long timeStamp;
  123. /**
  124. * @serial The notification user data.
  125. * Used for whatever other data the notification
  126. * source wishes to communicate to its consumers
  127. */
  128. private Object userData = null;
  129. /**
  130. * @serial The notification message.
  131. */
  132. private String message = "";
  133. /**
  134. * <p>This field hides the {@link EventObject#source} field in the
  135. * parent class to make it non-transient and therefore part of the
  136. * serialized form.</p>
  137. *
  138. * @serial The object on which the notification initially occurred.
  139. */
  140. protected Object source = null;
  141. /**
  142. * Creates a Notification object.
  143. * The notification timeStamp is set to the current date.
  144. *
  145. * @param type The notification type.
  146. * @param source The notification source.
  147. * @param sequenceNumber The notification sequence number within the source object.
  148. *
  149. */
  150. public Notification(String type, Object source, long sequenceNumber) {
  151. super (source) ;
  152. this.source = source;
  153. this.type = type;
  154. this.sequenceNumber = sequenceNumber ;
  155. this.timeStamp = (new java.util.Date()).getTime() ;
  156. }
  157. /**
  158. * Creates a Notification object.
  159. * The notification timeStamp is set to the current date.
  160. *
  161. * @param type The notification type.
  162. * @param source The notification source.
  163. * @param sequenceNumber The notification sequence number within the source object.
  164. * @param message The detailed message.
  165. *
  166. */
  167. public Notification(String type, Object source, long sequenceNumber, String message) {
  168. super (source) ;
  169. this.source = source;
  170. this.type = type;
  171. this.sequenceNumber = sequenceNumber ;
  172. this.timeStamp = (new java.util.Date()).getTime() ;
  173. this.message = message ;
  174. }
  175. /**
  176. * Creates a Notification object.
  177. *
  178. * @param type The notification type.
  179. * @param source The notification source.
  180. * @param sequenceNumber The notification sequence number within the source object.
  181. * @param timeStamp The notification emission date.
  182. *
  183. */
  184. public Notification(String type, Object source, long sequenceNumber, long timeStamp) {
  185. super (source) ;
  186. this.source = source;
  187. this.type = type ;
  188. this.sequenceNumber = sequenceNumber ;
  189. this.timeStamp = timeStamp ;
  190. }
  191. /**
  192. * Creates a Notification object.
  193. *
  194. * @param type The notification type.
  195. * @param source The notification source.
  196. * @param sequenceNumber The notification sequence number within the source object.
  197. * @param timeStamp The notification emission date.
  198. * @param message The detailed message.
  199. *
  200. */
  201. public Notification(String type, Object source, long sequenceNumber, long timeStamp, String message) {
  202. super (source) ;
  203. this.source = source;
  204. this.type = type ;
  205. this.sequenceNumber = sequenceNumber ;
  206. this.timeStamp = timeStamp ;
  207. this.message = message ;
  208. }
  209. /**
  210. * Sets the source.
  211. *
  212. * @param source the new source for this object.
  213. *
  214. * @see EventObject#getSource
  215. */
  216. public void setSource(Object source) {
  217. super.source = source;
  218. this.source = source;
  219. }
  220. /**
  221. * Get the notification sequence number.
  222. *
  223. * @return The notification sequence number within the source object. It's a serial number
  224. * identifying a particular instance of notification in the context of the notification source.
  225. * The notification model does not assume that notifications will be received in the same order
  226. * that they are sent. The sequence number helps listeners to sort received notifications.
  227. *
  228. * @see #setSequenceNumber
  229. */
  230. public long getSequenceNumber() {
  231. return sequenceNumber ;
  232. }
  233. /**
  234. * Set the notification sequence number.
  235. *
  236. * @param sequenceNumber The notification sequence number within the source object. It is
  237. * a serial number identifying a particular instance of notification in the
  238. * context of the notification source.
  239. *
  240. * @see #getSequenceNumber
  241. */
  242. public void setSequenceNumber(long sequenceNumber) {
  243. this.sequenceNumber = sequenceNumber;
  244. }
  245. /**
  246. * Get the notification type.
  247. *
  248. * @return The notification type. It's a string expressed in a dot notation similar
  249. * to Java properties. An example of a notification type is network.alarm.router .
  250. */
  251. public String getType() {
  252. return type ;
  253. }
  254. /**
  255. * Get the notification timestamp.
  256. *
  257. * @return The notification timestamp.
  258. *
  259. * @see #setTimeStamp
  260. */
  261. public long getTimeStamp() {
  262. return timeStamp ;
  263. }
  264. /**
  265. * Set the notification timestamp.
  266. *
  267. * @param timeStamp The notification timestamp. It indicates when the notification was generated.
  268. *
  269. * @see #getTimeStamp
  270. */
  271. public void setTimeStamp(long timeStamp) {
  272. this.timeStamp = timeStamp;
  273. }
  274. /**
  275. * Get the notification message.
  276. *
  277. * @return The message string of this notification object. It contains in a string,
  278. * which could be the explanation of the notification for displaying to a user
  279. *
  280. */
  281. public String getMessage() {
  282. return message ;
  283. }
  284. /**
  285. * Get the user data.
  286. *
  287. * @return The user data object. It is used for whatever data
  288. * the notification source wishes to communicate to its consumers.
  289. *
  290. * @see #setUserData
  291. */
  292. public Object getUserData() {
  293. return userData ;
  294. }
  295. /**
  296. * Set the user data.
  297. *
  298. * @param userData The user data object. It is used for whatever data
  299. * the notification source wishes to communicate to its consumers.
  300. *
  301. * @see #getUserData
  302. */
  303. public void setUserData(Object userData) {
  304. this.userData = userData ;
  305. }
  306. /**
  307. * Returns a String representation of this notification.
  308. *
  309. * @return A String representation of this notification.
  310. */
  311. public String toString() {
  312. return super.toString()+"[type="+type+"][message="+message+"]";
  313. }
  314. /**
  315. * Deserializes a {@link Notification} from an {@link ObjectInputStream}.
  316. */
  317. private void readObject(ObjectInputStream in)
  318. throws IOException, ClassNotFoundException {
  319. // New serial form ignores extra field "sourceObjectName"
  320. in.defaultReadObject();
  321. super.source = source;
  322. }
  323. /**
  324. * Serializes a {@link Notification} to an {@link ObjectOutputStream}.
  325. */
  326. private void writeObject(ObjectOutputStream out)
  327. throws IOException {
  328. if (compat) {
  329. // Serializes this instance in the old serial form
  330. //
  331. ObjectOutputStream.PutField fields = out.putFields();
  332. fields.put("type", type);
  333. fields.put("sequenceNumber", sequenceNumber);
  334. fields.put("timeStamp", timeStamp);
  335. fields.put("userData", userData);
  336. fields.put("message", message);
  337. fields.put("source", source);
  338. out.writeFields();
  339. } else {
  340. // Serializes this instance in the new serial form
  341. //
  342. out.defaultWriteObject();
  343. }
  344. }
  345. }