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.security.AccessController ; 34 import java.security.PrivilegedAction ; 35 36 import javax.management.Descriptor ; 37 import javax.management.DescriptorAccess ; 38 import javax.management.*; 39 import java.lang.reflect.*; 40 41 import com.sun.jmx.mbeanserver.GetPropertyAction; 42 import com.sun.jmx.trace.Trace; 43 44 45 64 65 public class ModelMBeanConstructorInfo extends MBeanConstructorInfo 66 implements DescriptorAccess , Cloneable 67 { 68 69 private static final long oldSerialVersionUID = -4440125391095574518L; 77 private static final long newSerialVersionUID = 3862947819818064362L; 80 private static final ObjectStreamField [] oldSerialPersistentFields = 83 { 84 new ObjectStreamField ("consDescriptor", Descriptor .class), 85 new ObjectStreamField ("currClass", String .class) 86 }; 87 private static final ObjectStreamField [] newSerialPersistentFields = 90 { 91 new ObjectStreamField ("consDescriptor", Descriptor .class) 92 }; 93 private static final long serialVersionUID; 96 99 private static final ObjectStreamField [] serialPersistentFields; 100 private static boolean compat = false; 101 static { 102 try { 103 PrivilegedAction act = new GetPropertyAction("jmx.serial.form"); 104 String form = (String ) AccessController.doPrivileged(act); 105 compat = (form != null && form.equals("1.0")); 106 } catch (Exception e) { 107 } 109 if (compat) { 110 serialPersistentFields = oldSerialPersistentFields; 111 serialVersionUID = oldSerialVersionUID; 112 } else { 113 serialPersistentFields = newSerialPersistentFields; 114 serialVersionUID = newSerialVersionUID; 115 } 116 } 117 120 123 private Descriptor consDescriptor = createDefaultDescriptor(); 124 125 private final static String currClass = "ModelMBeanConstructorInfo"; 126 127 128 135 public ModelMBeanConstructorInfo(String description, 136 Constructor constructorMethod) 137 { 138 super(description, constructorMethod); 139 140 if (tracing()) 141 { 142 trace("ModelMBeanConstructorInfo(String, Method)","Executed"); 143 } 144 consDescriptor = createDefaultDescriptor(); 145 146 149 } 150 151 171 172 public ModelMBeanConstructorInfo(String description, 173 Constructor constructorMethod, 174 Descriptor descriptor) 175 { 176 177 super(description, constructorMethod); 178 if (tracing()) 180 { 181 trace("ModelMBeanConstructorInfo(String, Method, Descriptor)","Executed"); 182 } 183 if (descriptor == null) 184 { 185 if (tracing()) 186 { 187 trace("ModelMBeanConstructorInfo(String, Method, Descriptor)","Descriptor passed in is null, setting descriptor to default values"); 188 } 189 190 consDescriptor = createDefaultDescriptor(); 191 } else 192 { 193 if (isValid(descriptor)) 194 { 195 consDescriptor = (Descriptor ) descriptor.clone(); 196 } else 197 { consDescriptor = createDefaultDescriptor(); 199 throw new RuntimeOperationsException(new IllegalArgumentException ("Invalid descriptor passed in parameter"), ("Exception occured in ModelMBeanConstructorInfo constructor")); 200 201 } 202 203 } 204 } 205 212 213 public ModelMBeanConstructorInfo(String name, 214 String description, 215 MBeanParameterInfo[] signature) 216 { 217 218 super(name, description, signature); 219 if (tracing()) 221 { 222 trace("ModelMBeanConstructorInfo(String, String, MBeanParameterInfo[])","Executed"); 223 } 224 consDescriptor = createDefaultDescriptor(); 225 } 226 240 241 public ModelMBeanConstructorInfo(String name, 242 String description, 243 MBeanParameterInfo[] signature, 244 Descriptor descriptor) 245 { 246 super(name, description, signature); 247 if (tracing()) 248 { 249 trace("ModelMBeanConstructorInfo(String, String, MBeanParameterInfo[], Descriptor)","Executed"); 250 } 251 if (descriptor == null) 252 { 253 if (tracing()) 254 { 255 trace("ModelMBeanConstructorInfo(String, Method, Descriptor)","Descriptor passed in is null, setting descriptor to default values"); 256 } 257 consDescriptor = createDefaultDescriptor(); 258 } else 259 { 260 if (isValid(descriptor)) 261 { 262 consDescriptor = (Descriptor ) descriptor.clone(); 263 } else 264 { consDescriptor = createDefaultDescriptor(); 266 throw new RuntimeOperationsException(new IllegalArgumentException ("Invalid descriptor passed in parameter"), ("Exception occured in ModelMBeanConstructorInfo constructor")); 267 268 } 269 } 270 } 271 272 278 ModelMBeanConstructorInfo(ModelMBeanConstructorInfo old) 279 { 280 super(old.getName(), old.getDescription(), old.getSignature()); 281 if (tracing()) 282 { 283 trace("ModelMBeanConstructorInfo(ModelMBeanConstructorInfo)","Executed"); 284 } 285 if (old.consDescriptor == null) 286 { 287 if (tracing()) 288 { 289 trace("ModelMBeanConstructorInfo(String, Method, Descriptor)","Existing descriptor passed in is null, setting new descriptor to default values"); 290 } 291 consDescriptor = createDefaultDescriptor(); 292 } else 293 { 294 if (isValid(consDescriptor)) 295 { 296 consDescriptor = (Descriptor ) old.consDescriptor.clone(); 297 } else 298 { consDescriptor = createDefaultDescriptor(); 300 throw new RuntimeOperationsException(new IllegalArgumentException ("Invalid descriptor passed in parameter"), ("Exception occured in ModelMBeanConstructorInfo constructor")); 301 302 } 303 304 } 305 } 306 307 311 public Object clone () 312 { 313 if (tracing()) 314 { 315 trace("ModelMBeanConstructorInfo.clone()","Executed"); 316 } 317 return(new ModelMBeanConstructorInfo (this)) ; 318 } 319 320 328 329 330 public Descriptor getDescriptor() 331 { 332 if (tracing()) 333 { 334 trace("ModelMBeanConstructorInfo.getDescriptor()","Executed"); 335 } 336 if (consDescriptor == null) 337 { 338 consDescriptor = createDefaultDescriptor(); 339 } 340 return((Descriptor )consDescriptor.clone()); 341 } 342 365 public void setDescriptor(Descriptor inDescriptor) 366 { 367 if (tracing()) 368 { 369 trace("ModelMBeanConstructorInfo.setDescriptor()","Executed"); 370 } 371 if (inDescriptor == null) 372 { 373 if (tracing()) 374 { 375 trace("ModelMBeanConstructorInfo(String, Method, Descriptor)","Descriptor passed in is null, setting descriptor to default values"); 376 } 377 consDescriptor = createDefaultDescriptor(); 378 } else 379 { 380 if (isValid(inDescriptor)) 381 { 382 consDescriptor = (Descriptor ) inDescriptor.clone(); 383 } else 384 { 385 throw new RuntimeOperationsException(new IllegalArgumentException ("Invalid descriptor passed in parameter"), ("Exception occured in ModelMBeanConstructorInfo setDescriptor")); 386 } 387 } 388 } 389 390 393 public String toString() 394 { 395 if (tracing()) 396 { 397 trace("ModelMBeanConstructorInfo.toString()","Executed"); 398 } 399 String retStr = 400 "ModelMBeanConstructorInfo: " + this.getName() + 401 " ; Description: " + this.getDescription() + 402 " ; Descriptor: " + this.getDescriptor() + 403 " ; Signature: "; 404 MBeanParameterInfo[] pTypes = this.getSignature(); 405 for (int i=0; i < pTypes.length; i++) 406 { 407 retStr = retStr.concat((pTypes[i]).getType() + ", "); 408 } 409 return retStr; 410 } 411 416 private Descriptor createDefaultDescriptor() 417 { 418 if (tracing()) 419 { 420 trace("ModelMBeanConstructorInfo.createDefaultDescriptor()","Executed"); 421 } 422 return new DescriptorSupport (new String [] {"descriptorType=operation", 423 "role=constructor", 424 ("name=" + this.getName()), 425 ("displayname=" + this.getName())}); 426 } 427 428 436 private boolean isValid(Descriptor inDesc) 437 { 438 boolean results = true; 439 String badField="none"; 440 if (inDesc == null) 444 { 445 badField="nullDescriptor"; 446 results = false; 447 } 448 449 else if (!inDesc.isValid()) 450 { badField="invalidDescriptor"; 453 results = false; 454 } 455 456 else 457 { 458 if (! ((String )inDesc.getFieldValue("name")).equalsIgnoreCase(this.getName())) 459 { 460 badField="name"; 461 results = false; 462 } 463 if (! ((String )inDesc.getFieldValue("descriptorType")).equalsIgnoreCase("operation")) 464 { 465 badField="descriptorType"; 466 results = false; 467 } 468 if (inDesc.getFieldValue("role") == null) 469 { 470 inDesc.setField("role","constructor"); 471 } 472 if (! ((String )inDesc.getFieldValue("role")).equalsIgnoreCase("constructor")) 473 { 474 badField = "role"; 475 results = false; 476 } else if ((inDesc.getFieldValue("displayName")) == null) 477 { 478 inDesc.setField("displayName",this.getName()); 479 } 480 } 481 if (tracing()) trace("isValid()",("Returning " + results + ": Invalid field is " + badField)); 482 return results; 483 } 484 485 private boolean tracing() 487 { 488 return Trace.isSelected(Trace.LEVEL_TRACE, Trace.INFO_MODELMBEAN); 490 } 491 492 private void trace(String inClass, String inMethod, String inText) 493 { 494 Trace.send(Trace.LEVEL_TRACE, Trace.INFO_MODELMBEAN, inClass, 496 inMethod, Integer.toHexString(this.hashCode()) + " " + inText); 497 } 498 499 private void trace(String inMethod, String inText) 500 { 501 trace(currClass, inMethod, inText); 502 } 503 504 507 private void readObject(ObjectInputStream in) 508 throws IOException , ClassNotFoundException { 509 in.defaultReadObject(); 511 } 512 513 514 517 private void writeObject(ObjectOutputStream out) 518 throws IOException { 519 if (compat) 520 { 521 ObjectOutputStream.PutField fields = out.putFields(); 524 fields.put("consDescriptor", consDescriptor); 525 fields.put("currClass", currClass); 526 out.writeFields(); 527 } 528 else 529 { 530 out.defaultWriteObject(); 533 } 534 } 535 536 } 537 | Popular Tags |