001 /*
002 * Copyright 1999-2006 Sun Microsystems, Inc. All Rights Reserved.
003 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004 *
005 * This code is free software; you can redistribute it and/or modify it
006 * under the terms of the GNU General Public License version 2 only, as
007 * published by the Free Software Foundation. Sun designates this
008 * particular file as subject to the "Classpath" exception as provided
009 * by Sun in the LICENSE file that accompanied this code.
010 *
011 * This code is distributed in the hope that it will be useful, but WITHOUT
012 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014 * version 2 for more details (a copy is included in the LICENSE file that
015 * accompanied this code).
016 *
017 * You should have received a copy of the GNU General Public License version
018 * 2 along with this work; if not, write to the Free Software Foundation,
019 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020 *
021 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022 * CA 95054 USA or visit www.sun.com if you need additional information or
023 * have any questions.
024 */
025
026 package javax.management;
027
028 import java.util.Arrays;
029
030 /**
031 * <p>The <CODE>MBeanNotificationInfo</CODE> class is used to describe the
032 * characteristics of the different notification instances
033 * emitted by an MBean, for a given Java class of notification.
034 * If an MBean emits notifications that can be instances of different Java classes,
035 * then the metadata for that MBean should provide an <CODE>MBeanNotificationInfo</CODE>
036 * object for each of these notification Java classes.</p>
037 *
038 * <p>Instances of this class are immutable. Subclasses may be
039 * mutable but this is not recommended.</p>
040 *
041 * <p>This class extends <CODE>javax.management.MBeanFeatureInfo</CODE>
042 * and thus provides <CODE>name</CODE> and <CODE>description</CODE> fields.
043 * The <CODE>name</CODE> field should be the fully qualified Java class name of
044 * the notification objects described by this class.</p>
045 *
046 * <p>The <CODE>getNotifTypes</CODE> method returns an array of
047 * strings containing the notification types that the MBean may
048 * emit. The notification type is a dot-notation string which
049 * describes what the emitted notification is about, not the Java
050 * class of the notification. A single generic notification class can
051 * be used to send notifications of several types. All of these types
052 * are returned in the string array result of the
053 * <CODE>getNotifTypes</CODE> method.
054 *
055 * @since 1.5
056 */
057 public class MBeanNotificationInfo extends MBeanFeatureInfo implements
058 Cloneable {
059
060 /* Serial version */
061 static final long serialVersionUID = -3888371564530107064L;
062
063 private static final String[] NO_TYPES = new String[0];
064
065 static final MBeanNotificationInfo[] NO_NOTIFICATIONS = new MBeanNotificationInfo[0];
066
067 /**
068 * @serial The different types of the notification.
069 */
070 private final String[] types;
071
072 /** @see MBeanInfo#arrayGettersSafe */
073 private final transient boolean arrayGettersSafe;
074
075 /**
076 * Constructs an <CODE>MBeanNotificationInfo</CODE> object.
077 *
078 * @param notifTypes The array of strings (in dot notation)
079 * containing the notification types that the MBean may emit.
080 * This may be null with the same effect as a zero-length array.
081 * @param name The fully qualified Java class name of the
082 * described notifications.
083 * @param description A human readable description of the data.
084 */
085 public MBeanNotificationInfo(String[] notifTypes, String name,
086 String description) {
087 this (notifTypes, name, description, null);
088 }
089
090 /**
091 * Constructs an <CODE>MBeanNotificationInfo</CODE> object.
092 *
093 * @param notifTypes The array of strings (in dot notation)
094 * containing the notification types that the MBean may emit.
095 * This may be null with the same effect as a zero-length array.
096 * @param name The fully qualified Java class name of the
097 * described notifications.
098 * @param description A human readable description of the data.
099 * @param descriptor The descriptor for the notifications. This may be null
100 * which is equivalent to an empty descriptor.
101 *
102 * @since 1.6
103 */
104 public MBeanNotificationInfo(String[] notifTypes, String name,
105 String description, Descriptor descriptor) {
106 super (name, description, descriptor);
107
108 /* We do not validate the notifTypes, since the spec just says
109 they are dot-separated, not that they must look like Java
110 classes. E.g. the spec doesn't forbid "sun.prob.25" as a
111 notifType, though it doesn't explicitly allow it
112 either. */
113
114 if (notifTypes == null)
115 notifTypes = NO_TYPES;
116 this .types = notifTypes;
117 this .arrayGettersSafe = MBeanInfo.arrayGettersSafe(this
118 .getClass(), MBeanNotificationInfo.class);
119 }
120
121 /**
122 * Returns a shallow clone of this instance.
123 * The clone is obtained by simply calling <tt>super.clone()</tt>,
124 * thus calling the default native shallow cloning mechanism
125 * implemented by <tt>Object.clone()</tt>.
126 * No deeper cloning of any internal field is made.
127 */
128 public Object clone() {
129 try {
130 return super .clone();
131 } catch (CloneNotSupportedException e) {
132 // should not happen as this class is cloneable
133 return null;
134 }
135 }
136
137 /**
138 * Returns the array of strings (in dot notation) containing the
139 * notification types that the MBean may emit.
140 *
141 * @return the array of strings. Changing the returned array has no
142 * effect on this MBeanNotificationInfo.
143 */
144 public String[] getNotifTypes() {
145 if (types.length == 0)
146 return NO_TYPES;
147 else
148 return types.clone();
149 }
150
151 private String[] fastGetNotifTypes() {
152 if (arrayGettersSafe)
153 return types;
154 else
155 return getNotifTypes();
156 }
157
158 public String toString() {
159 return getClass().getName() + "[" + "description="
160 + getDescription() + ", " + "name=" + getName() + ", "
161 + "notifTypes=" + Arrays.asList(fastGetNotifTypes())
162 + ", " + "descriptor=" + getDescriptor() + "]";
163 }
164
165 /**
166 * Compare this MBeanNotificationInfo to another.
167 *
168 * @param o the object to compare to.
169 *
170 * @return true if and only if <code>o</code> is an MBeanNotificationInfo
171 * such that its {@link #getName()}, {@link #getDescription()},
172 * {@link #getDescriptor()},
173 * and {@link #getNotifTypes()} values are equal (not necessarily
174 * identical) to those of this MBeanNotificationInfo. Two
175 * notification type arrays are equal if their corresponding
176 * elements are equal. They are not equal if they have the same
177 * elements but in a different order.
178 */
179 public boolean equals(Object o) {
180 if (o == this )
181 return true;
182 if (!(o instanceof MBeanNotificationInfo))
183 return false;
184 MBeanNotificationInfo p = (MBeanNotificationInfo) o;
185 return (p.getName().equals(getName())
186 && p.getDescription().equals(getDescription())
187 && p.getDescriptor().equals(getDescriptor()) && Arrays
188 .equals(p.fastGetNotifTypes(), fastGetNotifTypes()));
189 }
190
191 public int hashCode() {
192 int hash = getName().hashCode();
193 for (int i = 0; i < types.length; i++)
194 hash ^= types[i].hashCode();
195 return hash;
196 }
197 }
|