KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * @(#)RelationTypeSupport.java 1.31 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
15 import java.security.AccessController JavaDoc;
16 import java.security.PrivilegedAction JavaDoc;
17
18 import java.util.ArrayList JavaDoc;
19 import java.util.HashMap JavaDoc;
20 import java.util.List JavaDoc;
21 import java.util.Map JavaDoc;
22
23 import com.sun.jmx.mbeanserver.GetPropertyAction;
24 import com.sun.jmx.trace.Trace;
25
26
27 /**
28  * A RelationTypeSupport object implements the RelationType interface.
29  * <P>It represents a relation type, providing role information for each role
30  * expected to be supported in every relation of that type.
31  *
32  * <P>A relation type includes a relation type name and a list of
33  * role infos (represented by RoleInfo objects).
34  *
35  * <P>A relation type has to be declared in the Relation Service:
36  * <P>- either using the createRelationType() method, where a RelationTypeSupport
37  * object will be created and kept in the Relation Service
38  * <P>- either using the addRelationType() method where the user has to create
39  * an object implementing the RelationType interface, and this object will be
40  * used as representing a relation type in the Relation Service.
41  *
42  * @since 1.5
43  */

44 public class RelationTypeSupport implements RelationType JavaDoc {
45
46     // Serialization compatibility stuff:
47
// Two serial forms are supported in this class. The selected form depends
48
// on system property "jmx.serial.form":
49
// - "1.0" for JMX 1.0
50
// - any other value for JMX 1.1 and higher
51
//
52
// Serial version for old serial form
53
private static final long oldSerialVersionUID = -8179019472410837190L;
54     //
55
// Serial version for new serial form
56
private static final long newSerialVersionUID = 4611072955724144607L;
57     //
58
// Serializable fields in old serial form
59
private static final ObjectStreamField JavaDoc[] oldSerialPersistentFields =
60     {
61       new ObjectStreamField JavaDoc("myTypeName", String JavaDoc.class),
62       new ObjectStreamField JavaDoc("myRoleName2InfoMap", HashMap JavaDoc.class),
63       new ObjectStreamField JavaDoc("myIsInRelServFlg", boolean.class)
64     };
65     //
66
// Serializable fields in new serial form
67
private static final ObjectStreamField JavaDoc[] newSerialPersistentFields =
68     {
69       new ObjectStreamField JavaDoc("typeName", String JavaDoc.class),
70       new ObjectStreamField JavaDoc("roleName2InfoMap", Map JavaDoc.class),
71       new ObjectStreamField JavaDoc("isInRelationService", boolean.class)
72     };
73     //
74
// Actual serial version and serial form
75
private static final long serialVersionUID;
76     /**
77      * @serialField typeName String Relation type name
78      * @serialField roleName2InfoMap Map {@link Map} holding the mapping:
79      * &lt;role name ({@link String})&gt; -&gt; &lt;role info ({@link RoleInfo} object)&gt;
80      * @serialField isInRelationService boolean Flag specifying whether the relation type has been declared in the
81      * Relation Service (so can no longer be updated)
82      */

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

104     //
105
// Private members
106
//
107

108     /**
109      * @serial Relation type name
110      */

111     private String JavaDoc typeName = null;
112
113     /**
114      * @serial {@link Map} holding the mapping:
115      * &lt;role name ({@link String})&gt; -&gt; &lt;role info ({@link RoleInfo} object)&gt;
116      */

117     private Map JavaDoc roleName2InfoMap = new HashMap JavaDoc();
118
119     /**
120      * @serial Flag specifying whether the relation type has been declared in the
121      * Relation Service (so can no longer be updated)
122      */

123     private boolean isInRelationService = false;
124
125     //
126
// Constructors
127
//
128

129     /**
130      * Constructor where all role definitions are dynamically created and
131      * passed as parameter.
132      *
133      * @param theRelTypeName Name of relation type
134      * @param theRoleInfoArray List of role definitions (RoleInfo objects)
135      *
136      * @exception IllegalArgumentException if null parameter
137      * @exception InvalidRelationTypeException if:
138      * <P>- the same name has been used for two different roles
139      * <P>- no role info provided
140      * <P>- one null role info provided
141      */

142     public RelationTypeSupport(String JavaDoc theRelTypeName,
143                 RoleInfo JavaDoc[] theRoleInfoArray)
144     throws IllegalArgumentException JavaDoc,
145            InvalidRelationTypeException JavaDoc {
146
147     if (theRelTypeName == null || theRoleInfoArray == null) {
148         // Revisit [cebro] Localize message
149
String JavaDoc excMsg = "Invalid parameter.";
150         throw new IllegalArgumentException JavaDoc(excMsg);
151     }
152
153     if (isTraceOn())
154         trace("Constructor: entering", theRelTypeName);
155
156     // Can throw InvalidRelationTypeException, ClassNotFoundException
157
// and NotCompliantMBeanException
158
initMembers(theRelTypeName, theRoleInfoArray);
159
160     if (isTraceOn())
161         trace("Constructor: exiting", null);
162     return;
163     }
164
165     /**
166      * Constructor to be used for subclasses.
167      *
168      * @param theRelTypeName Name of relation type.
169      *
170      * @exception IllegalArgumentException if null parameter.
171      */

172     protected RelationTypeSupport(String JavaDoc theRelTypeName)
173     {
174     if (theRelTypeName == null) {
175         // Revisit [cebro] Localize message
176
String JavaDoc excMsg = "Invalid parameter.";
177         throw new IllegalArgumentException JavaDoc(excMsg);
178     }
179
180     if (isTraceOn())
181         trace("Protected constructor: entering", theRelTypeName);
182
183     typeName = theRelTypeName;
184
185     if (isTraceOn())
186         trace("Protected constructor: exiting", null);
187     return;
188     }
189
190     //
191
// Accessors
192
//
193

194     /**
195      * Returns the relation type name.
196      *
197      * @return the relation type name.
198      */

199     public String JavaDoc getRelationTypeName() {
200     return typeName;
201     }
202
203     /**
204      * Returns the list of role definitions (ArrayList of RoleInfo objects).
205      */

206     public List JavaDoc getRoleInfos() {
207     return new ArrayList JavaDoc(roleName2InfoMap.values());
208     }
209
210     /**
211      * Returns the role info (RoleInfo object) for the given role info name
212      * (null if not found).
213      *
214      * @param theRoleInfoName role info name
215      *
216      * @return RoleInfo object providing role definition
217      * does not exist
218      *
219      * @exception IllegalArgumentException if null parameter
220      * @exception RoleInfoNotFoundException if no role info with that name in
221      * relation type.
222      */

223     public RoleInfo JavaDoc getRoleInfo(String JavaDoc theRoleInfoName)
224     throws IllegalArgumentException JavaDoc,
225            RoleInfoNotFoundException JavaDoc {
226
227     if (theRoleInfoName == null) {
228         // Revisit [cebro] Localize message
229
String JavaDoc excMsg = "Invalid parameter.";
230         throw new IllegalArgumentException JavaDoc(excMsg);
231     }
232
233     if (isTraceOn())
234         trace("getRoleInfo: entering", theRoleInfoName);
235
236     // No null RoleInfo allowed, so use get()
237
RoleInfo JavaDoc result = (RoleInfo JavaDoc)(roleName2InfoMap.get(theRoleInfoName));
238
239     if (result == null) {
240         StringBuffer JavaDoc excMsgStrB = new StringBuffer JavaDoc();
241         // Revisit [cebro] Localize message
242
String JavaDoc excMsg = "No role info for role ";
243         excMsgStrB.append(excMsg);
244         excMsgStrB.append(theRoleInfoName);
245         throw new RoleInfoNotFoundException JavaDoc(excMsgStrB.toString());
246     }
247
248     if (isTraceOn())
249         trace("getRoleInfo: exiting", null);
250     return result;
251     }
252
253     //
254
// Misc
255
//
256

257     /**
258      * Add a role info.
259      * This method of course should not be used after the creation of the
260      * relation type, because updating it would invalidate that the relations
261      * created associated to that type still conform to it.
262      * Can throw a RuntimeException if trying to update a relation type
263      * declared in the Relation Service.
264      *
265      * @param theRoleInfo role info to be added.
266      *
267      * @exception IllegalArgumentException if null parameter.
268      * @exception InvalidRelationTypeException if there is already a role
269      * info in current relation type with the same name.
270      */

271     protected void addRoleInfo(RoleInfo JavaDoc theRoleInfo)
272     throws IllegalArgumentException JavaDoc,
273            InvalidRelationTypeException JavaDoc {
274
275     if (theRoleInfo == null) {
276         // Revisit [cebro] Localize message
277
String JavaDoc excMsg = "Invalid parameter.";
278         throw new IllegalArgumentException JavaDoc(excMsg);
279     }
280
281     if (isDebugOn())
282         debug("addRoleInfo: entering", theRoleInfo.toString());
283
284     if (isInRelationService) {
285         // Trying to update a declared relation type
286
// Revisit [cebro] Localize message
287
String JavaDoc excMsg = "Relation type cannot be updated as it is declared in the Relation Service.";
288         throw new RuntimeException JavaDoc(excMsg);
289     }
290
291     String JavaDoc roleName = theRoleInfo.getName();
292
293     // Checks if the role info has already been described
294
if (roleName2InfoMap.containsKey(roleName)) {
295         StringBuffer JavaDoc excMsgStrB = new StringBuffer JavaDoc();
296         // Revisit [cebro] Localize message
297
String JavaDoc excMsg = "Two role infos provided for role ";
298         excMsgStrB.append(excMsg);
299         excMsgStrB.append(roleName);
300         throw new InvalidRelationTypeException JavaDoc(excMsgStrB.toString());
301     }
302
303     roleName2InfoMap.put(roleName,
304                    new RoleInfo JavaDoc(theRoleInfo));
305
306     if (isDebugOn())
307         debug("addRoleInfo: exiting", null);
308     return;
309     }
310
311     // Sets the internal flag to specify that the relation type has been
312
// declared in the Relation Service
313
void setRelationServiceFlag(boolean theFlg) {
314     isInRelationService = theFlg;
315     return;
316     }
317
318     // Initializes the members, i.e. type name and role info list.
319
//
320
// -param theRelTypeName Name of relation type
321
// -param theRoleInfoArray List of role definitions (RoleInfo objects)
322
//
323
// -exception IllegalArgumentException if null parameter
324
// -exception InvalidRelationTypeException If:
325
// - the same name has been used for two different roles
326
// - no role info provided
327
// - one null role info provided
328
private void initMembers(String JavaDoc theRelTypeName,
329                  RoleInfo JavaDoc[] theRoleInfoArray)
330     throws IllegalArgumentException JavaDoc,
331            InvalidRelationTypeException JavaDoc {
332
333     if (theRelTypeName == null || theRoleInfoArray == null) {
334         // Revisit [cebro] Localize message
335
String JavaDoc excMsg = "Invalid parameter.";
336         throw new IllegalArgumentException JavaDoc(excMsg);
337     }
338
339     if (isDebugOn())
340         debug("initMembers: entering", theRelTypeName);
341
342     typeName = theRelTypeName;
343
344     // Verifies role infos before setting them
345
// Can throw InvalidRelationTypeException
346
checkRoleInfos(theRoleInfoArray);
347
348     for (int i = 0; i < theRoleInfoArray.length; i++) {
349         RoleInfo JavaDoc currRoleInfo = theRoleInfoArray[i];
350         roleName2InfoMap.put(new String JavaDoc(currRoleInfo.getName()),
351                    new RoleInfo JavaDoc(currRoleInfo));
352     }
353
354     if (isDebugOn())
355         debug("initMembers: exiting", null);
356     return;
357     }
358
359     // Checks the given RoleInfo array to verify that:
360
// - the array is not empty
361
// - it does not contain a null element
362
// - a given role name is used only for one RoleInfo
363
//
364
// -param theRoleInfoArray array to be checked
365
//
366
// -exception IllegalArgumentException
367
// -exception InvalidRelationTypeException If:
368
// - the same name has been used for two different roles
369
// - no role info provided
370
// - one null role info provided
371
static void checkRoleInfos(RoleInfo JavaDoc[] theRoleInfoArray)
372     throws IllegalArgumentException JavaDoc,
373            InvalidRelationTypeException JavaDoc {
374
375     if (theRoleInfoArray == null) {
376         // Revisit [cebro] Localize message
377
String JavaDoc excMsg = "Invalid parameter.";
378         throw new IllegalArgumentException JavaDoc(excMsg);
379     }
380
381     if (theRoleInfoArray.length == 0) {
382         // No role info provided
383
// Revisit [cebro] Localize message
384
String JavaDoc excMsg = "No role info provided.";
385         throw new InvalidRelationTypeException JavaDoc(excMsg);
386     }
387
388
389     ArrayList JavaDoc roleNameList = new ArrayList JavaDoc();
390
391     for (int i = 0; i < theRoleInfoArray.length; i++) {
392         RoleInfo JavaDoc currRoleInfo = theRoleInfoArray[i];
393
394         if (currRoleInfo == null) {
395         // Revisit [cebro] Localize message
396
String JavaDoc excMsg = "Null role info provided.";
397         throw new InvalidRelationTypeException JavaDoc(excMsg);
398         }
399
400         String JavaDoc roleName = currRoleInfo.getName();
401
402         // Checks if the role info has already been described
403
if (roleNameList.contains(roleName)) {
404         StringBuffer JavaDoc excMsgStrB = new StringBuffer JavaDoc();
405         // Revisit [cebro] Localize message
406
String JavaDoc excMsg = "Two role infos provided for role ";
407         excMsgStrB.append(excMsg);
408         excMsgStrB.append(roleName);
409         throw new InvalidRelationTypeException JavaDoc(excMsgStrB.toString());
410         }
411         roleNameList.add(roleName);
412     }
413
414     return;
415     }
416
417     // stuff for Tracing
418

419     private static String JavaDoc localClassName = "RelationTypeSupport";
420
421     // trace level
422
private boolean isTraceOn() {
423         return Trace.isSelected(Trace.LEVEL_TRACE, Trace.INFO_RELATION);
424     }
425
426 // private void trace(String className, String methodName, String info) {
427
// Trace.send(Trace.LEVEL_TRACE, Trace.INFO_RELATION, className, methodName, info);
428
// }
429

430     private void trace(String JavaDoc methodName, String JavaDoc info) {
431         Trace.send(Trace.LEVEL_TRACE, Trace.INFO_RELATION, localClassName, methodName, info);
432     Trace.send(Trace.LEVEL_TRACE, Trace.INFO_RELATION, "", "", "\n");
433     }
434
435 // private void trace(String className, String methodName, Exception e) {
436
// Trace.send(Trace.LEVEL_TRACE, Trace.INFO_RELATION, className, methodName, e);
437
// }
438

439 // private void trace(String methodName, Exception e) {
440
// Trace.send(Trace.LEVEL_TRACE, Trace.INFO_RELATION, localClassName, methodName, e);
441
// }
442

443     // debug level
444
private boolean isDebugOn() {
445         return Trace.isSelected(Trace.LEVEL_DEBUG, Trace.INFO_RELATION);
446     }
447
448 // private void debug(String className, String methodName, String info) {
449
// Trace.send(Trace.LEVEL_DEBUG, Trace.INFO_RELATION, className, methodName, info);
450
// }
451

452     private void debug(String JavaDoc methodName, String JavaDoc info) {
453         Trace.send(Trace.LEVEL_DEBUG, Trace.INFO_RELATION, localClassName, methodName, info);
454     Trace.send(Trace.LEVEL_DEBUG, Trace.INFO_RELATION, "", "", "\n");
455     }
456
457 // private void debug(String className, String methodName, Exception e) {
458
// Trace.send(Trace.LEVEL_DEBUG, Trace.INFO_RELATION, className, methodName, e);
459
// }
460

461 // private void debug(String methodName, Exception e) {
462
// Trace.send(Trace.LEVEL_DEBUG, Trace.INFO_RELATION, localClassName, methodName, e);
463
// }
464

465     /**
466      * Deserializes a {@link RelationTypeSupport} from an {@link ObjectInputStream}.
467      */

468     private void readObject(ObjectInputStream JavaDoc in)
469         throws IOException JavaDoc, ClassNotFoundException JavaDoc {
470       if (compat)
471       {
472         // Read an object serialized in the old serial form
473
//
474
ObjectInputStream.GetField JavaDoc fields = in.readFields();
475     typeName = (String JavaDoc) fields.get("myTypeName", null);
476     if (fields.defaulted("myTypeName"))
477         {
478           throw new NullPointerException JavaDoc("myTypeName");
479         }
480     roleName2InfoMap = (Map JavaDoc) fields.get("myRoleName2InfoMap", null);
481     if (fields.defaulted("myRoleName2InfoMap"))
482         {
483           throw new NullPointerException JavaDoc("myRoleName2InfoMap");
484         }
485     isInRelationService = fields.get("myIsInRelServFlg", false);
486     if (fields.defaulted("myIsInRelServFlg"))
487         {
488           throw new NullPointerException JavaDoc("myIsInRelServFlg");
489         }
490       }
491       else
492       {
493         // Read an object serialized in the new serial form
494
//
495
in.defaultReadObject();
496       }
497     }
498
499
500     /**
501      * Serializes a {@link RelationTypeSupport} to an {@link ObjectOutputStream}.
502      */

503     private void writeObject(ObjectOutputStream JavaDoc out)
504         throws IOException JavaDoc {
505       if (compat)
506       {
507         // Serializes this instance in the old serial form
508
//
509
ObjectOutputStream.PutField JavaDoc fields = out.putFields();
510     fields.put("myTypeName", typeName);
511     fields.put("myRoleName2InfoMap", (HashMap JavaDoc)roleName2InfoMap);
512     fields.put("myIsInRelServFlg", isInRelationService);
513     out.writeFields();
514       }
515       else
516       {
517         // Serializes this instance in the new serial form
518
//
519
out.defaultWriteObject();
520       }
521     }
522 }
523
Popular Tags