1 7 25 26 27 package javax.management.modelmbean; 28 29 import java.io.IOException ; 30 import java.io.ObjectInputStream ; 31 import java.io.ObjectOutputStream ; 32 import java.io.ObjectStreamField ; 33 import java.lang.reflect.*; 34 import java.security.AccessController ; 35 import java.security.PrivilegedAction ; 36 37 import javax.management.Descriptor ; 38 import javax.management.DescriptorAccess ; 39 import javax.management.*; 40 41 import com.sun.jmx.mbeanserver.GetPropertyAction; 42 import com.sun.jmx.trace.Trace; 43 44 78 79 80 public class ModelMBeanAttributeInfo extends MBeanAttributeInfo 81 implements DescriptorAccess , Cloneable 82 { 83 84 private static final long oldSerialVersionUID = 7098036920755973145L; 92 private static final long newSerialVersionUID = 6181543027787327345L; 95 private static final ObjectStreamField [] oldSerialPersistentFields = 98 { 99 new ObjectStreamField ("attrDescriptor", Descriptor .class), 100 new ObjectStreamField ("currClass", String .class) 101 }; 102 private static final ObjectStreamField [] newSerialPersistentFields = 105 { 106 new ObjectStreamField ("attrDescriptor", Descriptor .class) 107 }; 108 private static final long serialVersionUID; 111 115 private static final ObjectStreamField [] serialPersistentFields; 116 private static boolean compat = false; 117 static { 118 try { 119 PrivilegedAction act = new GetPropertyAction("jmx.serial.form"); 120 String form = (String ) AccessController.doPrivileged(act); 121 compat = (form != null && form.equals("1.0")); 122 } catch (Exception e) { 123 } 125 if (compat) { 126 serialPersistentFields = oldSerialPersistentFields; 127 serialVersionUID = oldSerialVersionUID; 128 } else { 129 serialPersistentFields = newSerialPersistentFields; 130 serialVersionUID = newSerialVersionUID; 131 } 132 } 133 136 140 private Descriptor attrDescriptor = createDefaultDescriptor(); 141 142 private final static String currClass = "ModelMBeanAttributeInfo"; 143 144 156 157 public ModelMBeanAttributeInfo(String name, 158 String description, 159 Method getter, 160 Method setter) 161 throws javax.management.IntrospectionException { 162 super(name, description, getter, setter); 163 if (tracing()) 164 { 165 trace("ModelMBeanAttributeInfo(" + name + ",String,Method,Method)","Entry"); 166 } 167 168 attrDescriptor = createDefaultDescriptor(); 169 172 } 173 174 192 193 public ModelMBeanAttributeInfo(String name, 194 String description, 195 Method getter, 196 Method setter, 197 Descriptor descriptor) 198 throws javax.management.IntrospectionException { 199 200 super(name, description, getter, setter); 201 if (tracing()) 203 { 204 trace("ModelMBeanAttributeInfo(" + name + ", String, Method, Method, Descriptor)","Entry"); 205 } 206 if (descriptor == null) 207 { 208 attrDescriptor = createDefaultDescriptor(); 209 } else 210 { 211 if (isValid(descriptor)) 212 { 213 attrDescriptor = (Descriptor ) descriptor.clone(); 214 } else 215 { 216 throw new RuntimeOperationsException(new IllegalArgumentException ("Invalid descriptor passed in parameter"), ("Exception occured in ModelMBeanAttributeInfo constructor")); 217 } 218 } 219 } 220 221 232 public ModelMBeanAttributeInfo(String name, 233 String type, 234 String description, 235 boolean isReadable, 236 boolean isWritable, 237 boolean isIs) 238 { 239 240 super(name, type, description, isReadable, isWritable, isIs); 241 if (tracing()) 243 { 244 trace("ModelMBeanAttributeInfo(" + name + ",String,String,boolean,boolean)","Entry"); 245 } 246 attrDescriptor = createDefaultDescriptor(); 247 248 } 249 266 public ModelMBeanAttributeInfo(String name, 267 String type, 268 String description, 269 boolean isReadable, 270 boolean isWritable, 271 boolean isIs, 272 Descriptor descriptor) 273 { 274 super(name, type, description, isReadable, isWritable, isIs); 275 if (tracing()) 276 { 277 trace("ModelMBeanAttributeInfo(" + name + ",String,String,boolean,boolean,Descriptor)","Entry"); 278 279 } 280 281 if (descriptor == null) 282 { 283 attrDescriptor = createDefaultDescriptor(); 284 } else 285 { 286 if (isValid(descriptor)) 287 { 288 attrDescriptor = (Descriptor ) descriptor.clone(); 289 } else 290 { 291 throw new RuntimeOperationsException(new IllegalArgumentException ("Invalid descriptor passed in parameter"), ("Exception occured in ModelMBeanAttributeInfo constructor")); 292 } 293 } 294 295 } 296 297 303 304 public ModelMBeanAttributeInfo(ModelMBeanAttributeInfo inInfo) 305 { 306 super(inInfo.getName(), 307 inInfo.getType(), 308 inInfo.getDescription(), 309 inInfo.isReadable(), 310 inInfo.isWritable(), 311 inInfo.isIs()); 312 if (tracing()) 313 { 314 trace("ModelMBeanAttributeInfo(ModelMBeanAttributeInfo)","Entry"); 315 } 316 Descriptor newDesc = inInfo.getDescriptor(); 317 318 320 if ((newDesc != null) && (isValid(newDesc))) 321 { 322 attrDescriptor = newDesc; 323 } else 324 { 325 attrDescriptor = createDefaultDescriptor(); 326 } 327 } 328 337 338 public Descriptor getDescriptor() 339 { 340 if (tracing()) 341 { 342 trace("ModelMBeanAttributeInfo.getDescriptor()","Entry"); 343 } 344 if (attrDescriptor == null) 345 { 346 attrDescriptor = createDefaultDescriptor(); 347 } 348 return((Descriptor )attrDescriptor.clone()); 349 } 350 351 352 369 public void setDescriptor(Descriptor inDescriptor) 370 { 371 if (inDescriptor != null) { 372 if (tracing()) { 373 trace("ModelMBeanAttributeInfo.setDescriptor()","Executed for " + inDescriptor.toString()); 374 } 375 } 376 if (inDescriptor == null) { 377 if (tracing()) { 378 trace("ModelMBeanAttributeInfo.setDescriptor()", 379 "Received null for new descriptor value, setting descriptor to default values"); 380 } 381 attrDescriptor = createDefaultDescriptor(); 382 } 383 else { 384 if (isValid(inDescriptor)) { 385 attrDescriptor = (Descriptor ) inDescriptor.clone(); 386 } 387 else { 388 throw new RuntimeOperationsException(new IllegalArgumentException ("Invalid descriptor passed in parameter"), 389 ("Exception occured in ModelMBeanAttributeInfo setDescriptor")); 390 } 391 } 392 } 393 394 400 401 public Object clone() 402 { 403 if (tracing()) 404 { 405 trace("ModelMBeanAttributeInfo.clone()","Entry"); 406 } 407 return(new ModelMBeanAttributeInfo (this)); 408 } 409 410 414 public String toString() 415 { 416 return 417 "ModelMBeanAttributeInfo: " + this.getName() + 418 " ; Description: " + this.getDescription() + 419 " ; Types: " + this.getType() + 420 " ; isReadable: " + this.isReadable() + 421 " ; isWritable: " + this.isWritable() + 422 " ; Descriptor: " + this.getDescriptor(); 423 } 424 425 430 431 private Descriptor createDefaultDescriptor() 432 { 433 if (tracing()) 434 { 435 trace("ModelMBeanAttributeInfo.createDefaultDescriptor()","Entry"); 436 } 437 return new DescriptorSupport ( 438 new String [] {"descriptorType=attribute", 439 ("name=" + this.getName()), 440 ("displayName=" + this.getName()) 441 }); 442 } 443 450 private boolean isValid(Descriptor inDesc) 451 { 452 boolean results=true; 455 String badField="none"; 456 if (inDesc == null) 457 { 458 badField="nullDescriptor"; 459 results = false; 460 } 461 462 else if (!inDesc.isValid()) 463 { badField="inValidDescriptor"; 466 results = false; 467 } 468 469 else if (! ((String )inDesc.getFieldValue("name")).equalsIgnoreCase(this.getName())) 470 { 471 badField="name"; 472 results = false; 473 } else 474 { 475 476 if (! ((String )inDesc.getFieldValue("descriptorType")).equalsIgnoreCase("attribute")) 477 { 478 badField="desriptorType"; 479 results = false; 480 } else if ((inDesc.getFieldValue("displayName")) == null) 481 { 482 inDesc.setField("displayName",this.getName()); 483 } 484 } 485 if (tracing()) trace("isValid()",("Returning " + results + ": Invalid field is " + badField)); 486 return results; 487 } 488 489 private boolean tracing() 491 { 492 return Trace.isSelected(Trace.LEVEL_TRACE, Trace.INFO_MODELMBEAN); 494 } 495 496 private void trace(String inClass, String inMethod, String inText) 497 { 498 Trace.send(Trace.LEVEL_TRACE, Trace.INFO_MODELMBEAN, inClass, 500 inMethod, Integer.toHexString(this.hashCode()) + " " + inText); 501 } 502 503 private void trace(String inMethod, String inText) 504 { 505 trace(currClass, inMethod, inText); 506 } 507 508 511 private void readObject(ObjectInputStream in) 512 throws IOException , ClassNotFoundException { 513 in.defaultReadObject(); 515 } 516 517 518 521 private void writeObject(ObjectOutputStream out) 522 throws IOException { 523 if (compat) 524 { 525 ObjectOutputStream.PutField fields = out.putFields(); 528 fields.put("attrDescriptor", attrDescriptor); 529 fields.put("currClass", currClass); 530 out.writeFields(); 531 } 532 else 533 { 534 out.defaultWriteObject(); 537 } 538 } 539 540 } 541 | Popular Tags |