KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * @(#)file ModelMBeanConstructorInfo.java
3  * @(#)author IBM Corp.
4  * @(#)version 1.33
5  * @(#)lastedit 03/12/19
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 import java.lang.reflect.*;
40
41 import com.sun.jmx.mbeanserver.GetPropertyAction;
42 import com.sun.jmx.trace.Trace;
43
44
45 /**
46  * The ModelMBeanConstructorInfo object describes a constructor of the ModelMBean.
47  * It is a subclass of MBeanConstructorInfo with the addition of an associated Descriptor
48  * and an implementation of the DescriptorAccess interface.
49  * <P>
50  * <PRE>
51  * The fields in the descriptor are defined, but not limited to, the following: <P>
52  * name : constructor name
53  * descriptorType : must be "operation"
54  * role : must be "constructor"
55  * displayName : human readable name of constructor
56  * visibility : 1-4 where 1: always visible 4: rarely visible
57  * presentationString : xml formatted string to describe how to present operation
58  *</PRE>
59  * The persistPolicy and currencyTimeLimit fields are not valid for the constructor.
60  * The default descriptor will have the name, descriptorType, displayName and role fields.
61  *
62  * @since 1.5
63  */

64
65 public class ModelMBeanConstructorInfo extends MBeanConstructorInfo
66      implements DescriptorAccess JavaDoc, Cloneable JavaDoc
67 {
68
69     // Serialization compatibility stuff:
70
// Two serial forms are supported in this class. The selected form depends
71
// on system property "jmx.serial.form":
72
// - "1.0" for JMX 1.0
73
// - any other value for JMX 1.1 and higher
74
//
75
// Serial version for old serial form
76
private static final long oldSerialVersionUID = -4440125391095574518L;
77     //
78
// Serial version for new serial form
79
private static final long newSerialVersionUID = 3862947819818064362L;
80     //
81
// Serializable fields in old serial form
82
private static final ObjectStreamField JavaDoc[] oldSerialPersistentFields =
83     {
84       new ObjectStreamField JavaDoc("consDescriptor", Descriptor JavaDoc.class),
85       new ObjectStreamField JavaDoc("currClass", String JavaDoc.class)
86     };
87     //
88
// Serializable fields in new serial form
89
private static final ObjectStreamField JavaDoc[] newSerialPersistentFields =
90     {
91       new ObjectStreamField JavaDoc("consDescriptor", Descriptor JavaDoc.class)
92     };
93     //
94
// Actual serial version and serial form
95
private static final long serialVersionUID;
96     /**
97      * @serialField consDescriptor Descriptor The {@link Descriptor} containing the metadata for this instance
98      */

99     private static final ObjectStreamField JavaDoc[] serialPersistentFields;
100     private static boolean compat = false;
101     static {
102     try {
103         PrivilegedAction JavaDoc act = new GetPropertyAction("jmx.serial.form");
104         String JavaDoc form = (String JavaDoc) AccessController.doPrivileged(act);
105         compat = (form != null && form.equals("1.0"));
106     } catch (Exception JavaDoc e) {
107         // OK: No compat with 1.0
108
}
109     if (compat) {
110         serialPersistentFields = oldSerialPersistentFields;
111         serialVersionUID = oldSerialVersionUID;
112     } else {
113         serialPersistentFields = newSerialPersistentFields;
114         serialVersionUID = newSerialVersionUID;
115     }
116     }
117     //
118
// END Serialization compatibility stuff
119

120         /**
121          * @serial The {@link Descriptor} containing the metadata for this instance
122          */

123     private Descriptor JavaDoc consDescriptor = createDefaultDescriptor();
124
125     private final static String JavaDoc currClass = "ModelMBeanConstructorInfo";
126
127
128     /**
129     * Constructs a MBeanConstructorInfo object with a default descriptor.
130     *
131     * @param description A human readable description of the constructor.
132     * @param constructorMethod The java.lang.reflect.Method object
133     * describing the MBean constructor.
134     */

135     public ModelMBeanConstructorInfo(String JavaDoc description,
136                      Constructor constructorMethod)
137     {
138         super(description, constructorMethod);
139
140         if (tracing())
141         {
142             trace("ModelMBeanConstructorInfo(String, Method)","Executed");
143         }
144         consDescriptor = createDefaultDescriptor();
145
146         // put getter and setter methods in constructors list
147
// create default descriptor
148

149     }
150
151     /**
152     * Constructs a MBeanConstructorInfo object.
153     *
154     * @param description A human readable description of the constructor.
155     * @param constructorMethod The java.lang.reflect.Method object
156     * describing the ModelMBean constructor.
157     * @param descriptor An instance of Descriptor containing the
158     * appropriate metadata for this instance of the
159     * ModelMBeanConstructorInfo. If it is null, then a default
160     * descriptor will be created.If the descriptor does not
161     * contain the field "displayName" this fields is added in the
162     * descriptor with its default value.
163     *
164     * @exception RuntimeOperationsException Wraps an
165     * IllegalArgumentException. The descriptor is invalid, or
166     * descriptor field "name" is not equal to name parameter, or
167     * descriptor field "DescriptorType" is not equal to
168     * "operation" or descriptor field "role" is not equal to
169     * "constructor".
170     */

171     
172     public ModelMBeanConstructorInfo(String JavaDoc description,
173                      Constructor constructorMethod,
174                      Descriptor JavaDoc descriptor)
175     {
176
177         super(description, constructorMethod);
178         // put getter and setter methods in constructors list
179
if (tracing())
180         {
181             trace("ModelMBeanConstructorInfo(String, Method, Descriptor)","Executed");
182         }
183         if (descriptor == null)
184         {
185             if (tracing())
186             {
187                 trace("ModelMBeanConstructorInfo(String, Method, Descriptor)","Descriptor passed in is null, setting descriptor to default values");
188             }
189
190             consDescriptor = createDefaultDescriptor();
191         } else
192         {
193             if (isValid(descriptor))
194             {
195                 consDescriptor = (Descriptor JavaDoc) descriptor.clone();
196             } else
197             { // exception
198
consDescriptor = createDefaultDescriptor();
199                 throw new RuntimeOperationsException(new IllegalArgumentException JavaDoc("Invalid descriptor passed in parameter"), ("Exception occured in ModelMBeanConstructorInfo constructor"));
200
201             }
202
203         }
204     }
205     /**
206     * Constructs a ModelMBeanConstructorInfo object with a default descriptor.
207     *
208     * @param name The name of the constructor.
209     * @param description A human readable description of the constructor.
210     * @param signature MBeanParameterInfo object array describing the parameters(arguments) of the constructor.
211     */

212
213     public ModelMBeanConstructorInfo(String JavaDoc name,
214                      String JavaDoc description,
215                      MBeanParameterInfo[] signature)
216     {
217
218         super(name, description, signature);
219         // create default descriptor
220
if (tracing())
221         {
222             trace("ModelMBeanConstructorInfo(String, String, MBeanParameterInfo[])","Executed");
223         }
224         consDescriptor = createDefaultDescriptor();
225     }
226     /**
227     * Constructs a MBeanConstructorInfo object.
228     *
229     * @param name The name of the constructor.
230     * @param description A human readable description of the constructor.
231     * @param signature MBeanParameterInfo objects describing the parameters(arguments) of the constructor.
232     * @param descriptor An instance of Descriptor containing the appropriate metadata
233     * for this instance of the MBeanConstructorInfo. If it is null then a default descriptor will be created.
234     * If the descriptor does not contain the field "displayName" this field is added in the descriptor with its default value.
235     *
236     * @exception RuntimeOperationsException Wraps an IllegalArgumentException. The descriptor is invalid, or descriptor field "name"
237     * is not equal to name parameter, or descriptor field "DescriptorType" is not equal to "operation" or descriptor field "role"
238     * is not equal to "constructor".
239     */

240
241     public ModelMBeanConstructorInfo(String JavaDoc name,
242                      String JavaDoc description,
243                      MBeanParameterInfo[] signature,
244                      Descriptor JavaDoc descriptor)
245     {
246         super(name, description, signature);
247         if (tracing())
248         {
249             trace("ModelMBeanConstructorInfo(String, String, MBeanParameterInfo[], Descriptor)","Executed");
250         }
251         if (descriptor == null)
252         {
253             if (tracing())
254             {
255                 trace("ModelMBeanConstructorInfo(String, Method, Descriptor)","Descriptor passed in is null, setting descriptor to default values");
256             }
257             consDescriptor = createDefaultDescriptor();
258         } else
259         {
260             if (isValid(descriptor))
261             {
262                 consDescriptor = (Descriptor JavaDoc) descriptor.clone();
263             } else
264             { // exception
265
consDescriptor = createDefaultDescriptor();
266                 throw new RuntimeOperationsException(new IllegalArgumentException JavaDoc("Invalid descriptor passed in parameter"), ("Exception occured in ModelMBeanConstructorInfo constructor"));
267
268             }
269         }
270     }
271
272     /**
273      * Constructs a new ModelMBeanConstructorInfo object from this ModelMBeanConstructor Object.
274      *
275      * @param old the ModelMBeanConstructorInfo to be duplicated
276      *
277      */

278     ModelMBeanConstructorInfo(ModelMBeanConstructorInfo JavaDoc old)
279     {
280         super(old.getName(), old.getDescription(), old.getSignature());
281         if (tracing())
282         {
283             trace("ModelMBeanConstructorInfo(ModelMBeanConstructorInfo)","Executed");
284         }
285         if (old.consDescriptor == null)
286         {
287             if (tracing())
288             {
289                 trace("ModelMBeanConstructorInfo(String, Method, Descriptor)","Existing descriptor passed in is null, setting new descriptor to default values");
290             }
291             consDescriptor = createDefaultDescriptor();
292         } else
293         {
294             if (isValid(consDescriptor))
295             {
296                 consDescriptor = (Descriptor JavaDoc) old.consDescriptor.clone();
297             } else
298             { // exception
299
consDescriptor = createDefaultDescriptor();
300                 throw new RuntimeOperationsException(new IllegalArgumentException JavaDoc("Invalid descriptor passed in parameter"), ("Exception occured in ModelMBeanConstructorInfo constructor"));
301
302             }
303
304         }
305     }
306
307     /**
308     * Creates and returns a new ModelMBeanConstructorInfo which is a duplicate of this ModelMBeanConstructorInfo.
309     *
310     */

311     public Object JavaDoc clone ()
312     {
313         if (tracing())
314         {
315             trace("ModelMBeanConstructorInfo.clone()","Executed");
316         }
317         return(new ModelMBeanConstructorInfo JavaDoc(this)) ;
318     }
319
320     /**
321      * Returns a copy of the associated Descriptor.
322      *
323      * @return Descriptor associated with the
324      * ModelMBeanConstructorInfo object.
325      *
326      * @see #setDescriptor
327      */

328
329
330     public Descriptor JavaDoc getDescriptor()
331     {
332         if (tracing())
333         {
334             trace("ModelMBeanConstructorInfo.getDescriptor()","Executed");
335         }
336         if (consDescriptor == null)
337         {
338             consDescriptor = createDefaultDescriptor();
339         }
340         return((Descriptor JavaDoc)consDescriptor.clone());
341     }
342     /**
343     * Sets associated Descriptor (full replace) of
344     * ModelMBeanConstructorInfo. If the new Descriptor is null,
345     * then the associated Descriptor reverts to a default
346     * descriptor. The Descriptor is validated before it is
347     * assigned. If the new Descriptor is invalid, then a
348     * RuntimeOperationsException wrapping an
349     * IllegalArgumentException is thrown.
350     *
351     * @param inDescriptor replaces the Descriptor associated with
352     * the ModelMBeanConstructor. If the descriptor does not
353     * contain the field "displayName" this field is added in the
354     * descriptor with its default value.
355     *
356     * @exception RuntimeOperationsException Wraps an
357     * IllegalArgumentException. The descriptor is invalid, or
358     * descriptor field "name" is not equal to name parameter, or
359     * descriptor field "DescriptorType" is not equal to
360     * "operation" or descriptor field "role" is not equal to
361     * "constructor".
362     *
363     * @see #getDescriptor
364     */

365     public void setDescriptor(Descriptor JavaDoc inDescriptor)
366     {
367         if (tracing())
368         {
369             trace("ModelMBeanConstructorInfo.setDescriptor()","Executed");
370         }
371         if (inDescriptor == null)
372         {
373             if (tracing())
374             {
375                 trace("ModelMBeanConstructorInfo(String, Method, Descriptor)","Descriptor passed in is null, setting descriptor to default values");
376             }
377             consDescriptor = createDefaultDescriptor();
378         } else
379         {
380             if (isValid(inDescriptor))
381             {
382                 consDescriptor = (Descriptor JavaDoc) inDescriptor.clone();
383             } else
384             {
385                 throw new RuntimeOperationsException(new IllegalArgumentException JavaDoc("Invalid descriptor passed in parameter"), ("Exception occured in ModelMBeanConstructorInfo setDescriptor"));
386             }
387         }
388     }
389
390     /**
391     * Returns a string containing the entire contents of the ModelMBeanConstructorInfo in human readable form.
392     */

393     public String JavaDoc toString()
394     {
395         if (tracing())
396         {
397             trace("ModelMBeanConstructorInfo.toString()","Executed");
398         }
399         String JavaDoc retStr =
400             "ModelMBeanConstructorInfo: " + this.getName() +
401             " ; Description: " + this.getDescription() +
402             " ; Descriptor: " + this.getDescriptor() +
403             " ; Signature: ";
404         MBeanParameterInfo[] pTypes = this.getSignature();
405         for (int i=0; i < pTypes.length; i++)
406         {
407             retStr = retStr.concat((pTypes[i]).getType() + ", ");
408         }
409         return retStr;
410     }
411     /**
412     * Creates default descriptor for constructor as follows:
413     * descriptorType=operation,role=constructor,
414     * name=this.getName(),displayname=this.getName(),visibility=1
415     */

416     private Descriptor JavaDoc createDefaultDescriptor()
417     {
418         if (tracing())
419         {
420             trace("ModelMBeanConstructorInfo.createDefaultDescriptor()","Executed");
421         }
422         return new DescriptorSupport JavaDoc(new String JavaDoc[] {"descriptorType=operation",
423                                    "role=constructor",
424                                    ("name=" + this.getName()),
425                                    ("displayname=" + this.getName())});
426     }
427
428     /**
429     * Tests that the descriptor is valid and adds appropriate default fields not already
430     * specified. Field values must be correct for field names.
431     * Descriptor must have the same name as the operation,the descriptorType field must
432     * be "operation", the role field must be set to "constructor".
433     * The following fields will be defaulted if they are not already set:
434     * displayName=this.getName()
435     */

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

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

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