KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > javax > management > modelmbean > ModelMBeanNotificationInfo


1 /*
2  * @(#)file ModelMBeanNotificationInfo.java
3  * @(#)author IBM Corp.
4  * @(#)version 1.35
5  * @(#)lastedit 04/02/10
6  */

7 /*
8  * Copyright IBM Corp. 1999-2000. All rights reserved.
9  *
10  * The program is provided "as is" without any warranty express or implied,
11  * including the warranty of non-infringement and the implied warranties of
12  * merchantibility and fitness for a particular purpose. IBM will not be
13  * liable for any damages suffered by you or any third party claim against
14  * you regarding the Program.
15  *
16  * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
17  * This software is the proprietary information of Sun Microsystems, Inc.
18  * Use is subject to license terms.
19  *
20  * Copyright 2004 Sun Microsystems, Inc. Tous droits reserves.
21  * Ce logiciel est propriete de Sun Microsystems, Inc.
22  * Distribue par des licences qui en restreignent l'utilisation.
23  *
24  */

25
26
27 package javax.management.modelmbean;
28
29 import java.io.IOException JavaDoc;
30 import java.io.ObjectInputStream JavaDoc;
31 import java.io.ObjectOutputStream JavaDoc;
32 import java.io.ObjectStreamField JavaDoc;
33 import java.security.AccessController JavaDoc;
34 import java.security.PrivilegedAction JavaDoc;
35
36 import javax.management.Descriptor JavaDoc;
37 import javax.management.DescriptorAccess JavaDoc;
38 import javax.management.*;
39
40 import com.sun.jmx.mbeanserver.GetPropertyAction;
41 import com.sun.jmx.trace.Trace;
42
43
44 /**
45  * The ModelMBeanNotificationInfo object describes a notification emitted
46  * by a ModelMBean.
47  * It is a subclass of MBeanNotificationInfo with the addition of an
48  * associated Descriptor and an implementation of the Descriptor interface.
49  * <P>
50  * The fields in the descriptor are defined, but not limited to,
51  * the following:
52  * <PRE>
53  * name : notification name
54  * descriptorType : must be "notification"
55  * severity : 0-6 where 0: unknown; 1: non-recoverable;
56  * 2: critical, failure; 3: major, severe;
57  * 4: minor, marginal, error; 5: warning;
58  * 6: normal, cleared, informative
59  * messageID : unique key for message text (to allow translation,
60  * analysis)
61  * messageText : text of notification
62  * log : T - log message F - do not log message
63  * logfile : string fully qualified file name appropriate for
64  * operating system
65  * visibility : 1-4 where 1: always visible 4: rarely visible
66  * presentationString : xml formatted string to allow presentation of data
67  * </PRE>
68  * The default descriptor contains the name, descriptorType, displayName
69  * and severity(=6) fields.
70  *
71  * @since 1.5
72  */

