MBeanServerNotification.java revision 729
0N/A/*
3845N/A * Copyright 1999-2008 Sun Microsystems, Inc. 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
0N/A * published by the Free Software Foundation. Sun designates this
0N/A * particular file as subject to the "Classpath" exception as provided
0N/A * by Sun 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,
1472N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
1472N/A *
1472N/A * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
0N/A * CA 95054 USA or visit www.sun.com if you need additional information or
0N/A * have any questions.
0N/A */
0N/A
0N/Apackage javax.management;
0N/A
0N/A
0N/A/**
0N/A * Represents a notification emitted by the MBean Server through the MBeanServerDelegate MBean.
0N/A * The MBean Server emits the following types of notifications: MBean registration, MBean
0N/A * unregistration.
0N/A * <P>
0N/A * To receive MBeanServerNotifications, you need to register a listener with
0N/A * the {@link MBeanServerDelegate MBeanServerDelegate} MBean
0N/A * that represents the MBeanServer. The ObjectName of the MBeanServerDelegate is
0N/A * {@link MBeanServerDelegate#DELEGATE_NAME}, which is
0N/A * <CODE>JMImplementation:type=MBeanServerDelegate</CODE>.
0N/A *
0N/A * <p>The following code prints a message every time an MBean is registered
0N/A * or unregistered in the MBean Server {@code mbeanServer}:</p>
0N/A *
0N/A * <pre>
0N/A * private static final NotificationListener printListener = new NotificationListener() {
0N/A * public void handleNotification(Notification n, Object handback) {
0N/A * if (!(n instanceof MBeanServerNotification)) {
0N/A * System.out.println("Ignored notification of class " + n.getClass().getName());
0N/A * return;
0N/A * }
0N/A * MBeanServerNotification mbsn = (MBeanServerNotification) n;
0N/A * String what;
0N/A * if (n.getType().equals(MBeanServerNotification.REGISTRATION_NOTIFICATION))
0N/A * what = "MBean registered";
0N/A * else if (n.getType().equals(MBeanServerNotification.UNREGISTRATION_NOTIFICATION))
0N/A * what = "MBean unregistered";
0N/A * else
0N/A * what = "Unknown type " + n.getType();
0N/A * System.out.println("Received MBean Server notification: " + what + ": " +
0N/A * mbsn.getMBeanName());
0N/A * }
0N/A * };
0N/A *
0N/A * ...
0N/A * mbeanServer.addNotificationListener(
0N/A * MBeanServerDelegate.DELEGATE_NAME, printListener, null, null);
0N/A * </pre>
0N/A * <p id="group">
0N/A * An MBean which is not an {@link MBeanServerDelegate} may also emit
0N/A * MBeanServerNotifications. In particular, a custom subclass of the
0N/A * {@link javax.management.namespace.JMXDomain JMXDomain} MBean or a custom
0N/A * subclass of the {@link javax.management.namespace.JMXNamespace JMXNamespace}
0N/A * MBean may emit an MBeanServerNotification for a group of MBeans.<br>
0N/A * An MBeanServerNotification emitted to denote the registration or
0N/A * unregistration of a group of MBeans has the following characteristics:
0N/A * <ul><li>Its {@linkplain Notification#getType() notification type} is
0N/A * {@code "JMX.mbean.registered.group"} or
0N/A * {@code "JMX.mbean.unregistered.group"}, which can also be written {@link
0N/A * MBeanServerNotification#REGISTRATION_NOTIFICATION}{@code + ".group"} or
0N/A * {@link
0N/A * MBeanServerNotification#UNREGISTRATION_NOTIFICATION}{@code + ".group"}.
0N/A * </li>
0N/A * <li>Its {@linkplain #getMBeanName() MBean name} is an ObjectName pattern
0N/A * that selects the set (or a superset) of the MBeans being registered
0N/A * or unregistered</li>
0N/A * <li>Its {@linkplain Notification#getUserData() user data} can optionally
0N/A * be set to an array of ObjectNames containing the names of all MBeans
0N/A * being registered or unregistered.</li>
0N/A * </ul>
0N/A * </p>
0N/A * <p>
0N/A * MBeans which emit these group registration/unregistration notifications will
0N/A * declare them in their {@link MBeanInfo#getNotifications()
0N/A * MBeanNotificationInfo}.
0N/A * </p>
0N/A * <P>
0N/A * To receive a group MBeanServerNotification, you need to register a listener
0N/A * with the MBean that emits it. For instance, assuming that the {@link
0N/A * javax.management.namespace.JMXNamespace JMXNamespace} MBean handling
0N/A * namespace {@code "foo"} has declared that it emits such a notification,
0N/A * you will need to register your notification listener with that MBean, which
0N/A * will be named {@link
0N/A * javax.management.namespace.JMXNamespaces#getNamespaceObjectName(java.lang.String)
0N/A * foo//:type=JMXNamespace}.
0N/A * </p>
0N/A * <p>The following code prints a message every time a group of MBean is
0N/A * registered or unregistered in the namespace {@code "foo"}, assumimg its
0N/A * {@link javax.management.namespace.JMXNamespace handler} supports
0N/A * group MBeanServerNotifications:</p>
0N/A *
0N/A * <pre>
0N/A * private static final NotificationListener printListener = new NotificationListener() {
0N/A * public void handleNotification(Notification n, Object handback) {
0N/A * if (!(n instanceof MBeanServerNotification)) {
0N/A * System.out.println("Ignored notification of class " + n.getClass().getName());
0N/A * return;
0N/A * }
0N/A * MBeanServerNotification mbsn = (MBeanServerNotification) n;
0N/A * String what;
0N/A * ObjectName[] names = null;
0N/A * if (n.getType().equals(MBeanServerNotification.REGISTRATION_NOTIFICATION)) {
0N/A * what = "MBean registered";
0N/A * } else if (n.getType().equals(MBeanServerNotification.UNREGISTRATION_NOTIFICATION)) {
0N/A * what = "MBean unregistered";
0N/A * } else if (n.getType().equals(MBeanServerNotification.REGISTRATION_NOTIFICATION+".group")) {
0N/A * what = "Group of MBeans registered matching";
0N/A * if (mbsn.getUserData() instanceof ObjectName[])
0N/A * names = (ObjectName[]) mbsn.getUserData();
0N/A * } else if (n.getType().equals(MBeanServerNotification.UNREGISTRATION_NOTIFICATION+".group")) {
0N/A * what = "Group of MBeans unregistered matching";
0N/A * if (mbsn.getUserData() instanceof ObjectName[])
0N/A * names = (ObjectName[]) mbsn.getUserData();
0N/A * } else
0N/A * what = "Unknown type " + n.getType();
0N/A * System.out.println("Received MBean Server notification: " + what + ": " +
0N/A * mbsn.getMBeanName());
0N/A * if (names != null) {
0N/A * for (ObjectName mb : names)
0N/A * System.out.println("\t"+mb);
0N/A * }
0N/A * }
0N/A * };
0N/A *
0N/A * ...
0N/A * mbeanServer.addNotificationListener(
0N/A * JMXNamespaces.getNamespaceObjectName("foo"), printListener, null, null);
0N/A * </pre>
0N/A *
0N/A * @since 1.5
0N/A */
0N/Apublic class MBeanServerNotification extends Notification {
0N/A
0N/A
0N/A /* Serial version */
0N/A private static final long serialVersionUID = 2876477500475969677L;
0N/A /**
0N/A * Notification type denoting that an MBean has been registered.
0N/A * Value is "JMX.mbean.registered".
0N/A */
0N/A public static final String REGISTRATION_NOTIFICATION =
0N/A "JMX.mbean.registered";
0N/A /**
0N/A * Notification type denoting that an MBean has been unregistered.
0N/A * Value is "JMX.mbean.unregistered".
0N/A */
0N/A public static final String UNREGISTRATION_NOTIFICATION =
0N/A "JMX.mbean.unregistered";
0N/A /**
0N/A * @serial The object names of the MBeans concerned by this notification
0N/A */
0N/A private final ObjectName objectName;
0N/A
0N/A /**
0N/A * Creates an MBeanServerNotification object specifying object names of
0N/A * the MBeans that caused the notification and the specified notification
0N/A * type.
0N/A *
0N/A * @param type A string denoting the type of the
0N/A * notification. Set it to one these values: {@link
0N/A * #REGISTRATION_NOTIFICATION}, {@link
0N/A * #UNREGISTRATION_NOTIFICATION}.
0N/A * @param source The MBeanServerNotification object responsible
0N/A * for forwarding MBean server notification.
0N/A * @param sequenceNumber A sequence number that can be used to order
0N/A * received notifications.
0N/A * @param objectName The object name of the MBean that caused the
0N/A * notification.
0N/A *
0N/A */
0N/A public MBeanServerNotification(String type, Object source,
0N/A long sequenceNumber, ObjectName objectName) {
0N/A super(type, source, sequenceNumber);
605N/A this.objectName = objectName;
0N/A }
0N/A
0N/A /**
1007N/A * Returns the object name of the MBean that caused the notification.
1007N/A *
1007N/A * @return the object name of the MBean that caused the notification.
1007N/A */
1007N/A public ObjectName getMBeanName() {
1007N/A return objectName;
1007N/A }
1007N/A
1007N/A @Override
1007N/A public String toString() {
1007N/A return super.toString() + "[mbeanName=" + objectName + "]";
1007N/A
1007N/A }
1007N/A
1007N/A }
1007N/A