blob: 4ee9766306b4b0e2aa86f95e1999d3d19ddb8c00 [file] [log] [blame]
J. Duke319a3b92007-12-01 00:00:00 +00001/*
2 * Copyright 1999-2006 Sun Microsystems, Inc. All Rights Reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Sun designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Sun in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
22 * CA 95054 USA or visit www.sun.com if you need additional information or
23 * have any questions.
24 */
25
26package javax.management;
27
28import com.sun.jmx.defaults.ServiceName;
29
30/**
31 * Represents the MBean server from the management point of view.
32 * The MBeanServerDelegate MBean emits the MBeanServerNotifications when
33 * an MBean is registered/unregistered in the MBean server.
34 *
35 * @since 1.5
36 */
37public class MBeanServerDelegate implements MBeanServerDelegateMBean,
38 NotificationEmitter {
39
40 /** The MBean server agent identification.*/
41 private String mbeanServerId ;
42
43 /** The NotificationBroadcasterSupport object that sends the
44 notifications */
45 private final NotificationBroadcasterSupport broadcaster;
46
47 private static long oldStamp = 0;
48 private final long stamp;
49 private long sequenceNumber = 1;
50
51 private static final MBeanNotificationInfo[] notifsInfo;
52
53 static {
54 final String[] types = {
55 MBeanServerNotification.UNREGISTRATION_NOTIFICATION,
56 MBeanServerNotification.REGISTRATION_NOTIFICATION
57 };
58 notifsInfo = new MBeanNotificationInfo[1];
59 notifsInfo[0] =
60 new MBeanNotificationInfo(types,
61 "javax.management.MBeanServerNotification",
62 "Notifications sent by the MBeanServerDelegate MBean");
63 }
64
65 /**
66 * Create a MBeanServerDelegate object.
67 */
68 public MBeanServerDelegate () {
69 stamp = getStamp();
70 broadcaster = new NotificationBroadcasterSupport() ;
71 }
72
73
74 /**
75 * Returns the MBean server agent identity.
76 *
77 * @return the identity.
78 */
79 public synchronized String getMBeanServerId() {
80 if (mbeanServerId == null) {
81 String localHost;
82 try {
83 localHost = java.net.InetAddress.getLocalHost().getHostName();
84 } catch (java.net.UnknownHostException e) {
85 localHost = "localhost";
86 }
87 mbeanServerId = localHost + "_" + stamp;
88 }
89 return mbeanServerId;
90 }
91
92 /**
93 * Returns the full name of the JMX specification implemented
94 * by this product.
95 *
96 * @return the specification name.
97 */
98 public String getSpecificationName() {
99 return ServiceName.JMX_SPEC_NAME;
100 }
101
102 /**
103 * Returns the version of the JMX specification implemented
104 * by this product.
105 *
106 * @return the specification version.
107 */
108 public String getSpecificationVersion() {
109 return ServiceName.JMX_SPEC_VERSION;
110 }
111
112 /**
113 * Returns the vendor of the JMX specification implemented
114 * by this product.
115 *
116 * @return the specification vendor.
117 */
118 public String getSpecificationVendor() {
119 return ServiceName.JMX_SPEC_VENDOR;
120 }
121
122 /**
123 * Returns the JMX implementation name (the name of this product).
124 *
125 * @return the implementation name.
126 */
127 public String getImplementationName() {
128 return ServiceName.JMX_IMPL_NAME;
129 }
130
131 /**
132 * Returns the JMX implementation version (the version of this product).
133 *
134 * @return the implementation version.
135 */
136 public String getImplementationVersion() {
137 try {
138 return System.getProperty("java.runtime.version");
139 } catch (SecurityException e) {
140 return "";
141 }
142 }
143
144 /**
145 * Returns the JMX implementation vendor (the vendor of this product).
146 *
147 * @return the implementation vendor.
148 */
149 public String getImplementationVendor() {
150 return ServiceName.JMX_IMPL_VENDOR;
151 }
152
153 // From NotificationEmitter extends NotificationBroacaster
154 //
155 public MBeanNotificationInfo[] getNotificationInfo() {
156 final int len = MBeanServerDelegate.notifsInfo.length;
157 final MBeanNotificationInfo[] infos =
158 new MBeanNotificationInfo[len];
159 System.arraycopy(MBeanServerDelegate.notifsInfo,0,infos,0,len);
160 return infos;
161 }
162
163 // From NotificationEmitter extends NotificationBroacaster
164 //
165 public synchronized
166 void addNotificationListener(NotificationListener listener,
167 NotificationFilter filter,
168 Object handback)
169 throws IllegalArgumentException {
170 broadcaster.addNotificationListener(listener,filter,handback) ;
171 }
172
173 // From NotificationEmitter extends NotificationBroacaster
174 //
175 public synchronized
176 void removeNotificationListener(NotificationListener listener,
177 NotificationFilter filter,
178 Object handback)
179 throws ListenerNotFoundException {
180 broadcaster.removeNotificationListener(listener,filter,handback) ;
181 }
182
183 // From NotificationEmitter extends NotificationBroacaster
184 //
185 public synchronized
186 void removeNotificationListener(NotificationListener listener)
187 throws ListenerNotFoundException {
188 broadcaster.removeNotificationListener(listener) ;
189 }
190
191 /**
192 * Enables the MBean server to send a notification.
193 * If the passed <var>notification</var> has a sequence number lesser
194 * or equal to 0, then replace it with the delegate's own sequence
195 * number.
196 * @param notification The notification to send.
197 *
198 */
199 public void sendNotification(Notification notification) {
200 if (notification.getSequenceNumber() < 1) {
201 synchronized (this) {
202 notification.setSequenceNumber(this.sequenceNumber++);
203 }
204 }
205 broadcaster.sendNotification(notification);
206 }
207
208 /**
209 * Defines the default ObjectName of the MBeanServerDelegate.
210 *
211 * @since 1.6
212 */
213 public static final ObjectName DELEGATE_NAME;
214 static {
215 try {
216 DELEGATE_NAME =
217 new ObjectName("JMImplementation:type=MBeanServerDelegate");
218 } catch (MalformedObjectNameException e) {
219 throw new Error("Can't initialize delegate name", e);
220 }
221 }
222
223 /* Return a timestamp that is monotonically increasing even if
224 System.currentTimeMillis() isn't (for example, if you call this
225 constructor more than once in the same millisecond, or if the
226 clock always returns the same value). This means that the ids
227 for a given JVM will always be distinact, though there is no
228 such guarantee for two different JVMs. */
229 private static synchronized long getStamp() {
230 long s = System.currentTimeMillis();
231 if (oldStamp >= s) {
232 s = oldStamp + 1;
233 }
234 oldStamp = s;
235 return s;
236 }
237}