J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * Portions Copyright 2000-2006 Sun Microsystems, Inc. All Rights Reserved. |
| 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
| 4 | * |
| 5 | * This code is free software; you can redistribute it and/or modify it |
| 6 | * under the terms of the GNU General Public License version 2 only, as |
| 7 | * published by the Free Software Foundation. Sun designates this |
| 8 | * particular file as subject to the "Classpath" exception as provided |
| 9 | * by Sun in the LICENSE file that accompanied this code. |
| 10 | * |
| 11 | * This code is distributed in the hope that it will be useful, but WITHOUT |
| 12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 13 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 14 | * version 2 for more details (a copy is included in the LICENSE file that |
| 15 | * accompanied this code). |
| 16 | * |
| 17 | * You should have received a copy of the GNU General Public License version |
| 18 | * 2 along with this work; if not, write to the Free Software Foundation, |
| 19 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
| 20 | * |
| 21 | * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
| 22 | * CA 95054 USA or visit www.sun.com if you need additional information or |
| 23 | * have any questions. |
| 24 | */ |
| 25 | /* |
| 26 | * @author IBM Corp. |
| 27 | * |
| 28 | * Copyright IBM Corp. 1999-2000. All rights reserved. |
| 29 | */ |
| 30 | |
| 31 | package javax.management.modelmbean; |
| 32 | |
| 33 | import static com.sun.jmx.defaults.JmxProperties.MODELMBEAN_LOGGER; |
| 34 | import com.sun.jmx.mbeanserver.GetPropertyAction; |
| 35 | |
| 36 | import java.io.IOException; |
| 37 | import java.io.ObjectInputStream; |
| 38 | import java.io.ObjectOutputStream; |
| 39 | import java.io.ObjectStreamField; |
| 40 | import java.lang.reflect.Constructor; |
| 41 | import java.security.AccessController; |
| 42 | import java.util.logging.Level; |
| 43 | |
| 44 | import javax.management.Descriptor; |
| 45 | import javax.management.DescriptorAccess; |
| 46 | import javax.management.DescriptorKey; |
| 47 | import javax.management.MBeanConstructorInfo; |
| 48 | import javax.management.MBeanParameterInfo; |
| 49 | import javax.management.RuntimeOperationsException; |
| 50 | |
| 51 | /** |
| 52 | * The ModelMBeanConstructorInfo object describes a constructor of the ModelMBean. |
| 53 | * It is a subclass of MBeanConstructorInfo with the addition of an associated Descriptor |
| 54 | * and an implementation of the DescriptorAccess interface. |
| 55 | * <P> |
| 56 | * <PRE> |
| 57 | * The fields in the descriptor are defined, but not limited to, the following: <P> |
| 58 | * name : constructor name |
| 59 | * descriptorType : must be "operation" |
| 60 | * role : must be "constructor" |
| 61 | * displayName : human readable name of constructor |
| 62 | * visibility : 1-4 where 1: always visible 4: rarely visible |
| 63 | * presentationString : xml formatted string to describe how to present operation |
| 64 | *</PRE> |
| 65 | * |
| 66 | * <p>The {@code persistPolicy} and {@code currencyTimeLimit} fields |
| 67 | * are meaningless for constructors, but are not considered invalid. |
| 68 | * |
| 69 | * <p>The default descriptor will have the {@code name}, {@code |
| 70 | * descriptorType}, {@code displayName} and {@code role} fields. The |
| 71 | * default value of the {@code name} and {@code displayName} fields is |
| 72 | * the name of the constructor. |
| 73 | * |
| 74 | * <p>The <b>serialVersionUID</b> of this class is <code>3862947819818064362L</code>. |
| 75 | * |
| 76 | * @since 1.5 |
| 77 | */ |
| 78 | |
| 79 | @SuppressWarnings("serial") // serialVersionUID is not constant |
| 80 | public class ModelMBeanConstructorInfo |
| 81 | extends MBeanConstructorInfo |
| 82 | implements DescriptorAccess { |
| 83 | |
| 84 | // Serialization compatibility stuff: |
| 85 | // Two serial forms are supported in this class. The selected form depends |
| 86 | // on system property "jmx.serial.form": |
| 87 | // - "1.0" for JMX 1.0 |
| 88 | // - any other value for JMX 1.1 and higher |
| 89 | // |
| 90 | // Serial version for old serial form |
| 91 | private static final long oldSerialVersionUID = -4440125391095574518L; |
| 92 | // |
| 93 | // Serial version for new serial form |
| 94 | private static final long newSerialVersionUID = 3862947819818064362L; |
| 95 | // |
| 96 | // Serializable fields in old serial form |
| 97 | private static final ObjectStreamField[] oldSerialPersistentFields = |
| 98 | { |
| 99 | new ObjectStreamField("consDescriptor", Descriptor.class), |
| 100 | new ObjectStreamField("currClass", String.class) |
| 101 | }; |
| 102 | // |
| 103 | // Serializable fields in new serial form |
| 104 | private static final ObjectStreamField[] newSerialPersistentFields = |
| 105 | { |
| 106 | new ObjectStreamField("consDescriptor", Descriptor.class) |
| 107 | }; |
| 108 | // |
| 109 | // Actual serial version and serial form |
| 110 | private static final long serialVersionUID; |
| 111 | /** |
| 112 | * @serialField consDescriptor Descriptor The {@link Descriptor} containing the metadata for this instance |
| 113 | */ |
| 114 | private static final ObjectStreamField[] serialPersistentFields; |
| 115 | private static boolean compat = false; |
| 116 | static { |
| 117 | try { |
| 118 | GetPropertyAction act = new GetPropertyAction("jmx.serial.form"); |
| 119 | String form = AccessController.doPrivileged(act); |
| 120 | compat = (form != null && form.equals("1.0")); |
| 121 | } catch (Exception e) { |
| 122 | // OK: No compat with 1.0 |
| 123 | } |
| 124 | if (compat) { |
| 125 | serialPersistentFields = oldSerialPersistentFields; |
| 126 | serialVersionUID = oldSerialVersionUID; |
| 127 | } else { |
| 128 | serialPersistentFields = newSerialPersistentFields; |
| 129 | serialVersionUID = newSerialVersionUID; |
| 130 | } |
| 131 | } |
| 132 | // |
| 133 | // END Serialization compatibility stuff |
| 134 | |
| 135 | /** |
| 136 | * @serial The {@link Descriptor} containing the metadata for this instance |
| 137 | */ |
| 138 | private Descriptor consDescriptor = validDescriptor(null); |
| 139 | |
| 140 | private final static String currClass = "ModelMBeanConstructorInfo"; |
| 141 | |
| 142 | |
| 143 | /** |
| 144 | * Constructs a ModelMBeanConstructorInfo object with a default |
| 145 | * descriptor. The {@link Descriptor} of the constructed |
| 146 | * object will include fields contributed by any annotations on |
| 147 | * the {@code Constructor} object that contain the {@link |
| 148 | * DescriptorKey} meta-annotation. |
| 149 | * |
| 150 | * @param description A human readable description of the constructor. |
| 151 | * @param constructorMethod The java.lang.reflect.Constructor object |
| 152 | * describing the MBean constructor. |
| 153 | */ |
| 154 | public ModelMBeanConstructorInfo(String description, |
| 155 | Constructor constructorMethod) |
| 156 | { |
| 157 | super(description, constructorMethod); |
| 158 | if (MODELMBEAN_LOGGER.isLoggable(Level.FINER)) { |
| 159 | MODELMBEAN_LOGGER.logp(Level.FINER, |
| 160 | ModelMBeanConstructorInfo.class.getName(), |
| 161 | "ModelMBeanConstructorInfo(String,Constructor)", |
| 162 | "Entry"); |
| 163 | } |
| 164 | consDescriptor = validDescriptor(null); |
| 165 | |
| 166 | // put getter and setter methods in constructors list |
| 167 | // create default descriptor |
| 168 | |
| 169 | } |
| 170 | |
| 171 | /** |
| 172 | * Constructs a ModelMBeanConstructorInfo object. The {@link |
| 173 | * Descriptor} of the constructed object will include fields |
| 174 | * contributed by any annotations on the {@code Constructor} |
| 175 | * object that contain the {@link DescriptorKey} |
| 176 | * meta-annotation. |
| 177 | * |
| 178 | * @param description A human readable description of the constructor. |
| 179 | * @param constructorMethod The java.lang.reflect.Constructor object |
| 180 | * describing the ModelMBean constructor. |
| 181 | * @param descriptor An instance of Descriptor containing the |
| 182 | * appropriate metadata for this instance of the |
| 183 | * ModelMBeanConstructorInfo. If it is null, then a default |
| 184 | * descriptor will be created. If the descriptor does not |
| 185 | * contain all the following fields, the missing ones are added with |
| 186 | * their default values: displayName, name, role, descriptorType. |
| 187 | * |
| 188 | * @exception RuntimeOperationsException Wraps an |
| 189 | * IllegalArgumentException. The descriptor is invalid, or |
| 190 | * descriptor field "name" is present but not equal to name |
| 191 | * parameter, or descriptor field "descriptorType" is present |
| 192 | * but not equal to "operation" or descriptor field "role" is |
| 193 | * present but not equal to "constructor". |
| 194 | */ |
| 195 | |
| 196 | public ModelMBeanConstructorInfo(String description, |
| 197 | Constructor constructorMethod, |
| 198 | Descriptor descriptor) |
| 199 | { |
| 200 | |
| 201 | super(description, constructorMethod); |
| 202 | // put getter and setter methods in constructors list |
| 203 | if (MODELMBEAN_LOGGER.isLoggable(Level.FINER)) { |
| 204 | MODELMBEAN_LOGGER.logp(Level.FINER, |
| 205 | ModelMBeanConstructorInfo.class.getName(), |
| 206 | "ModelMBeanConstructorInfo(" + |
| 207 | "String,Constructor,Descriptor)", "Entry"); |
| 208 | } |
| 209 | consDescriptor = validDescriptor(descriptor); |
| 210 | } |
| 211 | /** |
| 212 | * Constructs a ModelMBeanConstructorInfo object with a default descriptor. |
| 213 | * |
| 214 | * @param name The name of the constructor. |
| 215 | * @param description A human readable description of the constructor. |
| 216 | * @param signature MBeanParameterInfo object array describing the parameters(arguments) of the constructor. |
| 217 | */ |
| 218 | |
| 219 | public ModelMBeanConstructorInfo(String name, |
| 220 | String description, |
| 221 | MBeanParameterInfo[] signature) |
| 222 | { |
| 223 | |
| 224 | super(name, description, signature); |
| 225 | // create default descriptor |
| 226 | if (MODELMBEAN_LOGGER.isLoggable(Level.FINER)) { |
| 227 | MODELMBEAN_LOGGER.logp(Level.FINER, |
| 228 | ModelMBeanConstructorInfo.class.getName(), |
| 229 | "ModelMBeanConstructorInfo(" + |
| 230 | "String,String,MBeanParameterInfo[])", "Entry"); |
| 231 | } |
| 232 | consDescriptor = validDescriptor(null); |
| 233 | } |
| 234 | /** |
| 235 | * Constructs a ModelMBeanConstructorInfo object. |
| 236 | * |
| 237 | * @param name The name of the constructor. |
| 238 | * @param description A human readable description of the constructor. |
| 239 | * @param signature MBeanParameterInfo objects describing the parameters(arguments) of the constructor. |
| 240 | * @param descriptor An instance of Descriptor containing the appropriate metadata |
| 241 | * for this instance of the MBeanConstructorInfo. If it is null then a default descriptor will be created. |
| 242 | * If the descriptor does not |
| 243 | * contain all the following fields, the missing ones are added with |
| 244 | * their default values: displayName, name, role, descriptorType. |
| 245 | * |
| 246 | * @exception RuntimeOperationsException Wraps an |
| 247 | * IllegalArgumentException. The descriptor is invalid, or |
| 248 | * descriptor field "name" is present but not equal to name |
| 249 | * parameter, or descriptor field "descriptorType" is present |
| 250 | * but not equal to "operation" or descriptor field "role" is |
| 251 | * present but not equal to "constructor". |
| 252 | */ |
| 253 | |
| 254 | public ModelMBeanConstructorInfo(String name, |
| 255 | String description, |
| 256 | MBeanParameterInfo[] signature, |
| 257 | Descriptor descriptor) |
| 258 | { |
| 259 | super(name, description, signature); |
| 260 | if (MODELMBEAN_LOGGER.isLoggable(Level.FINER)) { |
| 261 | MODELMBEAN_LOGGER.logp(Level.FINER, |
| 262 | ModelMBeanConstructorInfo.class.getName(), |
| 263 | "ModelMBeanConstructorInfo(" + |
| 264 | "String,String,MBeanParameterInfo[],Descriptor)", |
| 265 | "Entry"); |
| 266 | } |
| 267 | consDescriptor = validDescriptor(descriptor); |
| 268 | } |
| 269 | |
| 270 | /** |
| 271 | * Constructs a new ModelMBeanConstructorInfo object from this ModelMBeanConstructor Object. |
| 272 | * |
| 273 | * @param old the ModelMBeanConstructorInfo to be duplicated |
| 274 | * |
| 275 | */ |
| 276 | ModelMBeanConstructorInfo(ModelMBeanConstructorInfo old) |
| 277 | { |
| 278 | super(old.getName(), old.getDescription(), old.getSignature()); |
| 279 | if (MODELMBEAN_LOGGER.isLoggable(Level.FINER)) { |
| 280 | MODELMBEAN_LOGGER.logp(Level.FINER, |
| 281 | ModelMBeanConstructorInfo.class.getName(), |
| 282 | "ModelMBeanConstructorInfo(" + |
| 283 | "ModelMBeanConstructorInfo)", "Entry"); |
| 284 | } |
| 285 | consDescriptor = validDescriptor(consDescriptor); |
| 286 | } |
| 287 | |
| 288 | /** |
| 289 | * Creates and returns a new ModelMBeanConstructorInfo which is a duplicate of this ModelMBeanConstructorInfo. |
| 290 | * |
| 291 | */ |
| 292 | public Object clone () |
| 293 | { |
| 294 | if (MODELMBEAN_LOGGER.isLoggable(Level.FINER)) { |
| 295 | MODELMBEAN_LOGGER.logp(Level.FINER, |
| 296 | ModelMBeanConstructorInfo.class.getName(), |
| 297 | "clone()", "Entry"); |
| 298 | } |
| 299 | return(new ModelMBeanConstructorInfo(this)) ; |
| 300 | } |
| 301 | |
| 302 | /** |
| 303 | * Returns a copy of the associated Descriptor. |
| 304 | * |
| 305 | * @return Descriptor associated with the |
| 306 | * ModelMBeanConstructorInfo object. |
| 307 | * |
| 308 | * @see #setDescriptor |
| 309 | */ |
| 310 | |
| 311 | |
| 312 | public Descriptor getDescriptor() |
| 313 | { |
| 314 | if (MODELMBEAN_LOGGER.isLoggable(Level.FINER)) { |
| 315 | MODELMBEAN_LOGGER.logp(Level.FINER, |
| 316 | ModelMBeanConstructorInfo.class.getName(), |
| 317 | "getDescriptor()", "Entry"); |
| 318 | } |
| 319 | if (consDescriptor == null){ |
| 320 | consDescriptor = validDescriptor(null); |
| 321 | } |
| 322 | return((Descriptor)consDescriptor.clone()); |
| 323 | } |
| 324 | /** |
| 325 | * Sets associated Descriptor (full replace) of |
| 326 | * ModelMBeanConstructorInfo. If the new Descriptor is null, |
| 327 | * then the associated Descriptor reverts to a default |
| 328 | * descriptor. The Descriptor is validated before it is |
| 329 | * assigned. If the new Descriptor is invalid, then a |
| 330 | * RuntimeOperationsException wrapping an |
| 331 | * IllegalArgumentException is thrown. |
| 332 | * |
| 333 | * @param inDescriptor replaces the Descriptor associated with |
| 334 | * the ModelMBeanConstructor. If the descriptor does not |
| 335 | * contain all the following fields, the missing ones are added with |
| 336 | * their default values: displayName, name, role, descriptorType. |
| 337 | * |
| 338 | * @exception RuntimeOperationsException Wraps an |
| 339 | * IllegalArgumentException. The descriptor is invalid, or |
| 340 | * descriptor field "name" is present but not equal to name |
| 341 | * parameter, or descriptor field "descriptorType" is present |
| 342 | * but not equal to "operation" or descriptor field "role" is |
| 343 | * present but not equal to "constructor". |
| 344 | * |
| 345 | * @see #getDescriptor |
| 346 | */ |
| 347 | public void setDescriptor(Descriptor inDescriptor) |
| 348 | { |
| 349 | if (MODELMBEAN_LOGGER.isLoggable(Level.FINER)) { |
| 350 | MODELMBEAN_LOGGER.logp(Level.FINER, |
| 351 | ModelMBeanConstructorInfo.class.getName(), |
| 352 | "setDescriptor()", "Entry"); |
| 353 | } |
| 354 | consDescriptor = validDescriptor(inDescriptor); |
| 355 | } |
| 356 | |
| 357 | /** |
| 358 | * Returns a string containing the entire contents of the ModelMBeanConstructorInfo in human readable form. |
| 359 | */ |
| 360 | public String toString() |
| 361 | { |
| 362 | if (MODELMBEAN_LOGGER.isLoggable(Level.FINER)) { |
| 363 | MODELMBEAN_LOGGER.logp(Level.FINER, |
| 364 | ModelMBeanConstructorInfo.class.getName(), |
| 365 | "toString()", "Entry"); |
| 366 | } |
| 367 | String retStr = |
| 368 | "ModelMBeanConstructorInfo: " + this.getName() + |
| 369 | " ; Description: " + this.getDescription() + |
| 370 | " ; Descriptor: " + this.getDescriptor() + |
| 371 | " ; Signature: "; |
| 372 | MBeanParameterInfo[] pTypes = this.getSignature(); |
| 373 | for (int i=0; i < pTypes.length; i++) |
| 374 | { |
| 375 | retStr = retStr.concat((pTypes[i]).getType() + ", "); |
| 376 | } |
| 377 | return retStr; |
| 378 | } |
| 379 | |
| 380 | |
| 381 | /** |
| 382 | * Clones the passed in Descriptor, sets default values, and checks for validity. |
| 383 | * If the Descriptor is invalid (for instance by having the wrong "name"), |
| 384 | * this indicates programming error and a RuntimeOperationsException will be thrown. |
| 385 | * |
| 386 | * The following fields will be defaulted if they are not already set: |
| 387 | * displayName=this.getName(), name=this.getName(), descriptorType="operation", |
| 388 | * role="constructor" |
| 389 | * |
| 390 | * |
| 391 | * @param in Descriptor to be checked, or null which is equivalent to |
| 392 | * an empty Descriptor. |
| 393 | * @exception RuntimeOperationsException if Descriptor is invalid |
| 394 | */ |
| 395 | private Descriptor validDescriptor(final Descriptor in) throws RuntimeOperationsException { |
| 396 | Descriptor clone = null; |
| 397 | if (in == null) { |
| 398 | clone = new DescriptorSupport(); |
| 399 | MODELMBEAN_LOGGER.finer("Null Descriptor, creating new."); |
| 400 | } else { |
| 401 | clone = (Descriptor) in.clone(); |
| 402 | } |
| 403 | |
| 404 | //Setting defaults. |
| 405 | if (clone.getFieldValue("name")==null) { |
| 406 | clone.setField("name", this.getName()); |
| 407 | MODELMBEAN_LOGGER.finer("Defaulting Descriptor name to " + this.getName()); |
| 408 | } |
| 409 | if (clone.getFieldValue("descriptorType")==null) { |
| 410 | clone.setField("descriptorType", "operation"); |
| 411 | MODELMBEAN_LOGGER.finer("Defaulting descriptorType to \"operation\""); |
| 412 | } |
| 413 | if (clone.getFieldValue("displayName") == null) { |
| 414 | clone.setField("displayName",this.getName()); |
| 415 | MODELMBEAN_LOGGER.finer("Defaulting Descriptor displayName to " + this.getName()); |
| 416 | } |
| 417 | if (clone.getFieldValue("role") == null) { |
| 418 | clone.setField("role","constructor"); |
| 419 | MODELMBEAN_LOGGER.finer("Defaulting Descriptor role field to \"constructor\""); |
| 420 | } |
| 421 | |
| 422 | //Checking validity |
| 423 | if (!clone.isValid()) { |
| 424 | throw new RuntimeOperationsException(new IllegalArgumentException("Invalid Descriptor argument"), |
| 425 | "The isValid() method of the Descriptor object itself returned false,"+ |
| 426 | "one or more required fields are invalid. Descriptor:" + clone.toString()); |
| 427 | } |
| 428 | if (! ((String)clone.getFieldValue("name")).equalsIgnoreCase(this.getName())) { |
| 429 | throw new RuntimeOperationsException(new IllegalArgumentException("Invalid Descriptor argument"), |
| 430 | "The Descriptor \"name\" field does not match the object described. " + |
| 431 | " Expected: "+ this.getName() + " , was: " + clone.getFieldValue("name")); |
| 432 | } |
| 433 | if (! ((String)clone.getFieldValue("descriptorType")).equalsIgnoreCase("operation")) { |
| 434 | throw new RuntimeOperationsException(new IllegalArgumentException("Invalid Descriptor argument"), |
| 435 | "The Descriptor \"descriptorType\" field does not match the object described. " + |
| 436 | " Expected: \"operation\" ," + " was: " + clone.getFieldValue("descriptorType")); |
| 437 | } |
| 438 | if (! ((String)clone.getFieldValue("role")).equalsIgnoreCase("constructor")) { |
| 439 | throw new RuntimeOperationsException(new IllegalArgumentException("Invalid Descriptor argument"), |
| 440 | "The Descriptor \"role\" field does not match the object described. " + |
| 441 | " Expected: \"constructor\" ," + " was: " + clone.getFieldValue("role")); |
| 442 | } |
| 443 | |
| 444 | return clone; |
| 445 | } |
| 446 | |
| 447 | /** |
| 448 | * Deserializes a {@link ModelMBeanConstructorInfo} from an {@link ObjectInputStream}. |
| 449 | */ |
| 450 | private void readObject(ObjectInputStream in) |
| 451 | throws IOException, ClassNotFoundException { |
| 452 | // New serial form ignores extra field "currClass" |
| 453 | in.defaultReadObject(); |
| 454 | } |
| 455 | |
| 456 | |
| 457 | /** |
| 458 | * Serializes a {@link ModelMBeanConstructorInfo} to an {@link ObjectOutputStream}. |
| 459 | */ |
| 460 | private void writeObject(ObjectOutputStream out) |
| 461 | throws IOException { |
| 462 | if (compat) |
| 463 | { |
| 464 | // Serializes this instance in the old serial form |
| 465 | // |
| 466 | ObjectOutputStream.PutField fields = out.putFields(); |
| 467 | fields.put("consDescriptor", consDescriptor); |
| 468 | fields.put("currClass", currClass); |
| 469 | out.writeFields(); |
| 470 | } |
| 471 | else |
| 472 | { |
| 473 | // Serializes this instance in the new serial form |
| 474 | // |
| 475 | out.defaultWriteObject(); |
| 476 | } |
| 477 | } |
| 478 | |
| 479 | } |