001: /*
002: * Copyright (C) The MX4J Contributors.
003: * All rights reserved.
004: *
005: * This software is distributed under the terms of the MX4J License version 1.0.
006: * See the terms of the MX4J License in the documentation provided with this software.
007: */
008:
009: package javax.management;
010:
011: import java.net.InetAddress;
012: import java.net.UnknownHostException;
013: import java.rmi.server.UID;
014:
015: /**
016: * The MBean that broadcasts notifications about registration and unregistration of other MBeans.
017: * It is registered with ObjectName <code>JMImplementation:type=MBeanServerDelegate</code>.
018: * It also gives information about the JMX version and implementation.
019: *
020: * @version $Revision: 1.23 $
021: */
022: public class MBeanServerDelegate implements MBeanServerDelegateMBean,
023: NotificationEmitter {
024: private static long mbeanServerCount;
025: // Only notifications of type MBeanServerNotification are emitted by this class, so the array must have length 1
026: private static final MBeanNotificationInfo[] notifications = new MBeanNotificationInfo[] { new MBeanNotificationInfo(
027: new String[] {
028: MBeanServerNotification.REGISTRATION_NOTIFICATION,
029: MBeanServerNotification.UNREGISTRATION_NOTIFICATION },
030: MBeanServerNotification.class.getName(), // as required by the spec
031: "Notifications emitted by the MBeanServerDelegate MBean upon registration or unregistration of MBeans") };
032:
033: private String mbeanServerID;
034: private NotificationBroadcasterSupport broadcaster = new NotificationBroadcasterSupport();
035:
036: /**
037: * Creates a new instance of the MBeanServerDelegate
038: */
039: public MBeanServerDelegate() {
040: }
041:
042: public void addNotificationListener(NotificationListener listener,
043: NotificationFilter filter, Object handback)
044: throws IllegalArgumentException {
045: broadcaster.addNotificationListener(listener, filter, handback);
046: }
047:
048: public void removeNotificationListener(NotificationListener listener)
049: throws ListenerNotFoundException {
050: broadcaster.removeNotificationListener(listener);
051: }
052:
053: public void removeNotificationListener(
054: NotificationListener listener, NotificationFilter filter,
055: Object handback) throws ListenerNotFoundException {
056: broadcaster.removeNotificationListener(listener, filter,
057: handback);
058: }
059:
060: public MBeanNotificationInfo[] getNotificationInfo() {
061: return notifications;
062: }
063:
064: /**
065: * @see NotificationBroadcasterSupport#sendNotification
066: */
067: public void sendNotification(Notification notification) {
068: broadcaster.sendNotification(notification);
069: }
070:
071: public String getMBeanServerId() {
072: // Evaluate lazily, since it may be an expensive operation
073: synchronized (this ) {
074: if (mbeanServerID == null)
075: mbeanServerID = generateMBeanServerID();
076: }
077: return mbeanServerID;
078: }
079:
080: public String getImplementationName() {
081: return "MX4J";
082: }
083:
084: public String getImplementationVendor() {
085: return "The MX4J Team";
086: }
087:
088: public String getImplementationVersion() {
089: return "3.0.1";
090: }
091:
092: public String getSpecificationName() {
093: return "Java Management Extensions";
094: }
095:
096: public String getSpecificationVendor() {
097: return "Sun Microsystems";
098: }
099:
100: public String getSpecificationVersion() {
101: return "1.2 Maintenance Release";
102: }
103:
104: private String getLocalHost() {
105: try {
106: return InetAddress.getLocalHost().getHostName();
107: } catch (UnknownHostException ignored) {
108: return "localhost";
109: }
110: }
111:
112: private String generateMBeanServerID() {
113: long count = 0;
114: synchronized (MBeanServerDelegate.class) {
115: ++mbeanServerCount;
116: count = mbeanServerCount;
117: }
118:
119: UID uid = new UID();
120: return uid.toString() + ":" + getLocalHost() + ":" + count;
121: }
122: }
|