NotificationBroadcasterSupport.java 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  1. /* NotificationBroadcasterSupport.java -- Supporting implementation.
  2. Copyright (C) 2007 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 gnu.javax.management.ListenerData;
  33. import java.util.ArrayList;
  34. import java.util.Iterator;
  35. import java.util.List;
  36. import java.util.concurrent.Executor;
  37. /**
  38. * <p>
  39. * Provides an implementation of the {@link NotificationEmitter}
  40. * interface, which beans may utilise by extension. By default,
  41. * a synchronous dispatch system is provided, whereby the
  42. * {@link #handleNotification(NotificationListener, Notification,
  43. * Object)} is called once per listener by
  44. * {*@link #sendNotification(Notification)} before returning.
  45. * Thus, unless the listener is remote, it will have received
  46. * the notification before the method returns.
  47. * This may be changed by overriding the <code>handleNotification</code>
  48. * method, or by providing an {@link java.util.concurrent.Executor} to
  49. * use. With the latter, the dispatch of a notification to a particular
  50. * listener will form one task performed by the executor.
  51. * </p>
  52. * <p>
  53. * Any exceptions thrown by the dispatch process will be caught, allowing
  54. * other listeners to still receive the notification. However, any
  55. * {@link Error}s that are thrown will be propogated to the caller of
  56. * {@link #sendNotification(Notification)}.
  57. * </p>
  58. *
  59. * @author Andrew John Hughes (gnu_andrew@member.fsf.org)
  60. * @since 1.5
  61. */
  62. public class NotificationBroadcasterSupport
  63. implements NotificationEmitter
  64. {
  65. /**
  66. * The executor for dispatch, or
  67. * <code>null</code> if this thread should
  68. * handle dispatch itself.
  69. */
  70. private Executor executor;
  71. /**
  72. * An array containing information on each
  73. * notification, or <code>null</code> if an
  74. * empty array should be returned by
  75. * {@link #getNotificationInfo()}.
  76. */
  77. private MBeanNotificationInfo[] info;
  78. /**
  79. * The list of listeners registered with
  80. * this broadcaster.
  81. */
  82. private final List<ListenerData> listeners =
  83. new ArrayList<ListenerData>();
  84. /**
  85. * Constructs a {@link NotificationBroadcasterSupport} using
  86. * the default synchronous dispatch model, where a single
  87. * thread sends the notification to all listeners. This
  88. * is equivalent to calling
  89. * <code>NotificationBroadcasterSupport(null, null)</code>.
  90. */
  91. public NotificationBroadcasterSupport()
  92. {
  93. this(null, (MBeanNotificationInfo[]) null);
  94. }
  95. /**
  96. * Constructs a {@link NotificationBroadcasterSupport} where
  97. * the specified (@link java.util.concurrent.Executor} is used
  98. * to perform each invocation of the
  99. * {@link #handleNotification(NotificationListener, Notification,
  100. * Object)} method. Filtering is performed beforehand, by this
  101. * thread; only calls which have successfully passed through the
  102. * filter are sent to the executor. This is equivalent to calling
  103. * <code>NotificationBroadcasterSupport(executor, null)</code>.
  104. *
  105. * @param executor the executor to use for each call to
  106. * <code>handleNotification()</code>.
  107. * @since 1.6
  108. */
  109. public NotificationBroadcasterSupport(Executor executor)
  110. {
  111. this(executor, (MBeanNotificationInfo) null);
  112. }
  113. /**
  114. * Constructs a {@link NotificationBroadcasterSupport} using
  115. * the default synchronous dispatch model, where a single
  116. * thread sends the notification to all listeners. The specified
  117. * {@link MBeanNotificationInfo} array is used to provide
  118. * information about the notifications on calls to
  119. * {@link #getNotificationInfo()}, where a clone will be
  120. * returned if the array is non-empty. This is equivalent to
  121. * calling <code>NotificationBroadcasterSupport(null, info)</code>.
  122. *
  123. * @param info an array of {@link MBeanNotificationInfo} objects
  124. * describing the notifications delivered by this
  125. * broadcaster. This may be <code>null</code>, which
  126. * is taken as being equivalent to an empty array.
  127. */
  128. public NotificationBroadcasterSupport(MBeanNotificationInfo... info)
  129. {
  130. this(null, info);
  131. }
  132. /**
  133. * Constructs a {@link NotificationBroadcasterSupport} where
  134. * the specified (@link java.util.concurrent.Executor} is used
  135. * to perform each invocation of the
  136. * {@link #handleNotification(NotificationListener, Notification,
  137. * Object)} method. Filtering is performed beforehand, by this
  138. * thread; only calls which have successfully passed through the
  139. * filter are sent to the executor. The specified
  140. * {@link MBeanNotificationInfo} array is used to provide
  141. * information about the notifications on calls to
  142. * {@link #getNotificationInfo()}, where a clone will be
  143. * returned if the array is non-empty.
  144. *
  145. * @param executor the executor to use for each call to
  146. * <code>handleNotification()</code>.
  147. * @param info an array of {@link MBeanNotificationInfo} objects
  148. * describing the notifications delivered by this
  149. * broadcaster. This may be <code>null</code>, which
  150. * is taken as being equivalent to an empty array.
  151. * @since 1.6
  152. */
  153. public NotificationBroadcasterSupport(Executor executor,
  154. MBeanNotificationInfo... info)
  155. {
  156. this.executor = executor;
  157. this.info = info;
  158. }
  159. /**
  160. * Registers the specified listener as a new recipient of
  161. * notifications from this bean. If non-null, the filter
  162. * argument will be used to select which notifications are
  163. * delivered. The supplied object will also be passed to
  164. * the recipient with each notification. This should not
  165. * be modified by the broadcaster, but instead should be
  166. * passed unmodified to the listener.
  167. *
  168. * @param listener the new listener, who will receive
  169. * notifications from this broadcasting bean.
  170. * @param filter a filter to determine which notifications are
  171. * delivered to the listener, or <code>null</code>
  172. * if no filtering is required.
  173. * @param passback an object to be passed to the listener with
  174. * each notification.
  175. * @throws IllegalArgumentException if <code>listener</code> is
  176. * <code>null</code>.
  177. * @see #removeNotificationListener(NotificationListener)
  178. */
  179. public void addNotificationListener(NotificationListener listener,
  180. NotificationFilter filter,
  181. Object passback)
  182. throws IllegalArgumentException
  183. {
  184. if (listener == null)
  185. throw new IllegalArgumentException("Null listener added to bean.");
  186. listeners.add(new ListenerData(listener, filter, passback));
  187. }
  188. /**
  189. * Returns an array describing the notifications this
  190. * bean may send to its registered listeners. Ideally, this
  191. * array should be complete, but in some cases, this may
  192. * not be possible. However, be aware that some listeners
  193. * may expect this to be so.
  194. *
  195. * @return the array of possible notifications.
  196. */
  197. public MBeanNotificationInfo[] getNotificationInfo()
  198. {
  199. if (info == null || info.length == 0)
  200. return new MBeanNotificationInfo[0];
  201. return (MBeanNotificationInfo[]) info.clone();
  202. }
  203. /**
  204. * This method is called on a per-listener basis, either
  205. * from this thread or the supplied executor, and may be
  206. * overridden by subclasses which wish to change how
  207. * notifications are delivered. The default
  208. * implementation simply calls
  209. * <code>listener.handleNotification(notif, passback)</code>.
  210. *
  211. * @param listener the listener to send the notification to.
  212. * @param notif the notification to dispatch.
  213. * @param passback the passback object of the listener.
  214. */
  215. protected void handleNotification(NotificationListener listener,
  216. Notification notif,
  217. Object passback)
  218. {
  219. listener.handleNotification(notif, passback);
  220. }
  221. /**
  222. * Removes the specified listener from the list of recipients
  223. * of notifications from this bean. This includes all combinations
  224. * of filters and passback objects registered for this listener.
  225. * For more specific removal of listeners, see the subinterface
  226. * {@link NotificationEmitter}.
  227. *
  228. * @param listener the listener to remove.
  229. * @throws ListenerNotFoundException if the specified listener
  230. * is not registered with this bean.
  231. * @see #addNotificationListener(NotificationListener, NotificationFilter,
  232. * java.lang.Object)
  233. */
  234. public void removeNotificationListener(NotificationListener listener)
  235. throws ListenerNotFoundException
  236. {
  237. Iterator<ListenerData> it = listeners.iterator();
  238. boolean foundOne = false;
  239. while (it.hasNext())
  240. {
  241. if (it.next().getListener() == listener)
  242. {
  243. it.remove();
  244. foundOne = true;
  245. }
  246. }
  247. if (!foundOne)
  248. throw new ListenerNotFoundException("The specified listener, " + listener +
  249. "is not registered with this bean.");
  250. }
  251. /**
  252. * Removes the specified listener from the list of recipients
  253. * of notifications from this bean. Only the first instance with
  254. * the supplied filter and passback object is removed.
  255. * <code>null</code> is used as a valid value for these parameters,
  256. * rather than as a way to remove all registration instances for
  257. * the specified listener; for this behaviour instead, see the details
  258. * of the same method in {@link NotificationBroadcaster}.
  259. *
  260. * @param listener the listener to remove.
  261. * @param filter the filter of the listener to remove.
  262. * @param passback the passback object of the listener to remove.
  263. * @throws ListenerNotFoundException if the specified listener
  264. * is not registered with this bean.
  265. * @see #addNotificationListener(NotificationListener, NotificationFilter,
  266. * java.lang.Object)
  267. */
  268. public void removeNotificationListener(NotificationListener listener,
  269. NotificationFilter filter,
  270. Object passback)
  271. throws ListenerNotFoundException
  272. {
  273. if (!(listeners.remove(new ListenerData(listener, filter, passback))))
  274. {
  275. throw new ListenerNotFoundException("The specified listener, " + listener +
  276. " with filter " + filter +
  277. "and passback " + passback +
  278. ", is not registered with this bean.");
  279. }
  280. }
  281. /**
  282. * <p>
  283. * Performs delivery of the notification. If an executor
  284. * was specified on construction, this will be used to call
  285. * {@link #handleNotification(NotificationListener, Notification,
  286. * Object)}. If the executor is <code>null</code>, however,
  287. * this thread calls the method itself in order to perform a
  288. * synchronous dispatch of the notification to all eligible
  289. * listeners.
  290. * </p>
  291. * <p>
  292. * Prior to either process taking place, the listeners are filtered.
  293. * Notifications are only delivered if the filter is either
  294. * <code>null</code> or returns true from the
  295. * {@link NotificationFilter#isNotificationEnabled(Notification)}
  296. * method.
  297. * </p>
  298. *
  299. * @param notif the notification to send.
  300. */
  301. public void sendNotification(Notification notif)
  302. {
  303. for (ListenerData ldata : listeners)
  304. {
  305. NotificationFilter filter = ldata.getFilter();
  306. if (filter == null || filter.isNotificationEnabled(notif))
  307. {
  308. if (executor == null)
  309. try
  310. {
  311. handleNotification(ldata.getListener(), notif,
  312. ldata.getPassback());
  313. }
  314. catch (Exception e) { /* Ignore */ }
  315. else
  316. executor.execute(new DispatchTask(ldata, notif));
  317. }
  318. }
  319. }
  320. /**
  321. * The dispatch task to be performed by an executor.
  322. */
  323. private final class DispatchTask
  324. implements Runnable
  325. {
  326. /**
  327. * The data on the listener being called.
  328. */
  329. private ListenerData ldata;
  330. /**
  331. * The notification to send.
  332. */
  333. private Notification notif;
  334. /**
  335. * Construct a new {@link DispatchTask}.
  336. *
  337. * @param ldata the listener data.
  338. * @param notif the notification to send.
  339. */
  340. public DispatchTask(ListenerData ldata,
  341. Notification notif)
  342. {
  343. this.ldata = ldata;
  344. this.notif = notif;
  345. }
  346. /**
  347. * Dispatch the notification.
  348. */
  349. public void run()
  350. {
  351. try
  352. {
  353. handleNotification(ldata.getListener(), notif,
  354. ldata.getPassback());
  355. }
  356. catch (Exception e) { /* Ignore */ }
  357. }
  358. }
  359. }