KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * @(#)Role.java 1.32 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 javax.management.ObjectName JavaDoc;
11
12 import java.util.ArrayList JavaDoc;
13 import java.util.List JavaDoc;
14 import java.util.Iterator JavaDoc;
15
16 import java.security.AccessController JavaDoc;
17 import java.security.PrivilegedAction JavaDoc;
18
19 import java.io.IOException JavaDoc;
20 import java.io.ObjectInputStream JavaDoc;
21 import java.io.ObjectOutputStream JavaDoc;
22 import java.io.ObjectStreamField JavaDoc;
23 import java.io.Serializable JavaDoc;
24
25 import com.sun.jmx.mbeanserver.GetPropertyAction;
26
27 /**
28  * Represents a role: includes a role name and referenced MBeans (via their
29  * ObjectNames). The role value is always represented as an ArrayList
30  * collection (of ObjectNames) to homogenize the access.
31  *
32  * @since 1.5
33  */

34 public class Role implements Serializable JavaDoc {
35
36     // Serialization compatibility stuff:
37
// Two serial forms are supported in this class. The selected form depends
38
// on system property "jmx.serial.form":
39
// - "1.0" for JMX 1.0
40
// - any other value for JMX 1.1 and higher
41
//
42
// Serial version for old serial form
43
private static final long oldSerialVersionUID = -1959486389343113026L;
44     //
45
// Serial version for new serial form
46
private static final long newSerialVersionUID = -279985518429862552L;
47     //
48
// Serializable fields in old serial form
49
private static final ObjectStreamField JavaDoc[] oldSerialPersistentFields =
50     {
51       new ObjectStreamField JavaDoc("myName", String JavaDoc.class),
52       new ObjectStreamField JavaDoc("myObjNameList", ArrayList JavaDoc.class)
53     };
54     //
55
// Serializable fields in new serial form
56
private static final ObjectStreamField JavaDoc[] newSerialPersistentFields =
57     {
58       new ObjectStreamField JavaDoc("name", String JavaDoc.class),
59       new ObjectStreamField JavaDoc("objectNameList", List JavaDoc.class)
60     };
61     //
62
// Actual serial version and serial form
63
private static final long serialVersionUID;
64     /**
65      * @serialField name String Role name
66      * @serialField objectNameList List {@link List} of {@link ObjectName}s of referenced MBeans
67      */

68     private static final ObjectStreamField JavaDoc[] serialPersistentFields;
69     private static boolean compat = false;
70     static {
71     try {
72         PrivilegedAction JavaDoc act = new GetPropertyAction("jmx.serial.form");
73         String JavaDoc form = (String JavaDoc) AccessController.doPrivileged(act);
74         compat = (form != null && form.equals("1.0"));
75     } catch (Exception JavaDoc e) {
76         // OK : Too bad, no compat with 1.0
77
}
78     if (compat) {
79         serialPersistentFields = oldSerialPersistentFields;
80         serialVersionUID = oldSerialVersionUID;
81     } else {
82         serialPersistentFields = newSerialPersistentFields;
83         serialVersionUID = newSerialVersionUID;
84     }
85     }
86     //
87
// END Serialization compatibility stuff
88

89     //
90
// Private members
91
//
92

93     /**
94      * @serial Role name
95      */

96     private String JavaDoc name = null;
97
98     /**
99      * @serial {@link List} of {@link ObjectName}s of referenced MBeans
100      */

101     private List JavaDoc objectNameList = new ArrayList JavaDoc();
102
103     //
104
// Constructors
105
//
106

107     /**
108      * <p>Make a new Role object.
109      * No check is made that the ObjectNames in the role value exist in
110      * an MBean server. That check will be made when the role is set
111      * in a relation.
112      *
113      * @param theRoleName role name
114      * @param theRoleValue role value (List of ObjectName objects)
115      *
116      * @exception IllegalArgumentException if null parameter
117      */

118     public Role(String JavaDoc theRoleName,
119         List JavaDoc theRoleValue)
120     throws IllegalArgumentException JavaDoc {
121
122     if (theRoleName == null || theRoleValue == null) {
123         String JavaDoc excMsg = "Invalid parameter";
124         throw new IllegalArgumentException JavaDoc(excMsg);
125     }
126
127     setRoleName(theRoleName);
128     setRoleValue(theRoleValue);
129
130     return;
131     }
132
133     //
134
// Accessors
135
//
136

137     /**
138      * Retrieves role name.
139      *
140      * @return the role name.
141      *
142      * @see #setRoleName
143      */

144     public String JavaDoc getRoleName() {
145     return name;
146     }
147
148     /**
149      * Retrieves role value.
150      *
151      * @return ArrayList of ObjectName objects for referenced MBeans.
152      *
153      * @see #setRoleValue
154      */

155     public List JavaDoc getRoleValue() {
156     return objectNameList;
157     }
158
159     /**
160      * Sets role name.
161      *
162      * @param theRoleName role name
163      *
164      * @exception IllegalArgumentException if null parameter
165      *
166      * @see #getRoleName
167      */

168     public void setRoleName(String JavaDoc theRoleName)
169     throws IllegalArgumentException JavaDoc {
170
171     if (theRoleName == null) {
172         // Revisit [cebro] Localize message
173
String JavaDoc excMsg = "Invalid parameter.";
174         throw new IllegalArgumentException JavaDoc(excMsg);
175     }
176
177     name = theRoleName;
178     return;
179     }
180
181     /**
182      * Sets role value.
183      *
184      * @param theRoleValue List of ObjectName objects for referenced
185      * MBeans.
186      *
187      * @exception IllegalArgumentException if null parameter
188      *
189      * @see #getRoleValue
190      */

191     public void setRoleValue(List JavaDoc theRoleValue)
192     throws IllegalArgumentException JavaDoc {
193
194     if (theRoleValue == null) {
195         // Revisit [cebro] Localize message
196
String JavaDoc excMsg = "Invalid parameter.";
197         throw new IllegalArgumentException JavaDoc(excMsg);
198     }
199
200     objectNameList = new ArrayList JavaDoc(theRoleValue);
201     return;
202     }
203
204     /**
205      * Returns a string describing the role.
206      *
207      * @return the description of the role.
208      */

209     public String JavaDoc toString() {
210     StringBuffer JavaDoc result = new StringBuffer JavaDoc();
211     result.append("role name: " + name + "; role value: ");
212     for (Iterator JavaDoc objNameIter = objectNameList.iterator();
213          objNameIter.hasNext();) {
214         ObjectName JavaDoc currObjName = (ObjectName JavaDoc)(objNameIter.next());
215         result.append(currObjName.toString());
216         if (objNameIter.hasNext()) {
217         result.append(", ");
218         }
219     }
220     return result.toString();
221     }
222
223     //
224
// Misc
225
//
226

227     /**
228      * Clone the role object.
229      *
230      * @return a Role that is an independent copy of the current Role object.
231      */

232     public Object JavaDoc clone() {
233
234     try {
235         return new Role JavaDoc(name, objectNameList);
236     } catch (IllegalArgumentException JavaDoc exc) {
237         return null; // can't happen
238
}
239     }
240
241     /**
242      * Returns a string for the given role value.
243      *
244      * @param theRoleValue List of ObjectName objects
245      *
246      * @return A String consisting of the ObjectNames separated by
247      * newlines (\n).
248      *
249      * @exception IllegalArgumentException if null parameter
250      */

251     public static String JavaDoc roleValueToString(List JavaDoc theRoleValue)
252     throws IllegalArgumentException JavaDoc {
253
254     if (theRoleValue == null) {
255         String JavaDoc excMsg = "Invalid parameter";
256         throw new IllegalArgumentException JavaDoc(excMsg);
257     }
258
259     StringBuffer JavaDoc result = new StringBuffer JavaDoc();
260     for (Iterator JavaDoc objNameIter = theRoleValue.iterator();
261          objNameIter.hasNext();) {
262         ObjectName JavaDoc currObjName = (ObjectName JavaDoc)(objNameIter.next());
263         result.append(currObjName.toString());
264         if (objNameIter.hasNext()) {
265         result.append("\n");
266         }
267     }
268     return result.toString();
269     }
270
271     /**
272      * Deserializes a {@link Role} from an {@link ObjectInputStream}.
273      */

274     private void readObject(ObjectInputStream JavaDoc in)
275         throws IOException JavaDoc, ClassNotFoundException JavaDoc {
276       if (compat)
277       {
278         // Read an object serialized in the old serial form
279
//
280
ObjectInputStream.GetField JavaDoc fields = in.readFields();
281     name = (String JavaDoc) fields.get("myName", null);
282     if (fields.defaulted("myName"))
283         {
284           throw new NullPointerException JavaDoc("myName");
285         }
286     objectNameList = (List JavaDoc) fields.get("myObjNameList", null);
287     if (fields.defaulted("myObjNameList"))
288         {
289           throw new NullPointerException JavaDoc("myObjNameList");
290         }
291       }
292       else
293       {
294         // Read an object serialized in the new serial form
295
//
296
in.defaultReadObject();
297       }
298     }
299
300
301     /**
302      * Serializes a {@link Role} to an {@link ObjectOutputStream}.
303      */

304     private void writeObject(ObjectOutputStream JavaDoc out)
305         throws IOException JavaDoc {
306       if (compat)
307       {
308         // Serializes this instance in the old serial form
309
//
310
ObjectOutputStream.PutField JavaDoc fields = out.putFields();
311     fields.put("myName", name);
312     fields.put("myObjNameList", (ArrayList JavaDoc)objectNameList);
313     out.writeFields();
314       }
315       else
316       {
317         // Serializes this instance in the new serial form
318
//
319
out.defaultWriteObject();
320       }
321     }
322 }
323
Popular Tags