73
74 // Sun Microsystems, Sept. 2002: Revisited for JMX 1.2 (DF)
75
//
76
public class ModelMBeanNotificationInfo extends MBeanNotificationInfo
77      implements DescriptorAccess JavaDoc, Cloneable JavaDoc
78 {
79
80     // Serialization compatibility stuff:
81
// Two serial forms are supported in this class. The selected form
82
// depends on system property "jmx.serial.form":
83
// - "1.0" for JMX 1.0
84
// - any other value for JMX 1.1 and higher
85
//
86
// Serial version for old serial form
87
private static final long oldSerialVersionUID = -5211564525059047097L;
88     //
89
// Serial version for new serial form
90
private static final long newSerialVersionUID = -7445681389570207141L;
91     //
92
// Serializable fields in old serial form
93
private static final ObjectStreamField JavaDoc[] oldSerialPersistentFields =
94     {
95       new ObjectStreamField JavaDoc("notificationDescriptor", Descriptor JavaDoc.class),
96       new ObjectStreamField JavaDoc("currClass", String JavaDoc.class)
97     };
98     //
99
// Serializable fields in new serial form
100
private static final ObjectStreamField JavaDoc[] newSerialPersistentFields =
101     {
102       new ObjectStreamField JavaDoc("notificationDescriptor", Descriptor JavaDoc.class)
103     };
104     //
105
// Actual serial version and serial form
106
private static final long serialVersionUID;
107     /**
108      * @serialField notificationDescriptor Descriptor The descriptor
109      * containing the appropriate metadata for this instance
110      */

111     private static final ObjectStreamField JavaDoc[] serialPersistentFields;
112     private static boolean compat = false;
113     static {
114     try {
115         PrivilegedAction JavaDoc act = new GetPropertyAction("jmx.serial.form");
116         String JavaDoc form = (String JavaDoc) AccessController.doPrivileged(act);
117         compat = (form != null && form.equals("1.0"));
118     } catch (Exception JavaDoc e) {
119         // OK: No compat with 1.0
120
}
121     if (compat) {
122         serialPersistentFields = oldSerialPersistentFields;
123         serialVersionUID = oldSerialVersionUID;
124     } else {
125         serialPersistentFields = newSerialPersistentFields;
126         serialVersionUID = newSerialVersionUID;
127     }
128     }
129     //
130
// END Serialization compatibility stuff
131

132     /**
133      * @serial The descriptor containing the appropriate metadata for
134      * this instance
135      */

136     private Descriptor JavaDoc notificationDescriptor;
137
138     private static final String JavaDoc currClass = "ModelMBeanNotificationInfo";
139
140     /**
141      * Constructs a ModelMBeanNotificationInfo object with a default
142      * descriptor.
143      *
144      * @param notifTypes The array of strings (in dot notation) containing
145      * the notification types that may be emitted.
146      * @param name The name of the Notification class.
147      * @param description A human readable description of the
148      * Notification. Optional.
149      **/

150     public ModelMBeanNotificationInfo(String JavaDoc[] notifTypes,
151                       String JavaDoc name,
152                       String JavaDoc description) {
153     this(notifTypes,name,description,null);
154     }
155
156     /**
157      * Constructs a ModelMBeanNotificationInfo object.
158      *
159      * @param notifTypes The array of strings (in dot notation)
160      * containing the notification types that may be emitted.
161      * @param name The name of the Notification class.
162      * @param description A human readable description of the Notification.
163      * Optional.
164      * @param descriptor An instance of Descriptor containing the
165      * appropriate metadata for this instance of the
166      * MBeanNotificationInfo. If it is null a default descriptor
167      * will be created. If the descriptor does not contain the
168      * fields "displayName" or "severity" these fields are added
169      * in the descriptor with their default values.
170      *
171      * @exception RuntimeOperationsException Wraps an
172      * {@link IllegalArgumentException}. The descriptor is invalid, or
173      * descriptor field "name" is not equal to parameter name, or
174      * descriptor field "DescriptorType" is not equal to "notification".
175      *
176      **/

177     public ModelMBeanNotificationInfo(String JavaDoc[] notifTypes,
178                       String JavaDoc name,
179                       String JavaDoc description,
180                       Descriptor JavaDoc descriptor) {
181     super(notifTypes, name, description);
182     if (tracing())
183         trace("ModelMBeanNotificationInfo()","Executed");
184     applyDescriptor(descriptor,"ModelMBeanNotificationInfo()");
185     }
186
187     /**
188      * Constructs a new ModelMBeanNotificationInfo object from this
189      * ModelMBeanNotfication Object.
190      *
191      * @param inInfo the ModelMBeanNotificationInfo to be duplicated
192      *
193      **/

194     public ModelMBeanNotificationInfo(ModelMBeanNotificationInfo JavaDoc inInfo) {
195     this(inInfo.getNotifTypes(),
196          inInfo.getName(),
197          inInfo.getDescription(),inInfo.getDescriptor());
198     }
199
200     /**
201      * Creates and returns a new ModelMBeanNotificationInfo which is a
202      * duplicate of this ModelMBeanNotificationInfo.
203      **/

204     public Object JavaDoc clone () {
205     if (tracing())
206         trace("ModelMBeanNotificationInfo.clone()","Executed");
207     return(new ModelMBeanNotificationInfo JavaDoc(this));
208     }
209
210     /**
211      * Returns a copy of the associated Descriptor for the
212      * ModelMBeanNotificationInfo.
213      *
214      * @return Descriptor associated with the
215      * ModelMBeanNotificationInfo object.
216      *
217      * @see #setDescriptor
218      **/

219     public Descriptor JavaDoc getDescriptor() {
220     if (tracing())
221         trace("ModelMBeanNotificationInfo.getDescriptor()","Executed");
222         
223     if (notificationDescriptor == null) {
224         // Dead code. Should never happen.
225
if (tracing())
226         trace("ModelMBeanNotificationInfo.getDescriptor()",
227               "Received null for new descriptor value, " +
228               "setting descriptor to default values");
229             
230         notificationDescriptor = createDefaultDescriptor();
231     }
232
233     return((Descriptor JavaDoc)notificationDescriptor.clone());
234     }
235
236     /**
237      * Sets associated Descriptor (full replace) for the
238      * ModelMBeanNotificationInfo If the new Descriptor is null,
239      * then the associated Descriptor reverts to a default
240      * descriptor. The Descriptor is validated before it is
241      * assigned. If the new Descriptor is invalid, then a
242      * RuntimeOperationsException wrapping an
243      * IllegalArgumentException is thrown.
244      *
245      * @param inDescriptor replaces the Descriptor associated with the
246      * ModelMBeanNotification interface
247      *
248      * @exception RuntimeOperationsException Wraps an
249      * {@link IllegalArgumentException} for invalid Descriptor.
250      *
251      * @see #getDescriptor
252      **/

253     public void setDescriptor(Descriptor JavaDoc inDescriptor) {
254     if (tracing())
255         trace("setDescriptor(Descriptor)",
256           "Executed");
257     applyDescriptor(inDescriptor,"setDescriptor(Descriptor)");
258     }
259
260     /**
261      * Returns a human readable string containing
262      * ModelMBeanNotificationInfo.
263      *
264      * @return a string describing this object.
265      **/

266     public String JavaDoc toString() {
267     if (tracing())
268         trace("toString()","Executed");
269         
270     final StringBuffer JavaDoc retStr = new StringBuffer JavaDoc();
271
272     retStr.append("ModelMBeanNotificationInfo: ")
273         .append(this.getName());
274
275     retStr.append(" ; Description: ")
276         .append(this.getDescription());
277
278     retStr.append(" ; Descriptor: ")
279         .append(this.getDescriptor());
280
281     retStr.append(" ; Types: ");
282     String JavaDoc[] nTypes = this.getNotifTypes();
283     for (int i=0; i < nTypes.length; i++) {
284         if (i > 0) retStr.append(", ");
285         retStr.append(nTypes[i]);
286     }
287     return retStr.toString();
288     }
289
290     /**
291      * Creates default descriptor for notification as follows:
292      * descriptorType=notification,
293      * name=this.getName(),displayname=this.getName(),severity=6
294      **/

295     private final Descriptor JavaDoc createDefaultDescriptor() {
296     if (tracing())
297         trace("createDefaultDescriptor()","Executed");
298         
299     return new DescriptorSupport JavaDoc(new
300         String JavaDoc[] {"descriptorType=notification",
301               ("name=" + this.getName()),
302               ("displayName=" + this.getName()),
303               "severity=6"});
304     }
305
306     /**
307      * Tests that the descriptor is valid and adds appropriate default
308      * fields not already specified. Field values must be correct for
309      * field names.
310      * Descriptor must have the same name as the notification,
311      * the descriptorType field must be "notification",
312      **/

313     private boolean isValid(Descriptor JavaDoc inDesc) {
314     boolean results = true;
315     String JavaDoc badField = "none";
316
317     if (inDesc == null) {
318         badField="nullDescriptor";
319         return false;
320     }
321
322     if (!inDesc.isValid()) {
323         // checks for empty descriptors, null,
324
// checks for empty name and descriptorType adn valid
325
// values for fields.
326
badField="invalidDescriptor";
327         results = false;
328     } else if (!((String JavaDoc)inDesc.getFieldValue("name")).
329            equalsIgnoreCase(this.getName())) {
330         badField="name";
331         results = false;
332     } else if (! ((String JavaDoc)inDesc.getFieldValue("descriptorType")).
333            equalsIgnoreCase("notification")) {
334         badField="descriptorType";
335         results = false;
336     }
337      
338     if (tracing()) trace("isValid()",("Returning " + results +
339                 ": Invalid field is " + badField));
340     return results;
341     }
342
343     /**
344      * The following fields will be defaulted if they are not already
345      * set:
346      * displayName=this.getName(),severity=6
347      * @return the given descriptor, possibly modified.
348      **/

349     private final Descriptor JavaDoc setDefaults(Descriptor JavaDoc descriptor) {
350     if ((descriptor.getFieldValue("displayName")) == null) {
351         descriptor.setField("displayName",this.getName());
352     }
353     if ((descriptor.getFieldValue("severity")) == null) {
354         descriptor.setField("severity","6");
355     }
356     return descriptor;
357     }
358
359     /**
360      * Set the given descriptor as this.notificationDescriptor.
361      * Creates a default descriptor if the given descriptor is null.
362      * If the given descriptor is null, check its validity.
363      * If it is valid, clones it and set the defaults fields
364      * "displayName" and "severity", if not present.
365      * If it is not valid, throws an exception.
366      * This method is called both by the constructors and by
367      * setDescriptor().
368      * @see #setDefaults
369      * @see #setDescriptor
370      **/

371     private final void applyDescriptor(Descriptor JavaDoc descriptor,
372                        String JavaDoc ftag) {
373     if (descriptor == null) {
374         if (tracing())
375         trace(ftag,
376               "Received null for new descriptor value, " +
377               "setting descriptor to default values");
378         
379         notificationDescriptor = createDefaultDescriptor();
380     } else if (isValid(descriptor)) {
381         notificationDescriptor =
382         setDefaults((Descriptor JavaDoc)descriptor.clone());
383     } else {
384         throw new RuntimeOperationsException(new
385         IllegalArgumentException JavaDoc(
386             "Invalid descriptor passed in parameter"),
387             "Exception occured in ModelMBeanNotificationInfo " + ftag);
388     }
389     }
390
391     // SUN Trace and debug functions
392
private boolean tracing() {
393     // return true;
394
return Trace.isSelected(Trace.LEVEL_TRACE, Trace.INFO_MODELMBEAN);
395     }
396
397     private void trace(String JavaDoc inClass, String JavaDoc inMethod, String JavaDoc inText) {
398     Trace.send(Trace.LEVEL_TRACE, Trace.INFO_MODELMBEAN, inClass,
399            inMethod, Integer.toHexString(this.hashCode()) +
400            " " + inText);
401     }
402
403     private void trace(String JavaDoc inMethod, String JavaDoc inText) {
404     trace(currClass, inMethod, inText);
405     }
406
407     /**
408      * Deserializes a {@link ModelMBeanNotificationInfo} from an
409      * {@link ObjectInputStream}.
410      **/

411     private void readObject(ObjectInputStream JavaDoc in)
412     throws IOException JavaDoc, ClassNotFoundException JavaDoc {
413     // New serial form ignores extra field "currClass"
414
in.defaultReadObject();
415     }
416
417
418     /**
419      * Serializes a {@link ModelMBeanNotificationInfo} to an
420      * {@link ObjectOutputStream}.
421      **/

422     private void writeObject(ObjectOutputStream JavaDoc out)
423     throws IOException JavaDoc {
424     if (compat) {
425         // Serializes this instance in the old serial form
426
//
427
ObjectOutputStream.PutField JavaDoc fields = out.putFields();
428         fields.put("notificationDescriptor", notificationDescriptor);
429         fields.put("currClass", currClass);
430         out.writeFields();
431     } else {
432         // Serializes this instance in the new serial form
433
//
434
out.defaultWriteObject();
435     }
436     }
437
438 }
439
Popular Tags