Path: blob/master/src/java.management/share/classes/javax/management/MBeanServerDelegate.java
41154 views
/*1* Copyright (c) 1999, 2017, 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 java.lang.System.Logger.Level;28import com.sun.jmx.defaults.JmxProperties;29import com.sun.jmx.defaults.ServiceName;30import com.sun.jmx.mbeanserver.Util;3132/**33* Represents the MBean server from the management point of view.34* The MBeanServerDelegate MBean emits the MBeanServerNotifications when35* an MBean is registered/unregistered in the MBean server.36*37* @since 1.538*/39public class MBeanServerDelegate implements MBeanServerDelegateMBean,40NotificationEmitter {4142/** The MBean server agent identification.*/43private String mbeanServerId ;4445/** The NotificationBroadcasterSupport object that sends the46notifications */47private final NotificationBroadcasterSupport broadcaster;4849private static long oldStamp = 0;50private final long stamp;51private long sequenceNumber = 1;5253private static final MBeanNotificationInfo[] notifsInfo;5455static {56final String[] types = {57MBeanServerNotification.UNREGISTRATION_NOTIFICATION,58MBeanServerNotification.REGISTRATION_NOTIFICATION59};60notifsInfo = new MBeanNotificationInfo[1];61notifsInfo[0] =62new MBeanNotificationInfo(types,63"javax.management.MBeanServerNotification",64"Notifications sent by the MBeanServerDelegate MBean");65}6667/**68* Create a MBeanServerDelegate object.69*/70public MBeanServerDelegate () {71stamp = getStamp();72broadcaster = new NotificationBroadcasterSupport() ;73}747576/**77* Returns the MBean server agent identity.78*79* @return the identity.80*/81public synchronized String getMBeanServerId() {82if (mbeanServerId == null) {83String localHost;84try {85localHost = java.net.InetAddress.getLocalHost().getHostName();86} catch (java.net.UnknownHostException e) {87JmxProperties.MISC_LOGGER.log(Level.TRACE,88"Can't get local host name, " +89"using \"localhost\" instead. Cause is: "+e);90localHost = "localhost";91}92mbeanServerId = localHost + "_" + stamp;93}94return mbeanServerId;95}9697/**98* Returns the full name of the JMX specification implemented99* by this product.100*101* @return the specification name.102*/103public String getSpecificationName() {104return ServiceName.JMX_SPEC_NAME;105}106107/**108* Returns the version of the JMX specification implemented109* by this product.110*111* @return the specification version.112*/113public String getSpecificationVersion() {114return ServiceName.JMX_SPEC_VERSION;115}116117/**118* Returns the vendor of the JMX specification implemented119* by this product.120*121* @return the specification vendor.122*/123public String getSpecificationVendor() {124return ServiceName.JMX_SPEC_VENDOR;125}126127/**128* Returns the JMX implementation name (the name of this product).129*130* @return the implementation name.131*/132public String getImplementationName() {133return ServiceName.JMX_IMPL_NAME;134}135136/**137* Returns the JMX implementation version (the version of this product).138*139* @return the implementation version.140*/141public String getImplementationVersion() {142try {143return System.getProperty("java.runtime.version");144} catch (SecurityException e) {145return "";146}147}148149/**150* Returns the JMX implementation vendor (the vendor of this product).151*152* @return the implementation vendor.153*/154public String getImplementationVendor() {155return ServiceName.JMX_IMPL_VENDOR;156}157158// From NotificationEmitter extends NotificationBroacaster159//160public MBeanNotificationInfo[] getNotificationInfo() {161final int len = MBeanServerDelegate.notifsInfo.length;162final MBeanNotificationInfo[] infos =163new MBeanNotificationInfo[len];164System.arraycopy(MBeanServerDelegate.notifsInfo,0,infos,0,len);165return infos;166}167168// From NotificationEmitter extends NotificationBroacaster169//170public synchronized171void addNotificationListener(NotificationListener listener,172NotificationFilter filter,173Object handback)174throws IllegalArgumentException {175broadcaster.addNotificationListener(listener,filter,handback) ;176}177178// From NotificationEmitter extends NotificationBroacaster179//180public synchronized181void removeNotificationListener(NotificationListener listener,182NotificationFilter filter,183Object handback)184throws ListenerNotFoundException {185broadcaster.removeNotificationListener(listener,filter,handback) ;186}187188// From NotificationEmitter extends NotificationBroacaster189//190public synchronized191void removeNotificationListener(NotificationListener listener)192throws ListenerNotFoundException {193broadcaster.removeNotificationListener(listener) ;194}195196/**197* Enables the MBean server to send a notification.198* If the passed <var>notification</var> has a sequence number lesser199* or equal to 0, then replace it with the delegate's own sequence200* number.201* @param notification The notification to send.202*203*/204public void sendNotification(Notification notification) {205if (notification.getSequenceNumber() < 1) {206synchronized (this) {207notification.setSequenceNumber(this.sequenceNumber++);208}209}210broadcaster.sendNotification(notification);211}212213/**214* Defines the default ObjectName of the MBeanServerDelegate.215*216* @since 1.6217*/218public static final ObjectName DELEGATE_NAME =219Util.newObjectName("JMImplementation:type=MBeanServerDelegate");220221/* Return a timestamp that is monotonically increasing even if222System.currentTimeMillis() isn't (for example, if you call this223constructor more than once in the same millisecond, or if the224clock always returns the same value). This means that the ids225for a given JVM will always be distinact, though there is no226such guarantee for two different JVMs. */227private static synchronized long getStamp() {228long s = System.currentTimeMillis();229if (oldStamp >= s) {230s = oldStamp + 1;231}232oldStamp = s;233return s;234}235}236237238