KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > javax > management > MBeanServerDelegate


1 /*
2  * @(#)MBeanServerDelegate.java 1.57 06/09/29
3  *
4  * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
5  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6  */

7
8 package javax.management;
9
10 //Jdk import
11
import java.util.Properties JavaDoc;
12 import java.util.Random JavaDoc;
13
14 //Jmx import
15
import com.sun.jmx.defaults.JmxProperties;
16 import com.sun.jmx.defaults.ServiceName;
17
18 /**
19  * Represents the MBean server from the management point of view.
20  * The MBeanServerDelegate MBean emits the MBeanServerNotifications when
21  * an MBean is registered/unregistered in the MBean server.
22  *
23  * @since 1.5
24  */

25 public class MBeanServerDelegate implements MBeanServerDelegateMBean JavaDoc,
26                         NotificationEmitter JavaDoc {
27     
28     /** The MBean server agent identification.*/
29     private String JavaDoc mbeanServerId ;
30     
31     /** The NotificationBroadcasterSupport object that sends the
32     notifications */

33     private final NotificationBroadcasterSupport JavaDoc broadcaster;
34
35     private static long oldStamp = 0;
36     private final long stamp;
37     private long sequenceNumber = 1;
38
39     private static final MBeanNotificationInfo JavaDoc[] notifsInfo;
40
41     static {
42         final String JavaDoc[] types = {
43         MBeanServerNotification.UNREGISTRATION_NOTIFICATION,
44         MBeanServerNotification.REGISTRATION_NOTIFICATION
45     };
46     notifsInfo = new MBeanNotificationInfo JavaDoc[1];
47     notifsInfo[0] =
48         new MBeanNotificationInfo JavaDoc(types,
49             "javax.management.MBeanServerNotification",
50             "Notifications sent by the MBeanServerDelegate MBean");
51     }
52
53     /**
54      * Create a MBeanServerDelegate object.
55      */

56     public MBeanServerDelegate () {
57     stamp = getStamp();
58         broadcaster = new NotificationBroadcasterSupport JavaDoc() ;
59     }
60     
61
62     /**
63      * Returns the MBean server agent identity.
64      *
65      * @return the identity.
66      */

67     public synchronized String JavaDoc getMBeanServerId() {
68     if (mbeanServerId == null) {
69         String JavaDoc localHost;
70         try {
71         localHost = java.net.InetAddress.getLocalHost().getHostName();
72         } catch (java.net.UnknownHostException JavaDoc e) {
73         localHost = "localhost";
74         }
75         mbeanServerId = new String JavaDoc(localHost + "_" + stamp);
76     }
77     return mbeanServerId;
78     }
79     
80     /**
81      * Returns the full name of the JMX specification implemented
82      * by this product.
83      *
84      * @return the specification name.
85      */

86     public String JavaDoc getSpecificationName() {
87     return ServiceName.JMX_SPEC_NAME;
88     }
89
90     /**
91      * Returns the version of the JMX specification implemented
92      * by this product.
93      *
94      * @return the specification version.
95      */

96     public String JavaDoc getSpecificationVersion() {
97     return ServiceName.JMX_SPEC_VERSION;
98     }
99
100     /**
101      * Returns the vendor of the JMX specification implemented
102      * by this product.
103      *
104      * @return the specification vendor.
105      */

106     public String JavaDoc getSpecificationVendor() {
107     return ServiceName.JMX_SPEC_VENDOR;
108     }
109
110     /**
111      * Returns the JMX implementation name (the name of this product).
112      *
113      * @return the implementation name.
114      */

115     public String JavaDoc getImplementationName() {
116     return ServiceName.JMX_IMPL_NAME;
117     }
118
119     /**
120      * Returns the JMX implementation version (the version of this product).
121      *
122      * @return the implementation version.
123      */

124     public String JavaDoc getImplementationVersion() {
125         try {
126             return System.getProperty("java.runtime.version");
127         } catch (SecurityException JavaDoc e) {
128             return "";
129         }
130     }
131
132     /**
133      * Returns the JMX implementation vendor (the vendor of this product).
134      *
135      * @return the implementation vendor.
136      */

137     public String JavaDoc getImplementationVendor() {
138     return ServiceName.JMX_IMPL_VENDOR;
139     }
140
141     // From NotificationEmitter extends NotificationBroacaster
142
//
143
public MBeanNotificationInfo JavaDoc[] getNotificationInfo() {
144     final int len = MBeanServerDelegate.notifsInfo.length;
145         final MBeanNotificationInfo JavaDoc[] infos =
146     new MBeanNotificationInfo JavaDoc[len];
147     System.arraycopy(MBeanServerDelegate.notifsInfo,0,infos,0,len);
148     return infos;
149     }
150
151     // From NotificationEmitter extends NotificationBroacaster
152
//
153
public synchronized
154     void addNotificationListener(NotificationListener JavaDoc listener,
155                      NotificationFilter JavaDoc filter,
156                      Object JavaDoc handback)
157         throws IllegalArgumentException JavaDoc {
158         broadcaster.addNotificationListener(listener,filter,handback) ;
159     }
160     
161     // From NotificationEmitter extends NotificationBroacaster
162
//
163
public synchronized
164     void removeNotificationListener(NotificationListener JavaDoc listener,
165                     NotificationFilter JavaDoc filter,
166                     Object JavaDoc handback)
167     throws ListenerNotFoundException JavaDoc {
168         broadcaster.removeNotificationListener(listener,filter,handback) ;
169     }
170     
171     // From NotificationEmitter extends NotificationBroacaster
172
//
173
public synchronized
174     void removeNotificationListener(NotificationListener JavaDoc listener)
175         throws ListenerNotFoundException JavaDoc {
176         broadcaster.removeNotificationListener(listener) ;
177     }
178
179     /**
180      * Enables the MBean server to send a notification.
181      * If the passed <var>notification</var> has a sequence number lesser
182      * or equal to 0, then replace it with the delegate's own sequence
183      * number.
184      * @param notification The notification to send.
185      *
186      */

187     public void sendNotification(Notification JavaDoc notification) {
188     if (notification.getSequenceNumber() < 1) {
189         synchronized (this) {
190         notification.setSequenceNumber(this.sequenceNumber++);
191         }
192     }
193         broadcaster.sendNotification(notification);
194     }
195
196     /* Return a timestamp that is monotonically increasing even if
197        System.currentTimeMillis() isn't (for example, if you call this
198        constructor more than once in the same millisecond, or if the
199        clock always returns the same value). This means that the ids
200        for a given JVM will always be distinact, though there is no
201        such guarantee for two different JVMs. */

202     private static synchronized long getStamp() {
203     long s = System.currentTimeMillis();
204     if (oldStamp >= s) {
205         s = oldStamp + 1;
206     }
207     oldStamp = s;
208     return s;
209     }
210 }
211
Popular Tags