KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > javax > management > MBeanOperationInfo


1 /*
2  * @(#)MBeanOperationInfo.java 1.31 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
8 package javax.management;
9
10 import java.lang.reflect.Method JavaDoc;
11 import java.util.Arrays JavaDoc;
12
13 /**
14  * Describes a management operation exposed by an MBean. Instances of
15  * this class are immutable. Subclasses may be mutable but this is
16  * not recommended.
17  *
18  * @since 1.5
19  */

20 public class MBeanOperationInfo extends MBeanFeatureInfo JavaDoc implements java.io.Serializable JavaDoc, Cloneable JavaDoc {
21
22     /* Serial version */
23     static final long serialVersionUID = -6178860474881375330L;
24
25     static final MBeanOperationInfo JavaDoc[] NO_OPERATIONS =
26     new MBeanOperationInfo JavaDoc[0];
27
28     /**
29      * Indicates that the operation is read-like,
30      * it basically returns information.
31      */

32     public static final int INFO = 0;
33
34     /**
35      * Indicates that the operation is a write-like,
36      * and would modify the MBean in some way, typically by writing some value
37      * or changing a configuration.
38      */

39     public static final int ACTION = 1 ;
40
41     /**
42      * Indicates that the operation is both read-like and write-like.
43      */

44     public static final int ACTION_INFO = 2;
45
46     /**
47      * Indicates that the operation has an "unknown" nature.
48      */

49     public static final int UNKNOWN = 3;
50
51     /**
52      * @serial The method's return value.
53      */

54     private final String JavaDoc type;
55
56     /**
57      * @serial The signature of the method, that is, the class names
58      * of the arguments.
59      */

60     private final MBeanParameterInfo JavaDoc[] signature;
61
62     /**
63      * @serial The impact of the method, one of
64      * <CODE>INFO</CODE>,
65      * <CODE>ACTION</CODE>,
66      * <CODE>ACTION_INFO</CODE>,
67      * <CODE>UNKNOWN</CODE>
68      */

69     private final int impact;
70
71     /** @see MBeanInfo#immutable */
72     private final transient boolean immutable;
73
74
75     /**
76      * Constructs an <CODE>MBeanOperationInfo</CODE> object.
77      *
78      * @param method The <CODE>java.lang.reflect.Method</CODE> object
79      * describing the MBean operation.
80      * @param description A human readable description of the operation.
81      */

82     public MBeanOperationInfo(String JavaDoc description,
83                   Method JavaDoc method)
84         throws IllegalArgumentException JavaDoc {
85
86     this(method.getName(),
87          description,
88          methodSignature(method),
89          method.getReturnType().getName(),
90          UNKNOWN);
91     }
92
93     /**
94      * Constructs an <CODE>MBeanOperationInfo</CODE> object.
95      *
96      * @param name The name of the method.
97      * @param description A human readable description of the operation.
98      * @param signature <CODE>MBeanParameterInfo</CODE> objects
99      * describing the parameters(arguments) of the method. This may be
100      * null with the same effect as a zero-length array.
101      * @param type The type of the method's return value.
102      * @param impact The impact of the method, one of <CODE>INFO,
103      * ACTION, ACTION_INFO, UNKNOWN</CODE>.
104      */

105     public MBeanOperationInfo(String JavaDoc name,
106                   String JavaDoc description,
107                   MBeanParameterInfo JavaDoc[] signature,
108                   String JavaDoc type,
109                   int impact)
110         throws IllegalArgumentException JavaDoc {
111
112     super(name, description);
113
114     if (signature == null || signature.length == 0)
115         signature = MBeanParameterInfo.NO_PARAMS;
116     else
117         signature = (MBeanParameterInfo JavaDoc[]) signature.clone();
118     this.signature = signature;
119     this.type = type;
120     this.impact = impact;
121     this.immutable =
122         MBeanInfo.isImmutableClass(this.getClass(),
123                        MBeanOperationInfo JavaDoc.class);
124     }
125
126     /**
127      * <p>Returns a shallow clone of this instance.
128      * The clone is obtained by simply calling <tt>super.clone()</tt>,
129      * thus calling the default native shallow cloning mechanism
130      * implemented by <tt>Object.clone()</tt>.
131      * No deeper cloning of any internal field is made.</p>
132      *
133      * <p>Since this class is immutable, cloning is chiefly of interest
134      * to subclasses.</p>
135      */

136      public Object JavaDoc clone () {
137      try {
138          return super.clone() ;
139      } catch (CloneNotSupportedException JavaDoc e) {
140          // should not happen as this class is cloneable
141
return null;
142      }
143      }
144
145     /**
146      * Returns the type of the method's return value.
147      *
148      * @return the return type.
149      */

150     public String JavaDoc getReturnType() {
151     return type;
152     }
153
154     /**
155      * <p>Returns the list of parameters for this operation. Each
156      * parameter is described by an <CODE>MBeanParameterInfo</CODE>
157      * object.</p>
158      *
159      * <p>The returned array is a shallow copy of the internal array,
160      * which means that it is a copy of the internal array of
161      * references to the <CODE>MBeanParameterInfo</CODE> objects but
162      * that each referenced <CODE>MBeanParameterInfo</CODE> object is
163      * not copied.</p>
164      *
165      * @return An array of <CODE>MBeanParameterInfo</CODE> objects.
166      */

167     public MBeanParameterInfo JavaDoc[] getSignature() {
168     if (signature.length == 0)
169         return signature;
170     else
171         return (MBeanParameterInfo JavaDoc[]) signature.clone();
172     }
173
174     private MBeanParameterInfo JavaDoc[] fastGetSignature() {
175     if (immutable)
176         return signature;
177     else
178         return getSignature();
179     }
180
181     /**
182      * Returns the impact of the method, one of
183      * <CODE>INFO</CODE>, <CODE>ACTION</CODE>, <CODE>ACTION_INFO</CODE>, <CODE>UNKNOWN</CODE>.
184      *
185      * @return the impact code.
186      */

187     public int getImpact() {
188     return impact;
189     }
190
191     /**
192      * Compare this MBeanOperationInfo to another.
193      *
194      * @param o the object to compare to.
195      *
196      * @return true iff <code>o</code> is an MBeanOperationInfo such
197      * that its {@link #getName()}, {@link #getReturnType()}, {@link
198      * #getDescription()}, {@link #getImpact()}, and {@link
199      * #getSignature()} values are equal (not necessarily identical)
200      * to those of this MBeanConstructorInfo. Two signature arrays
201      * are equal if their elements are pairwise equal.
202      */

203     public boolean equals(Object JavaDoc o) {
204     if (o == this)
205         return true;
206     if (!(o instanceof MBeanOperationInfo JavaDoc))
207         return false;
208     MBeanOperationInfo JavaDoc p = (MBeanOperationInfo JavaDoc) o;
209     return (p.getName().equals(getName()) &&
210         p.getReturnType().equals(getReturnType()) &&
211         p.getDescription().equals(getDescription()) &&
212         p.getImpact() == getImpact() &&
213         Arrays.equals(p.fastGetSignature(), fastGetSignature()));
214     }
215
216     /* We do not include everything in the hashcode. We assume that
217        if two operations are different they'll probably have different
218        names or types. The penalty we pay when this assumption is
219        wrong should be less than the penalty we would pay if it were
220        right and we needlessly hashed in the description and the
221        parameter array. */

222     public int hashCode() {
223     return getName().hashCode() ^ getReturnType().hashCode();
224     }
225
226     private static MBeanParameterInfo JavaDoc[] methodSignature(Method JavaDoc method) {
227     final Class JavaDoc[] classes = method.getParameterTypes();
228     final MBeanParameterInfo JavaDoc[] params =
229         new MBeanParameterInfo JavaDoc[classes.length];
230
231     for (int i = 0; i < classes.length; i++) {
232         final String JavaDoc pn = "p" + (i + 1);
233         params[i] = new MBeanParameterInfo JavaDoc(pn, classes[i].getName(), "");
234     }
235
236     return params;
237     }
238 }
239
Popular Tags