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. package javax.management.relation;
  8. import javax.management.ObjectName;
  9. import java.util.ArrayList;
  10. import java.util.List;
  11. import java.util.Iterator;
  12. import java.security.AccessController;
  13. import java.security.PrivilegedAction;
  14. import java.io.IOException;
  15. import java.io.ObjectInputStream;
  16. import java.io.ObjectOutputStream;
  17. import java.io.ObjectStreamField;
  18. import java.io.Serializable;
  19. import com.sun.jmx.mbeanserver.GetPropertyAction;
  20. /**
  21. * Represents an unresolved role: a role not retrieved from a relation due
  22. * to a problem. It provides the role name, value (if problem when trying to
  23. * set the role) and an integer defining the problem (constants defined in
  24. * RoleStatus).
  25. *
  26. * @since 1.5
  27. */
  28. public class RoleUnresolved implements Serializable {
  29. // Serialization compatibility stuff:
  30. // Two serial forms are supported in this class. The selected form depends
  31. // on system property "jmx.serial.form":
  32. // - "1.0" for JMX 1.0
  33. // - any other value for JMX 1.1 and higher
  34. //
  35. // Serial version for old serial form
  36. private static final long oldSerialVersionUID = -9026457686611660144L;
  37. //
  38. // Serial version for new serial form
  39. private static final long newSerialVersionUID = -48350262537070138L;
  40. //
  41. // Serializable fields in old serial form
  42. private static final ObjectStreamField[] oldSerialPersistentFields =
  43. {
  44. new ObjectStreamField("myRoleName", String.class),
  45. new ObjectStreamField("myRoleValue", ArrayList.class),
  46. new ObjectStreamField("myPbType", int.class)
  47. };
  48. //
  49. // Serializable fields in new serial form
  50. private static final ObjectStreamField[] newSerialPersistentFields =
  51. {
  52. new ObjectStreamField("roleName", String.class),
  53. new ObjectStreamField("roleValue", List.class),
  54. new ObjectStreamField("problemType", int.class)
  55. };
  56. //
  57. // Actual serial version and serial form
  58. private static final long serialVersionUID;
  59. /** @serialField roleName String Role name
  60. * @serialField roleValue List Role value ({@link List} of {@link ObjectName} objects)
  61. * @serialField problemType int Problem type
  62. */
  63. private static final ObjectStreamField[] serialPersistentFields;
  64. private static boolean compat = false;
  65. static {
  66. try {
  67. PrivilegedAction act = new GetPropertyAction("jmx.serial.form");
  68. String form = (String) AccessController.doPrivileged(act);
  69. compat = (form != null && form.equals("1.0"));
  70. } catch (Exception e) {
  71. // OK : Too bad, no compat with 1.0
  72. }
  73. if (compat) {
  74. serialPersistentFields = oldSerialPersistentFields;
  75. serialVersionUID = oldSerialVersionUID;
  76. } else {
  77. serialPersistentFields = newSerialPersistentFields;
  78. serialVersionUID = newSerialVersionUID;
  79. }
  80. }
  81. //
  82. // END Serialization compatibility stuff
  83. //
  84. // Private members
  85. //
  86. /**
  87. * @serial Role name
  88. */
  89. private String roleName = null;
  90. /**
  91. * @serial Role value ({@link List} of {@link ObjectName} objects)
  92. */
  93. private List roleValue = null;
  94. /**
  95. * @serial Problem type
  96. */
  97. private int problemType;
  98. //
  99. // Constructor
  100. //
  101. /**
  102. * Constructor.
  103. *
  104. * @param theRoleName name of the role
  105. * @param theRoleValue value of the role (if problem when setting the
  106. * role)
  107. * @param thePbType type of problem (according to known problem types,
  108. * listed as static final members).
  109. *
  110. * @exception IllegalArgumentException if null parameter or incorrect
  111. * problem type
  112. */
  113. public RoleUnresolved(String theRoleName,
  114. List theRoleValue,
  115. int thePbType)
  116. throws IllegalArgumentException {
  117. if (theRoleName == null) {
  118. // Revisit [cebro] Localize message
  119. String excMsg = "Invalid parameter.";
  120. throw new IllegalArgumentException(excMsg);
  121. }
  122. setRoleName(theRoleName);
  123. setRoleValue(theRoleValue);
  124. // Can throw IllegalArgumentException
  125. setProblemType(thePbType);
  126. return;
  127. }
  128. //
  129. // Accessors
  130. //
  131. /**
  132. * Retrieves role name.
  133. *
  134. * @return the role name.
  135. *
  136. * @see #setRoleName
  137. */
  138. public String getRoleName() {
  139. return roleName;
  140. }
  141. /**
  142. * Retrieves role value.
  143. *
  144. * @return an ArrayList of ObjectName objects, the one provided to be set
  145. * in given role. Null if the unresolved role is returned for a read
  146. * access.
  147. *
  148. * @see #setRoleValue
  149. */
  150. public List getRoleValue() {
  151. return roleValue;
  152. }
  153. /**
  154. * Retrieves problem type.
  155. *
  156. * @return an integer corresponding to a problem, those being described as
  157. * static final members of current class.
  158. *
  159. * @see #setProblemType
  160. */
  161. public int getProblemType() {
  162. return problemType;
  163. }
  164. /**
  165. * Sets role name.
  166. *
  167. * @param theRoleName the new role name.
  168. *
  169. * @exception IllegalArgumentException if null parameter
  170. *
  171. * @see #getRoleName
  172. */
  173. public void setRoleName(String theRoleName)
  174. throws IllegalArgumentException {
  175. if (theRoleName == null) {
  176. // Revisit [cebro] Localize message
  177. String excMsg = "Invalid parameter.";
  178. throw new IllegalArgumentException(excMsg);
  179. }
  180. roleName = theRoleName;
  181. return;
  182. }
  183. /**
  184. * Sets role value.
  185. *
  186. * @param theRoleValue List of ObjectName objects for referenced
  187. * MBeans not set in role.
  188. *
  189. * @see #getRoleValue
  190. */
  191. public void setRoleValue(List theRoleValue) {
  192. if (theRoleValue != null) {
  193. roleValue = new ArrayList(theRoleValue);
  194. } else {
  195. roleValue = null;
  196. }
  197. return;
  198. }
  199. /**
  200. * Sets problem type.
  201. *
  202. * @param thePbType integer corresponding to a problem. Must be one of
  203. * those described as static final members of current class.
  204. *
  205. * @exception IllegalArgumentException if incorrect problem type
  206. *
  207. * @see #getProblemType
  208. */
  209. public void setProblemType(int thePbType)
  210. throws IllegalArgumentException {
  211. if (!(RoleStatus.isRoleStatus(thePbType))) {
  212. // Revisit [cebro] Localize message
  213. String excMsg = "Incorrect problem type.";
  214. throw new IllegalArgumentException(excMsg);
  215. }
  216. problemType = thePbType;
  217. return;
  218. }
  219. /**
  220. * Clone this object.
  221. *
  222. * @return an independent clone.
  223. */
  224. public Object clone() {
  225. try {
  226. return new RoleUnresolved(roleName, roleValue, problemType);
  227. } catch (IllegalArgumentException exc) {
  228. return null; // :)
  229. }
  230. }
  231. /**
  232. * Return a string describing this object.
  233. *
  234. * @return a description of this RoleUnresolved object.
  235. */
  236. public String toString() {
  237. StringBuffer result = new StringBuffer();
  238. result.append("role name: " + roleName);
  239. if (roleValue != null) {
  240. result.append("; value: ");
  241. for (Iterator objNameIter = roleValue.iterator();
  242. objNameIter.hasNext();) {
  243. ObjectName currObjName = (ObjectName)(objNameIter.next());
  244. result.append(currObjName.toString());
  245. if (objNameIter.hasNext()) {
  246. result.append(", ");
  247. }
  248. }
  249. }
  250. result.append("; problem type: " + problemType);
  251. return result.toString();
  252. }
  253. /**
  254. * Deserializes a {@link RoleUnresolved} from an {@link ObjectInputStream}.
  255. */
  256. private void readObject(ObjectInputStream in)
  257. throws IOException, ClassNotFoundException {
  258. if (compat)
  259. {
  260. // Read an object serialized in the old serial form
  261. //
  262. ObjectInputStream.GetField fields = in.readFields();
  263. roleName = (String) fields.get("myRoleName", null);
  264. if (fields.defaulted("myRoleName"))
  265. {
  266. throw new NullPointerException("myRoleName");
  267. }
  268. roleValue = (List) fields.get("myRoleValue", null);
  269. if (fields.defaulted("myRoleValue"))
  270. {
  271. throw new NullPointerException("myRoleValue");
  272. }
  273. problemType = fields.get("myPbType", (int)0);
  274. if (fields.defaulted("myPbType"))
  275. {
  276. throw new NullPointerException("myPbType");
  277. }
  278. }
  279. else
  280. {
  281. // Read an object serialized in the new serial form
  282. //
  283. in.defaultReadObject();
  284. }
  285. }
  286. /**
  287. * Serializes a {@link RoleUnresolved} to an {@link ObjectOutputStream}.
  288. */
  289. private void writeObject(ObjectOutputStream out)
  290. throws IOException {
  291. if (compat)
  292. {
  293. // Serializes this instance in the old serial form
  294. //
  295. ObjectOutputStream.PutField fields = out.putFields();
  296. fields.put("myRoleName", roleName);
  297. fields.put("myRoleValue", (ArrayList)roleValue);
  298. fields.put("myPbType", problemType);
  299. out.writeFields();
  300. }
  301. else
  302. {
  303. // Serializes this instance in the new serial form
  304. //
  305. out.defaultWriteObject();
  306. }
  307. }
  308. }