Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/javax/management/MBeanServerDelegate.java
38829 views
/*1* Copyright (c) 1999, 2008, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package javax.management;2627import com.sun.jmx.defaults.JmxProperties;28import com.sun.jmx.defaults.ServiceName;29import com.sun.jmx.mbeanserver.Util;3031/**32* Represents the MBean server from the management point of view.33* The MBeanServerDelegate MBean emits the MBeanServerNotifications when34* an MBean is registered/unregistered in the MBean server.35*36* @since 1.537*/38public class MBeanServerDelegate implements MBeanServerDelegateMBean,39NotificationEmitter {4041/** The MBean server agent identification.*/42private String mbeanServerId ;4344/** The NotificationBroadcasterSupport object that sends the45notifications */46private final NotificationBroadcasterSupport broadcaster;4748private static long oldStamp = 0;49private final long stamp;50private long sequenceNumber = 1;5152private static final MBeanNotificationInfo[] notifsInfo;5354static {55final String[] types = {56MBeanServerNotification.UNREGISTRATION_NOTIFICATION,57MBeanServerNotification.REGISTRATION_NOTIFICATION58};59notifsInfo = new MBeanNotificationInfo[1];60notifsInfo[0] =61new MBeanNotificationInfo(types,62"javax.management.MBeanServerNotification",63"Notifications sent by the MBeanServerDelegate MBean");64}6566/**67* Create a MBeanServerDelegate object.68*/69public MBeanServerDelegate () {70stamp = getStamp();71broadcaster = new NotificationBroadcasterSupport() ;72}737475/**76* Returns the MBean server agent identity.77*78* @return the identity.79*/80public synchronized String getMBeanServerId() {81if (mbeanServerId == null) {82String localHost;83try {84localHost = java.net.InetAddress.getLocalHost().getHostName();85} catch (java.net.UnknownHostException e) {86JmxProperties.MISC_LOGGER.finest("Can't get local host name, " +87"using \"localhost\" instead. Cause is: "+e);88localHost = "localhost";89}90mbeanServerId = localHost + "_" + stamp;91}92return mbeanServerId;93}9495/**96* Returns the full name of the JMX specification implemented97* by this product.98*99* @return the specification name.100*/101public String getSpecificationName() {102return ServiceName.JMX_SPEC_NAME;103}104105/**106* Returns the version of the JMX specification implemented107* by this product.108*109* @return the specification version.110*/111public String getSpecificationVersion() {112return ServiceName.JMX_SPEC_VERSION;113}114115/**116* Returns the vendor of the JMX specification implemented117* by this product.118*119* @return the specification vendor.120*/121public String getSpecificationVendor() {122return ServiceName.JMX_SPEC_VENDOR;123}124125/**126* Returns the JMX implementation name (the name of this product).127*128* @return the implementation name.129*/130public String getImplementationName() {131return ServiceName.JMX_IMPL_NAME;132}133134/**135* Returns the JMX implementation version (the version of this product).136*137* @return the implementation version.138*/139public String getImplementationVersion() {140try {141return System.getProperty("java.runtime.version");142} catch (SecurityException e) {143return "";144}145}146147/**148* Returns the JMX implementation vendor (the vendor of this product).149*150* @return the implementation vendor.151*/152public String getImplementationVendor() {153return ServiceName.JMX_IMPL_VENDOR;154}155156// From NotificationEmitter extends NotificationBroacaster157//158public MBeanNotificationInfo[] getNotificationInfo() {159final int len = MBeanServerDelegate.notifsInfo.length;160final MBeanNotificationInfo[] infos =161new MBeanNotificationInfo[len];162System.arraycopy(MBeanServerDelegate.notifsInfo,0,infos,0,len);163return infos;164}165166// From NotificationEmitter extends NotificationBroacaster167//168public synchronized169void addNotificationListener(NotificationListener listener,170NotificationFilter filter,171Object handback)172throws IllegalArgumentException {173broadcaster.addNotificationListener(listener,filter,handback) ;174}175176// From NotificationEmitter extends NotificationBroacaster177//178public synchronized179void removeNotificationListener(NotificationListener listener,180NotificationFilter filter,181Object handback)182throws ListenerNotFoundException {183broadcaster.removeNotificationListener(listener,filter,handback) ;184}185186// From NotificationEmitter extends NotificationBroacaster187//188public synchronized189void removeNotificationListener(NotificationListener listener)190throws ListenerNotFoundException {191broadcaster.removeNotificationListener(listener) ;192}193194/**195* Enables the MBean server to send a notification.196* If the passed <var>notification</var> has a sequence number lesser197* or equal to 0, then replace it with the delegate's own sequence198* number.199* @param notification The notification to send.200*201*/202public void sendNotification(Notification notification) {203if (notification.getSequenceNumber() < 1) {204synchronized (this) {205notification.setSequenceNumber(this.sequenceNumber++);206}207}208broadcaster.sendNotification(notification);209}210211/**212* Defines the default ObjectName of the MBeanServerDelegate.213*214* @since 1.6215*/216public static final ObjectName DELEGATE_NAME =217Util.newObjectName("JMImplementation:type=MBeanServerDelegate");218219/* Return a timestamp that is monotonically increasing even if220System.currentTimeMillis() isn't (for example, if you call this221constructor more than once in the same millisecond, or if the222clock always returns the same value). This means that the ids223for a given JVM will always be distinact, though there is no224such guarantee for two different JVMs. */225private static synchronized long getStamp() {226long s = System.currentTimeMillis();227if (oldStamp >= s) {228s = oldStamp + 1;229}230oldStamp = s;231return s;232}233}234235236