MBeanServerDelegate.java 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. /* MBeanServerDelegate.java -- The management server delegate.
  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 gnu.javax.management.ListenerData;
  33. import gnu.classpath.SystemProperties;
  34. import java.net.InetAddress;
  35. import java.net.UnknownHostException;
  36. import java.util.ArrayList;
  37. import java.util.Date;
  38. import java.util.Iterator;
  39. import java.util.List;
  40. /**
  41. * Provides an implementation of a delegate bean, which is associated
  42. * with a management server. The delegate bean is responsible
  43. * for providing metadata about the server and handling the
  44. * registration and deregistration notifications.
  45. *
  46. * @author Andrew John Hughes (gnu_andrew@member.fsf.org)
  47. * @since 1.5
  48. */
  49. public class MBeanServerDelegate
  50. implements MBeanServerDelegateMBean, NotificationEmitter
  51. {
  52. /**
  53. * The identifier of the server associated with this delegate.
  54. */
  55. private String id;
  56. /**
  57. * The listeners registered with the delegate.
  58. */
  59. private final List<ListenerData> listeners =
  60. new ArrayList<ListenerData>();
  61. /**
  62. * The sequence identifier used by the delegate.
  63. */
  64. private long seqNo;
  65. /**
  66. * Default constructor which generates the id.
  67. */
  68. public MBeanServerDelegate()
  69. {
  70. String hostName;
  71. try
  72. {
  73. hostName = InetAddress.getLocalHost().getHostName();
  74. }
  75. catch (UnknownHostException e)
  76. {
  77. hostName = "Unknown host";
  78. }
  79. id = hostName + "_" + new Date().getTime();
  80. }
  81. /**
  82. * Registers the specified listener as a new recipient of
  83. * notifications from the delegate. If non-null, the filter
  84. * argument will be used to select which notifications are
  85. * delivered. The supplied object will also be passed to
  86. * the recipient with each notification. This should not
  87. * be modified by the broadcaster, but instead should be
  88. * passed unmodified to the listener.
  89. *
  90. * @param listener the new listener, who will receive
  91. * notifications from this broadcasting bean.
  92. * @param filter a filter to determine which notifications are
  93. * delivered to the listener, or <code>null</code>
  94. * if no filtering is required.
  95. * @param passback an object to be passed to the listener with
  96. * each notification.
  97. * @throws IllegalArgumentException if <code>listener</code> is
  98. * <code>null</code>.
  99. * @see #removeNotificationListener(NotificationListener)
  100. */
  101. public void addNotificationListener(NotificationListener listener,
  102. NotificationFilter filter,
  103. Object passback)
  104. throws IllegalArgumentException
  105. {
  106. if (listener == null)
  107. throw new IllegalArgumentException("A null listener was supplied.");
  108. listeners.add(new ListenerData(listener, filter, passback));
  109. }
  110. /**
  111. * Returns the name of this Java Management eXtensions (JMX) implementation.
  112. *
  113. * @return the implementation name.
  114. */
  115. public String getImplementationName()
  116. {
  117. return "GNU JMX";
  118. }
  119. /**
  120. * Returns the vendor of this Java Management eXtensions (JMX) implementation.
  121. *
  122. * @return the implementation vendor.
  123. */
  124. public String getImplementationVendor()
  125. {
  126. return "The GNU Classpath Project";
  127. }
  128. /**
  129. * Returns the version of this Java Management eXtensions (JMX) implementation.
  130. *
  131. * @return the implementation version.
  132. */
  133. public String getImplementationVersion()
  134. {
  135. return SystemProperties.getProperty("gnu.classpath.version");
  136. }
  137. /**
  138. * Returns the unique identifier for this management server.
  139. *
  140. * @return the unique id of the server.
  141. */
  142. public String getMBeanServerId()
  143. {
  144. return id;
  145. }
  146. /**
  147. * Returns an array describing the notifications this
  148. * bean may send to its registered listeners. Ideally, this
  149. * array should be complete, but in some cases, this may
  150. * not be possible. However, be aware that some listeners
  151. * may expect this to be so.
  152. *
  153. * @return the array of possible notifications.
  154. */
  155. public MBeanNotificationInfo[] getNotificationInfo()
  156. {
  157. return new MBeanNotificationInfo[]
  158. {
  159. new MBeanNotificationInfo(new String[]
  160. {
  161. MBeanServerNotification.REGISTRATION_NOTIFICATION,
  162. MBeanServerNotification.UNREGISTRATION_NOTIFICATION,
  163. },
  164. MBeanServerNotification.class.getName(),
  165. "Server registration notifications")
  166. };
  167. }
  168. /**
  169. * Returns the name of this Java Management eXtensions (JMX) specification.
  170. *
  171. * @return the specification name.
  172. */
  173. public String getSpecificationName()
  174. {
  175. return "JMX";
  176. }
  177. /**
  178. * Returns the vendor of this Java Management eXtensions (JMX) specification.
  179. *
  180. * @return the specification vendor.
  181. */
  182. public String getSpecificationVendor()
  183. {
  184. return "Sun Microsystems";
  185. }
  186. /**
  187. * Returns the version of this Java Management eXtensions (JMX) specification.
  188. *
  189. * @return the specification version.
  190. */
  191. public String getSpecificationVersion()
  192. {
  193. return "1.2";
  194. }
  195. /**
  196. * Removes the specified listener from the list of recipients
  197. * of notifications from this bean. This includes all combinations
  198. * of filters and passback objects registered for this listener.
  199. * For more specific removal of listeners, see
  200. * {@link #removeNotificationListener(NotificationListener,
  201. * NotificationFilter, java.lang.Object)}
  202. *
  203. * @param listener the listener to remove.
  204. * @throws ListenerNotFoundException if the specified listener
  205. * is not registered with this bean.
  206. * @see #addNotificationListener(NotificationListener, NotificationFilter,
  207. * java.lang.Object)
  208. */
  209. public void removeNotificationListener(NotificationListener listener)
  210. throws ListenerNotFoundException
  211. {
  212. Iterator<ListenerData> it = listeners.iterator();
  213. boolean foundOne = false;
  214. while (it.hasNext())
  215. {
  216. if (it.next().getListener() == listener)
  217. {
  218. it.remove();
  219. foundOne = true;
  220. }
  221. }
  222. if (!foundOne)
  223. throw new ListenerNotFoundException("The specified listener, " + listener +
  224. "is not registered with this bean.");
  225. }
  226. /**
  227. * Removes the specified listener from the list of recipients
  228. * of notifications from this delegate. Only the first instance with
  229. * the supplied filter and passback object is removed.
  230. * <code>null</code> is used as a valid value for these parameters,
  231. * rather than as a way to remove all registration instances for
  232. * the specified listener; for this behaviour instead, see
  233. * {@link #removeNotificationListener(NotificationListener)}.
  234. *
  235. * @param listener the listener to remove.
  236. * @param filter the filter of the listener to remove.
  237. * @param passback the passback object of the listener to remove.
  238. * @throws ListenerNotFoundException if the specified listener
  239. * is not registered with this bean.
  240. * @see #addNotificationListener(NotificationListener, NotificationFilter,
  241. * java.lang.Object)
  242. * @see #removeNotificationListener(NotificationListener)
  243. */
  244. public void removeNotificationListener(NotificationListener listener,
  245. NotificationFilter filter,
  246. Object passback)
  247. throws ListenerNotFoundException
  248. {
  249. if (!(listeners.remove(new ListenerData(listener, filter, passback))))
  250. {
  251. throw new ListenerNotFoundException("The specified listener, " + listener +
  252. " with filter " + filter +
  253. "and passback " + passback +
  254. ", is not registered with this bean.");
  255. }
  256. }
  257. /**
  258. * Allows the server to use the delegate to send a notification.
  259. * If the supplied notification has a sequence number <= 0, then
  260. * it is replaced with the delegate's own sequence number.
  261. *
  262. * @param notification the notification to send.
  263. */
  264. public void sendNotification(Notification notification)
  265. {
  266. if (notification.getSequenceNumber() <= 0)
  267. notification.setSequenceNumber(++seqNo);
  268. for (ListenerData ldata : listeners)
  269. {
  270. NotificationFilter filter = ldata.getFilter();
  271. if (filter == null || filter.isNotificationEnabled(notification))
  272. ldata.getListener().handleNotification(notification, ldata.getPassback());
  273. }
  274. }
  275. }