001 /*
002 * Portions Copyright 2000-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 * @(#)author IBM Corp.
027 *
028 * Copyright IBM Corp. 1999-2000. All rights reserved.
029 */
030
031 package javax.management.modelmbean;
032
033 import static com.sun.jmx.defaults.JmxProperties.MODELMBEAN_LOGGER;
034 import com.sun.jmx.mbeanserver.GetPropertyAction;
035
036 import java.io.IOException;
037 import java.io.ObjectInputStream;
038 import java.io.ObjectOutputStream;
039 import java.io.ObjectStreamField;
040 import java.security.AccessController;
041 import java.util.logging.Level;
042
043 import javax.management.Descriptor;
044 import javax.management.DescriptorAccess;
045 import javax.management.MBeanNotificationInfo;
046 import javax.management.RuntimeOperationsException;
047
048 /**
049 * The ModelMBeanNotificationInfo object describes a notification emitted
050 * by a ModelMBean.
051 * It is a subclass of MBeanNotificationInfo with the addition of an
052 * associated Descriptor and an implementation of the Descriptor interface.
053 * <P>
054 * The fields in the descriptor are defined, but not limited to,
055 * the following:
056 * <PRE>
057 * name : notification name
058 * descriptorType : must be "notification"
059 * severity : 0-6 where 0: unknown; 1: non-recoverable;
060 * 2: critical, failure; 3: major, severe;
061 * 4: minor, marginal, error; 5: warning;
062 * 6: normal, cleared, informative
063 * messageID : unique key for message text (to allow translation,
064 * analysis)
065 * messageText : text of notification
066 * log : T - log message F - do not log message
067 * logfile : string fully qualified file name appropriate for
068 * operating system
069 * visibility : 1-4 where 1: always visible 4: rarely visible
070 * presentationString : xml formatted string to allow presentation of data
071 * </PRE>
072 * The default descriptor contains the name, descriptorType,
073 * displayName and severity(=6) fields. The default value of the name
074 * and displayName fields is the name of the Notification class (as
075 * specified by the <code>name</code> parameter of the
076 * ModelMBeanNotificationInfo constructor).
077 *
078 * <p>The <b>serialVersionUID</b> of this class is <code>-7445681389570207141L</code>.
079 *
080 * @since 1.5
081 */
082
083 // Sun Microsystems, Sept. 2002: Revisited for JMX 1.2 (DF)
084 //
085 @SuppressWarnings("serial")
086 // serialVersionUID is not constant
087 public class ModelMBeanNotificationInfo extends MBeanNotificationInfo
088 implements DescriptorAccess {
089
090 // Serialization compatibility stuff:
091 // Two serial forms are supported in this class. The selected form
092 // depends on system property "jmx.serial.form":
093 // - "1.0" for JMX 1.0
094 // - any other value for JMX 1.1 and higher
095 //
096 // Serial version for old serial form
097 private static final long oldSerialVersionUID = -5211564525059047097L;
098 //
099 // Serial version for new serial form
100 private static final long newSerialVersionUID = -7445681389570207141L;
101 //
102 // Serializable fields in old serial form
103 private static final ObjectStreamField[] oldSerialPersistentFields = {
104 new ObjectStreamField("notificationDescriptor",
105 Descriptor.class),
106 new ObjectStreamField("currClass", String.class) };
107 //
108 // Serializable fields in new serial form
109 private static final ObjectStreamField[] newSerialPersistentFields = { new ObjectStreamField(
110 "notificationDescriptor", Descriptor.class) };
111 //
112 // Actual serial version and serial form
113 private static final long serialVersionUID;
114 /**
115 * @serialField notificationDescriptor Descriptor The descriptor
116 * containing the appropriate metadata for this instance
117 */
118 private static final ObjectStreamField[] serialPersistentFields;
119 private static boolean compat = false;
120 static {
121 try {
122 GetPropertyAction act = new GetPropertyAction(
123 "jmx.serial.form");
124 String form = AccessController.doPrivileged(act);
125 compat = (form != null && form.equals("1.0"));
126 } catch (Exception e) {
127 // OK: No compat with 1.0
128 }
129 if (compat) {
130 serialPersistentFields = oldSerialPersistentFields;
131 serialVersionUID = oldSerialVersionUID;
132 } else {
133 serialPersistentFields = newSerialPersistentFields;
134 serialVersionUID = newSerialVersionUID;
135 }
136 }
137 //
138 // END Serialization compatibility stuff
139
140 /**
141 * @serial The descriptor containing the appropriate metadata for
142 * this instance
143 */
144 private Descriptor notificationDescriptor;
145
146 private static final String currClass = "ModelMBeanNotificationInfo";
147
148 /**
149 * Constructs a ModelMBeanNotificationInfo object with a default
150 * descriptor.
151 *
152 * @param notifTypes The array of strings (in dot notation) containing
153 * the notification types that may be emitted.
154 * @param name The name of the Notification class.
155 * @param description A human readable description of the
156 * Notification. Optional.
157 **/
158 public ModelMBeanNotificationInfo(String[] notifTypes, String name,
159 String description) {
160 this (notifTypes, name, description, null);
161 }
162
163 /**
164 * Constructs a ModelMBeanNotificationInfo object.
165 *
166 * @param notifTypes The array of strings (in dot notation)
167 * containing the notification types that may be emitted.
168 * @param name The name of the Notification class.
169 * @param description A human readable description of the Notification.
170 * Optional.
171 * @param descriptor An instance of Descriptor containing the
172 * appropriate metadata for this instance of the
173 * MBeanNotificationInfo. If it is null a default descriptor
174 * will be created. If the descriptor does not contain all the
175 * fields "name", "descriptorType", "displayName" and "severity",
176 * the missing ones are added with their default values.
177 *
178 * @exception RuntimeOperationsException Wraps an
179 * {@link IllegalArgumentException}. The descriptor is invalid, or
180 * descriptor field "name" is present but not equal to parameter name, or
181 * descriptor field "descriptorType" is present but not equal to "notification".
182 *
183 **/
184 public ModelMBeanNotificationInfo(String[] notifTypes, String name,
185 String description, Descriptor descriptor) {
186 super (notifTypes, name, description);
187 if (MODELMBEAN_LOGGER.isLoggable(Level.FINER)) {
188 MODELMBEAN_LOGGER.logp(Level.FINER,
189 ModelMBeanNotificationInfo.class.getName(),
190 "ModelMBeanNotificationInfo", "Entry");
191 }
192 notificationDescriptor = validDescriptor(descriptor);
193 }
194
195 /**
196 * Constructs a new ModelMBeanNotificationInfo object from this
197 * ModelMBeanNotfication Object.
198 *
199 * @param inInfo the ModelMBeanNotificationInfo to be duplicated
200 *
201 **/
202 public ModelMBeanNotificationInfo(ModelMBeanNotificationInfo inInfo) {
203 this (inInfo.getNotifTypes(), inInfo.getName(), inInfo
204 .getDescription(), inInfo.getDescriptor());
205 }
206
207 /**
208 * Creates and returns a new ModelMBeanNotificationInfo which is a
209 * duplicate of this ModelMBeanNotificationInfo.
210 **/
211 public Object clone() {
212 if (MODELMBEAN_LOGGER.isLoggable(Level.FINER)) {
213 MODELMBEAN_LOGGER.logp(Level.FINER,
214 ModelMBeanNotificationInfo.class.getName(),
215 "clone()", "Entry");
216 }
217 return (new ModelMBeanNotificationInfo(this ));
218 }
219
220 /**
221 * Returns a copy of the associated Descriptor for the
222 * ModelMBeanNotificationInfo.
223 *
224 * @return Descriptor associated with the
225 * ModelMBeanNotificationInfo object.
226 *
227 * @see #setDescriptor
228 **/
229 public Descriptor getDescriptor() {
230 if (MODELMBEAN_LOGGER.isLoggable(Level.FINER)) {
231 MODELMBEAN_LOGGER.logp(Level.FINER,
232 ModelMBeanNotificationInfo.class.getName(),
233 "getDescriptor()", "Entry");
234 }
235
236 if (notificationDescriptor == null) {
237 // Dead code. Should never happen.
238 if (MODELMBEAN_LOGGER.isLoggable(Level.FINER)) {
239 MODELMBEAN_LOGGER
240 .logp(
241 Level.FINER,
242 ModelMBeanNotificationInfo.class
243 .getName(),
244 "getDescriptor()",
245 "Descriptor value is null, "
246 + "setting descriptor to default values");
247 }
248 notificationDescriptor = validDescriptor(null);
249 }
250
251 return ((Descriptor) notificationDescriptor.clone());
252 }
253
254 /**
255 * Sets associated Descriptor (full replace) for the
256 * ModelMBeanNotificationInfo If the new Descriptor is null,
257 * then the associated Descriptor reverts to a default
258 * descriptor. The Descriptor is validated before it is
259 * assigned. If the new Descriptor is invalid, then a
260 * RuntimeOperationsException wrapping an
261 * IllegalArgumentException is thrown.
262 *
263 * @param inDescriptor replaces the Descriptor associated with the
264 * ModelMBeanNotification interface
265 *
266 * @exception RuntimeOperationsException Wraps an
267 * {@link IllegalArgumentException} for invalid Descriptor.
268 *
269 * @see #getDescriptor
270 **/
271 public void setDescriptor(Descriptor inDescriptor) {
272 if (MODELMBEAN_LOGGER.isLoggable(Level.FINER)) {
273 MODELMBEAN_LOGGER.logp(Level.FINER,
274 ModelMBeanNotificationInfo.class.getName(),
275 "setDescriptor(Descriptor)", "Entry");
276 }
277 notificationDescriptor = validDescriptor(inDescriptor);
278 }
279
280 /**
281 * Returns a human readable string containing
282 * ModelMBeanNotificationInfo.
283 *
284 * @return a string describing this object.
285 **/
286 public String toString() {
287 if (MODELMBEAN_LOGGER.isLoggable(Level.FINER)) {
288 MODELMBEAN_LOGGER.logp(Level.FINER,
289 ModelMBeanNotificationInfo.class.getName(),
290 "toString()", "Entry");
291 }
292
293 final StringBuilder retStr = new StringBuilder();
294
295 retStr.append("ModelMBeanNotificationInfo: ").append(
296 this .getName());
297
298 retStr.append(" ; Description: ").append(this .getDescription());
299
300 retStr.append(" ; Descriptor: ").append(this .getDescriptor());
301
302 retStr.append(" ; Types: ");
303 String[] nTypes = this .getNotifTypes();
304 for (int i = 0; i < nTypes.length; i++) {
305 if (i > 0)
306 retStr.append(", ");
307 retStr.append(nTypes[i]);
308 }
309 return retStr.toString();
310 }
311
312 /**
313 * Clones the passed in Descriptor, sets default values, and checks for validity.
314 * If the Descriptor is invalid (for instance by having the wrong "name"),
315 * this indicates programming error and a RuntimeOperationsException will be thrown.
316 *
317 * The following fields will be defaulted if they are not already set:
318 * descriptorType="notification",displayName=this.getName(),
319 * name=this.getName(),severity="6"
320 *
321 *
322 * @param in Descriptor to be checked, or null which is equivalent to an
323 * empty Descriptor.
324 * @exception RuntimeOperationsException if Descriptor is invalid
325 */
326 private Descriptor validDescriptor(final Descriptor in)
327 throws RuntimeOperationsException {
328 Descriptor clone = null;
329 if (in == null) {
330 clone = new DescriptorSupport();
331 MODELMBEAN_LOGGER.finer("Null Descriptor, creating new.");
332 } else {
333 clone = (Descriptor) in.clone();
334 }
335
336 //Setting defaults.
337 if (clone.getFieldValue("name") == null) {
338 clone.setField("name", this .getName());
339 MODELMBEAN_LOGGER.finer("Defaulting Descriptor name to "
340 + this .getName());
341 }
342 if (clone.getFieldValue("descriptorType") == null) {
343 clone.setField("descriptorType", "notification");
344 MODELMBEAN_LOGGER
345 .finer("Defaulting descriptorType to \"notification\"");
346 }
347 if (clone.getFieldValue("displayName") == null) {
348 clone.setField("displayName", this .getName());
349 MODELMBEAN_LOGGER
350 .finer("Defaulting Descriptor displayName to "
351 + this .getName());
352 }
353 if (clone.getFieldValue("severity") == null) {
354 clone.setField("severity", "6");
355 MODELMBEAN_LOGGER
356 .finer("Defaulting Descriptor severity field to 6");
357 }
358
359 //Checking validity
360 if (!clone.isValid()) {
361 throw new RuntimeOperationsException(
362 new IllegalArgumentException(
363 "Invalid Descriptor argument"),
364 "The isValid() method of the Descriptor object itself returned false,"
365 + "one or more required fields are invalid. Descriptor:"
366 + clone.toString());
367 }
368 if (!((String) clone.getFieldValue("name"))
369 .equalsIgnoreCase(this .getName())) {
370 throw new RuntimeOperationsException(
371 new IllegalArgumentException(
372 "Invalid Descriptor argument"),
373 "The Descriptor \"name\" field does not match the object described. "
374 + " Expected: " + this .getName()
375 + " , was: " + clone.getFieldValue("name"));
376 }
377 if (!((String) clone.getFieldValue("descriptorType"))
378 .equalsIgnoreCase("notification")) {
379 throw new RuntimeOperationsException(
380 new IllegalArgumentException(
381 "Invalid Descriptor argument"),
382 "The Descriptor \"descriptorType\" field does not match the object described. "
383 + " Expected: \"notification\" ,"
384 + " was: "
385 + clone.getFieldValue("descriptorType"));
386 }
387
388 return clone;
389 }
390
391 /**
392 * Deserializes a {@link ModelMBeanNotificationInfo} from an
393 * {@link ObjectInputStream}.
394 **/
395 private void readObject(ObjectInputStream in) throws IOException,
396 ClassNotFoundException {
397 // New serial form ignores extra field "currClass"
398 in.defaultReadObject();
399 }
400
401 /**
402 * Serializes a {@link ModelMBeanNotificationInfo} to an
403 * {@link ObjectOutputStream}.
404 **/
405 private void writeObject(ObjectOutputStream out) throws IOException {
406 if (compat) {
407 // Serializes this instance in the old serial form
408 //
409 ObjectOutputStream.PutField fields = out.putFields();
410 fields
411 .put("notificationDescriptor",
412 notificationDescriptor);
413 fields.put("currClass", currClass);
414 out.writeFields();
415 } else {
416 // Serializes this instance in the new serial form
417 //
418 out.defaultWriteObject();
419 }
420 }
421
422 }
|