KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * @(#)file ModelMBeanAttributeInfo.java
3  * @(#)author IBM Corp.
4  * @(#)version 1.34
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.lang.reflect.*;
34 import java.security.AccessController JavaDoc;
35 import java.security.PrivilegedAction JavaDoc;
36
37 import javax.management.Descriptor JavaDoc;
38 import javax.management.DescriptorAccess JavaDoc;
39 import javax.management.*;
40
41 import com.sun.jmx.mbeanserver.GetPropertyAction;
42 import com.sun.jmx.trace.Trace;
43
44 /**
45  * The ModelMBeanAttributeInfo object describes an attribute of the ModelMBean.
46  * It is a subclass of MBeanAttributeInfo with the addition of an associated Descriptor
47  * and an implementation of the DescriptorAccess interface.
48  * <P>
49  * The fields in the descriptor are defined, but not limited to, the following: <P>
50  * <PRE>
51  * name : attribute name
52  * descriptorType : must be "attribute"
53  * value : current value for attribute
54  * default : default value for attribute
55  * displayName : name of attribute to be used in displays
56  * getMethod : name of operation descriptor for get method
57  * setMethod : name of operation descriptor for set method
58  * protocolMap : object which implements the Descriptor interface: mappings must be appropriate for the attribute
59  * and entries can be updated or augmented at runtime.
60  * persistPolicy : OnUpdate|OnTimer|NoMoreOftenThan|Always|Never
61  * persistPeriod : seconds - frequency of persist cycle. Used when persistPolicy is"OnTimer" or "NoMoreOftenThan".
62  * currencyTimeLimit : how long value is valid, &lt;0 never, =0 always, &gt;0 seconds
63  * lastUpdatedTimeStamp : when value was set
64  * visibility : 1-4 where 1: always visible 4: rarely visible
65  * presentationString : xml formatted string to allow presentation of data
66  * </PRE>
67  * The default descriptor contains the name, descriptorType and displayName fields.
68  *
69  * <p><b>Note:</b> because of inconsistencies in previous versions of
70  * this specification, it is recommended not to use negative or zero
71  * values for <code>currencyTimeLimit</code>. To indicate that a
72  * cached value is never valid, omit the
73  * <code>currencyTimeLimit</code> field. To indicate that it is
74  * always valid, use a very large number for this field.</p>
75  *
76  * @since 1.5
77  */

78
79
80 public class ModelMBeanAttributeInfo extends MBeanAttributeInfo
81      implements DescriptorAccess JavaDoc, Cloneable JavaDoc
82 {
83
84     // Serialization compatibility stuff:
85
// Two serial forms are supported in this class. The selected form depends
86
// on system property "jmx.serial.form":
87
// - "1.0" for JMX 1.0
88
// - any other value for JMX 1.1 and higher
89
//
90
// Serial version for old serial form
91
private static final long oldSerialVersionUID = 7098036920755973145L;
92     //
93
// Serial version for new serial form
94
private static final long newSerialVersionUID = 6181543027787327345L;
95     //
96
// Serializable fields in old serial form
97
private static final ObjectStreamField JavaDoc[] oldSerialPersistentFields =
98     {
99       new ObjectStreamField JavaDoc("attrDescriptor", Descriptor JavaDoc.class),
100       new ObjectStreamField JavaDoc("currClass", String JavaDoc.class)
101     };
102     //
103
// Serializable fields in new serial form
104
private static final ObjectStreamField JavaDoc[] newSerialPersistentFields =
105     {
106       new ObjectStreamField JavaDoc("attrDescriptor", Descriptor JavaDoc.class)
107     };
108     //
109
// Actual serial version and serial form
110
private static final long serialVersionUID;
111     /**
112      * @serialField attrDescriptor Descriptor The {@link Descriptor} containing the metadata corresponding to
113      * this attribute
114      */

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

136         /**
137          * @serial The {@link Descriptor} containing the metadata corresponding to
138          * this attribute
139          */

140         private Descriptor JavaDoc attrDescriptor = createDefaultDescriptor();
141
142     private final static String JavaDoc currClass = "ModelMBeanAttributeInfo";
143
144     /**
145      * Constructs a ModelMBeanAttributeInfo object with a default descriptor.
146      *
147      * @param name The name of the attribute.
148      * @param description A human readable description of the attribute. Optional.
149      * @param getter The method used for reading the attribute value.
150      * May be null if the property is write-only.
151      * @param setter The method used for writing the attribute value.
152      * May be null if the attribute is read-only.
153      * @exception IntrospectionException There is a consistency problem in the definition of this attribute.
154      *
155      */

156
157     public ModelMBeanAttributeInfo(String JavaDoc name,
158                        String JavaDoc description,
159                        Method getter,
160                        Method setter)
161     throws javax.management.IntrospectionException JavaDoc {
162         super(name, description, getter, setter);
163         if (tracing())
164         {
165             trace("ModelMBeanAttributeInfo(" + name + ",String,Method,Method)","Entry");
166         }
167
168         attrDescriptor = createDefaultDescriptor();
169         // put getter and setter methods in operations list
170
// create default descriptor
171

172     }
173
174     /**
175      * Constructs a ModelMBeanAttributeInfo object.
176      *
177      * @param name The name of the attribute.
178      * @param description A human readable description of the attribute. Optional.
179      * @param getter The method used for reading the attribute value.
180      * May be null if the property is write-only.
181      * @param setter The method used for writing the attribute value.
182      * May be null if the attribute is read-only.
183      * @param descriptor An instance of Descriptor containing the appropriate metadata
184      * for this instance of the Attribute. If it is null, then a default descriptor will be created.
185      * If the descriptor does not contain the field "displayName" this field is added in the descriptor with its default value.
186      *
187      * @exception IntrospectionException There is a consistency problem in the definition of this attribute.
188      * @exception RuntimeOperationsException Wraps an IllegalArgumentException. The descriptor is invalid, or descriptor field "name" is not
189      * equal to name parameter, or descriptor field "DescriptorType" is not equal to "attribute".
190      *
191      */

192
193     public ModelMBeanAttributeInfo(String JavaDoc name,
194                        String JavaDoc description,
195                        Method getter,
196                        Method setter,
197                        Descriptor JavaDoc descriptor)
198     throws javax.management.IntrospectionException JavaDoc {
199
200         super(name, description, getter, setter);
201         // put getter and setter methods in operations list
202
if (tracing())
203         {
204             trace("ModelMBeanAttributeInfo(" + name + ", String, Method, Method, Descriptor)","Entry");
205         }
206         if (descriptor == null)
207         {
208             attrDescriptor = createDefaultDescriptor();
209         } else
210         {
211             if (isValid(descriptor))
212             {
213                 attrDescriptor = (Descriptor JavaDoc) descriptor.clone();
214             } else
215             {
216                 throw new RuntimeOperationsException(new IllegalArgumentException JavaDoc("Invalid descriptor passed in parameter"), ("Exception occured in ModelMBeanAttributeInfo constructor"));
217             }
218         }
219     }
220
221     /**
222      * Constructs a ModelMBeanAttributeInfo object with a default descriptor.
223      *
224      * @param name The name of the attribute
225      * @param type The type or class name of the attribute
226      * @param description A human readable description of the attribute.
227      * @param isReadable True if the attribute has a getter method, false otherwise.
228      * @param isWritable True if the attribute has a setter method, false otherwise.
229      * @param isIs True if the attribute has an "is" getter, false otherwise.
230      *
231      */

232     public ModelMBeanAttributeInfo(String JavaDoc name,
233                        String JavaDoc type,
234                        String JavaDoc description,
235                        boolean isReadable,
236                        boolean isWritable,
237                        boolean isIs)
238     {
239
240         super(name, type, description, isReadable, isWritable, isIs);
241         // create default descriptor
242
if (tracing())
243         {
244             trace("ModelMBeanAttributeInfo(" + name + ",String,String,boolean,boolean)","Entry");
245         }
246         attrDescriptor = createDefaultDescriptor();
247
248     }
249     /**
250      * Constructs a ModelMBeanAttributeInfo object with a default descriptor.
251      *
252      * @param name The name of the attribute
253      * @param type The type or class name of the attribute
254      * @param description A human readable description of the attribute.
255      * @param isReadable True if the attribute has a getter method, false otherwise.
256      * @param isWritable True if the attribute has a setter method, false otherwise.
257      * @param isIs True if the attribute has an "is" getter, false otherwise.
258      * @param descriptor An instance of Descriptor containing the appropriate metadata
259      * for this instance of the Attribute. If it is null then a default descriptor will be created.
260      * If the descriptor does not contain the field "displayName" this field is added in the descriptor with its default value.
261      *
262      * @exception RuntimeOperationsException Wraps an IllegalArgumentException. The descriptor is invalid, or descriptor field "name" is not
263      * equal to name parameter, or descriptor field "DescriptorType" is not equal to "attribute".
264      *
265      */

266     public ModelMBeanAttributeInfo(String JavaDoc name,
267                        String JavaDoc type,
268                        String JavaDoc description,
269                        boolean isReadable,
270                        boolean isWritable,
271                        boolean isIs,
272                        Descriptor JavaDoc descriptor)
273     {
274         super(name, type, description, isReadable, isWritable, isIs);
275         if (tracing())
276         {
277             trace("ModelMBeanAttributeInfo(" + name + ",String,String,boolean,boolean,Descriptor)","Entry");
278
279         }
280
281         if (descriptor == null)
282         {
283             attrDescriptor = createDefaultDescriptor();
284         } else
285         {
286             if (isValid(descriptor))
287             {
288                 attrDescriptor = (Descriptor JavaDoc) descriptor.clone();
289             } else
290             {
291                 throw new RuntimeOperationsException(new IllegalArgumentException JavaDoc("Invalid descriptor passed in parameter"), ("Exception occured in ModelMBeanAttributeInfo constructor"));
292             }
293         }
294
295     }
296
297     /**
298      * Constructs a new ModelMBeanAttributeInfo object from this ModelMBeanAttributeInfo Object.
299      * A default descriptor will be created.
300      *
301      * @param inInfo the ModelMBeanAttributeInfo to be duplicated
302      */

303
304     public ModelMBeanAttributeInfo(ModelMBeanAttributeInfo JavaDoc inInfo)
305     {
306         super(inInfo.getName(),
307               inInfo.getType(),
308               inInfo.getDescription(),
309               inInfo.isReadable(),
310               inInfo.isWritable(),
311               inInfo.isIs());
312         if (tracing())
313         {
314             trace("ModelMBeanAttributeInfo(ModelMBeanAttributeInfo)","Entry");
315         }
316         Descriptor JavaDoc newDesc = inInfo.getDescriptor();
317
318         //Descriptor newDesc = inInfo.attrDescriptor;
319

320         if ((newDesc != null) && (isValid(newDesc)))
321         {
322             attrDescriptor = newDesc;
323         } else
324         {
325             attrDescriptor = createDefaultDescriptor();
326         }
327     }
328     /**
329      * Gets a copy of the associated Descriptor for the
330      * ModelMBeanAttributeInfo.
331      *
332      * @return Descriptor associated with the
333      * ModelMBeanAttributeInfo object.
334      *
335      * @see #setDescriptor
336      */

337
338     public Descriptor JavaDoc getDescriptor()
339     {
340         if (tracing())
341         {
342             trace("ModelMBeanAttributeInfo.getDescriptor()","Entry");
343         }
344         if (attrDescriptor == null)
345         {
346             attrDescriptor = createDefaultDescriptor();
347         }
348         return((Descriptor JavaDoc)attrDescriptor.clone());
349     }
350
351
352     /**
353     * Sets associated Descriptor (full replace) for the
354     * ModelMBeanAttributeDescriptor. If the new Descriptor is
355     * null, then the associated Descriptor reverts to a default
356     * descriptor. The Descriptor is validated before it is
357     * assigned. If the new Descriptor is invalid, then a
358     * RuntimeOperationsException wrapping an
359     * IllegalArgumentException is thrown.
360     *
361     * @param inDescriptor replaces the Descriptor associated with the
362     * ModelMBeanAttributeInfo
363     *
364     * @exception RuntimeOperationsException Wraps an
365     * IllegalArgumentException for an invalid Descriptor
366     *
367     * @see #getDescriptor
368     */

369     public void setDescriptor(Descriptor JavaDoc inDescriptor)
370     {
371         if (inDescriptor != null) {
372             if (tracing()) {
373                 trace("ModelMBeanAttributeInfo.setDescriptor()","Executed for " + inDescriptor.toString());
374             }
375         }
376         if (inDescriptor == null) {
377             if (tracing()) {
378                 trace("ModelMBeanAttributeInfo.setDescriptor()",
379                       "Received null for new descriptor value, setting descriptor to default values");
380             }
381             attrDescriptor = createDefaultDescriptor();
382         }
383         else {
384             if (isValid(inDescriptor)) {
385                 attrDescriptor = (Descriptor JavaDoc) inDescriptor.clone();
386             }
387             else {
388                 throw new RuntimeOperationsException(new IllegalArgumentException JavaDoc("Invalid descriptor passed in parameter"),
389                                                      ("Exception occured in ModelMBeanAttributeInfo setDescriptor"));
390             }
391         }
392     }
393
394     /**
395     * Creates and returns a new ModelMBeanAttributeInfo which is a duplicate of this ModelMBeanAttributeInfo.
396     *
397     * @exception RuntimeOperationsException for illegal value for field Names or field Values.
398     * If the descriptor construction fails for any reason, this exception will be thrown.
399     */

400
401     public Object JavaDoc clone()
402     {
403         if (tracing())
404         {
405             trace("ModelMBeanAttributeInfo.clone()","Entry");
406         }
407         return(new ModelMBeanAttributeInfo JavaDoc(this));
408     }
409
410     /**
411     * Returns a human-readable version of the
412     * ModelMBeanAttributeInfo instance.
413     */

414     public String JavaDoc toString()
415     {
416         return
417         "ModelMBeanAttributeInfo: " + this.getName() +
418         " ; Description: " + this.getDescription() +
419         " ; Types: " + this.getType() +
420         " ; isReadable: " + this.isReadable() +
421         " ; isWritable: " + this.isWritable() +
422         " ; Descriptor: " + this.getDescriptor();
423     }
424
425     /**
426     * Creates and returns a Descriptor with default values set:
427     * descriptorType=attribute,name=this.getName(),displayName=this.getName(),
428     * persistPolicy=never,visibility=1
429     */

430
431     private Descriptor JavaDoc createDefaultDescriptor()
432     {
433         if (tracing())
434         {
435             trace("ModelMBeanAttributeInfo.createDefaultDescriptor()","Entry");
436         }
437         return new DescriptorSupport JavaDoc(
438                          new String JavaDoc[] {"descriptorType=attribute",
439                                        ("name=" + this.getName()),
440                                        ("displayName=" + this.getName())
441                          });
442     }
443     /**
444     * Tests that the descriptor is valid and adds appropriate default fields not already
445     * specified. Field values must be correct for field names.
446     * Descriptor must have the same name as the attribute,the descriptorType field must be "attribute",
447     * The following fields will be defaulted if they are not already set:
448     * displayName=this.getName(),persistPolicy=never,visibility=1
449     */

450     private boolean isValid(Descriptor JavaDoc inDesc)
451     {
452         // name and descriptor type must be correct
453
// add in displayName, persistPolicy, visibility if they apply
454
boolean results=true;
455         String JavaDoc badField="none";
456         if (inDesc == null)
457         {
458             badField="nullDescriptor";
459             results = false;
460         }
461
462         else if (!inDesc.isValid())
463         { // checks for empty descriptors, null,
464
// checks for empty name and descriptorType adn valid values for fields.
465
badField="inValidDescriptor";
466             results = false;
467         }
468
469         else if (! ((String JavaDoc)inDesc.getFieldValue("name")).equalsIgnoreCase(this.getName()))
470         {
471             badField="name";
472             results = false;
473         } else
474         {
475
476             if (! ((String JavaDoc)inDesc.getFieldValue("descriptorType")).equalsIgnoreCase("attribute"))
477             {
478                 badField="desriptorType";
479                 results = false;
480             } else if ((inDesc.getFieldValue("displayName")) == null)
481             {
482                 inDesc.setField("displayName",this.getName());
483             }
484         }
485         if (tracing()) trace("isValid()",("Returning " + results + ": Invalid field is " + badField));
486         return results;
487     }
488
489     // SUN Trace and debug functions
490
private boolean tracing()
491     {
492         // return false;
493
return Trace.isSelected(Trace.LEVEL_TRACE, Trace.INFO_MODELMBEAN);
494     }
495
496     private void trace(String JavaDoc inClass, String JavaDoc inMethod, String JavaDoc inText)
497     {
498         // System.out.println("TRACE: " + inClass + ":" + inMethod + ": " + inText);
499
Trace.send(Trace.LEVEL_TRACE, Trace.INFO_MODELMBEAN, inClass,
500                    inMethod, Integer.toHexString(this.hashCode()) + " " + inText);
501     }
502
503     private void trace(String JavaDoc inMethod, String JavaDoc inText)
504     {
505         trace(currClass, inMethod, inText);
506     }
507
508     /**
509      * Deserializes a {@link ModelMBeanAttributeInfo} from an {@link ObjectInputStream}.
510      */

511     private void readObject(ObjectInputStream JavaDoc in)
512         throws IOException JavaDoc, ClassNotFoundException JavaDoc {
513       // New serial form ignores extra field "currClass"
514
in.defaultReadObject();
515     }
516
517
518     /**
519      * Serializes a {@link ModelMBeanAttributeInfo} to an {@link ObjectOutputStream}.
520      */

521     private void writeObject(ObjectOutputStream JavaDoc out)
522         throws IOException JavaDoc {
523       if (compat)
524       {
525         // Serializes this instance in the old serial form
526
//
527
ObjectOutputStream.PutField JavaDoc fields = out.putFields();
528     fields.put("attrDescriptor", attrDescriptor);
529     fields.put("currClass", currClass);
530     out.writeFields();
531       }
532       else
533       {
534         // Serializes this instance in the new serial form
535
//
536
out.defaultWriteObject();
537       }
538     }
539
540 }
541
Popular Tags