1. /*
  2. * @(#)RoleResult.java 1.23 04/02/10
  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 java.util.Iterator;
  9. import java.io.IOException;
  10. import java.io.ObjectInputStream;
  11. import java.io.ObjectOutputStream;
  12. import java.io.ObjectStreamField;
  13. import java.io.Serializable;
  14. import java.security.AccessController;
  15. import java.security.PrivilegedAction;
  16. import com.sun.jmx.mbeanserver.GetPropertyAction;
  17. /**
  18. * Represents the result of a multiple access to several roles of a relation
  19. * (either for reading or writing).
  20. *
  21. * @since 1.5
  22. */
  23. public class RoleResult implements Serializable {
  24. // Serialization compatibility stuff:
  25. // Two serial forms are supported in this class. The selected form depends
  26. // on system property "jmx.serial.form":
  27. // - "1.0" for JMX 1.0
  28. // - any other value for JMX 1.1 and higher
  29. //
  30. // Serial version for old serial form
  31. private static final long oldSerialVersionUID = 3786616013762091099L;
  32. //
  33. // Serial version for new serial form
  34. private static final long newSerialVersionUID = -6304063118040985512L;
  35. //
  36. // Serializable fields in old serial form
  37. private static final ObjectStreamField[] oldSerialPersistentFields =
  38. {
  39. new ObjectStreamField("myRoleList", RoleList.class),
  40. new ObjectStreamField("myRoleUnresList", RoleUnresolvedList.class)
  41. };
  42. //
  43. // Serializable fields in new serial form
  44. private static final ObjectStreamField[] newSerialPersistentFields =
  45. {
  46. new ObjectStreamField("roleList", RoleList.class),
  47. new ObjectStreamField("unresolvedRoleList", RoleUnresolvedList.class)
  48. };
  49. //
  50. // Actual serial version and serial form
  51. private static final long serialVersionUID;
  52. /**
  53. * @serialField roleList RoleList List of roles successfully accessed
  54. * @serialField unresolvedRoleList RoleUnresolvedList List of roles unsuccessfully accessed
  55. */
  56. private static final ObjectStreamField[] serialPersistentFields;
  57. private static boolean compat = false;
  58. static {
  59. try {
  60. PrivilegedAction act = new GetPropertyAction("jmx.serial.form");
  61. String form = (String) AccessController.doPrivileged(act);
  62. compat = (form != null && form.equals("1.0"));
  63. } catch (Exception e) {
  64. // OK : Too bad, no compat with 1.0
  65. }
  66. if (compat) {
  67. serialPersistentFields = oldSerialPersistentFields;
  68. serialVersionUID = oldSerialVersionUID;
  69. } else {
  70. serialPersistentFields = newSerialPersistentFields;
  71. serialVersionUID = newSerialVersionUID;
  72. }
  73. }
  74. //
  75. // END Serialization compatibility stuff
  76. //
  77. // Private members
  78. //
  79. /**
  80. * @serial List of roles successfully accessed
  81. */
  82. private RoleList roleList = null;
  83. /**
  84. * @serial List of roles unsuccessfully accessed
  85. */
  86. private RoleUnresolvedList unresolvedRoleList = null;
  87. //
  88. // Constructor
  89. //
  90. /**
  91. * Constructor.
  92. *
  93. * @param theRoleList list of roles successfully accessed.
  94. * @param theRoleUnresList list of roles not accessed (with problem
  95. * descriptions).
  96. */
  97. public RoleResult(RoleList theRoleList,
  98. RoleUnresolvedList theRoleUnresList) {
  99. setRoles(theRoleList);
  100. setRolesUnresolved(theRoleUnresList);
  101. return;
  102. }
  103. //
  104. // Accessors
  105. //
  106. /**
  107. * Retrieves list of roles successfully accessed.
  108. *
  109. * @return a RoleList
  110. *
  111. * @see #setRoles
  112. */
  113. public RoleList getRoles() {
  114. return roleList;
  115. }
  116. /**
  117. * Retrieves list of roles unsuccessfully accessed.
  118. *
  119. * @return a RoleUnresolvedList.
  120. *
  121. * @see #setRolesUnresolved
  122. */
  123. public RoleUnresolvedList getRolesUnresolved() {
  124. return unresolvedRoleList;
  125. }
  126. /**
  127. * Sets list of roles successfully accessed.
  128. *
  129. * @param theRoleList list of roles successfully accessed
  130. *
  131. * @see #getRoles
  132. */
  133. public void setRoles(RoleList theRoleList) {
  134. if (theRoleList != null) {
  135. roleList = new RoleList();
  136. for (Iterator roleIter = theRoleList.iterator();
  137. roleIter.hasNext();) {
  138. Role currRole = (Role)(roleIter.next());
  139. roleList.add((Role)(currRole.clone()));
  140. }
  141. } else {
  142. roleList = null;
  143. }
  144. return;
  145. }
  146. /**
  147. * Sets list of roles unsuccessfully accessed.
  148. *
  149. * @param theRoleUnresList list of roles unsuccessfully accessed
  150. *
  151. * @see #getRolesUnresolved
  152. */
  153. public void setRolesUnresolved(RoleUnresolvedList theRoleUnresList) {
  154. if (theRoleUnresList != null) {
  155. unresolvedRoleList = new RoleUnresolvedList();
  156. for (Iterator roleUnresIter = theRoleUnresList.iterator();
  157. roleUnresIter.hasNext();) {
  158. RoleUnresolved currRoleUnres =
  159. (RoleUnresolved)(roleUnresIter.next());
  160. unresolvedRoleList.add((RoleUnresolved)(currRoleUnres.clone()));
  161. }
  162. } else {
  163. unresolvedRoleList = null;
  164. }
  165. return;
  166. }
  167. /**
  168. * Deserializes a {@link RoleResult} from an {@link ObjectInputStream}.
  169. */
  170. private void readObject(ObjectInputStream in)
  171. throws IOException, ClassNotFoundException {
  172. if (compat)
  173. {
  174. // Read an object serialized in the old serial form
  175. //
  176. ObjectInputStream.GetField fields = in.readFields();
  177. roleList = (RoleList) fields.get("myRoleList", null);
  178. if (fields.defaulted("myRoleList"))
  179. {
  180. throw new NullPointerException("myRoleList");
  181. }
  182. unresolvedRoleList = (RoleUnresolvedList) fields.get("myRoleUnresList", null);
  183. if (fields.defaulted("myRoleUnresList"))
  184. {
  185. throw new NullPointerException("myRoleUnresList");
  186. }
  187. }
  188. else
  189. {
  190. // Read an object serialized in the new serial form
  191. //
  192. in.defaultReadObject();
  193. }
  194. }
  195. /**
  196. * Serializes a {@link RoleResult} to an {@link ObjectOutputStream}.
  197. */
  198. private void writeObject(ObjectOutputStream out)
  199. throws IOException {
  200. if (compat)
  201. {
  202. // Serializes this instance in the old serial form
  203. //
  204. ObjectOutputStream.PutField fields = out.putFields();
  205. fields.put("myRoleList", roleList);
  206. fields.put("myRoleUnresList", unresolvedRoleList);
  207. out.writeFields();
  208. }
  209. else
  210. {
  211. // Serializes this instance in the new serial form
  212. //
  213. out.defaultWriteObject();
  214. }
  215. }
  216. }