KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * @(#)file ModelMBeanInfoSupport.java
3  * @(#)author IBM Corp.
4  * @(#)version 1.38
5  * @(#)lastedit 04/02/10
6  *
7  * Copyright IBM Corp. 1999-2000. All rights reserved.
8  *
9  * The program is provided "as is" without any warranty express or implied,
10  * including the warranty of non-infringement and the implied warranties of
11  * merchantibility and fitness for a particular purpose. IBM will not be
12  * liable for any damages suffered by you or any third party claim against
13  * you regarding the Program.
14  *
15  * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
16  * This software is the proprietary information of Sun Microsystems, Inc.
17  * Use is subject to license terms.
18  *
19  * Copyright 2004 Sun Microsystems, Inc. Tous droits reserves.
20  * Ce logiciel est propriete de Sun Microsystems, Inc.
21  * Distribue par des licences qui en restreignent l'utilisation.
22  *
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.io.Serializable 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.MBeanAttributeInfo JavaDoc;
40 import javax.management.MBeanConstructorInfo JavaDoc;
41 import javax.management.MBeanException JavaDoc;
42 import javax.management.MBeanInfo JavaDoc;
43 import javax.management.MBeanNotificationInfo JavaDoc;
44 import javax.management.MBeanOperationInfo JavaDoc;
45 import javax.management.RuntimeOperationsException JavaDoc;
46
47 import com.sun.jmx.mbeanserver.GetPropertyAction;
48 import com.sun.jmx.trace.Trace;
49
50 /**
51  * This class represents the meta data for ModelMBeans. Descriptors have been added on the meta data objects.
52  * <P>
53  * Java resources wishing to be manageable instantiate the ModelMBean using the MBeanServer's
54  * createMBean method. The resource then sets the ModelMBeanInfo and Descriptors for the ModelMBean
55  * instance. The attributes and operations exposed via the ModelMBeanInfo for the ModelMBean are accessible
56  * from MBeans, connectors/adaptors like other MBeans. Through the Descriptors, values and methods in
57  * the managed application can be defined and mapped to attributes and operations of the ModelMBean.
58  * This mapping can be defined during development in a file or dynamically and
59  * programmatically at runtime.
60  * <P>
61  * Every ModelMBean which is instantiated in the MBeanServer becomes manageable:
62  * its attributes and operations
63  * become remotely accessible through the connectors/adaptors connected to that MBeanServer.
64  * A Java object cannot be registered in the MBeanServer unless it is a JMX compliant MBean.
65  * By instantiating a ModelMBean, resources are guaranteed that the MBean is valid.
66  *
67  * MBeanException and RuntimeOperationsException must be thrown on every public method. This allows
68  * for wrapping exceptions from distributed communications (RMI, EJB, etc.)
69  *
70  * @since 1.5
71  */

72 public class ModelMBeanInfoSupport extends MBeanInfo JavaDoc
73      implements ModelMBeanInfo JavaDoc, java.io.Serializable JavaDoc
74 {
75
76     // Serialization compatibility stuff:
77
// Two serial forms are supported in this class. The selected form depends
78
// on system property "jmx.serial.form":
79
// - "1.0" for JMX 1.0
80
// - any other value for JMX 1.1 and higher
81
//
82
// Serial version for old serial form
83
private static final long oldSerialVersionUID = -3944083498453227709L;
84     //
85
// Serial version for new serial form
86
private static final long newSerialVersionUID = -1935722590756516193L;
87     //
88
// Serializable fields in old serial form
89
private static final ObjectStreamField JavaDoc[] oldSerialPersistentFields =
90     {
91       new ObjectStreamField JavaDoc("modelMBeanDescriptor", Descriptor JavaDoc.class),
92       new ObjectStreamField JavaDoc("mmbAttributes", MBeanAttributeInfo JavaDoc[].class),
93       new ObjectStreamField JavaDoc("mmbConstructors", MBeanConstructorInfo JavaDoc[].class),
94       new ObjectStreamField JavaDoc("mmbNotifications", MBeanNotificationInfo JavaDoc[].class),
95       new ObjectStreamField JavaDoc("mmbOperations", MBeanOperationInfo 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("modelMBeanDescriptor", Descriptor JavaDoc.class),
103       new ObjectStreamField JavaDoc("modelMBeanAttributes", MBeanAttributeInfo JavaDoc[].class),
104       new ObjectStreamField JavaDoc("modelMBeanConstructors", MBeanConstructorInfo JavaDoc[].class),
105       new ObjectStreamField JavaDoc("modelMBeanNotifications", MBeanNotificationInfo JavaDoc[].class),
106       new ObjectStreamField JavaDoc("modelMBeanOperations", MBeanOperationInfo JavaDoc[].class)
107     };
108     //
109
// Actual serial version and serial form
110
private static final long serialVersionUID;
111     /**
112      * @serialField modelMBeanDescriptor Descriptor The descriptor containing MBean wide policy
113      * @serialField modelMBeanAttributes ModelMBeanAttributeInfo[] The array of {@link ModelMBeanAttributeInfo} objects which
114      * have descriptors
115      * @serialField modelMBeanConstructors MBeanConstructorInfo[] The array of {@link ModelMBeanConstructorInfo} objects which
116      * have descriptors
117      * @serialField modelMBeanNotifications MBeanNotificationInfo[] The array of {@link ModelMBeanNotificationInfo} objects which
118      * have descriptors
119      * @serialField modelMBeanOperations MBeanOperationInfo[] The array of {@link ModelMBeanOperationInfo} objects which
120      * have descriptors
121      */

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

143         /**
144          * @serial The descriptor containing MBean wide policy
145          */

146     private Descriptor JavaDoc modelMBeanDescriptor = null;
147
148     /* The following fields always have the same values as the
149        fields inherited from MBeanInfo and are retained only for
150        compatibility. By rewriting the serialization code we could
151        get rid of them.
152     
153        These fields can't be final because they are assigned to by
154        readObject(). */

155
156         /**
157          * @serial The array of {@link ModelMBeanAttributeInfo} objects which
158          * have descriptors
159          */

160         private MBeanAttributeInfo JavaDoc[] modelMBeanAttributes;
161
162     /**
163          * @serial The array of {@link ModelMBeanConstructorInfo} objects which
164          * have descriptors
165          */

166         private MBeanConstructorInfo JavaDoc[] modelMBeanConstructors;
167
168     /**
169          * @serial The array of {@link ModelMBeanNotificationInfo} objects which
170          * have descriptors
171          */

172         private MBeanNotificationInfo JavaDoc[] modelMBeanNotifications;
173
174     /**
175          * @serial The array of {@link ModelMBeanOperationInfo} objects which
176          * have descriptors
177          */

178         private MBeanOperationInfo JavaDoc[] modelMBeanOperations;
179
180     private static final String JavaDoc ATTR = "attribute";
181     private static final String JavaDoc OPER = "operation";
182     private static final String JavaDoc NOTF = "notification";
183     private static final String JavaDoc CONS = "constructor";
184     private static final String JavaDoc MMB = "mbean";
185     private static final String JavaDoc ALL = "all";
186     private static final String JavaDoc currClass = "ModelMBeanInfoSupport";
187
188     /**
189      * Constructs a ModelMBeanInfoSupport which is a duplicate of the one
190      * passed in.
191      *
192      * @param mbi the ModelMBeanInfo instance from which the ModelMBeanInfo
193      * being created is initialized.
194      */

195     public ModelMBeanInfoSupport(ModelMBeanInfo JavaDoc mbi)
196     {
197     super(mbi.getClassName(),
198           mbi.getDescription(),
199           mbi.getAttributes(),
200           mbi.getConstructors(),
201           mbi.getOperations(),
202           mbi.getNotifications());
203           
204     modelMBeanAttributes = mbi.getAttributes();
205     modelMBeanConstructors = mbi.getConstructors();
206     modelMBeanOperations = mbi.getOperations();
207     modelMBeanNotifications = mbi.getNotifications();
208
209     try
210         {
211         Descriptor JavaDoc mbeandescriptor = mbi.getMBeanDescriptor();
212
213         if ((mbeandescriptor != null) && isValidDescriptor(mbeandescriptor))
214             {
215             if (tracing()) trace("ModelMBeanInfo(ModelMBeanInfo)","ModelMBeanDescriptor is valid, cloning Descriptor *" + mbeandescriptor.toString() + "*");
216             modelMBeanDescriptor = (Descriptor JavaDoc) mbeandescriptor.clone();
217             addDefaultFields();
218             } else
219             {
220                 if (tracing()) trace("ModelMBeanInfo(ModelMBeanInfo)",
221                          "ModelMBeanDescriptor in ModelMBeanInfo is null or invalid, setting to default value");
222                 modelMBeanDescriptor = createDefaultDescriptor();
223             }
224         } catch (MBeanException JavaDoc mbe)
225         {
226             modelMBeanDescriptor = createDefaultDescriptor();
227             if (tracing()) trace("ModelMBeanInfo(ModelMBeanInfo)","Could not get modelMBeanDescriptor, setting to default value");
228         }
229
230     if (tracing())
231         {
232         trace("ModelMBeanInfo(ModelMBeanInfo)","Executed");
233         }
234     }
235
236     /**
237      * Creates a ModelMBeanInfoSupport with the provided information,
238      * but the descriptor is a default.
239      * The default descriptor is: name=mbeanName, descriptorType=mbean,
240      * displayName=ClassName, persistPolicy=never, log=F, visibility=1
241      *
242      * @param className classname of the MBean
243      * @param description human readable description of the
244      * ModelMBean
245      * @param attributes array of ModelMBeanAttributeInfo objects
246      * which have descriptors
247      * @param constructors array of ModelMBeanConstructorInfo
248      * objects which have descriptors
249      * @param operations array of ModelMBeanOperationInfo objects
250      * which have descriptors
251      * @param notifications array of ModelMBeanNotificationInfo
252      * objects which have descriptors
253      */

254     public ModelMBeanInfoSupport(String JavaDoc className,
255                  String JavaDoc description,
256                  ModelMBeanAttributeInfo JavaDoc[] attributes,
257                  ModelMBeanConstructorInfo JavaDoc[] constructors,
258                  ModelMBeanOperationInfo JavaDoc[] operations,
259                  ModelMBeanNotificationInfo JavaDoc[] notifications) {
260         this(className, description, attributes, constructors,
261          operations, notifications, null);
262     }
263
264     /**
265      * Creates a ModelMBeanInfoSupport with the provided information
266      * and the descriptor given in parameter.
267      *
268      * @param className classname of the MBean
269      * @param description human readable description of the
270      * ModelMBean
271      * @param attributes array of ModelMBeanAttributeInfo objects
272      * which have descriptors
273      * @param constructors array of ModelMBeanConstructorInfo
274      * objects which have descriptor
275      * @param operations array of ModelMBeanOperationInfo objects
276      * which have descriptor
277      * @param notifications array of ModelMBeanNotificationInfo
278      * objects which have descriptor
279      * @param mbeandescriptor descriptor to be used as the
280      * MBeanDescriptor containing MBean wide policy. If the
281      * descriptor is null, a default descriptor will be constructed.
282      * The default descriptor is:
283      * name=className, descriptorType=mbean, displayName=className,
284      * persistPolicy=never, log=F, visibility=1. If the
285      * descriptor does not contain all these fields, they will be
286      * added with these default values.
287      *
288      * @exception RuntimeOperationsException Wraps an
289      * IllegalArgumentException for invalid descriptor passed in
290      * parameter. (see {@link #getMBeanDescriptor
291      * getMBeanDescriptor} for the definition of a valid MBean
292      * descriptor.)
293      */

294
295     public ModelMBeanInfoSupport(String JavaDoc className,
296                  String JavaDoc description,
297                  ModelMBeanAttributeInfo JavaDoc[] attributes,
298                  ModelMBeanConstructorInfo JavaDoc[] constructors,
299                  ModelMBeanOperationInfo JavaDoc[] operations,
300                  ModelMBeanNotificationInfo JavaDoc[] notifications,
301                  Descriptor JavaDoc mbeandescriptor) {
302     super(className,
303           description,
304           (attributes != null) ? attributes : NO_ATTRIBUTES,
305           (constructors != null) ? constructors : NO_CONSTRUCTORS,
306           (operations != null) ? operations : NO_OPERATIONS,
307           (notifications != null) ? notifications : NO_NOTIFICATIONS);
308     /* The values saved here are possibly null, but we
309        check this everywhere they are referenced. If at
310        some stage we replace null with an empty array
311        here, as we do in the superclass constructor
312        parameters, then we must also do this in
313        readObject(). */

314     modelMBeanAttributes = attributes;
315     modelMBeanConstructors = constructors;
316     modelMBeanOperations = operations;
317     modelMBeanNotifications = notifications;
318     if (mbeandescriptor ==null) {
319         if (tracing())
320         trace("ModelMBeanInfo(String,String,ModelMBeanAttributeInfo[],ModelMBeanConstructorInfo[],ModelMBeanOperationInfo[],ModelMBeanNotificationInfo[],Descriptor)",
321               "MBeanDescriptor is null, setting default descriptor");
322
323         modelMBeanDescriptor = createDefaultDescriptor();
324     } else {
325         if (isValidDescriptor(mbeandescriptor)) {
326         modelMBeanDescriptor = (Descriptor JavaDoc) mbeandescriptor.clone();
327         addDefaultFields();
328         } else {
329         throw new RuntimeOperationsException JavaDoc(new IllegalArgumentException JavaDoc("Invalid descriptor passed in parameter"));
330         }
331     }
332     if (tracing())
333         {
334         trace("ModelMBeanInfo(String,String,ModelMBeanAttributeInfo[],ModelMBeanConstructorInfo[],ModelMBeanOperationInfo[],ModelMBeanNotificationInfo[],Descriptor)",
335               "Executed");
336         }
337     }
338
339     private static final ModelMBeanAttributeInfo JavaDoc[] NO_ATTRIBUTES =
340     new ModelMBeanAttributeInfo JavaDoc[0];
341     private static final ModelMBeanConstructorInfo JavaDoc[] NO_CONSTRUCTORS =
342     new ModelMBeanConstructorInfo JavaDoc[0];
343     private static final ModelMBeanNotificationInfo JavaDoc[] NO_NOTIFICATIONS =
344     new ModelMBeanNotificationInfo JavaDoc[0];
345     private static final ModelMBeanOperationInfo JavaDoc[] NO_OPERATIONS =
346     new ModelMBeanOperationInfo JavaDoc[0];
347
348     // Java doc inherited from MOdelMBeanInfo interface
349

350     public Object JavaDoc clone() {
351     return(new ModelMBeanInfoSupport JavaDoc(this));
352     }
353     
354
355     public Descriptor JavaDoc[] getDescriptors(String JavaDoc inDescriptorType)
356     throws MBeanException JavaDoc, RuntimeOperationsException JavaDoc {
357         if (tracing())
358         {
359             trace("ModelMBeanInfoSupport.getDescriptors()","Entry");
360         }
361
362         if ((inDescriptorType == null) || (inDescriptorType == ""))
363         {
364             inDescriptorType = "all";
365         }
366         // if no descriptors of that type, will return empty array
367
Descriptor JavaDoc[] retList;
368
369         if (inDescriptorType.equalsIgnoreCase(MMB))
370         {
371             retList = new Descriptor JavaDoc[] {modelMBeanDescriptor};
372         } else if (inDescriptorType.equalsIgnoreCase(ATTR))
373         {
374             MBeanAttributeInfo JavaDoc[] attrList = modelMBeanAttributes;
375             int numAttrs = 0;
376             if (attrList != null) numAttrs = attrList.length;
377
378             retList = new Descriptor JavaDoc[numAttrs];
379             for (int i=0; i < numAttrs; i++)
380             {
381                 retList[i] = (((ModelMBeanAttributeInfo JavaDoc) attrList[i]).getDescriptor());
382             }
383         } else if (inDescriptorType.equalsIgnoreCase(OPER))
384         {
385             MBeanOperationInfo JavaDoc[] operList = modelMBeanOperations;
386             int numOpers = 0;
387             if (operList != null) numOpers = operList.length;
388
389             retList = new Descriptor JavaDoc[numOpers];
390             for (int i=0; i < numOpers; i++)
391             {
392                 retList[i] = (((ModelMBeanOperationInfo JavaDoc) operList[i]).getDescriptor());
393             }
394         } else if (inDescriptorType.equalsIgnoreCase(CONS))
395         {
396             MBeanConstructorInfo JavaDoc[] consList = modelMBeanConstructors;
397             int numCons = 0;
398             if (consList != null) numCons = consList.length;
399
400             retList = new Descriptor JavaDoc[numCons];
401             for (int i=0; i < numCons; i++)
402             {
403                 retList[i] = (((ModelMBeanConstructorInfo JavaDoc) consList[i]).getDescriptor());
404             }
405         } else if (inDescriptorType.equalsIgnoreCase(NOTF))
406         {
407             MBeanNotificationInfo JavaDoc[] notifList = modelMBeanNotifications;
408             int numNotifs = 0;
409             if (notifList != null) numNotifs = notifList.length;
410
411             retList = new Descriptor JavaDoc[numNotifs];
412             for (int i=0; i < numNotifs; i++)
413             {
414                 retList[i] = (((ModelMBeanNotificationInfo JavaDoc) notifList[i]).getDescriptor());
415             }
416         } else if (inDescriptorType.equalsIgnoreCase(ALL))
417         {
418
419             MBeanAttributeInfo JavaDoc[] attrList = modelMBeanAttributes;
420             int numAttrs = 0;
421             if (attrList != null) numAttrs = attrList.length;
422
423             MBeanOperationInfo JavaDoc[] operList = modelMBeanOperations;
424             int numOpers = 0;
425             if (operList != null) numOpers = operList.length;
426
427             MBeanConstructorInfo JavaDoc[] consList = modelMBeanConstructors;
428             int numCons = 0;
429             if (consList != null) numCons = consList.length;
430
431             MBeanNotificationInfo JavaDoc[] notifList = modelMBeanNotifications;
432             int numNotifs = 0;
433             if (notifList != null) numNotifs = notifList.length;
434
435
436             retList = new Descriptor JavaDoc[numAttrs + numCons + numOpers + numNotifs];
437             int j=0;
438             for (int i=0; i < numAttrs; i++)
439             {
440                 retList[j] = (((ModelMBeanAttributeInfo JavaDoc) attrList[i]).getDescriptor());
441                 j++;
442             }
443             for (int i=0; i < numCons; i++)
444             {
445                 retList[j] = (((ModelMBeanConstructorInfo JavaDoc)consList[i]).getDescriptor());
446                 j++;
447             }
448             for (int i=0; i < numOpers; i++)
449             {
450                 retList[j] = (((ModelMBeanOperationInfo JavaDoc)operList[i]).getDescriptor());
451                 j++;
452             }
453             for (int i=0; i < numNotifs; i++)
454             {
455                 retList[j] = (((ModelMBeanNotificationInfo JavaDoc)notifList[i]).getDescriptor());
456                 j++;
457             }
458         } else
459         {
460             throw new RuntimeOperationsException JavaDoc(new IllegalArgumentException JavaDoc("Descriptor Type is invalid"),
461                                                  ("Exception occurred trying to find the descriptors of the MBean"));
462         }
463         if (tracing())
464         {
465             trace("ModelMBeanInfoSupport.getDescriptors()","Exit");
466         }
467
468         return retList;
469     }
470
471
472     public void setDescriptors(Descriptor JavaDoc[] inDescriptors)
473     throws MBeanException JavaDoc, RuntimeOperationsException JavaDoc {
474         if (tracing())
475         {
476             trace("ModelMBeanInfoSupport.setDescriptors(Descriptor[])","Entry");
477         }
478         if (inDescriptors==null)
479         { // throw RuntimeOperationsException - invalid descriptor
480
throw new RuntimeOperationsException JavaDoc(new IllegalArgumentException JavaDoc("Descriptor list is invalid"),
481                                                  ("Exception occurred trying to set the descriptors of the MBeanInfo"));
482         }
483         if (inDescriptors.length == 0)
484         { // empty list, no-op
485
return;
486         }
487         for (int j=0; j < inDescriptors.length; j++)
488         {
489             setDescriptor(inDescriptors[j],null);
490         }
491         if (tracing())
492         {
493             trace("ModelMBeanInfoSupport.setDescriptors(Descriptor[])","Exit");
494         }
495
496     }
497
498
499     /**
500      * Returns a Descriptor requested by name.
501      *
502      * @param inDescriptorName The name of the descriptor.
503      *
504      * @return Descriptor containing a descriptor for the ModelMBean with the same name.
505      * If no descriptor is found, null is returned.
506      *
507      * @exception MBeanException Wraps a distributed communication Exception.
508      * @exception RuntimeOperationsException Wraps an IllegalArgumentException for null name.
509      *
510      * @see #setDescriptor
511      */

512
513     public Descriptor JavaDoc getDescriptor(String JavaDoc inDescriptorName)
514     throws MBeanException JavaDoc, RuntimeOperationsException JavaDoc {
515         if (tracing())
516         {
517             trace("ModelMBeanInfoSupport.getDescriptor(String)","Entry");
518         }
519         return(getDescriptor(inDescriptorName, null));
520     }
521     
522
523     public Descriptor JavaDoc getDescriptor(String JavaDoc inDescriptorName,
524                     String JavaDoc inDescriptorType)
525         throws MBeanException JavaDoc, RuntimeOperationsException JavaDoc {
526         if (inDescriptorName==null) {
527             // throw RuntimeOperationsException - invalid descriptor
528
throw new RuntimeOperationsException JavaDoc(new IllegalArgumentException JavaDoc("Descriptor is invalid"),
529                              ("Exception occurred trying to set the descriptors of the MBeanInfo"));
530         }
531
532         if (MMB.equalsIgnoreCase(inDescriptorType)) {
533         return (Descriptor JavaDoc) modelMBeanDescriptor.clone();
534         }
535
536         /* The logic here is a bit convoluted, because we are
537            dealing with two possible cases, depending on whether
538            inDescriptorType is null. If it's not null, then only
539            one of the following ifs will run, and it will either
540            return a descriptor or null. If inDescriptorType is
541            null, then all of the following ifs will run until one
542            of them finds a descriptor. */

543         if (ATTR.equalsIgnoreCase(inDescriptorType) || inDescriptorType == null) {
544         ModelMBeanAttributeInfo JavaDoc attr = getAttribute(inDescriptorName);
545         if (attr != null)
546             return attr.getDescriptor();
547         if (inDescriptorType != null)
548             return null;
549         }
550         if (OPER.equalsIgnoreCase(inDescriptorType) || inDescriptorType == null) {
551         ModelMBeanOperationInfo JavaDoc oper = getOperation(inDescriptorName);
552         if (oper != null)
553             return oper.getDescriptor();
554         if (inDescriptorType != null)
555             return null;
556         }
557         if (CONS.equalsIgnoreCase(inDescriptorType) || inDescriptorType == null) {
558         ModelMBeanConstructorInfo JavaDoc oper =
559             getConstructor(inDescriptorName);
560         if (oper != null)
561             return oper.getDescriptor();
562         if (inDescriptorType != null)
563             return null;
564         }
565         if (NOTF.equalsIgnoreCase(inDescriptorType) || inDescriptorType == null) {
566         ModelMBeanNotificationInfo JavaDoc notif =
567             getNotification(inDescriptorName);
568         if (notif != null)
569             return notif.getDescriptor();
570         if (inDescriptorType != null)
571             return null;
572         }
573         if (inDescriptorType == null)
574         return null;
575         throw new RuntimeOperationsException JavaDoc(new IllegalArgumentException JavaDoc("Descriptor Type is invalid"),
576                          "Exception occurred trying to find the descriptors of the MBean");
577
578     }
579
580     
581
582     public void setDescriptor(Descriptor JavaDoc inDescriptor,
583                   String JavaDoc inDescriptorType)
584         throws MBeanException JavaDoc, RuntimeOperationsException JavaDoc {
585     final String JavaDoc excMsg =
586         "Exception occurred trying to set the descriptors of the MBean";
587
588     if (tracing()) {
589         trace("ModelMBeanInfoSupport.setDescriptor(Descriptor,String)",
590           "Entry");
591     }
592
593     if (inDescriptor==null) {
594         RuntimeException JavaDoc iae =
595         new IllegalArgumentException JavaDoc("Null Descriptor");
596         throw new RuntimeOperationsException JavaDoc(iae, excMsg);
597     }
598
599     if ((inDescriptorType == null) || (inDescriptorType == "")) {
600         inDescriptorType =
601         (String JavaDoc) inDescriptor.getFieldValue("descriptorType");
602
603         if (inDescriptorType == null) {
604         RuntimeException JavaDoc iae =
605             new IllegalArgumentException JavaDoc("Descriptor type is invalid");
606         throw new RuntimeOperationsException JavaDoc(iae, excMsg);
607         }
608     }
609
610     String JavaDoc inDescriptorName =
611         (String JavaDoc) inDescriptor.getFieldValue("name");
612     if (inDescriptorName == null) {
613         RuntimeException JavaDoc iae =
614             new IllegalArgumentException JavaDoc("Descriptor name is invalid");
615         throw new RuntimeOperationsException JavaDoc(iae, excMsg);
616     }
617     boolean found = false;
618     if (inDescriptorType.equalsIgnoreCase(MMB)) {
619         setMBeanDescriptor(inDescriptor);
620         found = true;
621     } else if (inDescriptorType.equalsIgnoreCase(ATTR)) {
622         MBeanAttributeInfo JavaDoc[] attrList = modelMBeanAttributes;
623         int numAttrs = 0;
624         if (attrList != null) numAttrs = attrList.length;
625
626         for (int i=0; i < numAttrs; i++) {
627         if (inDescriptorName.equals(attrList[i].getName())) {
628             found = true;
629             ModelMBeanAttributeInfo JavaDoc mmbai =
630             (ModelMBeanAttributeInfo JavaDoc) attrList[i];
631             mmbai.setDescriptor(inDescriptor);
632             if (tracing()) {
633             trace("ModelMBeanInfoSupport.setDescriptor",
634                   "setting descriptor to " + inDescriptor);
635             trace("ModelMBeanInfoSupport.setDescriptor",
636                   "local: AttributeInfo descriptor is " +
637                   mmbai.getDescriptor());
638             trace("ModelMBeanInfoSupport.setDescriptor",
639                   "modelMBeanInfo: AttributeInfo descriptor is " +
640                   this.getDescriptor(inDescriptorName,
641                          "attribute"));
642             }
643         }
644         }
645     } else if (inDescriptorType.equalsIgnoreCase(OPER)) {
646         MBeanOperationInfo JavaDoc[] operList = modelMBeanOperations;
647         int numOpers = 0;
648         if (operList != null) numOpers = operList.length;
649
650         for (int i=0; i < numOpers; i++) {
651         if (inDescriptorName.equals(operList[i].getName())) {
652             found = true;
653             ModelMBeanOperationInfo JavaDoc mmboi =
654             (ModelMBeanOperationInfo JavaDoc) operList[i];
655             mmboi.setDescriptor(inDescriptor);
656         }
657         }
658     } else if (inDescriptorType.equalsIgnoreCase(CONS)) {
659         MBeanConstructorInfo JavaDoc[] consList = modelMBeanConstructors;
660         int numCons = 0;
661         if (consList != null) numCons = consList.length;
662
663         for (int i=0; i < numCons; i++) {
664         if (inDescriptorName.equals(consList[i].getName())) {
665             found = true;
666             ModelMBeanConstructorInfo JavaDoc mmbci =
667             (ModelMBeanConstructorInfo JavaDoc) consList[i];
668             mmbci.setDescriptor(inDescriptor);
669         }
670         }
671     } else if (inDescriptorType.equalsIgnoreCase(NOTF)) {
672         MBeanNotificationInfo JavaDoc[] notifList = modelMBeanNotifications;
673         int numNotifs = 0;
674         if (notifList != null) numNotifs = notifList.length;
675
676         for (int i=0; i < numNotifs; i++) {
677         if (inDescriptorName.equals(notifList[i].getName())) {
678             found = true;
679             ModelMBeanNotificationInfo JavaDoc mmbni =
680             (ModelMBeanNotificationInfo JavaDoc) notifList[i];
681             mmbni.setDescriptor(inDescriptor);
682         }
683         }
684     } else {
685         RuntimeException JavaDoc iae =
686         new IllegalArgumentException JavaDoc("Invalid descriptor type: " +
687                          inDescriptorType);
688         throw new RuntimeOperationsException JavaDoc(iae, excMsg);
689     }
690
691     if (!found) {
692         RuntimeException JavaDoc iae =
693         new IllegalArgumentException JavaDoc("Descriptor name is invalid: " +
694                          "type=" + inDescriptorType +
695                          "; name=" + inDescriptorName);
696         throw new RuntimeOperationsException JavaDoc(iae, excMsg);
697     }
698     if (tracing()) {
699         trace("ModelMBeanInfoSupport.setDescriptor(Descriptor,String)",
700           "Exit");
701     }
702
703     }
704
705
706     public ModelMBeanAttributeInfo JavaDoc getAttribute(String JavaDoc inName)
707     throws MBeanException JavaDoc, RuntimeOperationsException JavaDoc {
708         ModelMBeanAttributeInfo JavaDoc retInfo = null;
709         if (tracing())
710         {
711             trace("ModelMBeanInfoSupport.getAttributeInfo(String)","Entry");
712         }
713         if (inName == null)
714         {
715             throw new RuntimeOperationsException JavaDoc(new IllegalArgumentException JavaDoc("Attribute Name is null"),
716                              ("Exception occurred trying to get the ModelMBeanAttributeInfo of the MBean"));
717         }
718         MBeanAttributeInfo JavaDoc[] attrList = modelMBeanAttributes;
719         int numAttrs = 0;
720         if (attrList != null) numAttrs = attrList.length;
721
722         for (int i=0; (i < numAttrs) && (retInfo == null); i++)
723         {
724             if (tracing())
725             {
726                 trace("ModelMBeanInfoSupport.getAttribute","this.getAttributes() MBeanAttributeInfo Array " + i + ":" + ((ModelMBeanAttributeInfo JavaDoc)attrList[i]).getDescriptor().toString());
727                 trace("ModelMBeanInfoSupport.getAttribute","this.modelMBeanAttributes MBeanAttributeInfo Array " + i + ":" + ((ModelMBeanAttributeInfo JavaDoc)modelMBeanAttributes[i]).getDescriptor().toString());
728             }
729             if (inName.equals(attrList[i].getName()))
730             {
731                 retInfo = ((ModelMBeanAttributeInfo JavaDoc)attrList[i].clone());
732             }
733         }
734         if (tracing())
735         {
736             trace("ModelMBeanInfoSupport.getAttribute()","Exit");
737         }
738
739         return retInfo;
740     }
741
742
743
744     public ModelMBeanOperationInfo JavaDoc getOperation(String JavaDoc inName)
745     throws MBeanException JavaDoc, RuntimeOperationsException JavaDoc {
746         ModelMBeanOperationInfo JavaDoc retInfo = null;
747         if (tracing())
748         {
749             trace("ModelMBeanInfoSupport.getOperation(String)","Entry");
750         }
751         if (inName == null)
752         {
753             throw new RuntimeOperationsException JavaDoc(new IllegalArgumentException JavaDoc("inName is null"),
754                              ("Exception occurred trying to get the ModelMBeanOperationInfo of the MBean"));
755         }
756         MBeanOperationInfo JavaDoc[] operList = modelMBeanOperations; //this.getOperations();
757
int numOpers = 0;
758         if (operList != null) numOpers = operList.length;
759
760         for (int i=0; (i < numOpers) && (retInfo == null); i++)
761         {
762             if (inName.equals(operList[i].getName()))
763             {
764                 retInfo = ((ModelMBeanOperationInfo JavaDoc) operList[i].clone());
765             }
766         }
767         if (tracing())
768         {
769             trace("ModelMBeanInfoSupport.getOperation(String)","Exit");
770         }
771
772         return retInfo;
773     }
774
775    /**
776      * Returns the ModelMBeanConstructorInfo requested by name.
777      * If no ModelMBeanConstructorInfo exists for this name null is returned.
778      *
779      * @param inName the name of the constructor.
780      *
781      * @return the constructor info for the named constructor, or null
782      * if there is none.
783      *
784      * @exception MBeanException Wraps a distributed communication Exception.
785      * @exception RuntimeOperationsException Wraps an IllegalArgumentException for a null constructor name.
786      */

787
788     public ModelMBeanConstructorInfo JavaDoc getConstructor(String JavaDoc inName)
789     throws MBeanException JavaDoc, RuntimeOperationsException JavaDoc {
790         ModelMBeanConstructorInfo JavaDoc retInfo = null;
791         if (tracing())
792         {
793             trace("ModelMBeanInfoSupport.getConstructor(String)","Entry");
794         }
795         if (inName == null)
796         {
797             throw new RuntimeOperationsException JavaDoc(new IllegalArgumentException JavaDoc("Constructor name is null"),
798                              ("Exception occurred trying to get the ModelMBeanConstructorInfo of the MBean"));
799         }
800         MBeanConstructorInfo JavaDoc[] consList = modelMBeanConstructors; //this.getConstructors();
801
int numCons = 0;
802         if (consList != null) numCons = consList.length;
803
804         for (int i=0; (i < numCons) && (retInfo == null); i++)
805         {
806             if (inName.equals(consList[i].getName()))
807             {
808                 retInfo = ((ModelMBeanConstructorInfo JavaDoc) consList[i].clone());
809             }
810         }
811         if (tracing())
812         {
813             trace("ModelMBeanInfoSupport.getConstructor(String)","Exit");
814         }
815
816         return retInfo;
817     }
818     
819
820     public ModelMBeanNotificationInfo JavaDoc getNotification(String JavaDoc inName)
821     throws MBeanException JavaDoc, RuntimeOperationsException JavaDoc {
822         ModelMBeanNotificationInfo JavaDoc retInfo = null;
823         if (tracing())
824         {
825             trace("ModelMBeanInfoSupport.getNotification(String)","Entry");
826         }
827         if (inName == null)
828         {
829             throw new RuntimeOperationsException JavaDoc(new IllegalArgumentException JavaDoc("Notification name is null"),
830                              ("Exception occurred trying to get the ModelMBeanNotificationInfo of the MBean"));
831         }
832         MBeanNotificationInfo JavaDoc[] notifList = modelMBeanNotifications; //this.getNotifications();
833
int numNotifs = 0;
834         if (notifList != null) numNotifs = notifList.length;
835
836         for (int i=0; (i < numNotifs) && (retInfo == null); i++)
837         {
838             if (inName.equals(notifList[i].getName()))
839             {
840                 retInfo = ((ModelMBeanNotificationInfo JavaDoc) notifList[i].clone());
841             }
842         }
843         if (tracing())
844         {
845             trace("ModelMBeanInfoSupport.getNotification(String)","Exit");
846         }
847
848         return retInfo;
849     }
850
851
852     public Descriptor JavaDoc getMBeanDescriptor()
853     throws MBeanException JavaDoc, RuntimeOperationsException JavaDoc {
854         if (tracing())
855         {
856             trace("ModelMBeanInfoSupport.getMBeanDescriptor()","Executed");
857         }
858         if (modelMBeanDescriptor == null)
859         {
860             return null;
861         }
862         if (tracing()) trace("ModelMBeanInfoSupport.getMBeanDesriptor()", "Returning " + modelMBeanDescriptor.toString());
863         return((Descriptor JavaDoc) modelMBeanDescriptor.clone());
864     }
865
866
867     public void setMBeanDescriptor(Descriptor JavaDoc inMBeanDescriptor)
868     throws MBeanException JavaDoc, RuntimeOperationsException JavaDoc {
869         if (tracing())
870         {
871             trace("ModelMBeanInfoSupport.setMBeanDescriptor(Descriptor)","Executed");
872         }
873
874         if (inMBeanDescriptor == null)
875         {
876             if (tracing()) trace("ModelMBeanInfoSupport.setMBeanDescriptor(Descriptor)","MBean Descriptor is not valid");
877             modelMBeanDescriptor = createDefaultDescriptor();
878         } else
879         {
880             if (isValidDescriptor(inMBeanDescriptor)) {
881             modelMBeanDescriptor = (Descriptor JavaDoc) inMBeanDescriptor.clone();
882             addDefaultFields();
883             } else {
884             throw new RuntimeOperationsException JavaDoc(new IllegalArgumentException JavaDoc("Invalid descriptor passed in parameter"));
885             }
886
887         }
888     }
889
890     /* The default descriptor is:
891     * name=mbeanName,descriptorType=mbean, displayName=this.getClassName(),
892     * persistPolicy=never,log=F,visibility=1
893     */

894     private Descriptor JavaDoc createDefaultDescriptor()
895     {
896
897         Descriptor JavaDoc dftDesc = null;
898         dftDesc = new DescriptorSupport JavaDoc(new String JavaDoc[] {("name=" + this.getClassName()),
899                                   "descriptorType=mbean",
900                                   ("displayName=" + this.getClassName()),
901                                   "persistPolicy=never",
902                                   "log=F",
903                                   "visibility=1"});
904         return dftDesc;
905     }
906
907     /*
908     * Validates the ModelMBeanDescriptor
909     * If the descriptor does not contain all these fields,
910     * they will be added with these default values.
911     * name=mbeanName,descriptorType=mbean, displayName=this.getClassName(),
912     * persistPolicy=never,log=F,visibility=1
913     *
914     * Will return false if the MBeanDescriptor has a null name or descriptorType.
915     */

916     private boolean isValidDescriptor(Descriptor JavaDoc inDesc)
917     {
918     String JavaDoc badField = null;
919     // if name != mbi.getClassName
920
// if (descriptorType != mbean)
921
// look for displayName, persistPolicy, logging, visibility and add in
922
if (tracing())
923         trace("isValidDescriptor",
924           "Validating descriptor: " + inDesc.toString());
925     if (inDesc == null)
926         badField = "nullDescriptor";
927     else if (!inDesc.isValid())
928         // checks for empty descriptors, null,
929
// checks for empty name and descriptorType and
930
// valid values for fields.
931
badField="InvalidDescriptor";
932     else if ((((String JavaDoc)inDesc.getFieldValue("name")) == null))
933         badField="name";
934     else if (! ((String JavaDoc)inDesc.getFieldValue("descriptorType"))
935          .equalsIgnoreCase(MMB))
936         badField="descriptorType";
937     else { // no bad fields
938
if (tracing())
939         trace("isValidDescriptor", "returning true");
940         return true;
941     }
942
943     if (tracing())
944         trace("isValidDescriptor",
945           "returning false: invalid field is " + badField);
946
947     return false;
948     }
949
950     private void addDefaultFields() {
951     final Descriptor JavaDoc d = modelMBeanDescriptor;
952
953     if ((d.getFieldValue("displayName")) == null)
954         d.setField("displayName",this.getClassName());
955     if ((d.getFieldValue("persistPolicy")) == null)
956         d.setField("persistPolicy","never");
957     if ((d.getFieldValue("log")) == null)
958         d.setField("log","F");
959     if ((d.getFieldValue("visibility")) == null)
960         d.setField("visibility","1");
961     }
962
963     // SUN Trace and debug functions
964
private boolean tracing()
965     {
966     return Trace.isSelected(Trace.LEVEL_TRACE, Trace.INFO_MODELMBEAN);
967     }
968
969     private void trace(String JavaDoc inClass, String JavaDoc inMethod, String JavaDoc inText)
970     {
971     Trace.send(Trace.LEVEL_TRACE, Trace.INFO_MODELMBEAN, inClass,
972            inMethod, inText);
973     }
974
975     private void trace(String JavaDoc inMethod, String JavaDoc inText)
976     {
977     trace(currClass, inMethod, inText);
978     }
979
980     /**
981      * Deserializes a {@link ModelMBeanInfoSupport} from an {@link ObjectInputStream}.
982      */

983     private void readObject(ObjectInputStream JavaDoc in)
984         throws IOException JavaDoc, ClassNotFoundException JavaDoc {
985       if (compat)
986       {
987         // Read an object serialized in the old serial form
988
//
989
ObjectInputStream.GetField JavaDoc fields = in.readFields();
990     modelMBeanDescriptor = (Descriptor JavaDoc) fields.get("modelMBeanDescriptor", null);
991     if (fields.defaulted("modelMBeanDescriptor"))
992         {
993           throw new NullPointerException JavaDoc("modelMBeanDescriptor");
994         }
995     modelMBeanAttributes = (MBeanAttributeInfo JavaDoc[]) fields.get("mmbAttributes", null);
996     if (fields.defaulted("mmbAttributes"))
997         {
998           throw new NullPointerException JavaDoc("mmbAttributes");
999         }
1000    modelMBeanConstructors = (MBeanConstructorInfo JavaDoc[]) fields.get("mmbConstructors", null);
1001    if (fields.defaulted("mmbConstructors"))
1002        {
1003          throw new NullPointerException JavaDoc("mmbConstructors");
1004        }
1005    modelMBeanNotifications = (MBeanNotificationInfo JavaDoc[]) fields.get("mmbNotifications", null);
1006    if (fields.defaulted("mmbNotifications"))
1007        {
1008          throw new NullPointerException JavaDoc("mmbNotifications");
1009        }
1010    modelMBeanOperations = (MBeanOperationInfo JavaDoc[]) fields.get("mmbOperations", null);
1011    if (fields.defaulted("mmbOperations"))
1012        {
1013          throw new NullPointerException JavaDoc("mmbOperations");
1014        }
1015      }
1016      else
1017      {
1018        // Read an object serialized in the new serial form
1019
//
1020
in.defaultReadObject();
1021      }
1022    }
1023
1024
1025    /**
1026     * Serializes a {@link ModelMBeanInfoSupport} to an {@link ObjectOutputStream}.
1027     */

1028    private void writeObject(ObjectOutputStream JavaDoc out)
1029        throws IOException JavaDoc {
1030      if (compat)
1031      {
1032        // Serializes this instance in the old serial form
1033
//
1034
ObjectOutputStream.PutField JavaDoc fields = out.putFields();
1035    fields.put("modelMBeanDescriptor", modelMBeanDescriptor);
1036    fields.put("mmbAttributes", modelMBeanAttributes);
1037    fields.put("mmbConstructors", modelMBeanConstructors);
1038    fields.put("mmbNotifications", modelMBeanNotifications);
1039    fields.put("mmbOperations", modelMBeanOperations);
1040    fields.put("currClass", currClass);
1041    out.writeFields();
1042      }
1043      else
1044      {
1045        // Serializes this instance in the new serial form
1046
//
1047
out.defaultWriteObject();
1048      }
1049    }
1050
1051
1052}
1053
1054
1055
Popular Tags