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. 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 a role: includes a role name and referenced MBeans (via their
  22. * ObjectNames). The role value is always represented as an ArrayList
  23. * collection (of ObjectNames) to homogenize the access.
  24. *
  25. * @since 1.5
  26. */
  27. public class Role implements Serializable {
  28. // Serialization compatibility stuff:
  29. // Two serial forms are supported in this class. The selected form depends
  30. // on system property "jmx.serial.form":
  31. // - "1.0" for JMX 1.0
  32. // - any other value for JMX 1.1 and higher
  33. //
  34. // Serial version for old serial form
  35. private static final long oldSerialVersionUID = -1959486389343113026L;
  36. //
  37. // Serial version for new serial form
  38. private static final long newSerialVersionUID = -279985518429862552L;
  39. //
  40. // Serializable fields in old serial form
  41. private static final ObjectStreamField[] oldSerialPersistentFields =
  42. {
  43. new ObjectStreamField("myName", String.class),
  44. new ObjectStreamField("myObjNameList", ArrayList.class)
  45. };
  46. //
  47. // Serializable fields in new serial form
  48. private static final ObjectStreamField[] newSerialPersistentFields =
  49. {
  50. new ObjectStreamField("name", String.class),
  51. new ObjectStreamField("objectNameList", List.class)
  52. };
  53. //
  54. // Actual serial version and serial form
  55. private static final long serialVersionUID;
  56. /**
  57. * @serialField name String Role name
  58. * @serialField objectNameList List {@link List} of {@link ObjectName}s of referenced MBeans
  59. */
  60. private static final ObjectStreamField[] serialPersistentFields;
  61. private static boolean compat = false;
  62. static {
  63. try {
  64. PrivilegedAction act = new GetPropertyAction("jmx.serial.form");
  65. String form = (String) AccessController.doPrivileged(act);
  66. compat = (form != null && form.equals("1.0"));
  67. } catch (Exception e) {
  68. // OK : Too bad, no compat with 1.0
  69. }
  70. if (compat) {
  71. serialPersistentFields = oldSerialPersistentFields;
  72. serialVersionUID = oldSerialVersionUID;
  73. } else {
  74. serialPersistentFields = newSerialPersistentFields;
  75. serialVersionUID = newSerialVersionUID;
  76. }
  77. }
  78. //
  79. // END Serialization compatibility stuff
  80. //
  81. // Private members
  82. //
  83. /**
  84. * @serial Role name
  85. */
  86. private String name = null;
  87. /**
  88. * @serial {@link List} of {@link ObjectName}s of referenced MBeans
  89. */
  90. private List objectNameList = new ArrayList();
  91. //
  92. // Constructors
  93. //
  94. /**
  95. * <p>Make a new Role object.
  96. * No check is made that the ObjectNames in the role value exist in
  97. * an MBean server. That check will be made when the role is set
  98. * in a relation.
  99. *
  100. * @param theRoleName role name
  101. * @param theRoleValue role value (List of ObjectName objects)
  102. *
  103. * @exception IllegalArgumentException if null parameter
  104. */
  105. public Role(String theRoleName,
  106. List theRoleValue)
  107. throws IllegalArgumentException {
  108. if (theRoleName == null || theRoleValue == null) {
  109. String excMsg = "Invalid parameter";
  110. throw new IllegalArgumentException(excMsg);
  111. }
  112. setRoleName(theRoleName);
  113. setRoleValue(theRoleValue);
  114. return;
  115. }
  116. //
  117. // Accessors
  118. //
  119. /**
  120. * Retrieves role name.
  121. *
  122. * @return the role name.
  123. *
  124. * @see #setRoleName
  125. */
  126. public String getRoleName() {
  127. return name;
  128. }
  129. /**
  130. * Retrieves role value.
  131. *
  132. * @return ArrayList of ObjectName objects for referenced MBeans.
  133. *
  134. * @see #setRoleValue
  135. */
  136. public List getRoleValue() {
  137. return objectNameList;
  138. }
  139. /**
  140. * Sets role name.
  141. *
  142. * @param theRoleName role name
  143. *
  144. * @exception IllegalArgumentException if null parameter
  145. *
  146. * @see #getRoleName
  147. */
  148. public void setRoleName(String theRoleName)
  149. throws IllegalArgumentException {
  150. if (theRoleName == null) {
  151. // Revisit [cebro] Localize message
  152. String excMsg = "Invalid parameter.";
  153. throw new IllegalArgumentException(excMsg);
  154. }
  155. name = theRoleName;
  156. return;
  157. }
  158. /**
  159. * Sets role value.
  160. *
  161. * @param theRoleValue List of ObjectName objects for referenced
  162. * MBeans.
  163. *
  164. * @exception IllegalArgumentException if null parameter
  165. *
  166. * @see #getRoleValue
  167. */
  168. public void setRoleValue(List theRoleValue)
  169. throws IllegalArgumentException {
  170. if (theRoleValue == null) {
  171. // Revisit [cebro] Localize message
  172. String excMsg = "Invalid parameter.";
  173. throw new IllegalArgumentException(excMsg);
  174. }
  175. objectNameList = new ArrayList(theRoleValue);
  176. return;
  177. }
  178. /**
  179. * Returns a string describing the role.
  180. *
  181. * @return the description of the role.
  182. */
  183. public String toString() {
  184. StringBuffer result = new StringBuffer();
  185. result.append("role name: " + name + "; role value: ");
  186. for (Iterator objNameIter = objectNameList.iterator();
  187. objNameIter.hasNext();) {
  188. ObjectName currObjName = (ObjectName)(objNameIter.next());
  189. result.append(currObjName.toString());
  190. if (objNameIter.hasNext()) {
  191. result.append(", ");
  192. }
  193. }
  194. return result.toString();
  195. }
  196. //
  197. // Misc
  198. //
  199. /**
  200. * Clone the role object.
  201. *
  202. * @return a Role that is an independent copy of the current Role object.
  203. */
  204. public Object clone() {
  205. try {
  206. return new Role(name, objectNameList);
  207. } catch (IllegalArgumentException exc) {
  208. return null; // can't happen
  209. }
  210. }
  211. /**
  212. * Returns a string for the given role value.
  213. *
  214. * @param theRoleValue List of ObjectName objects
  215. *
  216. * @return A String consisting of the ObjectNames separated by
  217. * newlines (\n).
  218. *
  219. * @exception IllegalArgumentException if null parameter
  220. */
  221. public static String roleValueToString(List theRoleValue)
  222. throws IllegalArgumentException {
  223. if (theRoleValue == null) {
  224. String excMsg = "Invalid parameter";
  225. throw new IllegalArgumentException(excMsg);
  226. }
  227. StringBuffer result = new StringBuffer();
  228. for (Iterator objNameIter = theRoleValue.iterator();
  229. objNameIter.hasNext();) {
  230. ObjectName currObjName = (ObjectName)(objNameIter.next());
  231. result.append(currObjName.toString());
  232. if (objNameIter.hasNext()) {
  233. result.append("\n");
  234. }
  235. }
  236. return result.toString();
  237. }
  238. /**
  239. * Deserializes a {@link Role} from an {@link ObjectInputStream}.
  240. */
  241. private void readObject(ObjectInputStream in)
  242. throws IOException, ClassNotFoundException {
  243. if (compat)
  244. {
  245. // Read an object serialized in the old serial form
  246. //
  247. ObjectInputStream.GetField fields = in.readFields();
  248. name = (String) fields.get("myName", null);
  249. if (fields.defaulted("myName"))
  250. {
  251. throw new NullPointerException("myName");
  252. }
  253. objectNameList = (List) fields.get("myObjNameList", null);
  254. if (fields.defaulted("myObjNameList"))
  255. {
  256. throw new NullPointerException("myObjNameList");
  257. }
  258. }
  259. else
  260. {
  261. // Read an object serialized in the new serial form
  262. //
  263. in.defaultReadObject();
  264. }
  265. }
  266. /**
  267. * Serializes a {@link Role} to an {@link ObjectOutputStream}.
  268. */
  269. private void writeObject(ObjectOutputStream out)
  270. throws IOException {
  271. if (compat)
  272. {
  273. // Serializes this instance in the old serial form
  274. //
  275. ObjectOutputStream.PutField fields = out.putFields();
  276. fields.put("myName", name);
  277. fields.put("myObjNameList", (ArrayList)objectNameList);
  278. out.writeFields();
  279. }
  280. else
  281. {
  282. // Serializes this instance in the new serial form
  283. //
  284. out.defaultWriteObject();
  285. }
  286. }
  287. }