KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * @(#)file ModelMBeanOperationInfo.java
3  * @(#)author IBM Corp.
4  * @(#)version 1.35
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
28 package javax.management.modelmbean;
29
30 import java.io.IOException JavaDoc;
31 import java.io.ObjectInputStream JavaDoc;
32 import java.io.ObjectOutputStream JavaDoc;
33 import java.io.ObjectStreamField JavaDoc;
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 import java.lang.reflect.*;
41
42 import com.sun.jmx.mbeanserver.GetPropertyAction;
43 import com.sun.jmx.trace.Trace;
44
45
46 /**
47  * The ModelMBeanOperationInfo object describes a management operation of the ModelMBean.
48  * It is a subclass of MBeanOperationInfo with the addition of an associated Descriptor
49  * and an implementation of the DescriptorAccess interface.
50  * <P>
51  * <PRE>
52  * The fields in the descriptor are defined, but not limited to, the following:
53  * name : operation name
54  * descriptorType : must be "operation"
55  * class : class where method is defined (fully qualified)
56  * role : must be "operation", "getter", or "setter
57  * targetObject : object on which to execute this method
58  * targetType : type of object reference for targetObject. Can be: ObjectReference | Handle | EJBHandle | IOR | RMIReference.
59  * value : cached value for operation
60  * currencyTimeLimit : how long cached value is valid
61  * lastUpdatedTimeStamp : when cached value was set
62  * visibility : 1-4 where 1: always visible 4: rarely visible
63  * presentationString : xml formatted string to describe how to present operation
64  * </PRE>
65  * The default descriptor will have name, descriptorType, displayName and role fields set.
66  *
67  * <p><b>Note:</b> because of inconsistencies in previous versions of
68  * this specification, it is recommended not to use negative or zero
69  * values for <code>currencyTimeLimit</code>. To indicate that a
70  * cached value is never valid, omit the
71  * <code>currencyTimeLimit</code> field. To indicate that it is
72  * always valid, use a very large number for this field.</p>
73  *
74  * @since 1.5
75  */

76
77 public class ModelMBeanOperationInfo extends MBeanOperationInfo
78      implements DescriptorAccess JavaDoc
79 {
80
81     // Serialization compatibility stuff:
82
// Two serial forms are supported in this class. The selected form depends
83
// on system property "jmx.serial.form":
84
// - "1.0" for JMX 1.0
85
// - any other value for JMX 1.1 and higher
86
//
87
// Serial version for old serial form
88
private static final long oldSerialVersionUID = 9087646304346171239L;
89     //
90
// Serial version for new serial form
91
private static final long newSerialVersionUID = 6532732096650090465L;
92     //
93
// Serializable fields in old serial form
94
private static final ObjectStreamField JavaDoc[] oldSerialPersistentFields =
95     {
96       new ObjectStreamField JavaDoc("operationDescriptor", Descriptor JavaDoc.class),
97       new ObjectStreamField JavaDoc("currClass", String JavaDoc.class)
98     };
99     //
100
// Serializable fields in new serial form
101
private static final ObjectStreamField JavaDoc[] newSerialPersistentFields =
102     {
103       new ObjectStreamField JavaDoc("operationDescriptor", Descriptor JavaDoc.class)
104     };
105     //
106
// Actual serial version and serial form
107
private static final long serialVersionUID;
108     /**
109      * @serialField operationDescriptor Descriptor The descriptor 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 this instance
134          */

135     private Descriptor JavaDoc operationDescriptor = createDefaultDescriptor();
136
137     private static final String JavaDoc currClass = "ModelMBeanOperationInfo";
138
139     /**
140      * Constructs a ModelMBeanOperationInfo object with a default
141      * descriptor.
142      *
143      * @param operationMethod The java.lang.reflect.Method object
144      * describing the MBean operation.
145      * @param description A human readable description of the operation.
146      */

147
148     public ModelMBeanOperationInfo(String JavaDoc description,
149                        Method operationMethod)
150     {
151         super(description, operationMethod);
152         // create default descriptor
153
if (tracing())
154         {
155             trace("ModelMBeanOperationInfo(String,Method)","Executed");
156         }
157         operationDescriptor = createDefaultDescriptor();
158
159
160     }
161
162     /**
163      * Constructs a ModelMBeanOperationInfo object.
164      *
165      * @param operationMethod The java.lang.reflect.Method object
166      * describing the MBean operation.
167      * @param description A human readable description of the
168      * operation.
169      * @param descriptor An instance of Descriptor containing the
170      * appropriate metadata for this instance of the
171      * ModelMBeanOperationInfo. If it is null a default
172      * descriptor will be created. If the descriptor does not
173      * contain the fields "displayName" or "role" these fields are
174      * added in the descriptor with their default values.
175      *
176      * @exception RuntimeOperationsException Wraps an
177      * IllegalArgumentException. The descriptor is invalid, or
178      * descriptor field "name" is not equal to operation name, or
179      * descriptor field "DescriptorType" is not equal to
180      * "operation", or descriptor field "role" is not equal to
181      * "operation".
182      *
183      */

184
185     public ModelMBeanOperationInfo(String JavaDoc description,
186                        Method operationMethod,
187                        Descriptor JavaDoc descriptor)
188     {
189
190         super(description, operationMethod);
191         if (tracing())
192         {
193             trace("ModelMBeanOperationInfo(String,Method,Descriptor)","Executed");
194         }
195         if (descriptor == null)
196         {
197             if (tracing())
198             {
199                 trace("ModelMBeanOperationInfo()","Received null for new descriptor value, setting descriptor to default values");
200             }
201             operationDescriptor = createDefaultDescriptor();
202         } else
203         {
204             if (isValid(descriptor))
205             {
206                 operationDescriptor = (Descriptor JavaDoc) descriptor.clone();
207             } else
208             {
209                 operationDescriptor = createDefaultDescriptor();
210                 throw new RuntimeOperationsException(new IllegalArgumentException JavaDoc("Invalid descriptor passed in parameter"), ("Exception occured in ModelMBeanOperationInfo constructor"));
211             }
212         }
213     }
214
215     /**
216     * Constructs a ModelMBeanOperationInfo object with a default descriptor.
217     *
218     * @param name The name of the method.
219     * @param description A human readable description of the operation.
220     * @param signature MBeanParameterInfo objects describing the parameters(arguments) of the method.
221     * @param type The type of the method's return value.
222     * @param impact The impact of the method, one of INFO, ACTION, ACTION_INFO, UNKNOWN.
223     */

224
225     public ModelMBeanOperationInfo(String JavaDoc name,
226                        String JavaDoc description,
227                        MBeanParameterInfo[] signature,
228                        String JavaDoc type,
229                        int impact)
230     {
231
232         super(name, description, signature, type, impact);
233         // create default descriptor
234
if (tracing())
235         {
236             trace("ModelMBeanOperationInfo(String,String,MBeanParameterInfo[],String,int)","Executed");
237         }
238         operationDescriptor = createDefaultDescriptor();
239
240     }
241
242     /**
243     * Constructs a ModelMBeanOperationInfo object.
244     *
245     * @param name The name of the method.
246     * @param description A human readable description of the operation.
247     * @param signature MBeanParameterInfo objects describing the parameters(arguments) of the method.
248     * @param type The type of the method's return value.
249     * @param impact The impact of the method, one of INFO, ACTION, ACTION_INFO, UNKNOWN.
250     * @param descriptor An instance of Descriptor containing the appropriate metadata.
251     * for this instance of the MBeanOperationInfo.If it is null then a default descriptor will be created.
252     * If the descriptor does not contain the fields
253     * "displayName" or "role" these fields are added in the descriptor with their default values.
254     *
255     * @exception RuntimeOperationsException Wraps an IllegalArgumentException. The descriptor is invalid, or descriptor field "name"
256     * is not equal to operation name, or descriptor field "DescriptorType" is not equal to "operation", or descriptor field "role" is not equal to
257     * "operation".
258     */

259
260     public ModelMBeanOperationInfo(String JavaDoc name,
261                        String JavaDoc description,
262                        MBeanParameterInfo[] signature,
263                        String JavaDoc type,
264                        int impact,
265                        Descriptor JavaDoc descriptor)
266     {
267         super(name, description, signature, type, impact);
268         if (tracing())
269         {
270             trace("ModelMBeanOperationInfo(String,String,MBeanParameterInfo[],String,int,Descriptor)","Executed");
271         }
272         if (descriptor == null)
273         {
274             if (tracing())
275             {
276                 trace("ModelMBeanOperationInfo()","Received null for new descriptor value, setting descriptor to default values");
277             }
278             operationDescriptor = createDefaultDescriptor();
279         } else
280         {
281             if (isValid(descriptor))
282             {
283                 operationDescriptor = (Descriptor JavaDoc) descriptor.clone();
284             } else
285             {
286                 operationDescriptor = createDefaultDescriptor();
287                 throw new RuntimeOperationsException(new IllegalArgumentException JavaDoc("Invalid descriptor passed in parameter"), ("Exception occured in ModelMBeanOperationInfo constructor"));
288             }
289
290         }
291     }
292
293     /**
294      * Constructs a new ModelMBeanOperationInfo object from this ModelMBeanOperation Object.
295      *
296      * @param inInfo the ModelMBeanOperationInfo to be duplicated
297      *
298      */

299
300     public ModelMBeanOperationInfo(ModelMBeanOperationInfo JavaDoc inInfo)
301     {
302         super(inInfo.getName(),
303               inInfo.getDescription(),
304               inInfo.getSignature(),
305               inInfo.getReturnType(),
306               inInfo.getImpact());
307         if (tracing())
308         {
309             trace("ModelMBeanOperationInfo(ModelMBeanOperationInfo)","Executed");
310         }
311         Descriptor JavaDoc newDesc = inInfo.getDescriptor();
312         if (newDesc == null)
313         {
314             operationDescriptor = createDefaultDescriptor();
315         } else
316         {
317             if (isValid(newDesc))
318             {
319                 operationDescriptor = (Descriptor JavaDoc) newDesc.clone();
320             } else
321             {
322                 operationDescriptor = createDefaultDescriptor();
323                 throw new RuntimeOperationsException(new IllegalArgumentException JavaDoc("Invalid descriptor passed in parameter"), ("Exception occured in ModelMBeanOperationInfo constructor"));
324             }
325
326         }
327     }
328
329     /**
330     * Creates and returns a new ModelMBeanOperationInfo which is a duplicate of this ModelMBeanOperationInfo.
331     *
332     */

333
334     public Object JavaDoc clone ()
335     {
336         if (tracing())
337         {
338             trace("ModelMBeanOperationInfo.clone()","Executed");
339         }
340         return(new ModelMBeanOperationInfo JavaDoc(this)) ;
341     }
342
343     /**
344      * Returns a copy of the associated Descriptor of the
345      * ModelMBeanOperationInfo.
346      *
347      * @return Descriptor associated with the
348      * ModelMBeanOperationInfo object.
349      *
350      * @see #setDescriptor
351      */

352
353     public Descriptor JavaDoc getDescriptor()
354     {
355         if (tracing())
356         {
357             trace("ModelMBeanOperationInfo.getDescriptor()","Executed");
358         }
359         if (operationDescriptor == null)
360         {
361             operationDescriptor = createDefaultDescriptor();
362         }
363
364         return((Descriptor JavaDoc) operationDescriptor.clone());
365     }
366
367     /**
368      * Sets associated Descriptor (full replace) for the
369      * ModelMBeanOperationInfo If the new Descriptor is null, then
370      * the associated Descriptor reverts to a default descriptor.
371      * The Descriptor is validated before it is assigned. If the
372      * new Descriptor is invalid, then a
373      * RuntimeOperationsException wrapping an
374      * IllegalArgumentException is thrown.
375      *
376      * @param inDescriptor replaces the Descriptor associated with the
377      * ModelMBeanOperation.
378      *
379      * @exception RuntimeOperationsException Wraps an
380      * IllegalArgumentException for invalid Descriptor.
381      *
382      * @see #getDescriptor
383      */

384     public void setDescriptor(Descriptor JavaDoc inDescriptor)
385     {
386         if (tracing())
387         {
388             trace("ModelMBeanOperationInfo.setDescriptor(Descriptor)","Executed");
389         }
390         if (inDescriptor == null)
391         {
392             if (tracing())
393             {
394                 trace("ModelMBeanOperationInfo.setDescriptor()","Received null for new descriptor value, setting descriptor to default values");
395             }
396             operationDescriptor = createDefaultDescriptor();
397         } else
398         {
399             if (isValid(inDescriptor))
400             {
401                 operationDescriptor = (Descriptor JavaDoc) inDescriptor.clone();
402             } else
403             {
404                 throw new RuntimeOperationsException(new IllegalArgumentException JavaDoc("Invalid descriptor passed in parameter"), ("Exception occured in ModelMBeanOperationInfo setDescriptor"));
405             }
406         }
407     }
408
409     /**
410     * Returns a string containing the entire contents of the ModelMBeanOperationInfo in human readable form.
411     */

412     public String JavaDoc toString()
413     {
414         if (tracing())
415         {
416             trace("ModelMBeanConstructorInfo.toString()","Executed");
417         }
418         String JavaDoc retStr =
419             "ModelMBeanOperationInfo: " + this.getName() +
420             " ; Description: " + this.getDescription() +
421             " ; Descriptor: " + this.getDescriptor() +
422             " ; ReturnType: " + this.getReturnType() +
423             " ; Signature: ";
424         MBeanParameterInfo[] pTypes = this.getSignature();
425         for (int i=0; i < pTypes.length; i++)
426         {
427             retStr = retStr.concat((pTypes[i]).getType() + ", ");
428         }
429         return retStr;
430     }
431     /**
432     * Creates default descriptor for operation as follows:
433     * descriptorType=operation,role=operation, name=this.getName(),displayname=this.getName().
434     */

435     private Descriptor JavaDoc createDefaultDescriptor()
436     {
437         if (tracing())
438         {
439             trace("ModelMBeanOperationInfo.createDefaultDescriptor()","Executed");
440         }
441         return new DescriptorSupport JavaDoc(new String JavaDoc[] {"descriptorType=operation",
442                                    ("name=" + this.getName()),
443                                    "role=operation",
444                                    ("displayname=" + this.getName())});
445     }
446
447
448     /**
449     * Tests that the descriptor is valid and adds appropriate
450     * default fields not already specified. Field values must be
451     * correct for field names. descriptorType field must be
452     * "operation". We do not check the targetType because a
453     * custom implementation of ModelMBean could recognize
454     * additional types beyond the "standard" ones. The following
455     * fields will be defaulted if they are not already set:
456     * role=operation,displayName=this.getName()
457     */

458     private boolean isValid(Descriptor JavaDoc inDesc)
459     {
460         boolean results = true;
461         String JavaDoc badField = "none";
462         // if name != this.getName
463
// if (descriptorType != operation)
464
// look for displayName, persistPolicy, visibility and add in
465
if (inDesc == null)
466         {
467             results = false;
468         }
469
470         else if (!inDesc.isValid())
471         { // checks for empty descriptors, null,
472
// checks for empty name and descriptorType
473
// and valid values for fields.
474
results = false;
475         } else
476         {
477             if (! ((String JavaDoc)inDesc.getFieldValue("name")).equalsIgnoreCase(this.getName()))
478             {
479                 results = false;
480             }
481             if (! ((String JavaDoc)inDesc.getFieldValue("descriptorType")).equalsIgnoreCase("operation"))
482             {
483                 results = false;
484             }
485             Object JavaDoc roleValue = inDesc.getFieldValue("role");
486             if (roleValue == null)
487             {
488                 inDesc.setField("role","operation");
489             } else {
490                 final String JavaDoc role = (String JavaDoc)roleValue;
491                 if (!(role.equalsIgnoreCase("operation")
492                   || role.equalsIgnoreCase("setter")
493                   || role.equalsIgnoreCase("getter"))) {
494                 results = false;
495                 badField="role";
496                 }
497             }
498
499             Object JavaDoc targetValue = inDesc.getFieldValue("targetType");
500             if (targetValue != null) {
501                 if (!(targetValue instanceof java.lang.String JavaDoc)) {
502                 results = false;
503                 badField="targetType";
504                 
505                 }
506             }
507             if ((inDesc.getFieldValue("displayName")) == null)
508                 {
509                 inDesc.setField("displayName",this.getName());
510                 }
511         }
512         if (tracing()) trace("isValid()",("Returning " + results + ": Invalid field is " + badField));
513         return results;
514     }
515
516     // SUN Trace and debug functions
517
private boolean tracing()
518     {
519         // return false;
520
return Trace.isSelected(Trace.LEVEL_TRACE, Trace.INFO_MODELMBEAN);
521     }
522
523     private void trace(String JavaDoc inClass, String JavaDoc inMethod, String JavaDoc inText)
524     {
525         // System.out.println("TRACE: " + inClass + ":" + inMethod + ": " + inText);
526
Trace.send(Trace.LEVEL_TRACE, Trace.INFO_MODELMBEAN, inClass,
527                    inMethod, Integer.toHexString(this.hashCode()) + " " + inText);
528     }
529
530     private void trace(String JavaDoc inMethod, String JavaDoc inText)
531     {
532         trace(currClass, inMethod, inText);
533     }
534
535     /**
536      * Deserializes a {@link ModelMBeanOperationInfo} from an {@link ObjectInputStream}.
537      */

538     private void readObject(ObjectInputStream JavaDoc in)
539         throws IOException JavaDoc, ClassNotFoundException JavaDoc {
540       // New serial form ignores extra field "currClass"
541
in.defaultReadObject();
542     }
543
544
545     /**
546      * Serializes a {@link ModelMBeanOperationInfo} to an {@link ObjectOutputStream}.
547      */

548     private void writeObject(ObjectOutputStream JavaDoc out)
549         throws IOException JavaDoc {
550       if (compat)
551       {
552         // Serializes this instance in the old serial form
553
//
554
ObjectOutputStream.PutField JavaDoc fields = out.putFields();
555     fields.put("operationDescriptor", operationDescriptor);
556     fields.put("currClass", currClass);
557     out.writeFields();
558       }
559       else
560       {
561         // Serializes this instance in the new serial form
562
//
563
out.defaultWriteObject();
564       }
565     }
566
567 }
568
Popular Tags