001 /*
002 * Copyright 2000-2007 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 package javax.management.openmbean;
027
028 // java import
029 //
030 import java.util.Arrays;
031
032 import javax.management.Descriptor;
033 import javax.management.ImmutableDescriptor;
034 import javax.management.MBeanOperationInfo;
035 import javax.management.MBeanParameterInfo;
036
037 /**
038 * Describes an operation of an Open MBean.
039 *
040 *
041 * @since 1.5
042 */
043 public class OpenMBeanOperationInfoSupport extends MBeanOperationInfo
044 implements OpenMBeanOperationInfo {
045
046 /* Serial version */
047 static final long serialVersionUID = 4996859732565369366L;
048
049 /**
050 * @serial The <i>open type</i> of the values returned by the operation
051 * described by this {@link OpenMBeanOperationInfo} instance
052 *
053 */
054 private OpenType<?> returnOpenType;
055
056 // As this instance is immutable,
057 // these two values need only be calculated once.
058 private transient Integer myHashCode = null;
059 private transient String myToString = null;
060
061 /**
062 * <p>Constructs an {@code OpenMBeanOperationInfoSupport}
063 * instance, which describes the operation of a class of open
064 * MBeans, with the specified {@code name}, {@code description},
065 * {@code signature}, {@code returnOpenType} and {@code
066 * impact}.</p>
067 *
068 * <p>The {@code signature} array parameter is internally copied,
069 * so that subsequent changes to the array referenced by {@code
070 * signature} have no effect on this instance.</p>
071 *
072 * @param name cannot be a null or empty string.
073 *
074 * @param description cannot be a null or empty string.
075 *
076 * @param signature can be null or empty if there are no
077 * parameters to describe.
078 *
079 * @param returnOpenType cannot be null: use {@code
080 * SimpleType.VOID} for operations that return nothing.
081 *
082 * @param impact must be one of {@code ACTION}, {@code
083 * ACTION_INFO}, {@code INFO}, or {@code UNKNOWN}.
084 *
085 * @throws IllegalArgumentException if {@code name} or {@code
086 * description} are null or empty string, or {@code
087 * returnOpenType} is null, or {@code impact} is not one of {@code
088 * ACTION}, {@code ACTION_INFO}, {@code INFO}, or {@code UNKNOWN}.
089 *
090 * @throws ArrayStoreException If {@code signature} is not an
091 * array of instances of a subclass of {@code MBeanParameterInfo}.
092 */
093 public OpenMBeanOperationInfoSupport(String name,
094 String description, OpenMBeanParameterInfo[] signature,
095 OpenType<?> returnOpenType, int impact) {
096 this (name, description, signature, returnOpenType, impact,
097 (Descriptor) null);
098 }
099
100 /**
101 * <p>Constructs an {@code OpenMBeanOperationInfoSupport}
102 * instance, which describes the operation of a class of open
103 * MBeans, with the specified {@code name}, {@code description},
104 * {@code signature}, {@code returnOpenType}, {@code
105 * impact}, and {@code descriptor}.</p>
106 *
107 * <p>The {@code signature} array parameter is internally copied,
108 * so that subsequent changes to the array referenced by {@code
109 * signature} have no effect on this instance.</p>
110 *
111 * @param name cannot be a null or empty string.
112 *
113 * @param description cannot be a null or empty string.
114 *
115 * @param signature can be null or empty if there are no
116 * parameters to describe.
117 *
118 * @param returnOpenType cannot be null: use {@code
119 * SimpleType.VOID} for operations that return nothing.
120 *
121 * @param impact must be one of {@code ACTION}, {@code
122 * ACTION_INFO}, {@code INFO}, or {@code UNKNOWN}.
123 *
124 * @param descriptor The descriptor for the operation. This may
125 * be null, which is equivalent to an empty descriptor.
126 *
127 * @throws IllegalArgumentException if {@code name} or {@code
128 * description} are null or empty string, or {@code
129 * returnOpenType} is null, or {@code impact} is not one of {@code
130 * ACTION}, {@code ACTION_INFO}, {@code INFO}, or {@code UNKNOWN}.
131 *
132 * @throws ArrayStoreException If {@code signature} is not an
133 * array of instances of a subclass of {@code MBeanParameterInfo}.
134 *
135 * @since 1.6
136 */
137 public OpenMBeanOperationInfoSupport(String name,
138 String description, OpenMBeanParameterInfo[] signature,
139 OpenType<?> returnOpenType, int impact,
140 Descriptor descriptor) {
141 super (name, description, arrayCopyCast(signature),
142 // must prevent NPE here - we will throw IAE later on if
143 // returnOpenType is null
144 (returnOpenType == null) ? null : returnOpenType
145 .getClassName(), impact, ImmutableDescriptor
146 .union(descriptor,
147 // must prevent NPE here - we will throw IAE later on if
148 // returnOpenType is null
149 (returnOpenType == null) ? null
150 : returnOpenType
151 .getDescriptor()));
152
153 // check parameters that should not be null or empty
154 // (unfortunately it is not done in superclass :-( ! )
155 //
156 if (name == null || name.trim().equals("")) {
157 throw new IllegalArgumentException("Argument name cannot "
158 + "be null or empty");
159 }
160 if (description == null || description.trim().equals("")) {
161 throw new IllegalArgumentException(
162 "Argument description cannot " + "be null or empty");
163 }
164 if (returnOpenType == null) {
165 throw new IllegalArgumentException(
166 "Argument returnOpenType " + "cannot be null");
167 }
168
169 if (impact != ACTION && impact != ACTION_INFO && impact != INFO
170 && impact != UNKNOWN) {
171 throw new IllegalArgumentException(
172 "Argument impact can only be "
173 + "one of ACTION, ACTION_INFO, "
174 + "INFO, or UNKNOWN: " + impact);
175 }
176
177 this .returnOpenType = returnOpenType;
178 }
179
180 // Converts an array of OpenMBeanParameterInfo objects extending
181 // MBeanParameterInfo into an array of MBeanParameterInfo.
182 //
183 private static MBeanParameterInfo[] arrayCopyCast(
184 OpenMBeanParameterInfo[] src) {
185 if (src == null)
186 return null;
187
188 MBeanParameterInfo[] dst = new MBeanParameterInfo[src.length];
189 System.arraycopy(src, 0, dst, 0, src.length);
190 // may throw an ArrayStoreException
191 return dst;
192 }
193
194 // Converts an array of MBeanParameterInfo objects implementing
195 // OpenMBeanParameterInfo into an array of OpenMBeanParameterInfo.
196 //
197 private static OpenMBeanParameterInfo[] arrayCopyCast(
198 MBeanParameterInfo[] src) {
199 if (src == null)
200 return null;
201
202 OpenMBeanParameterInfo[] dst = new OpenMBeanParameterInfo[src.length];
203 System.arraycopy(src, 0, dst, 0, src.length);
204 // may throw an ArrayStoreException
205 return dst;
206 }
207
208 // [JF]: should we add constructor with java.lang.reflect.Method
209 // method parameter ? would need to add consistency check between
210 // OpenType<?> returnOpenType and method.getReturnType().
211
212 /**
213 * Returns the <i>open type</i> of the values returned by the
214 * operation described by this {@code OpenMBeanOperationInfo}
215 * instance.
216 */
217 public OpenType<?> getReturnOpenType() {
218
219 return returnOpenType;
220 }
221
222 /* *** Commodity methods from java.lang.Object *** */
223
224 /**
225 * <p>Compares the specified {@code obj} parameter with this
226 * {@code OpenMBeanOperationInfoSupport} instance for
227 * equality.</p>
228 *
229 * <p>Returns {@code true} if and only if all of the following
230 * statements are true:
231 *
232 * <ul>
233 * <li>{@code obj} is non null,</li>
234 * <li>{@code obj} also implements the {@code
235 * OpenMBeanOperationInfo} interface,</li>
236 * <li>their names are equal</li>
237 * <li>their signatures are equal</li>
238 * <li>their return open types are equal</li>
239 * <li>their impacts are equal</li>
240 * </ul>
241 *
242 * This ensures that this {@code equals} method works properly for
243 * {@code obj} parameters which are different implementations of
244 * the {@code OpenMBeanOperationInfo} interface.
245 *
246 * @param obj the object to be compared for equality with this
247 * {@code OpenMBeanOperationInfoSupport} instance;
248 *
249 * @return {@code true} if the specified object is equal to this
250 * {@code OpenMBeanOperationInfoSupport} instance.
251 */
252 public boolean equals(Object obj) {
253
254 // if obj is null, return false
255 //
256 if (obj == null) {
257 return false;
258 }
259
260 // if obj is not a OpenMBeanOperationInfo, return false
261 //
262 OpenMBeanOperationInfo other;
263 try {
264 other = (OpenMBeanOperationInfo) obj;
265 } catch (ClassCastException e) {
266 return false;
267 }
268
269 // Now, really test for equality between this
270 // OpenMBeanOperationInfo implementation and the other:
271 //
272
273 // their Name should be equal
274 if (!this .getName().equals(other.getName())) {
275 return false;
276 }
277
278 // their Signatures should be equal
279 if (!Arrays.equals(this .getSignature(), other.getSignature())) {
280 return false;
281 }
282
283 // their return open types should be equal
284 if (!this .getReturnOpenType().equals(other.getReturnOpenType())) {
285 return false;
286 }
287
288 // their impacts should be equal
289 if (this .getImpact() != other.getImpact()) {
290 return false;
291 }
292
293 // All tests for equality were successfull
294 //
295 return true;
296 }
297
298 /**
299 * <p>Returns the hash code value for this {@code
300 * OpenMBeanOperationInfoSupport} instance.</p>
301 *
302 * <p>The hash code of an {@code OpenMBeanOperationInfoSupport}
303 * instance is the sum of the hash codes of all elements of
304 * information used in {@code equals} comparisons (ie: its name,
305 * return open type, impact and signature, where the signature
306 * hashCode is calculated by a call to {@code
307 * java.util.Arrays.asList(this.getSignature).hashCode()}).</p>
308 *
309 * <p>This ensures that {@code t1.equals(t2) } implies that {@code
310 * t1.hashCode()==t2.hashCode() } for any two {@code
311 * OpenMBeanOperationInfoSupport} instances {@code t1} and {@code
312 * t2}, as required by the general contract of the method {@link
313 * Object#hashCode() Object.hashCode()}.</p>
314 *
315 * <p>However, note that another instance of a class implementing
316 * the {@code OpenMBeanOperationInfo} interface may be equal to
317 * this {@code OpenMBeanOperationInfoSupport} instance as defined
318 * by {@link #equals(java.lang.Object)}, but may have a different
319 * hash code if it is calculated differently.</p>
320 *
321 * <p>As {@code OpenMBeanOperationInfoSupport} instances are
322 * immutable, the hash code for this instance is calculated once,
323 * on the first call to {@code hashCode}, and then the same value
324 * is returned for subsequent calls.</p>
325 *
326 * @return the hash code value for this {@code
327 * OpenMBeanOperationInfoSupport} instance
328 */
329 public int hashCode() {
330
331 // Calculate the hash code value if it has not yet been done
332 // (ie 1st call to hashCode())
333 //
334 if (myHashCode == null) {
335 int value = 0;
336 value += this .getName().hashCode();
337 value += Arrays.asList(this .getSignature()).hashCode();
338 value += this .getReturnOpenType().hashCode();
339 value += this .getImpact();
340 myHashCode = new Integer(value);
341 }
342
343 // return always the same hash code for this instance (immutable)
344 //
345 return myHashCode.intValue();
346 }
347
348 /**
349 * <p>Returns a string representation of this {@code
350 * OpenMBeanOperationInfoSupport} instance.</p>
351 *
352 * <p>The string representation consists of the name of this class
353 * (ie {@code
354 * javax.management.openmbean.OpenMBeanOperationInfoSupport}), and
355 * the name, signature, return open type and impact of the
356 * described operation and the string representation of its descriptor.</p>
357 *
358 * <p>As {@code OpenMBeanOperationInfoSupport} instances are
359 * immutable, the string representation for this instance is
360 * calculated once, on the first call to {@code toString}, and
361 * then the same value is returned for subsequent calls.</p>
362 *
363 * @return a string representation of this {@code
364 * OpenMBeanOperationInfoSupport} instance
365 */
366 public String toString() {
367
368 // Calculate the hash code value if it has not yet been done
369 // (ie 1st call to toString())
370 //
371 if (myToString == null) {
372 myToString = new StringBuilder().append(
373 this .getClass().getName()).append("(name=").append(
374 this .getName()).append(",signature=").append(
375 Arrays.asList(this .getSignature()).toString())
376 .append(",return=").append(
377 this .getReturnOpenType().toString())
378 .append(",impact=").append(this .getImpact())
379 .append(",descriptor=")
380 .append(this .getDescriptor()).append(")")
381 .toString();
382 }
383
384 // return always the same string representation for this
385 // instance (immutable)
386 //
387 return myToString;
388 }
389
390 /**
391 * An object serialized in a version of the API before Descriptors were
392 * added to this class will have an empty or null Descriptor.
393 * For consistency with our
394 * behavior in this version, we must replace the object with one
395 * where the Descriptors reflect the same value of returned openType.
396 **/
397 private Object readResolve() {
398 if (getDescriptor().getFieldNames().length == 0) {
399 // This constructor will construct the expected default Descriptor.
400 //
401 return new OpenMBeanOperationInfoSupport(name, description,
402 arrayCopyCast(getSignature()), returnOpenType,
403 getImpact());
404 } else
405 return this;
406 }
407
408 }
|