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