Path: blob/master/src/java.management/share/classes/javax/management/MBeanNotificationInfo.java
41154 views
/*1* Copyright (c) 1999, 2013, 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.io.IOException;28import java.io.InvalidObjectException;29import java.io.ObjectInputStream;30import java.util.Arrays;31import java.util.Objects;3233/**34* <p>The {@code MBeanNotificationInfo} class is used to describe the35* characteristics of the different notification instances36* emitted by an MBean, for a given Java class of notification.37* If an MBean emits notifications that can be instances of different Java classes,38* then the metadata for that MBean should provide an {@code MBeanNotificationInfo}39* object for each of these notification Java classes.</p>40*41* <p>Instances of this class are immutable. Subclasses may be42* mutable but this is not recommended.</p>43*44* <p>This class extends {@code javax.management.MBeanFeatureInfo}45* and thus provides {@code name} and {@code description} fields.46* The {@code name} field should be the fully qualified Java class name of47* the notification objects described by this class.</p>48*49* <p>The {@code getNotifTypes} method returns an array of50* strings containing the notification types that the MBean may51* emit. The notification type is a dot-notation string which52* describes what the emitted notification is about, not the Java53* class of the notification. A single generic notification class can54* be used to send notifications of several types. All of these types55* are returned in the string array result of the56* {@code getNotifTypes} method.57*58* @since 1.559*/60public class MBeanNotificationInfo extends MBeanFeatureInfo implements Cloneable {6162/* Serial version */63static final long serialVersionUID = -3888371564530107064L;6465private static final String[] NO_TYPES = new String[0];6667static final MBeanNotificationInfo[] NO_NOTIFICATIONS =68new MBeanNotificationInfo[0];6970/**71* @serial The different types of the notification.72*/73private String[] types;7475/** @see MBeanInfo#arrayGettersSafe */76private final transient boolean arrayGettersSafe;7778/**79* Constructs an {@code MBeanNotificationInfo} object.80*81* @param notifTypes The array of strings (in dot notation)82* containing the notification types that the MBean may emit.83* This may be null with the same effect as a zero-length array.84* @param name The fully qualified Java class name of the85* described notifications.86* @param description A human readable description of the data.87*/88public MBeanNotificationInfo(String[] notifTypes,89String name,90String description) {91this(notifTypes, name, description, null);92}9394/**95* Constructs an {@code MBeanNotificationInfo} object.96*97* @param notifTypes The array of strings (in dot notation)98* containing the notification types that the MBean may emit.99* This may be null with the same effect as a zero-length array.100* @param name The fully qualified Java class name of the101* described notifications.102* @param description A human readable description of the data.103* @param descriptor The descriptor for the notifications. This may be null104* which is equivalent to an empty descriptor.105*106* @since 1.6107*/108public MBeanNotificationInfo(String[] notifTypes,109String name,110String description,111Descriptor descriptor) {112super(name, description, descriptor);113114/* We do not validate the notifTypes, since the spec just says115they are dot-separated, not that they must look like Java116classes. E.g. the spec doesn't forbid "sun.prob.25" as a117notifType, though it doesn't explicitly allow it118either. */119120this.types = (notifTypes != null && notifTypes.length > 0) ?121notifTypes.clone() : NO_TYPES;122this.arrayGettersSafe =123MBeanInfo.arrayGettersSafe(this.getClass(),124MBeanNotificationInfo.class);125}126127128/**129* Returns a shallow clone of this instance.130* The clone is obtained by simply calling {@code super.clone()},131* thus calling the default native shallow cloning mechanism132* implemented by {@code Object.clone()}.133* No deeper cloning of any internal field is made.134*/135public Object clone () {136try {137return super.clone() ;138} catch (CloneNotSupportedException e) {139// should not happen as this class is cloneable140return null;141}142}143144145/**146* Returns the array of strings (in dot notation) containing the147* notification types that the MBean may emit.148*149* @return the array of strings. Changing the returned array has no150* effect on this MBeanNotificationInfo.151*/152public String[] getNotifTypes() {153if (types.length == 0)154return NO_TYPES;155else156return types.clone();157}158159private String[] fastGetNotifTypes() {160if (arrayGettersSafe)161return types;162else163return getNotifTypes();164}165166public String toString() {167return168getClass().getName() + "[" +169"description=" + getDescription() + ", " +170"name=" + getName() + ", " +171"notifTypes=" + Arrays.asList(fastGetNotifTypes()) + ", " +172"descriptor=" + getDescriptor() +173"]";174}175176/**177* Compare this MBeanNotificationInfo to another.178*179* @param o the object to compare to.180*181* @return true if and only if {@code o} is an MBeanNotificationInfo182* such that its {@link #getName()}, {@link #getDescription()},183* {@link #getDescriptor()},184* and {@link #getNotifTypes()} values are equal (not necessarily185* identical) to those of this MBeanNotificationInfo. Two186* notification type arrays are equal if their corresponding187* elements are equal. They are not equal if they have the same188* elements but in a different order.189*/190public boolean equals(Object o) {191if (o == this)192return true;193if (!(o instanceof MBeanNotificationInfo))194return false;195MBeanNotificationInfo p = (MBeanNotificationInfo) o;196return (Objects.equals(p.getName(), getName()) &&197Objects.equals(p.getDescription(), getDescription()) &&198Objects.equals(p.getDescriptor(), getDescriptor()) &&199Arrays.equals(p.fastGetNotifTypes(), fastGetNotifTypes()));200}201202public int hashCode() {203int hash = getName().hashCode();204for (int i = 0; i < types.length; i++)205hash ^= types[i].hashCode();206return hash;207}208209private void readObject(ObjectInputStream ois) throws IOException, ClassNotFoundException {210ObjectInputStream.GetField gf = ois.readFields();211String[] t = (String[])gf.get("types", null);212213types = (t != null && t.length != 0) ? t.clone() : NO_TYPES;214}215}216217218