1. /*
  2. * @(#)MBeanServerDelegate.java 1.56 04/04/13
  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. //Jdk import
  9. import java.util.Properties;
  10. import java.util.Random;
  11. //Jmx import
  12. import com.sun.jmx.defaults.JmxProperties;
  13. import com.sun.jmx.defaults.ServiceName;
  14. /**
  15. * Represents the MBean server from the management point of view.
  16. * The MBeanServerDelegate MBean emits the MBeanServerNotifications when
  17. * an MBean is registered/unregistered in the MBean server.
  18. *
  19. * @since 1.5
  20. */
  21. public class MBeanServerDelegate implements MBeanServerDelegateMBean,
  22. NotificationEmitter {
  23. /** The MBean server agent identification.*/
  24. private String mbeanServerId ;
  25. /** The NotificationBroadcasterSupport object that sends the
  26. notifications */
  27. private final NotificationBroadcasterSupport broadcaster;
  28. private static long oldStamp = 0;
  29. private final long stamp;
  30. private long sequenceNumber = 1;
  31. private static final MBeanNotificationInfo[] notifsInfo;
  32. static {
  33. final String[] types = {
  34. MBeanServerNotification.UNREGISTRATION_NOTIFICATION,
  35. MBeanServerNotification.REGISTRATION_NOTIFICATION
  36. };
  37. notifsInfo = new MBeanNotificationInfo[1];
  38. notifsInfo[0] =
  39. new MBeanNotificationInfo(types,
  40. "javax.management.MBeanServerNotification",
  41. "Notifications sent by the MBeanServerDelegate MBean");
  42. }
  43. /**
  44. * Create a MBeanServerDelegate object.
  45. */
  46. public MBeanServerDelegate () {
  47. stamp = getStamp();
  48. broadcaster = new NotificationBroadcasterSupport() ;
  49. }
  50. /**
  51. * Returns the MBean server agent identity.
  52. *
  53. * @return the identity.
  54. */
  55. public synchronized String getMBeanServerId() {
  56. if (mbeanServerId == null) {
  57. String localHost;
  58. try {
  59. localHost = java.net.InetAddress.getLocalHost().getHostName();
  60. } catch (java.net.UnknownHostException e) {
  61. localHost = "localhost";
  62. }
  63. mbeanServerId = new String(localHost + "_" + stamp);
  64. }
  65. return mbeanServerId;
  66. }
  67. /**
  68. * Returns the full name of the JMX specification implemented
  69. * by this product.
  70. *
  71. * @return the specification name.
  72. */
  73. public String getSpecificationName() {
  74. return ServiceName.JMX_SPEC_NAME;
  75. }
  76. /**
  77. * Returns the version of the JMX specification implemented
  78. * by this product.
  79. *
  80. * @return the specification version.
  81. */
  82. public String getSpecificationVersion() {
  83. return ServiceName.JMX_SPEC_VERSION;
  84. }
  85. /**
  86. * Returns the vendor of the JMX specification implemented
  87. * by this product.
  88. *
  89. * @return the specification vendor.
  90. */
  91. public String getSpecificationVendor() {
  92. return ServiceName.JMX_SPEC_VENDOR;
  93. }
  94. /**
  95. * Returns the JMX implementation name (the name of this product).
  96. *
  97. * @return the implementation name.
  98. */
  99. public String getImplementationName() {
  100. return ServiceName.JMX_IMPL_NAME;
  101. }
  102. /**
  103. * Returns the JMX implementation version (the version of this product).
  104. *
  105. * @return the implementation version.
  106. */
  107. public String getImplementationVersion() {
  108. return ServiceName.JMX_IMPL_VERSION;
  109. }
  110. /**
  111. * Returns the JMX implementation vendor (the vendor of this product).
  112. *
  113. * @return the implementation vendor.
  114. */
  115. public String getImplementationVendor() {
  116. return ServiceName.JMX_IMPL_VENDOR;
  117. }
  118. // From NotificationEmitter extends NotificationBroacaster
  119. //
  120. public MBeanNotificationInfo[] getNotificationInfo() {
  121. final int len = MBeanServerDelegate.notifsInfo.length;
  122. final MBeanNotificationInfo[] infos =
  123. new MBeanNotificationInfo[len];
  124. System.arraycopy(MBeanServerDelegate.notifsInfo,0,infos,0,len);
  125. return infos;
  126. }
  127. // From NotificationEmitter extends NotificationBroacaster
  128. //
  129. public synchronized
  130. void addNotificationListener(NotificationListener listener,
  131. NotificationFilter filter,
  132. Object handback)
  133. throws IllegalArgumentException {
  134. broadcaster.addNotificationListener(listener,filter,handback) ;
  135. }
  136. // From NotificationEmitter extends NotificationBroacaster
  137. //
  138. public synchronized
  139. void removeNotificationListener(NotificationListener listener,
  140. NotificationFilter filter,
  141. Object handback)
  142. throws ListenerNotFoundException {
  143. broadcaster.removeNotificationListener(listener,filter,handback) ;
  144. }
  145. // From NotificationEmitter extends NotificationBroacaster
  146. //
  147. public synchronized
  148. void removeNotificationListener(NotificationListener listener)
  149. throws ListenerNotFoundException {
  150. broadcaster.removeNotificationListener(listener) ;
  151. }
  152. /**
  153. * Enables the MBean server to send a notification.
  154. * If the passed <var>notification</var> has a sequence number lesser
  155. * or equal to 0, then replace it with the delegate's own sequence
  156. * number.
  157. * @param notification The notification to send.
  158. *
  159. */
  160. public void sendNotification(Notification notification) {
  161. if (notification.getSequenceNumber() < 1) {
  162. synchronized (this) {
  163. notification.setSequenceNumber(this.sequenceNumber++);
  164. }
  165. }
  166. broadcaster.sendNotification(notification);
  167. }
  168. /* Return a timestamp that is monotonically increasing even if
  169. System.currentTimeMillis() isn't (for example, if you call this
  170. constructor more than once in the same millisecond, or if the
  171. clock always returns the same value). This means that the ids
  172. for a given JVM will always be distinact, though there is no
  173. such guarantee for two different JVMs. */
  174. private static synchronized long getStamp() {
  175. long s = System.currentTimeMillis();
  176. if (oldStamp >= s) {
  177. s = oldStamp + 1;
  178. }
  179. oldStamp = s;
  180. return s;
  181. }
  182. }