1. /*
  2. * @(#)file ModelMBeanConstructorInfo.java
  3. * @(#)author IBM Corp.
  4. * @(#)version 1.33
  5. * @(#)lastedit 03/12/19
  6. */
  7. /*
  8. * Copyright IBM Corp. 1999-2000. All rights reserved.
  9. *
  10. * The program is provided "as is" without any warranty express or implied,
  11. * including the warranty of non-infringement and the implied warranties of
  12. * merchantibility and fitness for a particular purpose. IBM will not be
  13. * liable for any damages suffered by you or any third party claim against
  14. * you regarding the Program.
  15. *
  16. * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
  17. * This software is the proprietary information of Sun Microsystems, Inc.
  18. * Use is subject to license terms.
  19. *
  20. * Copyright 2004 Sun Microsystems, Inc. Tous droits reserves.
  21. * Ce logiciel est propriete de Sun Microsystems, Inc.
  22. * Distribue par des licences qui en restreignent l'utilisation.
  23. *
  24. */
  25. package javax.management.modelmbean;
  26. import java.io.IOException;
  27. import java.io.ObjectInputStream;
  28. import java.io.ObjectOutputStream;
  29. import java.io.ObjectStreamField;
  30. import java.security.AccessController;
  31. import java.security.PrivilegedAction;
  32. import javax.management.Descriptor;
  33. import javax.management.DescriptorAccess;
  34. import javax.management.*;
  35. import java.lang.reflect.*;
  36. import com.sun.jmx.mbeanserver.GetPropertyAction;
  37. import com.sun.jmx.trace.Trace;
  38. /**
  39. * The ModelMBeanConstructorInfo object describes a constructor of the ModelMBean.
  40. * It is a subclass of MBeanConstructorInfo with the addition of an associated Descriptor
  41. * and an implementation of the DescriptorAccess interface.
  42. * <P>
  43. * <PRE>
  44. * The fields in the descriptor are defined, but not limited to, the following: <P>
  45. * name : constructor name
  46. * descriptorType : must be "operation"
  47. * role : must be "constructor"
  48. * displayName : human readable name of constructor
  49. * visibility : 1-4 where 1: always visible 4: rarely visible
  50. * presentationString : xml formatted string to describe how to present operation
  51. *</PRE>
  52. * The persistPolicy and currencyTimeLimit fields are not valid for the constructor.
  53. * The default descriptor will have the name, descriptorType, displayName and role fields.
  54. *
  55. * @since 1.5
  56. */
  57. public class ModelMBeanConstructorInfo extends MBeanConstructorInfo
  58. implements DescriptorAccess, Cloneable
  59. {
  60. // Serialization compatibility stuff:
  61. // Two serial forms are supported in this class. The selected form depends
  62. // on system property "jmx.serial.form":
  63. // - "1.0" for JMX 1.0
  64. // - any other value for JMX 1.1 and higher
  65. //
  66. // Serial version for old serial form
  67. private static final long oldSerialVersionUID = -4440125391095574518L;
  68. //
  69. // Serial version for new serial form
  70. private static final long newSerialVersionUID = 3862947819818064362L;
  71. //
  72. // Serializable fields in old serial form
  73. private static final ObjectStreamField[] oldSerialPersistentFields =
  74. {
  75. new ObjectStreamField("consDescriptor", Descriptor.class),
  76. new ObjectStreamField("currClass", String.class)
  77. };
  78. //
  79. // Serializable fields in new serial form
  80. private static final ObjectStreamField[] newSerialPersistentFields =
  81. {
  82. new ObjectStreamField("consDescriptor", Descriptor.class)
  83. };
  84. //
  85. // Actual serial version and serial form
  86. private static final long serialVersionUID;
  87. /**
  88. * @serialField consDescriptor Descriptor The {@link Descriptor} containing the metadata for this instance
  89. */
  90. private static final ObjectStreamField[] serialPersistentFields;
  91. private static boolean compat = false;
  92. static {
  93. try {
  94. PrivilegedAction act = new GetPropertyAction("jmx.serial.form");
  95. String form = (String) AccessController.doPrivileged(act);
  96. compat = (form != null && form.equals("1.0"));
  97. } catch (Exception e) {
  98. // OK: No compat with 1.0
  99. }
  100. if (compat) {
  101. serialPersistentFields = oldSerialPersistentFields;
  102. serialVersionUID = oldSerialVersionUID;
  103. } else {
  104. serialPersistentFields = newSerialPersistentFields;
  105. serialVersionUID = newSerialVersionUID;
  106. }
  107. }
  108. //
  109. // END Serialization compatibility stuff
  110. /**
  111. * @serial The {@link Descriptor} containing the metadata for this instance
  112. */
  113. private Descriptor consDescriptor = createDefaultDescriptor();
  114. private final static String currClass = "ModelMBeanConstructorInfo";
  115. /**
  116. * Constructs a MBeanConstructorInfo object with a default descriptor.
  117. *
  118. * @param description A human readable description of the constructor.
  119. * @param constructorMethod The java.lang.reflect.Method object
  120. * describing the MBean constructor.
  121. */
  122. public ModelMBeanConstructorInfo(String description,
  123. Constructor constructorMethod)
  124. {
  125. super(description, constructorMethod);
  126. if (tracing())
  127. {
  128. trace("ModelMBeanConstructorInfo(String, Method)","Executed");
  129. }
  130. consDescriptor = createDefaultDescriptor();
  131. // put getter and setter methods in constructors list
  132. // create default descriptor
  133. }
  134. /**
  135. * Constructs a MBeanConstructorInfo object.
  136. *
  137. * @param description A human readable description of the constructor.
  138. * @param constructorMethod The java.lang.reflect.Method object
  139. * describing the ModelMBean constructor.
  140. * @param descriptor An instance of Descriptor containing the
  141. * appropriate metadata for this instance of the
  142. * ModelMBeanConstructorInfo. If it is null, then a default
  143. * descriptor will be created.If the descriptor does not
  144. * contain the field "displayName" this fields is added in the
  145. * descriptor with its default value.
  146. *
  147. * @exception RuntimeOperationsException Wraps an
  148. * IllegalArgumentException. The descriptor is invalid, or
  149. * descriptor field "name" is not equal to name parameter, or
  150. * descriptor field "DescriptorType" is not equal to
  151. * "operation" or descriptor field "role" is not equal to
  152. * "constructor".
  153. */
  154. public ModelMBeanConstructorInfo(String description,
  155. Constructor constructorMethod,
  156. Descriptor descriptor)
  157. {
  158. super(description, constructorMethod);
  159. // put getter and setter methods in constructors list
  160. if (tracing())
  161. {
  162. trace("ModelMBeanConstructorInfo(String, Method, Descriptor)","Executed");
  163. }
  164. if (descriptor == null)
  165. {
  166. if (tracing())
  167. {
  168. trace("ModelMBeanConstructorInfo(String, Method, Descriptor)","Descriptor passed in is null, setting descriptor to default values");
  169. }
  170. consDescriptor = createDefaultDescriptor();
  171. } else
  172. {
  173. if (isValid(descriptor))
  174. {
  175. consDescriptor = (Descriptor) descriptor.clone();
  176. } else
  177. { // exception
  178. consDescriptor = createDefaultDescriptor();
  179. throw new RuntimeOperationsException(new IllegalArgumentException("Invalid descriptor passed in parameter"), ("Exception occured in ModelMBeanConstructorInfo constructor"));
  180. }
  181. }
  182. }
  183. /**
  184. * Constructs a ModelMBeanConstructorInfo object with a default descriptor.
  185. *
  186. * @param name The name of the constructor.
  187. * @param description A human readable description of the constructor.
  188. * @param signature MBeanParameterInfo object array describing the parameters(arguments) of the constructor.
  189. */
  190. public ModelMBeanConstructorInfo(String name,
  191. String description,
  192. MBeanParameterInfo[] signature)
  193. {
  194. super(name, description, signature);
  195. // create default descriptor
  196. if (tracing())
  197. {
  198. trace("ModelMBeanConstructorInfo(String, String, MBeanParameterInfo[])","Executed");
  199. }
  200. consDescriptor = createDefaultDescriptor();
  201. }
  202. /**
  203. * Constructs a MBeanConstructorInfo object.
  204. *
  205. * @param name The name of the constructor.
  206. * @param description A human readable description of the constructor.
  207. * @param signature MBeanParameterInfo objects describing the parameters(arguments) of the constructor.
  208. * @param descriptor An instance of Descriptor containing the appropriate metadata
  209. * for this instance of the MBeanConstructorInfo. If it is null then a default descriptor will be created.
  210. * If the descriptor does not contain the field "displayName" this field is added in the descriptor with its default value.
  211. *
  212. * @exception RuntimeOperationsException Wraps an IllegalArgumentException. The descriptor is invalid, or descriptor field "name"
  213. * is not equal to name parameter, or descriptor field "DescriptorType" is not equal to "operation" or descriptor field "role"
  214. * is not equal to "constructor".
  215. */
  216. public ModelMBeanConstructorInfo(String name,
  217. String description,
  218. MBeanParameterInfo[] signature,
  219. Descriptor descriptor)
  220. {
  221. super(name, description, signature);
  222. if (tracing())
  223. {
  224. trace("ModelMBeanConstructorInfo(String, String, MBeanParameterInfo[], Descriptor)","Executed");
  225. }
  226. if (descriptor == null)
  227. {
  228. if (tracing())
  229. {
  230. trace("ModelMBeanConstructorInfo(String, Method, Descriptor)","Descriptor passed in is null, setting descriptor to default values");
  231. }
  232. consDescriptor = createDefaultDescriptor();
  233. } else
  234. {
  235. if (isValid(descriptor))
  236. {
  237. consDescriptor = (Descriptor) descriptor.clone();
  238. } else
  239. { // exception
  240. consDescriptor = createDefaultDescriptor();
  241. throw new RuntimeOperationsException(new IllegalArgumentException("Invalid descriptor passed in parameter"), ("Exception occured in ModelMBeanConstructorInfo constructor"));
  242. }
  243. }
  244. }
  245. /**
  246. * Constructs a new ModelMBeanConstructorInfo object from this ModelMBeanConstructor Object.
  247. *
  248. * @param old the ModelMBeanConstructorInfo to be duplicated
  249. *
  250. */
  251. ModelMBeanConstructorInfo(ModelMBeanConstructorInfo old)
  252. {
  253. super(old.getName(), old.getDescription(), old.getSignature());
  254. if (tracing())
  255. {
  256. trace("ModelMBeanConstructorInfo(ModelMBeanConstructorInfo)","Executed");
  257. }
  258. if (old.consDescriptor == null)
  259. {
  260. if (tracing())
  261. {
  262. trace("ModelMBeanConstructorInfo(String, Method, Descriptor)","Existing descriptor passed in is null, setting new descriptor to default values");
  263. }
  264. consDescriptor = createDefaultDescriptor();
  265. } else
  266. {
  267. if (isValid(consDescriptor))
  268. {
  269. consDescriptor = (Descriptor) old.consDescriptor.clone();
  270. } else
  271. { // exception
  272. consDescriptor = createDefaultDescriptor();
  273. throw new RuntimeOperationsException(new IllegalArgumentException("Invalid descriptor passed in parameter"), ("Exception occured in ModelMBeanConstructorInfo constructor"));
  274. }
  275. }
  276. }
  277. /**
  278. * Creates and returns a new ModelMBeanConstructorInfo which is a duplicate of this ModelMBeanConstructorInfo.
  279. *
  280. */
  281. public Object clone ()
  282. {
  283. if (tracing())
  284. {
  285. trace("ModelMBeanConstructorInfo.clone()","Executed");
  286. }
  287. return(new ModelMBeanConstructorInfo(this)) ;
  288. }
  289. /**
  290. * Returns a copy of the associated Descriptor.
  291. *
  292. * @return Descriptor associated with the
  293. * ModelMBeanConstructorInfo object.
  294. *
  295. * @see #setDescriptor
  296. */
  297. public Descriptor getDescriptor()
  298. {
  299. if (tracing())
  300. {
  301. trace("ModelMBeanConstructorInfo.getDescriptor()","Executed");
  302. }
  303. if (consDescriptor == null)
  304. {
  305. consDescriptor = createDefaultDescriptor();
  306. }
  307. return((Descriptor)consDescriptor.clone());
  308. }
  309. /**
  310. * Sets associated Descriptor (full replace) of
  311. * ModelMBeanConstructorInfo. If the new Descriptor is null,
  312. * then the associated Descriptor reverts to a default
  313. * descriptor. The Descriptor is validated before it is
  314. * assigned. If the new Descriptor is invalid, then a
  315. * RuntimeOperationsException wrapping an
  316. * IllegalArgumentException is thrown.
  317. *
  318. * @param inDescriptor replaces the Descriptor associated with
  319. * the ModelMBeanConstructor. If the descriptor does not
  320. * contain the field "displayName" this field is added in the
  321. * descriptor with its default value.
  322. *
  323. * @exception RuntimeOperationsException Wraps an
  324. * IllegalArgumentException. The descriptor is invalid, or
  325. * descriptor field "name" is not equal to name parameter, or
  326. * descriptor field "DescriptorType" is not equal to
  327. * "operation" or descriptor field "role" is not equal to
  328. * "constructor".
  329. *
  330. * @see #getDescriptor
  331. */
  332. public void setDescriptor(Descriptor inDescriptor)
  333. {
  334. if (tracing())
  335. {
  336. trace("ModelMBeanConstructorInfo.setDescriptor()","Executed");
  337. }
  338. if (inDescriptor == null)
  339. {
  340. if (tracing())
  341. {
  342. trace("ModelMBeanConstructorInfo(String, Method, Descriptor)","Descriptor passed in is null, setting descriptor to default values");
  343. }
  344. consDescriptor = createDefaultDescriptor();
  345. } else
  346. {
  347. if (isValid(inDescriptor))
  348. {
  349. consDescriptor = (Descriptor) inDescriptor.clone();
  350. } else
  351. {
  352. throw new RuntimeOperationsException(new IllegalArgumentException("Invalid descriptor passed in parameter"), ("Exception occured in ModelMBeanConstructorInfo setDescriptor"));
  353. }
  354. }
  355. }
  356. /**
  357. * Returns a string containing the entire contents of the ModelMBeanConstructorInfo in human readable form.
  358. */
  359. public String toString()
  360. {
  361. if (tracing())
  362. {
  363. trace("ModelMBeanConstructorInfo.toString()","Executed");
  364. }
  365. String retStr =
  366. "ModelMBeanConstructorInfo: " + this.getName() +
  367. " ; Description: " + this.getDescription() +
  368. " ; Descriptor: " + this.getDescriptor() +
  369. " ; Signature: ";
  370. MBeanParameterInfo[] pTypes = this.getSignature();
  371. for (int i=0; i < pTypes.length; i++)
  372. {
  373. retStr = retStr.concat((pTypes[i]).getType() + ", ");
  374. }
  375. return retStr;
  376. }
  377. /**
  378. * Creates default descriptor for constructor as follows:
  379. * descriptorType=operation,role=constructor,
  380. * name=this.getName(),displayname=this.getName(),visibility=1
  381. */
  382. private Descriptor createDefaultDescriptor()
  383. {
  384. if (tracing())
  385. {
  386. trace("ModelMBeanConstructorInfo.createDefaultDescriptor()","Executed");
  387. }
  388. return new DescriptorSupport(new String[] {"descriptorType=operation",
  389. "role=constructor",
  390. ("name=" + this.getName()),
  391. ("displayname=" + this.getName())});
  392. }
  393. /**
  394. * Tests that the descriptor is valid and adds appropriate default fields not already
  395. * specified. Field values must be correct for field names.
  396. * Descriptor must have the same name as the operation,the descriptorType field must
  397. * be "operation", the role field must be set to "constructor".
  398. * The following fields will be defaulted if they are not already set:
  399. * displayName=this.getName()
  400. */
  401. private boolean isValid(Descriptor inDesc)
  402. {
  403. boolean results = true;
  404. String badField="none";
  405. // if name != this.getName
  406. // if (descriptorType != operation)
  407. // look for displayName, persistPolicy, visibility and add in
  408. if (inDesc == null)
  409. {
  410. badField="nullDescriptor";
  411. results = false;
  412. }
  413. else if (!inDesc.isValid())
  414. { // checks for empty descriptors, null,
  415. // checks for empty name and descriptorType adn valid values for fields.
  416. badField="invalidDescriptor";
  417. results = false;
  418. }
  419. else
  420. {
  421. if (! ((String)inDesc.getFieldValue("name")).equalsIgnoreCase(this.getName()))
  422. {
  423. badField="name";
  424. results = false;
  425. }
  426. if (! ((String)inDesc.getFieldValue("descriptorType")).equalsIgnoreCase("operation"))
  427. {
  428. badField="descriptorType";
  429. results = false;
  430. }
  431. if (inDesc.getFieldValue("role") == null)
  432. {
  433. inDesc.setField("role","constructor");
  434. }
  435. if (! ((String)inDesc.getFieldValue("role")).equalsIgnoreCase("constructor"))
  436. {
  437. badField = "role";
  438. results = false;
  439. } else if ((inDesc.getFieldValue("displayName")) == null)
  440. {
  441. inDesc.setField("displayName",this.getName());
  442. }
  443. }
  444. if (tracing()) trace("isValid()",("Returning " + results + ": Invalid field is " + badField));
  445. return results;
  446. }
  447. // SUN Trace and debug functions
  448. private boolean tracing()
  449. {
  450. // return false;
  451. return Trace.isSelected(Trace.LEVEL_TRACE, Trace.INFO_MODELMBEAN);
  452. }
  453. private void trace(String inClass, String inMethod, String inText)
  454. {
  455. // System.out.println("TRACE: " + inClass + ":" + inMethod + ": " + inText);
  456. Trace.send(Trace.LEVEL_TRACE, Trace.INFO_MODELMBEAN, inClass,
  457. inMethod, Integer.toHexString(this.hashCode()) + " " + inText);
  458. }
  459. private void trace(String inMethod, String inText)
  460. {
  461. trace(currClass, inMethod, inText);
  462. }
  463. /**
  464. * Deserializes a {@link ModelMBeanConstructorInfo} from an {@link ObjectInputStream}.
  465. */
  466. private void readObject(ObjectInputStream in)
  467. throws IOException, ClassNotFoundException {
  468. // New serial form ignores extra field "currClass"
  469. in.defaultReadObject();
  470. }
  471. /**
  472. * Serializes a {@link ModelMBeanConstructorInfo} to an {@link ObjectOutputStream}.
  473. */
  474. private void writeObject(ObjectOutputStream out)
  475. throws IOException {
  476. if (compat)
  477. {
  478. // Serializes this instance in the old serial form
  479. //
  480. ObjectOutputStream.PutField fields = out.putFields();
  481. fields.put("consDescriptor", consDescriptor);
  482. fields.put("currClass", currClass);
  483. out.writeFields();
  484. }
  485. else
  486. {
  487. // Serializes this instance in the new serial form
  488. //
  489. out.defaultWriteObject();
  490. }
  491. }
  492. }