KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * @(#)RoleUnresolved.java 1.26 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 an unresolved role: a role not retrieved from a relation due
29  * to a problem. It provides the role name, value (if problem when trying to
30  * set the role) and an integer defining the problem (constants defined in
31  * RoleStatus).
32  *
33  * @since 1.5
34  */

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

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

92     //
93
// Private members
94
//
95

96     /**
97      * @serial Role name
98      */

99     private String JavaDoc roleName = null;
100
101     /**
102      * @serial Role value ({@link List} of {@link ObjectName} objects)
103      */

104     private List JavaDoc roleValue = null;
105
106     /**
107      * @serial Problem type
108      */

109     private int problemType;
110
111     //
112
// Constructor
113
//
114

115     /**
116      * Constructor.
117      *
118      * @param theRoleName name of the role
119      * @param theRoleValue value of the role (if problem when setting the
120      * role)
121      * @param thePbType type of problem (according to known problem types,
122      * listed as static final members).
123      *
124      * @exception IllegalArgumentException if null parameter or incorrect
125      * problem type
126      */

127     public RoleUnresolved(String JavaDoc theRoleName,
128               List JavaDoc theRoleValue,
129               int thePbType)
130     throws IllegalArgumentException JavaDoc {
131
132     if (theRoleName == null) {
133         // Revisit [cebro] Localize message
134
String JavaDoc excMsg = "Invalid parameter.";
135         throw new IllegalArgumentException JavaDoc(excMsg);
136     }
137
138     setRoleName(theRoleName);
139     setRoleValue(theRoleValue);
140     // Can throw IllegalArgumentException
141
setProblemType(thePbType);
142     return;
143     }
144
145     //
146
// Accessors
147
//
148

149     /**
150      * Retrieves role name.
151      *
152      * @return the role name.
153      *
154      * @see #setRoleName
155      */

156     public String JavaDoc getRoleName() {
157     return roleName;
158     }
159
160     /**
161      * Retrieves role value.
162      *
163      * @return an ArrayList of ObjectName objects, the one provided to be set
164      * in given role. Null if the unresolved role is returned for a read
165      * access.
166      *
167      * @see #setRoleValue
168      */

169     public List JavaDoc getRoleValue() {
170     return roleValue;
171     }
172
173     /**
174      * Retrieves problem type.
175      *
176      * @return an integer corresponding to a problem, those being described as
177      * static final members of current class.
178      *
179      * @see #setProblemType
180      */

181     public int getProblemType() {
182     return problemType;
183     }
184
185     /**
186      * Sets role name.
187      *
188      * @param theRoleName the new role name.
189      *
190      * @exception IllegalArgumentException if null parameter
191      *
192      * @see #getRoleName
193      */

194     public void setRoleName(String JavaDoc theRoleName)
195     throws IllegalArgumentException JavaDoc {
196
197     if (theRoleName == null) {
198         // Revisit [cebro] Localize message
199
String JavaDoc excMsg = "Invalid parameter.";
200         throw new IllegalArgumentException JavaDoc(excMsg);
201     }
202
203     roleName = theRoleName;
204     return;
205     }
206
207     /**
208      * Sets role value.
209      *
210      * @param theRoleValue List of ObjectName objects for referenced
211      * MBeans not set in role.
212      *
213      * @see #getRoleValue
214      */

215     public void setRoleValue(List JavaDoc theRoleValue) {
216
217     if (theRoleValue != null) {
218         roleValue = new ArrayList JavaDoc(theRoleValue);
219     } else {
220         roleValue = null;
221     }
222     return;
223     }
224
225     /**
226      * Sets problem type.
227      *
228      * @param thePbType integer corresponding to a problem. Must be one of
229      * those described as static final members of current class.
230      *
231      * @exception IllegalArgumentException if incorrect problem type
232      *
233      * @see #getProblemType
234      */

235     public void setProblemType(int thePbType)
236     throws IllegalArgumentException JavaDoc {
237
238     if (!(RoleStatus.isRoleStatus(thePbType))) {
239         // Revisit [cebro] Localize message
240
String JavaDoc excMsg = "Incorrect problem type.";
241         throw new IllegalArgumentException JavaDoc(excMsg);
242     }
243     problemType = thePbType;
244     return;
245     }
246
247     /**
248      * Clone this object.
249      *
250      * @return an independent clone.
251      */

252     public Object JavaDoc clone() {
253     try {
254         return new RoleUnresolved JavaDoc(roleName, roleValue, problemType);
255     } catch (IllegalArgumentException JavaDoc exc) {
256         return null; // :)
257
}
258     }
259
260     /**
261      * Return a string describing this object.
262      *
263      * @return a description of this RoleUnresolved object.
264      */

265     public String JavaDoc toString() {
266     StringBuffer JavaDoc result = new StringBuffer JavaDoc();
267     result.append("role name: " + roleName);
268     if (roleValue != null) {
269         result.append("; value: ");
270         for (Iterator JavaDoc objNameIter = roleValue.iterator();
271          objNameIter.hasNext();) {
272         ObjectName JavaDoc currObjName = (ObjectName JavaDoc)(objNameIter.next());
273         result.append(currObjName.toString());
274         if (objNameIter.hasNext()) {
275             result.append(", ");
276         }
277         }
278     }
279     result.append("; problem type: " + problemType);
280     return result.toString();
281     }
282
283     /**
284      * Deserializes a {@link RoleUnresolved} from an {@link ObjectInputStream}.
285      */

286     private void readObject(ObjectInputStream JavaDoc in)
287         throws IOException JavaDoc, ClassNotFoundException JavaDoc {
288       if (compat)
289       {
290         // Read an object serialized in the old serial form
291
//
292
ObjectInputStream.GetField JavaDoc fields = in.readFields();
293     roleName = (String JavaDoc) fields.get("myRoleName", null);
294     if (fields.defaulted("myRoleName"))
295         {
296           throw new NullPointerException JavaDoc("myRoleName");
297         }
298     roleValue = (List JavaDoc) fields.get("myRoleValue", null);
299     if (fields.defaulted("myRoleValue"))
300         {
301           throw new NullPointerException JavaDoc("myRoleValue");
302         }
303     problemType = fields.get("myPbType", (int)0);
304     if (fields.defaulted("myPbType"))
305         {
306           throw new NullPointerException JavaDoc("myPbType");
307         }
308       }
309       else
310       {
311         // Read an object serialized in the new serial form
312
//
313
in.defaultReadObject();
314       }
315     }
316
317
318     /**
319      * Serializes a {@link RoleUnresolved} to an {@link ObjectOutputStream}.
320      */

321     private void writeObject(ObjectOutputStream JavaDoc out)
322         throws IOException JavaDoc {
323       if (compat)
324       {
325         // Serializes this instance in the old serial form
326
//
327
ObjectOutputStream.PutField JavaDoc fields = out.putFields();
328     fields.put("myRoleName", roleName);
329     fields.put("myRoleValue", (ArrayList JavaDoc)roleValue);
330     fields.put("myPbType", problemType);
331     out.writeFields();
332       }
333       else
334       {
335         // Serializes this instance in the new serial form
336
//
337
out.defaultWriteObject();
338       }
339     }
340 }
341
Popular Tags