1 7 8 package javax.management.relation; 9 10 import javax.management.ObjectName ; 11 12 import java.util.ArrayList ; 13 import java.util.List ; 14 import java.util.Iterator ; 15 16 import java.security.AccessController ; 17 import java.security.PrivilegedAction ; 18 19 import java.io.IOException ; 20 import java.io.ObjectInputStream ; 21 import java.io.ObjectOutputStream ; 22 import java.io.ObjectStreamField ; 23 import java.io.Serializable ; 24 25 import com.sun.jmx.mbeanserver.GetPropertyAction; 26 27 34 public class Role implements Serializable { 35 36 private static final long oldSerialVersionUID = -1959486389343113026L; 44 private static final long newSerialVersionUID = -279985518429862552L; 47 private static final ObjectStreamField [] oldSerialPersistentFields = 50 { 51 new ObjectStreamField ("myName", String .class), 52 new ObjectStreamField ("myObjNameList", ArrayList .class) 53 }; 54 private static final ObjectStreamField [] newSerialPersistentFields = 57 { 58 new ObjectStreamField ("name", String .class), 59 new ObjectStreamField ("objectNameList", List .class) 60 }; 61 private static final long serialVersionUID; 64 68 private static final ObjectStreamField [] serialPersistentFields; 69 private static boolean compat = false; 70 static { 71 try { 72 PrivilegedAction act = new GetPropertyAction("jmx.serial.form"); 73 String form = (String ) AccessController.doPrivileged(act); 74 compat = (form != null && form.equals("1.0")); 75 } catch (Exception e) { 76 } 78 if (compat) { 79 serialPersistentFields = oldSerialPersistentFields; 80 serialVersionUID = oldSerialVersionUID; 81 } else { 82 serialPersistentFields = newSerialPersistentFields; 83 serialVersionUID = newSerialVersionUID; 84 } 85 } 86 89 93 96 private String name = null; 97 98 101 private List objectNameList = new ArrayList (); 102 103 107 118 public Role(String theRoleName, 119 List theRoleValue) 120 throws IllegalArgumentException { 121 122 if (theRoleName == null || theRoleValue == null) { 123 String excMsg = "Invalid parameter"; 124 throw new IllegalArgumentException (excMsg); 125 } 126 127 setRoleName(theRoleName); 128 setRoleValue(theRoleValue); 129 130 return; 131 } 132 133 137 144 public String getRoleName() { 145 return name; 146 } 147 148 155 public List getRoleValue() { 156 return objectNameList; 157 } 158 159 168 public void setRoleName(String theRoleName) 169 throws IllegalArgumentException { 170 171 if (theRoleName == null) { 172 String excMsg = "Invalid parameter."; 174 throw new IllegalArgumentException (excMsg); 175 } 176 177 name = theRoleName; 178 return; 179 } 180 181 191 public void setRoleValue(List theRoleValue) 192 throws IllegalArgumentException { 193 194 if (theRoleValue == null) { 195 String excMsg = "Invalid parameter."; 197 throw new IllegalArgumentException (excMsg); 198 } 199 200 objectNameList = new ArrayList (theRoleValue); 201 return; 202 } 203 204 209 public String toString() { 210 StringBuffer result = new StringBuffer (); 211 result.append("role name: " + name + "; role value: "); 212 for (Iterator objNameIter = objectNameList.iterator(); 213 objNameIter.hasNext();) { 214 ObjectName currObjName = (ObjectName )(objNameIter.next()); 215 result.append(currObjName.toString()); 216 if (objNameIter.hasNext()) { 217 result.append(", "); 218 } 219 } 220 return result.toString(); 221 } 222 223 227 232 public Object clone() { 233 234 try { 235 return new Role (name, objectNameList); 236 } catch (IllegalArgumentException exc) { 237 return null; } 239 } 240 241 251 public static String roleValueToString(List theRoleValue) 252 throws IllegalArgumentException { 253 254 if (theRoleValue == null) { 255 String excMsg = "Invalid parameter"; 256 throw new IllegalArgumentException (excMsg); 257 } 258 259 StringBuffer result = new StringBuffer (); 260 for (Iterator objNameIter = theRoleValue.iterator(); 261 objNameIter.hasNext();) { 262 ObjectName currObjName = (ObjectName )(objNameIter.next()); 263 result.append(currObjName.toString()); 264 if (objNameIter.hasNext()) { 265 result.append("\n"); 266 } 267 } 268 return result.toString(); 269 } 270 271 274 private void readObject(ObjectInputStream in) 275 throws IOException , ClassNotFoundException { 276 if (compat) 277 { 278 ObjectInputStream.GetField fields = in.readFields(); 281 name = (String ) fields.get("myName", null); 282 if (fields.defaulted("myName")) 283 { 284 throw new NullPointerException ("myName"); 285 } 286 objectNameList = (List ) fields.get("myObjNameList", null); 287 if (fields.defaulted("myObjNameList")) 288 { 289 throw new NullPointerException ("myObjNameList"); 290 } 291 } 292 else 293 { 294 in.defaultReadObject(); 297 } 298 } 299 300 301 304 private void writeObject(ObjectOutputStream out) 305 throws IOException { 306 if (compat) 307 { 308 ObjectOutputStream.PutField fields = out.putFields(); 311 fields.put("myName", name); 312 fields.put("myObjNameList", (ArrayList )objectNameList); 313 out.writeFields(); 314 } 315 else 316 { 317 out.defaultWriteObject(); 320 } 321 } 322 } 323 | Popular Tags |