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 40 import com.sun.jmx.mbeanserver.GetPropertyAction; 41 import com.sun.jmx.trace.Trace; 42 43 44 73 74 public class ModelMBeanNotificationInfo extends MBeanNotificationInfo 77 implements DescriptorAccess , Cloneable 78 { 79 80 private static final long oldSerialVersionUID = -5211564525059047097L; 88 private static final long newSerialVersionUID = -7445681389570207141L; 91 private static final ObjectStreamField [] oldSerialPersistentFields = 94 { 95 new ObjectStreamField ("notificationDescriptor", Descriptor .class), 96 new ObjectStreamField ("currClass", String .class) 97 }; 98 private static final ObjectStreamField [] newSerialPersistentFields = 101 { 102 new ObjectStreamField ("notificationDescriptor", Descriptor .class) 103 }; 104 private static final long serialVersionUID; 107 111 private static final ObjectStreamField [] serialPersistentFields; 112 private static boolean compat = false; 113 static { 114 try { 115 PrivilegedAction act = new GetPropertyAction("jmx.serial.form"); 116 String form = (String ) AccessController.doPrivileged(act); 117 compat = (form != null && form.equals("1.0")); 118 } catch (Exception e) { 119 } 121 if (compat) { 122 serialPersistentFields = oldSerialPersistentFields; 123 serialVersionUID = oldSerialVersionUID; 124 } else { 125 serialPersistentFields = newSerialPersistentFields; 126 serialVersionUID = newSerialVersionUID; 127 } 128 } 129 132 136 private Descriptor notificationDescriptor; 137 138 private static final String currClass = "ModelMBeanNotificationInfo"; 139 140 150 public ModelMBeanNotificationInfo(String [] notifTypes, 151 String name, 152 String description) { 153 this(notifTypes,name,description,null); 154 } 155 156 177 public ModelMBeanNotificationInfo(String [] notifTypes, 178 String name, 179 String description, 180 Descriptor descriptor) { 181 super(notifTypes, name, description); 182 if (tracing()) 183 trace("ModelMBeanNotificationInfo()","Executed"); 184 applyDescriptor(descriptor,"ModelMBeanNotificationInfo()"); 185 } 186 187 194 public ModelMBeanNotificationInfo(ModelMBeanNotificationInfo inInfo) { 195 this(inInfo.getNotifTypes(), 196 inInfo.getName(), 197 inInfo.getDescription(),inInfo.getDescriptor()); 198 } 199 200 204 public Object clone () { 205 if (tracing()) 206 trace("ModelMBeanNotificationInfo.clone()","Executed"); 207 return(new ModelMBeanNotificationInfo (this)); 208 } 209 210 219 public Descriptor getDescriptor() { 220 if (tracing()) 221 trace("ModelMBeanNotificationInfo.getDescriptor()","Executed"); 222 223 if (notificationDescriptor == null) { 224 if (tracing()) 226 trace("ModelMBeanNotificationInfo.getDescriptor()", 227 "Received null for new descriptor value, " + 228 "setting descriptor to default values"); 229 230 notificationDescriptor = createDefaultDescriptor(); 231 } 232 233 return((Descriptor )notificationDescriptor.clone()); 234 } 235 236 253 public void setDescriptor(Descriptor inDescriptor) { 254 if (tracing()) 255 trace("setDescriptor(Descriptor)", 256 "Executed"); 257 applyDescriptor(inDescriptor,"setDescriptor(Descriptor)"); 258 } 259 260 266 public String toString() { 267 if (tracing()) 268 trace("toString()","Executed"); 269 270 final StringBuffer retStr = new StringBuffer (); 271 272 retStr.append("ModelMBeanNotificationInfo: ") 273 .append(this.getName()); 274 275 retStr.append(" ; Description: ") 276 .append(this.getDescription()); 277 278 retStr.append(" ; Descriptor: ") 279 .append(this.getDescriptor()); 280 281 retStr.append(" ; Types: "); 282 String [] nTypes = this.getNotifTypes(); 283 for (int i=0; i < nTypes.length; i++) { 284 if (i > 0) retStr.append(", "); 285 retStr.append(nTypes[i]); 286 } 287 return retStr.toString(); 288 } 289 290 295 private final Descriptor createDefaultDescriptor() { 296 if (tracing()) 297 trace("createDefaultDescriptor()","Executed"); 298 299 return new DescriptorSupport (new 300 String [] {"descriptorType=notification", 301 ("name=" + this.getName()), 302 ("displayName=" + this.getName()), 303 "severity=6"}); 304 } 305 306 313 private boolean isValid(Descriptor inDesc) { 314 boolean results = true; 315 String badField = "none"; 316 317 if (inDesc == null) { 318 badField="nullDescriptor"; 319 return false; 320 } 321 322 if (!inDesc.isValid()) { 323 badField="invalidDescriptor"; 327 results = false; 328 } else if (!((String )inDesc.getFieldValue("name")). 329 equalsIgnoreCase(this.getName())) { 330 badField="name"; 331 results = false; 332 } else if (! ((String )inDesc.getFieldValue("descriptorType")). 333 equalsIgnoreCase("notification")) { 334 badField="descriptorType"; 335 results = false; 336 } 337 338 if (tracing()) trace("isValid()",("Returning " + results + 339 ": Invalid field is " + badField)); 340 return results; 341 } 342 343 349 private final Descriptor setDefaults(Descriptor descriptor) { 350 if ((descriptor.getFieldValue("displayName")) == null) { 351 descriptor.setField("displayName",this.getName()); 352 } 353 if ((descriptor.getFieldValue("severity")) == null) { 354 descriptor.setField("severity","6"); 355 } 356 return descriptor; 357 } 358 359 371 private final void applyDescriptor(Descriptor descriptor, 372 String ftag) { 373 if (descriptor == null) { 374 if (tracing()) 375 trace(ftag, 376 "Received null for new descriptor value, " + 377 "setting descriptor to default values"); 378 379 notificationDescriptor = createDefaultDescriptor(); 380 } else if (isValid(descriptor)) { 381 notificationDescriptor = 382 setDefaults((Descriptor )descriptor.clone()); 383 } else { 384 throw new RuntimeOperationsException(new 385 IllegalArgumentException ( 386 "Invalid descriptor passed in parameter"), 387 "Exception occured in ModelMBeanNotificationInfo " + ftag); 388 } 389 } 390 391 private boolean tracing() { 393 return Trace.isSelected(Trace.LEVEL_TRACE, Trace.INFO_MODELMBEAN); 395 } 396 397 private void trace(String inClass, String inMethod, String inText) { 398 Trace.send(Trace.LEVEL_TRACE, Trace.INFO_MODELMBEAN, inClass, 399 inMethod, Integer.toHexString(this.hashCode()) + 400 " " + inText); 401 } 402 403 private void trace(String inMethod, String inText) { 404 trace(currClass, inMethod, inText); 405 } 406 407 411 private void readObject(ObjectInputStream in) 412 throws IOException , ClassNotFoundException { 413 in.defaultReadObject(); 415 } 416 417 418 422 private void writeObject(ObjectOutputStream out) 423 throws IOException { 424 if (compat) { 425 ObjectOutputStream.PutField fields = out.putFields(); 428 fields.put("notificationDescriptor", notificationDescriptor); 429 fields.put("currClass", currClass); 430 out.writeFields(); 431 } else { 432 out.defaultWriteObject(); 435 } 436 } 437 438 } 439 | Popular Tags |