KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > javax > management > relation > RoleInfo


1 /*
2  * @(#)RoleInfo.java 1.34 03/12/19
3  *
4  * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
5  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6  */

7
8 package javax.management.relation;
9
10 import java.io.IOException JavaDoc;
11 import java.io.ObjectInputStream JavaDoc;
12 import java.io.ObjectOutputStream JavaDoc;
13 import java.io.ObjectStreamField JavaDoc;
14 import java.io.Serializable JavaDoc;
15 import java.security.AccessController JavaDoc;
16 import java.security.PrivilegedAction JavaDoc;
17
18 import javax.management.NotCompliantMBeanException JavaDoc;
19 import javax.management.MBeanInfo JavaDoc;
20 import javax.management.MBeanServer JavaDoc;
21 import javax.management.loading.ClassLoaderRepository JavaDoc;
22
23 import com.sun.jmx.mbeanserver.GetPropertyAction;
24 import com.sun.jmx.mbeanserver.Introspector;
25
26
27 /**
28  * A RoleInfo object summarises a role in a relation type.
29  *
30  * @since 1.5
31  */

32 public class RoleInfo implements Serializable JavaDoc {
33
34     // Serialization compatibility stuff:
35
// Two serial forms are supported in this class. The selected form depends
36
// on system property "jmx.serial.form":
37
// - "1.0" for JMX 1.0
38
// - any other value for JMX 1.1 and higher
39
//
40
// Serial version for old serial form
41
private static final long oldSerialVersionUID = 7227256952085334351L;
42     //
43
// Serial version for new serial form
44
private static final long newSerialVersionUID = 2504952983494636987L;
45     //
46
// Serializable fields in old serial form
47
private static final ObjectStreamField JavaDoc[] oldSerialPersistentFields =
48     {
49       new ObjectStreamField JavaDoc("myName", String JavaDoc.class),
50       new ObjectStreamField JavaDoc("myIsReadableFlg", boolean.class),
51       new ObjectStreamField JavaDoc("myIsWritableFlg", boolean.class),
52       new ObjectStreamField JavaDoc("myDescription", String JavaDoc.class),
53       new ObjectStreamField JavaDoc("myMinDegree", int.class),
54       new ObjectStreamField JavaDoc("myMaxDegree", int.class),
55       new ObjectStreamField JavaDoc("myRefMBeanClassName", String JavaDoc.class)
56     };
57     //
58
// Serializable fields in new serial form
59
private static final ObjectStreamField JavaDoc[] newSerialPersistentFields =
60     {
61       new ObjectStreamField JavaDoc("name", String JavaDoc.class),
62       new ObjectStreamField JavaDoc("isReadable", boolean.class),
63       new ObjectStreamField JavaDoc("isWritable", boolean.class),
64       new ObjectStreamField JavaDoc("description", String JavaDoc.class),
65       new ObjectStreamField JavaDoc("minDegree", int.class),
66       new ObjectStreamField JavaDoc("maxDegree", int.class),
67       new ObjectStreamField JavaDoc("referencedMBeanClassName", String JavaDoc.class)
68     };
69     //
70
// Actual serial version and serial form
71
private static final long serialVersionUID;
72     /**
73      * @serialField name String Role name
74      * @serialField isReadable boolean Read access mode: <code>true</code> if role is readable
75      * @serialField isWritable boolean Write access mode: <code>true</code> if role is writable
76      * @serialField description String Role description
77      * @serialField minDegree int Minimum degree (i.e. minimum number of referenced MBeans in corresponding role)
78      * @serialField maxDegree int Maximum degree (i.e. maximum number of referenced MBeans in corresponding role)
79      * @serialField referencedMBeanClassName String Name of class of MBean(s) expected to be referenced in corresponding role
80      */

81     private static final ObjectStreamField JavaDoc[] serialPersistentFields;
82     private static boolean compat = false;
83     static {
84     try {
85         PrivilegedAction JavaDoc act = new GetPropertyAction("jmx.serial.form");
86         String JavaDoc form = (String JavaDoc) AccessController.doPrivileged(act);
87         compat = (form != null && form.equals("1.0"));
88     } catch (Exception JavaDoc e) {
89         // OK : Too bad, no compat with 1.0
90
}
91     if (compat) {
92         serialPersistentFields = oldSerialPersistentFields;
93         serialVersionUID = oldSerialVersionUID;
94     } else {
95         serialPersistentFields = newSerialPersistentFields;
96         serialVersionUID = newSerialVersionUID;
97     }
98     }
99     //
100
// END Serialization compatibility stuff
101

102     //
103
// Public constants
104
//
105

106     /**
107      * To specify an unlimited cardinality.
108      */

109     public static int ROLE_CARDINALITY_INFINITY = -1;
110
111     //
112
// Private members
113
//
114

115     /**
116      * @serial Role name
117      */

118     private String JavaDoc name = null;
119
120     /**
121      * @serial Read access mode: <code>true</code> if role is readable
122      */

123     private boolean isReadable;
124
125     /**
126      * @serial Write access mode: <code>true</code> if role is writable
127      */

128     private boolean isWritable;
129
130     /**
131      * @serial Role description
132      */

133     private String JavaDoc description = null;
134
135     /**
136      * @serial Minimum degree (i.e. minimum number of referenced MBeans in corresponding role)
137      */

138     private int minDegree;
139
140     /**
141      * @serial Maximum degree (i.e. maximum number of referenced MBeans in corresponding role)
142      */

143     private int maxDegree;
144
145     /**
146      * @serial Name of class of MBean(s) expected to be referenced in corresponding role
147      */

148     private String JavaDoc referencedMBeanClassName = null;
149
150     //
151
// Constructors
152
//
153

154     /**
155      * Constructor.
156      *
157      * @param theName name of the role.
158      * @param theRefMBeanClassName name of the class of MBean(s) expected to
159      * be referenced in corresponding role. If an MBean <em>M</em> is in
160      * this role, then the MBean server must return true for
161      * {@link MBeanServer#isInstanceOf isInstanceOf(M, theRefMBeanClassName)}.
162      * @param theIsReadable flag to indicate if the corresponding role
163      * can be read
164      * @param theIsWritable flag to indicate if the corresponding role
165      * can be set
166      * @param theMinDegree minimum degree for role, i.e. minimum number of
167      * MBeans to provide in corresponding role
168      * Must be less or equal than theMaxDegree.
169      * (ROLE_CARDINALITY_INFINITY for unlimited)
170      * @param theMaxDegree maximum degree for role, i.e. maximum number of
171      * MBeans to provide in corresponding role
172      * Must be greater or equal than theMinDegree
173      * (ROLE_CARDINALITY_INFINITY for unlimited)
174      * @param theDescription description of the role (can be null)
175      *
176      * @exception IllegalArgumentException if null parameter
177      * @exception InvalidRoleInfoException if the minimum degree is
178      * greater than the maximum degree.
179      * @exception ClassNotFoundException As of JMX 1.2, this exception
180      * can no longer be thrown. It is retained in the declaration of
181      * this class for compatibility with existing code.
182      * @exception NotCompliantMBeanException if the class theRefMBeanClassName
183      * is not a MBean class.
184      */

185     public RoleInfo(String JavaDoc theName,
186             String JavaDoc theRefMBeanClassName,
187             boolean theIsReadable,
188             boolean theIsWritable,
189             int theMinDegree,
190             int theMaxDegree,
191             String JavaDoc theDescription)
192     throws IllegalArgumentException JavaDoc,
193        InvalidRoleInfoException JavaDoc,
194            ClassNotFoundException JavaDoc,
195            NotCompliantMBeanException JavaDoc {
196
197     init(theName,
198          theRefMBeanClassName,
199          theIsReadable,
200          theIsWritable,
201          theMinDegree,
202          theMaxDegree,
203          theDescription);
204     return;
205     }
206
207     /**
208      * Constructor.
209      *
210      * @param theName name of the role
211      * @param theRefMBeanClassName name of the class of MBean(s) expected to
212      * be referenced in corresponding role. If an MBean <em>M</em> is in
213      * this role, then the MBean server must return true for
214      * {@link MBeanServer#isInstanceOf isInstanceOf(M, theRefMBeanClassName)}.
215      * @param theIsReadable flag to indicate if the corresponding role
216      * can be read
217      * @param theIsWritable flag to indicate if the corresponding role
218      * can be set
219      *
220      * <P>Minimum and maximum degrees defaulted to 1.
221      * <P>Description of role defaulted to null.
222      *
223      * @exception IllegalArgumentException if null parameter
224      * @exception ClassNotFoundException As of JMX 1.2, this exception
225      * can no longer be thrown. It is retained in the declaration of
226      * this class for compatibility with existing code.
227      * @exception NotCompliantMBeanException As of JMX 1.2, this
228      * exception can no longer be thrown. It is retained in the
229      * declaration of this class for compatibility with existing code.
230      */

231     public RoleInfo(String JavaDoc theName,
232             String JavaDoc theRefMBeanClassName,
233             boolean theIsReadable,
234             boolean theIsWritable)
235     throws IllegalArgumentException JavaDoc,
236        ClassNotFoundException JavaDoc,
237            NotCompliantMBeanException JavaDoc {
238
239     try {
240         init(theName,
241          theRefMBeanClassName,
242          theIsReadable,
243          theIsWritable,
244          1,
245          1,
246          null);
247     } catch (InvalidRoleInfoException JavaDoc exc) {
248         // OK : Can never happen as the minimum
249
// degree equals the maximum degree.
250
}
251
252     return;
253     }
254
255     /**
256      * Constructor.
257      *
258      * @param theName name of the role
259      * @param theRefMBeanClassName name of the class of MBean(s) expected to
260      * be referenced in corresponding role. If an MBean <em>M</em> is in
261      * this role, then the MBean server must return true for
262      * {@link MBeanServer#isInstanceOf isInstanceOf(M, theRefMBeanClassName)}.
263      *
264      * <P>IsReadable and IsWritable defaulted to true.
265      * <P>Minimum and maximum degrees defaulted to 1.
266      * <P>Description of role defaulted to null.
267      *
268      * @exception IllegalArgumentException if null parameter
269      * @exception ClassNotFoundException As of JMX 1.2, this exception
270      * can no longer be thrown. It is retained in the declaration of
271      * this class for compatibility with existing code.
272      * @exception NotCompliantMBeanException As of JMX 1.2, this
273      * exception can no longer be thrown. It is retained in the
274      * declaration of this class for compatibility with existing code.
275       */

276     public RoleInfo(String JavaDoc theName,
277             String JavaDoc theRefMBeanClassName)
278     throws IllegalArgumentException JavaDoc,
279        ClassNotFoundException JavaDoc,
280            NotCompliantMBeanException JavaDoc {
281
282     try {
283         init(theName,
284          theRefMBeanClassName,
285          true,
286          true,
287          1,
288          1,
289          null);
290     } catch (InvalidRoleInfoException JavaDoc exc) {
291         // OK : Can never happen as the minimum
292
// degree equals the maximum degree.
293
}
294
295     return;
296     }
297
298     /**
299      * Copy constructor.
300      *
301      * @param theRoleInfo the RoleInfo to be copied.
302      *
303      * @exception IllegalArgumentException if null parameter
304      */

305     public RoleInfo(RoleInfo JavaDoc theRoleInfo)
306     throws IllegalArgumentException JavaDoc {
307
308     if (theRoleInfo == null) {
309         // Revisit [cebro] Localize message
310
String JavaDoc excMsg = "Invalid parameter.";
311         throw new IllegalArgumentException JavaDoc(excMsg);
312     }
313
314     try {
315         init(theRoleInfo.getName(),
316          theRoleInfo.getRefMBeanClassName(),
317          theRoleInfo.isReadable(),
318          theRoleInfo.isWritable(),
319          theRoleInfo.getMinDegree(),
320          theRoleInfo.getMaxDegree(),
321          theRoleInfo.getDescription());
322     } catch (InvalidRoleInfoException JavaDoc exc3) {
323         // OK : Can never happen as the minimum degree and the maximum
324
// degree were already checked at the time the theRoleInfo
325
// instance was created.
326
}
327     }
328
329     //
330
// Accessors
331
//
332

333     /**
334      * Returns the name of the role.
335      *
336      * @return the name of the role.
337      */

338     public String JavaDoc getName() {
339     return name;
340     }
341
342     /**
343      * Returns read access mode for the role (true if it is readable).
344      *
345      * @return true if the role is readable.
346      */

347     public boolean isReadable() {
348     return isReadable;
349     }
350
351     /**
352      * Returns write access mode for the role (true if it is writable).
353      *
354      * @return true if the role is writable.
355      */

356     public boolean isWritable() {
357     return isWritable;
358     }
359
360     /**
361      * Returns description text for the role.
362      *
363      * @return the description of the role.
364      */

365     public String JavaDoc getDescription() {
366     return description;
367     }
368
369     /**
370      * Returns minimum degree for corresponding role reference.
371      *
372      * @return the minimum degree.
373      */

374     public int getMinDegree() {
375     return minDegree;
376     }
377
378     /**
379      * Returns maximum degree for corresponding role reference.
380      *
381      * @return the maximum degree.
382      */

383     public int getMaxDegree() {
384     return maxDegree;
385     }
386
387     /**
388      * <p>Returns name of type of MBean expected to be referenced in
389      * corresponding role.</p>
390      *
391      * @return the name of the referenced type.
392      */

393     public String JavaDoc getRefMBeanClassName() {
394     return referencedMBeanClassName;
395     }
396
397     /**
398      * Returns a boolean to specify if given value is greater or equal than
399      * expected minimum degree (true if yes).
400      *
401      * @param theValue value
402      *
403      * @return true if greater or equal than minimum degree, false otherwise.
404      */

405     public boolean checkMinDegree(int theValue) {
406     if (theValue >= ROLE_CARDINALITY_INFINITY &&
407         (minDegree == ROLE_CARDINALITY_INFINITY
408          || theValue >= minDegree)) {
409         return true;
410     } else {
411         return false;
412     }
413     }
414
415     /**
416      * Returns a boolean to specify if given value is less or equal than
417      * expected maximum degree (true if yes).
418      *
419      * @param theValue value
420      *
421      * @return true if less or equal than maximum degree, false otherwise.
422      */

423     public boolean checkMaxDegree(int theValue) {
424     if (theValue >= ROLE_CARDINALITY_INFINITY &&
425         (maxDegree == ROLE_CARDINALITY_INFINITY ||
426          (theValue != ROLE_CARDINALITY_INFINITY &&
427           theValue <= maxDegree))) {
428         return true;
429     } else {
430         return false;
431     }
432     }
433
434     /**
435      * Returns a string describing the role info.
436      *
437      * @return a description of the role info.
438      */

439     public String JavaDoc toString() {
440     StringBuffer JavaDoc result = new StringBuffer JavaDoc();
441     result.append("role info name: " + name);
442     result.append("; isReadable: " + isReadable);
443     result.append("; isWritable: " + isWritable);
444     result.append("; description: " + description);
445     result.append("; minimum degree: " + minDegree);
446     result.append("; maximum degree: " + maxDegree);
447     result.append("; MBean class: " + referencedMBeanClassName);
448     return result.toString();
449     }
450
451     //
452
// Misc
453
//
454

455     // Initialisation
456
private void init(String JavaDoc theName,
457               String JavaDoc theRefMBeanClassName,
458               boolean theIsReadable,
459               boolean theIsWritable,
460               int theMinDegree,
461               int theMaxDegree,
462               String JavaDoc theDescription)
463         throws IllegalArgumentException JavaDoc,
464            InvalidRoleInfoException JavaDoc {
465
466     if (theName == null ||
467         theRefMBeanClassName == null) {
468         // Revisit [cebro] Localize message
469
String JavaDoc excMsg = "Invalid parameter.";
470         throw new IllegalArgumentException JavaDoc(excMsg);
471     }
472
473     name = theName;
474     isReadable = theIsReadable;
475     isWritable = theIsWritable;
476     if (theDescription != null) {
477         description = theDescription;
478     }
479
480     boolean invalidRoleInfoFlg = false;
481     StringBuffer JavaDoc excMsgStrB = new StringBuffer JavaDoc();
482     if (theMaxDegree != ROLE_CARDINALITY_INFINITY &&
483         (theMinDegree == ROLE_CARDINALITY_INFINITY ||
484          theMinDegree > theMaxDegree)) {
485         // Revisit [cebro] Localize message
486
excMsgStrB.append("Minimum degree ");
487         excMsgStrB.append(theMinDegree);
488         excMsgStrB.append(" is greater than maximum degree ");
489         excMsgStrB.append(theMaxDegree);
490         invalidRoleInfoFlg = true;
491
492     } else if (theMinDegree < ROLE_CARDINALITY_INFINITY ||
493            theMaxDegree < ROLE_CARDINALITY_INFINITY) {
494         // Revisit [cebro] Localize message
495
excMsgStrB.append("Minimum or maximum degree has an illegal value, must be [0, ROLE_CARDINALITY_INFINITY].");
496         invalidRoleInfoFlg = true;
497     }
498     if (invalidRoleInfoFlg) {
499         throw new InvalidRoleInfoException JavaDoc(excMsgStrB.toString());
500     }
501     minDegree = theMinDegree;
502     maxDegree = theMaxDegree;
503
504         referencedMBeanClassName = theRefMBeanClassName;
505
506     return;
507     }
508
509     /**
510      * Deserializes a {@link RoleInfo} from an {@link ObjectInputStream}.
511      */

512     private void readObject(ObjectInputStream JavaDoc in)
513         throws IOException JavaDoc, ClassNotFoundException JavaDoc {
514       if (compat)
515       {
516         // Read an object serialized in the old serial form
517
//
518
ObjectInputStream.GetField JavaDoc fields = in.readFields();
519     name = (String JavaDoc) fields.get("myName", null);
520     if (fields.defaulted("myName"))
521         {
522           throw new NullPointerException JavaDoc("myName");
523         }
524     isReadable = fields.get("myIsReadableFlg", false);
525     if (fields.defaulted("myIsReadableFlg"))
526         {
527           throw new NullPointerException JavaDoc("myIsReadableFlg");
528         }
529     isWritable = fields.get("myIsWritableFlg", false);
530     if (fields.defaulted("myIsWritableFlg"))
531         {
532           throw new NullPointerException JavaDoc("myIsWritableFlg");
533         }
534     description = (String JavaDoc) fields.get("myDescription", null);
535     if (fields.defaulted("myDescription"))
536         {
537           throw new NullPointerException JavaDoc("myDescription");
538         }
539     minDegree = fields.get("myMinDegree", (int)0);
540     if (fields.defaulted("myMinDegree"))
541         {
542           throw new NullPointerException JavaDoc("myMinDegree");
543         }
544     maxDegree = fields.get("myMaxDegree", (int)0);
545     if (fields.defaulted("myMaxDegree"))
546         {
547           throw new NullPointerException JavaDoc("myMaxDegree");
548         }
549     referencedMBeanClassName = (String JavaDoc) fields.get("myRefMBeanClassName", null);
550     if (fields.defaulted("myRefMBeanClassName"))
551         {
552           throw new NullPointerException JavaDoc("myRefMBeanClassName");
553         }
554       }
555       else
556       {
557         // Read an object serialized in the new serial form
558
//
559
in.defaultReadObject();
560       }
561     }
562
563
564     /**
565      * Serializes a {@link RoleInfo} to an {@link ObjectOutputStream}.
566      */

567     private void writeObject(ObjectOutputStream JavaDoc out)
568         throws IOException JavaDoc {
569       if (compat)
570       {
571         // Serializes this instance in the old serial form
572
//
573
ObjectOutputStream.PutField JavaDoc fields = out.putFields();
574     fields.put("myName", name);
575     fields.put("myIsReadableFlg", isReadable);
576     fields.put("myIsWritableFlg", isWritable);
577     fields.put("myDescription", description);
578     fields.put("myMinDegree", minDegree);
579     fields.put("myMaxDegree", maxDegree);
580     fields.put("myRefMBeanClassName", referencedMBeanClassName);
581     out.writeFields();
582       }
583       else
584       {
585         // Serializes this instance in the new serial form
586
//
587
out.defaultWriteObject();
588       }
589     }
590
591 }
592
Popular Tags