1. /*
  2. * @(#)OpenMBeanOperationInfoSupport.java 3.25 03/12/19
  3. *
  4. * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
  5. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  6. */
  7. package javax.management.openmbean;
  8. // java import
  9. //
  10. import java.io.Serializable;
  11. import java.util.Arrays;
  12. // jmx import
  13. //
  14. import javax.management.MBeanOperationInfo;
  15. import javax.management.MBeanParameterInfo;
  16. /**
  17. * Describes an operation of an Open MBean.
  18. *
  19. * @version 3.25 03/12/19
  20. * @author Sun Microsystems, Inc.
  21. *
  22. * @since 1.5
  23. * @since.unbundled JMX 1.1
  24. */
  25. public class OpenMBeanOperationInfoSupport
  26. extends MBeanOperationInfo
  27. implements OpenMBeanOperationInfo, Serializable {
  28. /* Serial version */
  29. static final long serialVersionUID = 4996859732565369366L;
  30. /**
  31. * @serial The <i>open type</i> of the values returned by the operation
  32. * described by this {@link OpenMBeanOperationInfo} instance
  33. *
  34. */
  35. private OpenType returnOpenType;
  36. private transient Integer myHashCode = null; // As this instance is immutable, these two values
  37. private transient String myToString = null; // need only be calculated once.
  38. /**
  39. * Constructs an <tt>OpenMBeanOperationInfoSupport</tt> instance, which describes the operation
  40. * of a class of open MBeans, with the specified
  41. * <var>name</var>, <var>description</var>, <var>signature</var>, <var>returnOpenType</var> and <var>impact</var>.
  42. * <p>
  43. * The <var>signature</var> array parameter is internally copied, so that subsequent changes
  44. * to the array referenced by <var>signature</var> have no effect on this instance.
  45. * <p>
  46. *
  47. * @param name cannot be a null or empty string.
  48. *
  49. * @param description cannot be a null or empty string.
  50. *
  51. * @param signature can be null or empty if there are no parameters to describe.
  52. *
  53. * @param returnOpenType cannot be null: use <tt>SimpleType.VOID</tt> for operations that return nothing.
  54. *
  55. * @param impact can be only one of <tt>ACTION</tt>, <tt>ACTION_INFO</tt> or <tt>INFO</tt>.
  56. *
  57. * @throws IllegalArgumentException if <var>name</var> or <var>description</var> are null or empty string,
  58. * or <var>returnOpenType</var> is null,
  59. * or <var>impact</var> is not one of <tt>ACTION</tt>, <tt>ACTION_INFO</tt> or <tt>INFO</tt>.
  60. *
  61. * @throws ArrayStoreException If <var>signature</var> is not an array of instances of a subclass of <tt>MBeanParameterInfo</tt>.
  62. */
  63. public OpenMBeanOperationInfoSupport(String name,
  64. String description,
  65. OpenMBeanParameterInfo[] signature,
  66. OpenType returnOpenType,
  67. int impact) {
  68. super(name,
  69. description,
  70. ( signature == null ? null : arrayCopyCast(signature) ), // may throw an ArrayStoreException
  71. ( returnOpenType == null ? null : returnOpenType.getClassName() ),
  72. impact);
  73. // check parameters that should not be null or empty (unfortunately it is not done in superclass :-( ! )
  74. //
  75. if ( (name == null) || (name.trim().equals("")) ) {
  76. throw new IllegalArgumentException("Argument name cannot be null or empty.");
  77. }
  78. if ( (description == null) || (description.trim().equals("")) ) {
  79. throw new IllegalArgumentException("Argument description cannot be null or empty.");
  80. }
  81. if (returnOpenType == null) {
  82. throw new IllegalArgumentException("Argument returnOpenType cannot be null.");
  83. }
  84. // check impact's value is only one of the 3 allowed (UNKNOWN not allowed)
  85. //
  86. if ( (impact != super.ACTION) && (impact != super.ACTION_INFO) && (impact != super.INFO) ) {
  87. throw new IllegalArgumentException("Argument impact can be only one of ACTION, ACTION_INFO or INFO.");
  88. }
  89. this.returnOpenType = returnOpenType;
  90. }
  91. private static MBeanParameterInfo[] arrayCopyCast(OpenMBeanParameterInfo[] src) throws ArrayStoreException {
  92. MBeanParameterInfo[] dst = new MBeanParameterInfo[src.length];
  93. System.arraycopy(src, 0, dst, 0, src.length); // may throw an ArrayStoreException
  94. return dst;
  95. }
  96. // [JF]: should we add constructor with java.lang.reflect.Method method parameter ?
  97. // would need to add consistency check between OpenType returnOpenType and mehtod.getReturnType().
  98. /**
  99. * Returns the <i>open type</i> of the values returned by the operation described by this <tt>OpenMBeanOperationInfo</tt> instance.
  100. */
  101. public OpenType getReturnOpenType() {
  102. return returnOpenType;
  103. }
  104. /* *** Commodity methods from java.lang.Object *** */
  105. /**
  106. * Compares the specified <var>obj</var> parameter with this <code>OpenMBeanOperationInfoSupport</code> instance for equality.
  107. * <p>
  108. * Returns <tt>true</tt> if and only if all of the following statements are true:
  109. * <ul>
  110. * <li><var>obj</var> is non null,</li>
  111. * <li><var>obj</var> also implements the <code>OpenMBeanOperationInfo</code> interface,</li>
  112. * <li>their names are equal</li>
  113. * <li>their signatures are equal</li>
  114. * <li>their return open types are equal</li>
  115. * <li>their impacts are equal</li>
  116. * </ul>
  117. * This ensures that this <tt>equals</tt> method works properly for <var>obj</var> parameters which are
  118. * different implementations of the <code>OpenMBeanOperationInfo</code> interface.
  119. * <br> 
  120. * @param obj the object to be compared for equality with this <code>OpenMBeanOperationInfoSupport</code> instance;
  121. *
  122. * @return <code>true</code> if the specified object is equal to this <code>OpenMBeanOperationInfoSupport</code> instance.
  123. */
  124. public boolean equals(Object obj) {
  125. // if obj is null, return false
  126. //
  127. if (obj == null) {
  128. return false;
  129. }
  130. // if obj is not a OpenMBeanOperationInfo, return false
  131. //
  132. OpenMBeanOperationInfo other;
  133. try {
  134. other = (OpenMBeanOperationInfo) obj;
  135. } catch (ClassCastException e) {
  136. return false;
  137. }
  138. // Now, really test for equality between this OpenMBeanOperationInfo implementation and the other:
  139. //
  140. // their Name should be equal
  141. if ( ! this.getName().equals(other.getName()) ) {
  142. return false;
  143. }
  144. // their Signatures should be equal
  145. if ( ! Arrays.equals(this.getSignature(), other.getSignature()) ) {
  146. return false;
  147. }
  148. // their return open types should be equal
  149. if ( ! this.getReturnOpenType().equals(other.getReturnOpenType()) ) {
  150. return false;
  151. }
  152. // their impacts should be equal
  153. if ( this.getImpact() != other.getImpact() ) {
  154. return false;
  155. }
  156. // All tests for equality were successfull
  157. //
  158. return true;
  159. }
  160. /**
  161. * Returns the hash code value for this <code>OpenMBeanOperationInfoSupport</code> instance.
  162. * <p>
  163. * The hash code of an <code>OpenMBeanOperationInfoSupport</code> instance is the sum of the hash codes
  164. * of all elements of information used in <code>equals</code> comparisons
  165. * (ie: its name, return open type, impact and signature, where the signature hashCode is calculated by a call to
  166. * <tt>java.util.Arrays.asList(this.getSignature).hashCode()</tt>).
  167. * <p>
  168. * This ensures that <code> t1.equals(t2) </code> implies that <code> t1.hashCode()==t2.hashCode() </code>
  169. * for any two <code>OpenMBeanOperationInfoSupport</code> instances <code>t1</code> and <code>t2</code>,
  170. * as required by the general contract of the method
  171. * {@link Object#hashCode() Object.hashCode()}.
  172. * <p>
  173. * However, note that another instance of a class implementing the <code>OpenMBeanOperationInfo</code> interface
  174. * may be equal to this <code>OpenMBeanOperationInfoSupport</code> instance as defined by {@link #equals(java.lang.Object)},
  175. * but may have a different hash code if it is calculated differently.
  176. * <p>
  177. * As <code>OpenMBeanOperationInfoSupport</code> instances are immutable, the hash code for this instance is calculated once,
  178. * on the first call to <code>hashCode</code>, and then the same value is returned for subsequent calls.
  179. *
  180. * @return the hash code value for this <code>OpenMBeanOperationInfoSupport</code> instance
  181. */
  182. public int hashCode() {
  183. // Calculate the hash code value if it has not yet been done (ie 1st call to hashCode())
  184. //
  185. if (myHashCode == null) {
  186. int value = 0;
  187. value += this.getName().hashCode();
  188. value += Arrays.asList(this.getSignature()).hashCode();
  189. value += this.getReturnOpenType().hashCode();
  190. value += this.getImpact();
  191. myHashCode = new Integer(value);
  192. }
  193. // return always the same hash code for this instance (immutable)
  194. //
  195. return myHashCode.intValue();
  196. }
  197. /**
  198. * Returns a string representation of this <code>OpenMBeanOperationInfoSupport</code> instance.
  199. * <p>
  200. * The string representation consists of the name of this class (ie <code>javax.management.openmbean.OpenMBeanOperationInfoSupport</code>),
  201. * and the name, signature, return open type and impact of the described operation.
  202. * <p>
  203. * As <code>OpenMBeanOperationInfoSupport</code> instances are immutable,
  204. * the string representation for this instance is calculated once,
  205. * on the first call to <code>toString</code>, and then the same value is returned for subsequent calls.
  206. *
  207. * @return a string representation of this <code>OpenMBeanOperationInfoSupport</code> instance
  208. */
  209. public String toString() {
  210. // Calculate the hash code value if it has not yet been done (ie 1st call to toString())
  211. //
  212. if (myToString == null) {
  213. myToString = new StringBuffer()
  214. .append(this.getClass().getName())
  215. .append("(name=")
  216. .append(this.getName())
  217. .append(",signature=")
  218. .append(Arrays.asList(this.getSignature()).toString())
  219. .append(",return=")
  220. .append(this.getReturnOpenType().toString())
  221. .append(",impact=")
  222. .append(this.getImpact())
  223. .append(")")
  224. .toString();
  225. }
  226. // return always the same string representation for this instance (immutable)
  227. //
  228. return myToString;
  229. }
  230. }