/* * @(#)OpenMBeanOperationInfoSupport.java 3.25 03/12/19 * * Copyright 2004 Sun Microsystems, Inc. All rights reserved. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms. */ package javax.management.openmbean; // java import // import java.io.Serializable; import java.util.Arrays; // jmx import // import javax.management.MBeanOperationInfo; import javax.management.MBeanParameterInfo; /** * Describes an operation of an Open MBean. * * @version 3.25 03/12/19 * @author Sun Microsystems, Inc. * * @since 1.5 * @since.unbundled JMX 1.1 */ public class OpenMBeanOperationInfoSupport extends MBeanOperationInfo implements OpenMBeanOperationInfo, Serializable { /* Serial version */ static final long serialVersionUID = 4996859732565369366L; /** * @serial The open type of the values returned by the operation * described by this {@link OpenMBeanOperationInfo} instance * */ private OpenType returnOpenType; private transient Integer myHashCode = null; // As this instance is immutable, these two values private transient String myToString = null; // need only be calculated once. /** * Constructs an OpenMBeanOperationInfoSupport instance, which describes the operation * of a class of open MBeans, with the specified * name, description, signature, returnOpenType and impact. *
* The signature array parameter is internally copied, so that subsequent changes * to the array referenced by signature have no effect on this instance. *
*
* @param name cannot be a null or empty string.
*
* @param description cannot be a null or empty string.
*
* @param signature can be null or empty if there are no parameters to describe.
*
* @param returnOpenType cannot be null: use SimpleType.VOID for operations that return nothing.
*
* @param impact can be only one of ACTION, ACTION_INFO or INFO.
*
* @throws IllegalArgumentException if name or description are null or empty string,
* or returnOpenType is null,
* or impact is not one of ACTION, ACTION_INFO or INFO.
*
* @throws ArrayStoreException If signature is not an array of instances of a subclass of MBeanParameterInfo.
*/
public OpenMBeanOperationInfoSupport(String name,
String description,
OpenMBeanParameterInfo[] signature,
OpenType returnOpenType,
int impact) {
super(name,
description,
( signature == null ? null : arrayCopyCast(signature) ), // may throw an ArrayStoreException
( returnOpenType == null ? null : returnOpenType.getClassName() ),
impact);
// check parameters that should not be null or empty (unfortunately it is not done in superclass :-( ! )
//
if ( (name == null) || (name.trim().equals("")) ) {
throw new IllegalArgumentException("Argument name cannot be null or empty.");
}
if ( (description == null) || (description.trim().equals("")) ) {
throw new IllegalArgumentException("Argument description cannot be null or empty.");
}
if (returnOpenType == null) {
throw new IllegalArgumentException("Argument returnOpenType cannot be null.");
}
// check impact's value is only one of the 3 allowed (UNKNOWN not allowed)
//
if ( (impact != super.ACTION) && (impact != super.ACTION_INFO) && (impact != super.INFO) ) {
throw new IllegalArgumentException("Argument impact can be only one of ACTION, ACTION_INFO or INFO.");
}
this.returnOpenType = returnOpenType;
}
private static MBeanParameterInfo[] arrayCopyCast(OpenMBeanParameterInfo[] src) throws ArrayStoreException {
MBeanParameterInfo[] dst = new MBeanParameterInfo[src.length];
System.arraycopy(src, 0, dst, 0, src.length); // may throw an ArrayStoreException
return dst;
}
// [JF]: should we add constructor with java.lang.reflect.Method method parameter ?
// would need to add consistency check between OpenType returnOpenType and mehtod.getReturnType().
/**
* Returns the open type of the values returned by the operation described by this OpenMBeanOperationInfo instance.
*/
public OpenType getReturnOpenType() {
return returnOpenType;
}
/* *** Commodity methods from java.lang.Object *** */
/**
* Compares the specified obj parameter with this OpenMBeanOperationInfoSupport
instance for equality.
*
* Returns true if and only if all of the following statements are true: *
OpenMBeanOperationInfo
interface,OpenMBeanOperationInfo
interface.
* OpenMBeanOperationInfoSupport
instance;
*
* @return true
if the specified object is equal to this OpenMBeanOperationInfoSupport
instance.
*/
public boolean equals(Object obj) {
// if obj is null, return false
//
if (obj == null) {
return false;
}
// if obj is not a OpenMBeanOperationInfo, return false
//
OpenMBeanOperationInfo other;
try {
other = (OpenMBeanOperationInfo) obj;
} catch (ClassCastException e) {
return false;
}
// Now, really test for equality between this OpenMBeanOperationInfo implementation and the other:
//
// their Name should be equal
if ( ! this.getName().equals(other.getName()) ) {
return false;
}
// their Signatures should be equal
if ( ! Arrays.equals(this.getSignature(), other.getSignature()) ) {
return false;
}
// their return open types should be equal
if ( ! this.getReturnOpenType().equals(other.getReturnOpenType()) ) {
return false;
}
// their impacts should be equal
if ( this.getImpact() != other.getImpact() ) {
return false;
}
// All tests for equality were successfull
//
return true;
}
/**
* Returns the hash code value for this OpenMBeanOperationInfoSupport
instance.
*
* The hash code of an OpenMBeanOperationInfoSupport
instance is the sum of the hash codes
* of all elements of information used in equals
comparisons
* (ie: its name, return open type, impact and signature, where the signature hashCode is calculated by a call to
* java.util.Arrays.asList(this.getSignature).hashCode()).
*
* This ensures that t1.equals(t2)
implies that t1.hashCode()==t2.hashCode()
* for any two OpenMBeanOperationInfoSupport
instances t1
and t2
,
* as required by the general contract of the method
* {@link Object#hashCode() Object.hashCode()}.
*
* However, note that another instance of a class implementing the OpenMBeanOperationInfo
interface
* may be equal to this OpenMBeanOperationInfoSupport
instance as defined by {@link #equals(java.lang.Object)},
* but may have a different hash code if it is calculated differently.
*
* As OpenMBeanOperationInfoSupport
instances are immutable, the hash code for this instance is calculated once,
* on the first call to hashCode
, and then the same value is returned for subsequent calls.
*
* @return the hash code value for this OpenMBeanOperationInfoSupport
instance
*/
public int hashCode() {
// Calculate the hash code value if it has not yet been done (ie 1st call to hashCode())
//
if (myHashCode == null) {
int value = 0;
value += this.getName().hashCode();
value += Arrays.asList(this.getSignature()).hashCode();
value += this.getReturnOpenType().hashCode();
value += this.getImpact();
myHashCode = new Integer(value);
}
// return always the same hash code for this instance (immutable)
//
return myHashCode.intValue();
}
/**
* Returns a string representation of this OpenMBeanOperationInfoSupport
instance.
*
* The string representation consists of the name of this class (ie javax.management.openmbean.OpenMBeanOperationInfoSupport
),
* and the name, signature, return open type and impact of the described operation.
*
* As OpenMBeanOperationInfoSupport
instances are immutable,
* the string representation for this instance is calculated once,
* on the first call to toString
, and then the same value is returned for subsequent calls.
*
* @return a string representation of this OpenMBeanOperationInfoSupport
instance
*/
public String toString() {
// Calculate the hash code value if it has not yet been done (ie 1st call to toString())
//
if (myToString == null) {
myToString = new StringBuffer()
.append(this.getClass().getName())
.append("(name=")
.append(this.getName())
.append(",signature=")
.append(Arrays.asList(this.getSignature()).toString())
.append(",return=")
.append(this.getReturnOpenType().toString())
.append(",impact=")
.append(this.getImpact())
.append(")")
.toString();
}
// return always the same string representation for this instance (immutable)
//
return myToString;
}
}