001 /*
002 * Copyright 2000-2006 Sun Microsystems, Inc. All Rights Reserved.
003 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004 *
005 * This code is free software; you can redistribute it and/or modify it
006 * under the terms of the GNU General Public License version 2 only, as
007 * published by the Free Software Foundation. Sun designates this
008 * particular file as subject to the "Classpath" exception as provided
009 * by Sun in the LICENSE file that accompanied this code.
010 *
011 * This code is distributed in the hope that it will be useful, but WITHOUT
012 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014 * version 2 for more details (a copy is included in the LICENSE file that
015 * accompanied this code).
016 *
017 * You should have received a copy of the GNU General Public License version
018 * 2 along with this work; if not, write to the Free Software Foundation,
019 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020 *
021 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022 * CA 95054 USA or visit www.sun.com if you need additional information or
023 * have any questions.
024 */
025
026 package javax.management.relation;
027
028 import static com.sun.jmx.mbeanserver.Util.cast;
029 import com.sun.jmx.mbeanserver.GetPropertyAction;
030
031 import java.io.IOException;
032 import java.io.ObjectInputStream;
033 import java.io.ObjectOutputStream;
034 import java.io.ObjectStreamField;
035 import java.io.Serializable;
036
037 import java.security.AccessController;
038
039 import java.util.ArrayList;
040 import java.util.Iterator;
041 import java.util.List;
042
043 import javax.management.ObjectName;
044
045 /**
046 * Represents a role: includes a role name and referenced MBeans (via their
047 * ObjectNames). The role value is always represented as an ArrayList
048 * collection (of ObjectNames) to homogenize the access.
049 *
050 * <p>The <b>serialVersionUID</b> of this class is <code>-279985518429862552L</code>.
051 *
052 * @since 1.5
053 */
054 @SuppressWarnings("serial")
055 // serialVersionUID not constant
056 public class Role implements Serializable {
057
058 // Serialization compatibility stuff:
059 // Two serial forms are supported in this class. The selected form depends
060 // on system property "jmx.serial.form":
061 // - "1.0" for JMX 1.0
062 // - any other value for JMX 1.1 and higher
063 //
064 // Serial version for old serial form
065 private static final long oldSerialVersionUID = -1959486389343113026L;
066 //
067 // Serial version for new serial form
068 private static final long newSerialVersionUID = -279985518429862552L;
069 //
070 // Serializable fields in old serial form
071 private static final ObjectStreamField[] oldSerialPersistentFields = {
072 new ObjectStreamField("myName", String.class),
073 new ObjectStreamField("myObjNameList", ArrayList.class) };
074 //
075 // Serializable fields in new serial form
076 private static final ObjectStreamField[] newSerialPersistentFields = {
077 new ObjectStreamField("name", String.class),
078 new ObjectStreamField("objectNameList", List.class) };
079 //
080 // Actual serial version and serial form
081 private static final long serialVersionUID;
082 /**
083 * @serialField name String Role name
084 * @serialField objectNameList List {@link List} of {@link ObjectName}s of referenced MBeans
085 */
086 private static final ObjectStreamField[] serialPersistentFields;
087 private static boolean compat = false;
088 static {
089 try {
090 GetPropertyAction act = new GetPropertyAction(
091 "jmx.serial.form");
092 String form = AccessController.doPrivileged(act);
093 compat = (form != null && form.equals("1.0"));
094 } catch (Exception e) {
095 // OK : Too bad, no compat with 1.0
096 }
097 if (compat) {
098 serialPersistentFields = oldSerialPersistentFields;
099 serialVersionUID = oldSerialVersionUID;
100 } else {
101 serialPersistentFields = newSerialPersistentFields;
102 serialVersionUID = newSerialVersionUID;
103 }
104 }
105 //
106 // END Serialization compatibility stuff
107
108 //
109 // Private members
110 //
111
112 /**
113 * @serial Role name
114 */
115 private String name = null;
116
117 /**
118 * @serial {@link List} of {@link ObjectName}s of referenced MBeans
119 */
120 private List<ObjectName> objectNameList = new ArrayList<ObjectName>();
121
122 //
123 // Constructors
124 //
125
126 /**
127 * <p>Make a new Role object.
128 * No check is made that the ObjectNames in the role value exist in
129 * an MBean server. That check will be made when the role is set
130 * in a relation.
131 *
132 * @param roleName role name
133 * @param roleValue role value (List of ObjectName objects)
134 *
135 * @exception IllegalArgumentException if null parameter
136 */
137 public Role(String roleName, List<ObjectName> roleValue)
138 throws IllegalArgumentException {
139
140 if (roleName == null || roleValue == null) {
141 String excMsg = "Invalid parameter";
142 throw new IllegalArgumentException(excMsg);
143 }
144
145 setRoleName(roleName);
146 setRoleValue(roleValue);
147
148 return;
149 }
150
151 //
152 // Accessors
153 //
154
155 /**
156 * Retrieves role name.
157 *
158 * @return the role name.
159 *
160 * @see #setRoleName
161 */
162 public String getRoleName() {
163 return name;
164 }
165
166 /**
167 * Retrieves role value.
168 *
169 * @return ArrayList of ObjectName objects for referenced MBeans.
170 *
171 * @see #setRoleValue
172 */
173 public List<ObjectName> getRoleValue() {
174 return objectNameList;
175 }
176
177 /**
178 * Sets role name.
179 *
180 * @param roleName role name
181 *
182 * @exception IllegalArgumentException if null parameter
183 *
184 * @see #getRoleName
185 */
186 public void setRoleName(String roleName)
187 throws IllegalArgumentException {
188
189 if (roleName == null) {
190 String excMsg = "Invalid parameter.";
191 throw new IllegalArgumentException(excMsg);
192 }
193
194 name = roleName;
195 return;
196 }
197
198 /**
199 * Sets role value.
200 *
201 * @param roleValue List of ObjectName objects for referenced
202 * MBeans.
203 *
204 * @exception IllegalArgumentException if null parameter
205 *
206 * @see #getRoleValue
207 */
208 public void setRoleValue(List<ObjectName> roleValue)
209 throws IllegalArgumentException {
210
211 if (roleValue == null) {
212 String excMsg = "Invalid parameter.";
213 throw new IllegalArgumentException(excMsg);
214 }
215
216 objectNameList = new ArrayList<ObjectName>(roleValue);
217 return;
218 }
219
220 /**
221 * Returns a string describing the role.
222 *
223 * @return the description of the role.
224 */
225 public String toString() {
226 StringBuilder result = new StringBuilder();
227 result.append("role name: " + name + "; role value: ");
228 for (Iterator objNameIter = objectNameList.iterator(); objNameIter
229 .hasNext();) {
230 ObjectName currObjName = (ObjectName) (objNameIter.next());
231 result.append(currObjName.toString());
232 if (objNameIter.hasNext()) {
233 result.append(", ");
234 }
235 }
236 return result.toString();
237 }
238
239 //
240 // Misc
241 //
242
243 /**
244 * Clone the role object.
245 *
246 * @return a Role that is an independent copy of the current Role object.
247 */
248 public Object clone() {
249
250 try {
251 return new Role(name, objectNameList);
252 } catch (IllegalArgumentException exc) {
253 return null; // can't happen
254 }
255 }
256
257 /**
258 * Returns a string for the given role value.
259 *
260 * @param roleValue List of ObjectName objects
261 *
262 * @return A String consisting of the ObjectNames separated by
263 * newlines (\n).
264 *
265 * @exception IllegalArgumentException if null parameter
266 */
267 public static String roleValueToString(List<ObjectName> roleValue)
268 throws IllegalArgumentException {
269
270 if (roleValue == null) {
271 String excMsg = "Invalid parameter";
272 throw new IllegalArgumentException(excMsg);
273 }
274
275 StringBuilder result = new StringBuilder();
276 for (ObjectName currObjName : roleValue) {
277 if (result.length() > 0)
278 result.append("\n");
279 result.append(currObjName.toString());
280 }
281 return result.toString();
282 }
283
284 /**
285 * Deserializes a {@link Role} from an {@link ObjectInputStream}.
286 */
287 private void readObject(ObjectInputStream in) throws IOException,
288 ClassNotFoundException {
289 if (compat) {
290 // Read an object serialized in the old serial form
291 //
292 ObjectInputStream.GetField fields = in.readFields();
293 name = (String) fields.get("myName", null);
294 if (fields.defaulted("myName")) {
295 throw new NullPointerException("myName");
296 }
297 objectNameList = cast(fields.get("myObjNameList", null));
298 if (fields.defaulted("myObjNameList")) {
299 throw new NullPointerException("myObjNameList");
300 }
301 } else {
302 // Read an object serialized in the new serial form
303 //
304 in.defaultReadObject();
305 }
306 }
307
308 /**
309 * Serializes a {@link Role} to an {@link ObjectOutputStream}.
310 */
311 private void writeObject(ObjectOutputStream out) throws IOException {
312 if (compat) {
313 // Serializes this instance in the old serial form
314 //
315 ObjectOutputStream.PutField fields = out.putFields();
316 fields.put("myName", name);
317 fields.put("myObjNameList", (ArrayList) objectNameList);
318 out.writeFields();
319 } else {
320 // Serializes this instance in the new serial form
321 //
322 out.defaultWriteObject();
323 }
324 }
325 }
|