0N/A/*
2362N/A * Copyright (c) 1999, 2008, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/Apackage javax.management;
0N/A
528N/Aimport com.sun.jmx.defaults.JmxProperties;
0N/Aimport com.sun.jmx.defaults.ServiceName;
528N/Aimport com.sun.jmx.mbeanserver.Util;
0N/A
0N/A/**
0N/A * Represents the MBean server from the management point of view.
0N/A * The MBeanServerDelegate MBean emits the MBeanServerNotifications when
0N/A * an MBean is registered/unregistered in the MBean server.
0N/A *
0N/A * @since 1.5
0N/A */
0N/Apublic class MBeanServerDelegate implements MBeanServerDelegateMBean,
0N/A NotificationEmitter {
0N/A
0N/A /** The MBean server agent identification.*/
0N/A private String mbeanServerId ;
0N/A
0N/A /** The NotificationBroadcasterSupport object that sends the
0N/A notifications */
0N/A private final NotificationBroadcasterSupport broadcaster;
0N/A
0N/A private static long oldStamp = 0;
0N/A private final long stamp;
0N/A private long sequenceNumber = 1;
0N/A
0N/A private static final MBeanNotificationInfo[] notifsInfo;
0N/A
0N/A static {
0N/A final String[] types = {
0N/A MBeanServerNotification.UNREGISTRATION_NOTIFICATION,
0N/A MBeanServerNotification.REGISTRATION_NOTIFICATION
0N/A };
0N/A notifsInfo = new MBeanNotificationInfo[1];
0N/A notifsInfo[0] =
0N/A new MBeanNotificationInfo(types,
0N/A "javax.management.MBeanServerNotification",
0N/A "Notifications sent by the MBeanServerDelegate MBean");
0N/A }
0N/A
0N/A /**
0N/A * Create a MBeanServerDelegate object.
0N/A */
0N/A public MBeanServerDelegate () {
0N/A stamp = getStamp();
0N/A broadcaster = new NotificationBroadcasterSupport() ;
0N/A }
0N/A
0N/A
0N/A /**
0N/A * Returns the MBean server agent identity.
0N/A *
0N/A * @return the identity.
0N/A */
0N/A public synchronized String getMBeanServerId() {
0N/A if (mbeanServerId == null) {
0N/A String localHost;
0N/A try {
0N/A localHost = java.net.InetAddress.getLocalHost().getHostName();
0N/A } catch (java.net.UnknownHostException e) {
528N/A JmxProperties.MISC_LOGGER.finest("Can't get local host name, " +
528N/A "using \"localhost\" instead. Cause is: "+e);
0N/A localHost = "localhost";
0N/A }
1790N/A mbeanServerId = localHost + "_" + stamp;
0N/A }
0N/A return mbeanServerId;
0N/A }
0N/A
0N/A /**
0N/A * Returns the full name of the JMX specification implemented
0N/A * by this product.
0N/A *
0N/A * @return the specification name.
0N/A */
0N/A public String getSpecificationName() {
0N/A return ServiceName.JMX_SPEC_NAME;
0N/A }
0N/A
0N/A /**
0N/A * Returns the version of the JMX specification implemented
0N/A * by this product.
0N/A *
0N/A * @return the specification version.
0N/A */
0N/A public String getSpecificationVersion() {
0N/A return ServiceName.JMX_SPEC_VERSION;
0N/A }
0N/A
0N/A /**
0N/A * Returns the vendor of the JMX specification implemented
0N/A * by this product.
0N/A *
0N/A * @return the specification vendor.
0N/A */
0N/A public String getSpecificationVendor() {
0N/A return ServiceName.JMX_SPEC_VENDOR;
0N/A }
0N/A
0N/A /**
0N/A * Returns the JMX implementation name (the name of this product).
0N/A *
0N/A * @return the implementation name.
0N/A */
0N/A public String getImplementationName() {
0N/A return ServiceName.JMX_IMPL_NAME;
0N/A }
0N/A
0N/A /**
0N/A * Returns the JMX implementation version (the version of this product).
0N/A *
0N/A * @return the implementation version.
0N/A */
0N/A public String getImplementationVersion() {
0N/A try {
0N/A return System.getProperty("java.runtime.version");
0N/A } catch (SecurityException e) {
0N/A return "";
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Returns the JMX implementation vendor (the vendor of this product).
0N/A *
0N/A * @return the implementation vendor.
0N/A */
0N/A public String getImplementationVendor() {
0N/A return ServiceName.JMX_IMPL_VENDOR;
0N/A }
0N/A
0N/A // From NotificationEmitter extends NotificationBroacaster
0N/A //
0N/A public MBeanNotificationInfo[] getNotificationInfo() {
0N/A final int len = MBeanServerDelegate.notifsInfo.length;
0N/A final MBeanNotificationInfo[] infos =
0N/A new MBeanNotificationInfo[len];
0N/A System.arraycopy(MBeanServerDelegate.notifsInfo,0,infos,0,len);
0N/A return infos;
0N/A }
0N/A
0N/A // From NotificationEmitter extends NotificationBroacaster
0N/A //
0N/A public synchronized
0N/A void addNotificationListener(NotificationListener listener,
0N/A NotificationFilter filter,
0N/A Object handback)
0N/A throws IllegalArgumentException {
0N/A broadcaster.addNotificationListener(listener,filter,handback) ;
0N/A }
0N/A
0N/A // From NotificationEmitter extends NotificationBroacaster
0N/A //
0N/A public synchronized
0N/A void removeNotificationListener(NotificationListener listener,
0N/A NotificationFilter filter,
0N/A Object handback)
0N/A throws ListenerNotFoundException {
0N/A broadcaster.removeNotificationListener(listener,filter,handback) ;
0N/A }
0N/A
0N/A // From NotificationEmitter extends NotificationBroacaster
0N/A //
0N/A public synchronized
0N/A void removeNotificationListener(NotificationListener listener)
0N/A throws ListenerNotFoundException {
0N/A broadcaster.removeNotificationListener(listener) ;
0N/A }
0N/A
0N/A /**
0N/A * Enables the MBean server to send a notification.
0N/A * If the passed <var>notification</var> has a sequence number lesser
0N/A * or equal to 0, then replace it with the delegate's own sequence
0N/A * number.
0N/A * @param notification The notification to send.
0N/A *
0N/A */
0N/A public void sendNotification(Notification notification) {
0N/A if (notification.getSequenceNumber() < 1) {
0N/A synchronized (this) {
0N/A notification.setSequenceNumber(this.sequenceNumber++);
0N/A }
0N/A }
0N/A broadcaster.sendNotification(notification);
0N/A }
0N/A
0N/A /**
0N/A * Defines the default ObjectName of the MBeanServerDelegate.
0N/A *
0N/A * @since 1.6
0N/A */
528N/A public static final ObjectName DELEGATE_NAME =
1790N/A Util.newObjectName("JMImplementation:type=MBeanServerDelegate");
0N/A
0N/A /* Return a timestamp that is monotonically increasing even if
0N/A System.currentTimeMillis() isn't (for example, if you call this
0N/A constructor more than once in the same millisecond, or if the
0N/A clock always returns the same value). This means that the ids
0N/A for a given JVM will always be distinact, though there is no
0N/A such guarantee for two different JVMs. */
0N/A private static synchronized long getStamp() {
0N/A long s = System.currentTimeMillis();
0N/A if (oldStamp >= s) {
0N/A s = oldStamp + 1;
0N/A }
0N/A oldStamp = s;
0N/A return s;
0N/A }
0N/A}