1 7 8 package javax.management.relation; 9 10 import java.io.IOException ; 11 import java.io.ObjectInputStream ; 12 import java.io.ObjectOutputStream ; 13 import java.io.ObjectStreamField ; 14 15 import java.security.AccessController ; 16 import java.security.PrivilegedAction ; 17 18 import java.util.ArrayList ; 19 import java.util.HashMap ; 20 import java.util.List ; 21 import java.util.Map ; 22 23 import com.sun.jmx.mbeanserver.GetPropertyAction; 24 import com.sun.jmx.trace.Trace; 25 26 27 44 public class RelationTypeSupport implements RelationType { 45 46 private static final long oldSerialVersionUID = -8179019472410837190L; 54 private static final long newSerialVersionUID = 4611072955724144607L; 57 private static final ObjectStreamField [] oldSerialPersistentFields = 60 { 61 new ObjectStreamField ("myTypeName", String .class), 62 new ObjectStreamField ("myRoleName2InfoMap", HashMap .class), 63 new ObjectStreamField ("myIsInRelServFlg", boolean.class) 64 }; 65 private static final ObjectStreamField [] newSerialPersistentFields = 68 { 69 new ObjectStreamField ("typeName", String .class), 70 new ObjectStreamField ("roleName2InfoMap", Map .class), 71 new ObjectStreamField ("isInRelationService", boolean.class) 72 }; 73 private static final long serialVersionUID; 76 83 private static final ObjectStreamField [] serialPersistentFields; 84 private static boolean compat = false; 85 static { 86 try { 87 PrivilegedAction act = new GetPropertyAction("jmx.serial.form"); 88 String form = (String ) AccessController.doPrivileged(act); 89 compat = (form != null && form.equals("1.0")); 90 } catch (Exception e) { 91 } 93 if (compat) { 94 serialPersistentFields = oldSerialPersistentFields; 95 serialVersionUID = oldSerialVersionUID; 96 } else { 97 serialPersistentFields = newSerialPersistentFields; 98 serialVersionUID = newSerialVersionUID; 99 } 100 } 101 104 108 111 private String typeName = null; 112 113 117 private Map roleName2InfoMap = new HashMap (); 118 119 123 private boolean isInRelationService = false; 124 125 129 142 public RelationTypeSupport(String theRelTypeName, 143 RoleInfo [] theRoleInfoArray) 144 throws IllegalArgumentException , 145 InvalidRelationTypeException { 146 147 if (theRelTypeName == null || theRoleInfoArray == null) { 148 String excMsg = "Invalid parameter."; 150 throw new IllegalArgumentException (excMsg); 151 } 152 153 if (isTraceOn()) 154 trace("Constructor: entering", theRelTypeName); 155 156 initMembers(theRelTypeName, theRoleInfoArray); 159 160 if (isTraceOn()) 161 trace("Constructor: exiting", null); 162 return; 163 } 164 165 172 protected RelationTypeSupport(String theRelTypeName) 173 { 174 if (theRelTypeName == null) { 175 String excMsg = "Invalid parameter."; 177 throw new IllegalArgumentException (excMsg); 178 } 179 180 if (isTraceOn()) 181 trace("Protected constructor: entering", theRelTypeName); 182 183 typeName = theRelTypeName; 184 185 if (isTraceOn()) 186 trace("Protected constructor: exiting", null); 187 return; 188 } 189 190 194 199 public String getRelationTypeName() { 200 return typeName; 201 } 202 203 206 public List getRoleInfos() { 207 return new ArrayList (roleName2InfoMap.values()); 208 } 209 210 223 public RoleInfo getRoleInfo(String theRoleInfoName) 224 throws IllegalArgumentException , 225 RoleInfoNotFoundException { 226 227 if (theRoleInfoName == null) { 228 String excMsg = "Invalid parameter."; 230 throw new IllegalArgumentException (excMsg); 231 } 232 233 if (isTraceOn()) 234 trace("getRoleInfo: entering", theRoleInfoName); 235 236 RoleInfo result = (RoleInfo )(roleName2InfoMap.get(theRoleInfoName)); 238 239 if (result == null) { 240 StringBuffer excMsgStrB = new StringBuffer (); 241 String excMsg = "No role info for role "; 243 excMsgStrB.append(excMsg); 244 excMsgStrB.append(theRoleInfoName); 245 throw new RoleInfoNotFoundException (excMsgStrB.toString()); 246 } 247 248 if (isTraceOn()) 249 trace("getRoleInfo: exiting", null); 250 return result; 251 } 252 253 257 271 protected void addRoleInfo(RoleInfo theRoleInfo) 272 throws IllegalArgumentException , 273 InvalidRelationTypeException { 274 275 if (theRoleInfo == null) { 276 String excMsg = "Invalid parameter."; 278 throw new IllegalArgumentException (excMsg); 279 } 280 281 if (isDebugOn()) 282 debug("addRoleInfo: entering", theRoleInfo.toString()); 283 284 if (isInRelationService) { 285 String excMsg = "Relation type cannot be updated as it is declared in the Relation Service."; 288 throw new RuntimeException (excMsg); 289 } 290 291 String roleName = theRoleInfo.getName(); 292 293 if (roleName2InfoMap.containsKey(roleName)) { 295 StringBuffer excMsgStrB = new StringBuffer (); 296 String excMsg = "Two role infos provided for role "; 298 excMsgStrB.append(excMsg); 299 excMsgStrB.append(roleName); 300 throw new InvalidRelationTypeException (excMsgStrB.toString()); 301 } 302 303 roleName2InfoMap.put(roleName, 304 new RoleInfo (theRoleInfo)); 305 306 if (isDebugOn()) 307 debug("addRoleInfo: exiting", null); 308 return; 309 } 310 311 void setRelationServiceFlag(boolean theFlg) { 314 isInRelationService = theFlg; 315 return; 316 } 317 318 private void initMembers(String theRelTypeName, 329 RoleInfo [] theRoleInfoArray) 330 throws IllegalArgumentException , 331 InvalidRelationTypeException { 332 333 if (theRelTypeName == null || theRoleInfoArray == null) { 334 String excMsg = "Invalid parameter."; 336 throw new IllegalArgumentException (excMsg); 337 } 338 339 if (isDebugOn()) 340 debug("initMembers: entering", theRelTypeName); 341 342 typeName = theRelTypeName; 343 344 checkRoleInfos(theRoleInfoArray); 347 348 for (int i = 0; i < theRoleInfoArray.length; i++) { 349 RoleInfo currRoleInfo = theRoleInfoArray[i]; 350 roleName2InfoMap.put(new String (currRoleInfo.getName()), 351 new RoleInfo (currRoleInfo)); 352 } 353 354 if (isDebugOn()) 355 debug("initMembers: exiting", null); 356 return; 357 } 358 359 static void checkRoleInfos(RoleInfo [] theRoleInfoArray) 372 throws IllegalArgumentException , 373 InvalidRelationTypeException { 374 375 if (theRoleInfoArray == null) { 376 String excMsg = "Invalid parameter."; 378 throw new IllegalArgumentException (excMsg); 379 } 380 381 if (theRoleInfoArray.length == 0) { 382 String excMsg = "No role info provided."; 385 throw new InvalidRelationTypeException (excMsg); 386 } 387 388 389 ArrayList roleNameList = new ArrayList (); 390 391 for (int i = 0; i < theRoleInfoArray.length; i++) { 392 RoleInfo currRoleInfo = theRoleInfoArray[i]; 393 394 if (currRoleInfo == null) { 395 String excMsg = "Null role info provided."; 397 throw new InvalidRelationTypeException (excMsg); 398 } 399 400 String roleName = currRoleInfo.getName(); 401 402 if (roleNameList.contains(roleName)) { 404 StringBuffer excMsgStrB = new StringBuffer (); 405 String excMsg = "Two role infos provided for role "; 407 excMsgStrB.append(excMsg); 408 excMsgStrB.append(roleName); 409 throw new InvalidRelationTypeException (excMsgStrB.toString()); 410 } 411 roleNameList.add(roleName); 412 } 413 414 return; 415 } 416 417 419 private static String localClassName = "RelationTypeSupport"; 420 421 private boolean isTraceOn() { 423 return Trace.isSelected(Trace.LEVEL_TRACE, Trace.INFO_RELATION); 424 } 425 426 430 private void trace(String methodName, String info) { 431 Trace.send(Trace.LEVEL_TRACE, Trace.INFO_RELATION, localClassName, methodName, info); 432 Trace.send(Trace.LEVEL_TRACE, Trace.INFO_RELATION, "", "", "\n"); 433 } 434 435 439 443 private boolean isDebugOn() { 445 return Trace.isSelected(Trace.LEVEL_DEBUG, Trace.INFO_RELATION); 446 } 447 448 452 private void debug(String methodName, String info) { 453 Trace.send(Trace.LEVEL_DEBUG, Trace.INFO_RELATION, localClassName, methodName, info); 454 Trace.send(Trace.LEVEL_DEBUG, Trace.INFO_RELATION, "", "", "\n"); 455 } 456 457 461 465 468 private void readObject(ObjectInputStream in) 469 throws IOException , ClassNotFoundException { 470 if (compat) 471 { 472 ObjectInputStream.GetField fields = in.readFields(); 475 typeName = (String ) fields.get("myTypeName", null); 476 if (fields.defaulted("myTypeName")) 477 { 478 throw new NullPointerException ("myTypeName"); 479 } 480 roleName2InfoMap = (Map ) fields.get("myRoleName2InfoMap", null); 481 if (fields.defaulted("myRoleName2InfoMap")) 482 { 483 throw new NullPointerException ("myRoleName2InfoMap"); 484 } 485 isInRelationService = fields.get("myIsInRelServFlg", false); 486 if (fields.defaulted("myIsInRelServFlg")) 487 { 488 throw new NullPointerException ("myIsInRelServFlg"); 489 } 490 } 491 else 492 { 493 in.defaultReadObject(); 496 } 497 } 498 499 500 503 private void writeObject(ObjectOutputStream out) 504 throws IOException { 505 if (compat) 506 { 507 ObjectOutputStream.PutField fields = out.putFields(); 510 fields.put("myTypeName", typeName); 511 fields.put("myRoleName2InfoMap", (HashMap )roleName2InfoMap); 512 fields.put("myIsInRelServFlg", isInRelationService); 513 out.writeFields(); 514 } 515 else 516 { 517 out.defaultWriteObject(); 520 } 521 } 522 } 523 | Popular Tags |