Notification.java 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  1. /* Notification.java -- A notification emitted by a bean.
  2. Copyright (C) 2006 Free Software Foundation, Inc.
  3. This file is part of GNU Classpath.
  4. GNU Classpath is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2, or (at your option)
  7. any later version.
  8. GNU Classpath is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GNU Classpath; see the file COPYING. If not, write to the
  14. Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  15. 02110-1301 USA.
  16. Linking this library statically or dynamically with other modules is
  17. making a combined work based on this library. Thus, the terms and
  18. conditions of the GNU General Public License cover the whole
  19. combination.
  20. As a special exception, the copyright holders of this library give you
  21. permission to link this library with independent modules to produce an
  22. executable, regardless of the license terms of these independent
  23. modules, and to copy and distribute the resulting executable under
  24. terms of your choice, provided that you also meet, for each linked
  25. independent module, the terms and conditions of the license of that
  26. module. An independent module is a module which is not derived from
  27. or based on this library. If you modify this library, you may extend
  28. this exception to your version of the library, but you are not
  29. obligated to do so. If you do not wish to do so, delete this
  30. exception statement from your version. */
  31. package javax.management;
  32. import java.io.IOException;
  33. import java.io.ObjectOutputStream;
  34. import java.util.Date;
  35. import java.util.EventObject;
  36. /**
  37. * <p>
  38. * A notification message that may be emitted by a bean.
  39. * Notifications have both a message and a type, so individual
  40. * notifications can be grouped by type. They also incorporate
  41. * sequencing, so that the recipient can order the delivered
  42. * messages correctly (there is no guarantee that they will
  43. * be delivered in order).
  44. * </p>
  45. * <p>
  46. * Notifications also include a reference to the source of
  47. * the notification. The source bean is represented either
  48. * by an {@link ObjectName} or by a direct reference to the
  49. * bean. The former is preferable, and notifications emitted
  50. * via a {@link MBeanServer} will automatically have the source
  51. * transformed into an {@link ObjectName}.
  52. * </p>
  53. *
  54. * @author Andrew John Hughes (gnu_andrew@member.fsf.org)
  55. * @since 1.5
  56. */
  57. public class Notification
  58. extends EventObject
  59. {
  60. /**
  61. * Compatible with JDK 1.6
  62. */
  63. private static final long serialVersionUID = -7516092053498031989L;
  64. /**
  65. * The notification message.
  66. *
  67. * @serial the notification message.
  68. */
  69. private String message;
  70. /**
  71. * The notification's sequence number, relative to the notifications
  72. * emitted by the bean.
  73. *
  74. * @serial the notification sequence number.
  75. */
  76. private long sequenceNumber;
  77. /**
  78. * The source of the notification. This is redeclared in order to
  79. * replace the <code>source</code> variable in {@link java.util.EventObject}
  80. * with a non-transient version.
  81. *
  82. * @serial the notification source.
  83. */
  84. protected Object source;
  85. /**
  86. * The time the notification was generated.
  87. *
  88. * @serial the notification timestamp.
  89. */
  90. private long timeStamp;
  91. /**
  92. * The type of notification sent. This utilises the same style
  93. * as Java property and package names. For example,
  94. * <code>gnu.gcj.compiler</code> may be one type of notifications.
  95. *
  96. * @serial the notification type.
  97. */
  98. private String type;
  99. /**
  100. * The user data associated with the notification. This includes
  101. * any additional data which should be transmitted with the notification,
  102. * but can't be achieved using the {@link java.lang.String} format
  103. * of the <code>message</code>.
  104. *
  105. * @serial the notification user data.
  106. */
  107. private Object userData;
  108. /**
  109. * Creates a new {@link Notification} object with the specified type,
  110. * source and sequence number. The timestamp is created using the
  111. * current date and time.
  112. *
  113. * @param type the type of the notification.
  114. * @param source the source of the notification.
  115. * @param sequenceNumber the sequence number of the notifcation.
  116. */
  117. public Notification(String type, Object source, long sequenceNumber)
  118. {
  119. this(type, source, sequenceNumber, new Date().getTime());
  120. }
  121. /**
  122. * Creates a new {@link Notification} object with the specified type,
  123. * source, sequence number and timestamp.
  124. *
  125. * @param type the type of the notification.
  126. * @param source the source of the notification.
  127. * @param sequenceNumber the sequence number of the notifcation.
  128. * @param timeStamp the time the notification was emitted.
  129. */
  130. public Notification(String type, Object source, long sequenceNumber,
  131. long timeStamp)
  132. {
  133. this(type, source, sequenceNumber, timeStamp, "");
  134. }
  135. /**
  136. * Creates a new {@link Notification} object with the specified type,
  137. * source, sequence number, timestamp and message.
  138. *
  139. * @param type the type of the notification.
  140. * @param source the source of the notification.
  141. * @param sequenceNumber the sequence number of the notifcation.
  142. * @param timeStamp the time the notification was emitted.
  143. * @param message the message contained in the notification.
  144. */
  145. public Notification(String type, Object source, long sequenceNumber,
  146. long timeStamp, String message)
  147. {
  148. super(source);
  149. this.type = type;
  150. this.source = source;
  151. this.sequenceNumber = sequenceNumber;
  152. this.timeStamp = timeStamp;
  153. this.message = message;
  154. }
  155. /**
  156. * Creates a new {@link Notification} object with the specified type,
  157. * source, sequence number and message. The timestamp is created using
  158. * the current date and time.
  159. *
  160. * @param type the type of the notification.
  161. * @param source the source of the notification.
  162. * @param sequenceNumber the sequence number of the notifcation.
  163. * @param message the message contained in the notification.
  164. */
  165. public Notification(String type, Object source, long sequenceNumber,
  166. String message)
  167. {
  168. this(type, source, sequenceNumber, new Date().getTime(), message);
  169. }
  170. /**
  171. * Returns the message contained in this notification. The message
  172. * is in {@link java.lang.String} form, and is thus intended for
  173. * display to the end-user. Data transferred as part of the notification
  174. * which shouldn't be displayed is included in the <code>userData</code>
  175. * field.
  176. *
  177. * @return the notification message.
  178. * @see #getUserData()
  179. * @see #setUserData(java.lang.Object)
  180. */
  181. public String getMessage()
  182. {
  183. return message;
  184. }
  185. /**
  186. * Returns the sequence number of this notification. This
  187. * can be used to determine the order in which notifications
  188. * were emitted by the broadcasting bean.
  189. *
  190. * @return the sequence number.
  191. * @see #setSequenceNumber(long)
  192. */
  193. public long getSequenceNumber()
  194. {
  195. return sequenceNumber;
  196. }
  197. /**
  198. * Returns the date and time at which this notification was
  199. * emitted.
  200. *
  201. * @return the notification timestamp.
  202. * @see #setTimeStamp(long)
  203. */
  204. public long getTimeStamp()
  205. {
  206. return timeStamp;
  207. }
  208. /**
  209. * Returns the type of this notification. Types take the same
  210. * form as Java package and property names.
  211. *
  212. * @return the type of the notification.
  213. */
  214. public String getType()
  215. {
  216. return type;
  217. }
  218. /**
  219. * Returns the additional user data associated with the notification.
  220. * This is used to attach additional non-textual information to the
  221. * notification.
  222. *
  223. * @return the user data associated with the notification.
  224. * @see #setUserData(java.lang.Object)
  225. */
  226. public Object getUserData()
  227. {
  228. return userData;
  229. }
  230. /**
  231. * Sets the sequence number to the value specified.
  232. *
  233. * @param sequenceNumber the new sequence number.
  234. * @see #getSequenceNumber()
  235. */
  236. public void setSequenceNumber(long sequenceNumber)
  237. {
  238. this.sequenceNumber = sequenceNumber;
  239. }
  240. /**
  241. * Sets the source of this notification to the value
  242. * specified.
  243. *
  244. * @param source the new source of the notification.
  245. * @see java.util.EventSource#getSource()
  246. */
  247. public void setSource(Object source)
  248. {
  249. this.source = source;
  250. }
  251. /**
  252. * Sets the date and time at which this notification
  253. * was emitted.
  254. *
  255. * @param timeStamp the new time stamp of the notification.
  256. * @see #getTimeStamp()
  257. */
  258. public void setTimeStamp(long timeStamp)
  259. {
  260. this.timeStamp = timeStamp;
  261. }
  262. /**
  263. * Sets the additional user data associated with the notification
  264. * to the specified value. This is used to attach additional
  265. * non-textual information to the notification.
  266. *
  267. * @param userData the new user data associated with the notification.
  268. * @see #getUserData()
  269. */
  270. public void setUserData(Object userData)
  271. {
  272. this.userData = userData;
  273. }
  274. /**
  275. * A textual representation of the notification.
  276. *
  277. * @return the notification in {@link java.lang.String} form.
  278. */
  279. public String toString()
  280. {
  281. return getClass().getName()
  282. + "[message=" + message
  283. + ", sequenceNumber=" + sequenceNumber
  284. + ", source=" + source
  285. + ", timeStamp=" + timeStamp
  286. + ", type=" + type
  287. + ", userData=" + userData
  288. + "]";
  289. }
  290. /**
  291. * Serialize the {@link Notification}.
  292. *
  293. * @param out the output stream to write to.
  294. * @throws IOException if an I/O error occurs.
  295. */
  296. private void writeObject(ObjectOutputStream out)
  297. throws IOException
  298. {
  299. out.defaultWriteObject();
  300. }
  301. }