|
1 /* |
|
2 * Copyright 2000-2007 Sun Microsystems, Inc. All Rights Reserved. |
|
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
|
4 * |
|
5 * This code is free software; you can redistribute it and/or modify it |
|
6 * under the terms of the GNU General Public License version 2 only, as |
|
7 * published by the Free Software Foundation. Sun designates this |
|
8 * particular file as subject to the "Classpath" exception as provided |
|
9 * by Sun in the LICENSE file that accompanied this code. |
|
10 * |
|
11 * This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 * version 2 for more details (a copy is included in the LICENSE file that |
|
15 * accompanied this code). |
|
16 * |
|
17 * You should have received a copy of the GNU General Public License version |
|
18 * 2 along with this work; if not, write to the Free Software Foundation, |
|
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 * |
|
21 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
|
22 * CA 95054 USA or visit www.sun.com if you need additional information or |
|
23 * have any questions. |
|
24 */ |
|
25 |
|
26 |
|
27 package javax.management.openmbean; |
|
28 |
|
29 |
|
30 // java import |
|
31 // |
|
32 import java.lang.reflect.Array; |
|
33 import java.lang.reflect.Constructor; |
|
34 import java.lang.reflect.Method; |
|
35 import java.lang.reflect.Modifier; |
|
36 import java.util.Arrays; |
|
37 import java.util.Collection; |
|
38 import java.util.Collections; |
|
39 import java.util.HashMap; |
|
40 import java.util.HashSet; |
|
41 import java.util.Map; |
|
42 import java.util.Set; |
|
43 import javax.management.Descriptor; |
|
44 import javax.management.DescriptorRead; |
|
45 import javax.management.ImmutableDescriptor; |
|
46 import javax.management.MBeanAttributeInfo; |
|
47 import com.sun.jmx.remote.util.EnvHelp; |
|
48 |
|
49 /** |
|
50 * Describes an attribute of an open MBean. |
|
51 * |
|
52 * |
|
53 * @since 1.5 |
|
54 */ |
|
55 public class OpenMBeanAttributeInfoSupport |
|
56 extends MBeanAttributeInfo |
|
57 implements OpenMBeanAttributeInfo { |
|
58 |
|
59 /* Serial version */ |
|
60 static final long serialVersionUID = -4867215622149721849L; |
|
61 |
|
62 /** |
|
63 * @serial The open mbean attribute's <i>open type</i> |
|
64 */ |
|
65 private OpenType<?> openType; |
|
66 |
|
67 /** |
|
68 * @serial The open mbean attribute's default value |
|
69 */ |
|
70 private final Object defaultValue; |
|
71 |
|
72 /** |
|
73 * @serial The open mbean attribute's legal values. This {@link |
|
74 * Set} is unmodifiable |
|
75 */ |
|
76 private final Set<?> legalValues; // to be constructed unmodifiable |
|
77 |
|
78 /** |
|
79 * @serial The open mbean attribute's min value |
|
80 */ |
|
81 private final Comparable minValue; |
|
82 |
|
83 /** |
|
84 * @serial The open mbean attribute's max value |
|
85 */ |
|
86 private final Comparable maxValue; |
|
87 |
|
88 |
|
89 // As this instance is immutable, these two values need only |
|
90 // be calculated once. |
|
91 private transient Integer myHashCode = null; |
|
92 private transient String myToString = null; |
|
93 |
|
94 |
|
95 /** |
|
96 * Constructs an {@code OpenMBeanAttributeInfoSupport} instance, |
|
97 * which describes the attribute of an open MBean with the |
|
98 * specified {@code name}, {@code openType} and {@code |
|
99 * description}, and the specified read/write access properties. |
|
100 * |
|
101 * @param name cannot be a null or empty string. |
|
102 * |
|
103 * @param description cannot be a null or empty string. |
|
104 * |
|
105 * @param openType cannot be null. |
|
106 * |
|
107 * @param isReadable {@code true} if the attribute has a getter |
|
108 * exposed for management. |
|
109 * |
|
110 * @param isWritable {@code true} if the attribute has a setter |
|
111 * exposed for management. |
|
112 * |
|
113 * @param isIs {@code true} if the attribute's getter is of the |
|
114 * form <tt>is<i>XXX</i></tt>. |
|
115 * |
|
116 * @throws IllegalArgumentException if {@code name} or {@code |
|
117 * description} are null or empty string, or {@code openType} is |
|
118 * null. |
|
119 */ |
|
120 public OpenMBeanAttributeInfoSupport(String name, |
|
121 String description, |
|
122 OpenType<?> openType, |
|
123 boolean isReadable, |
|
124 boolean isWritable, |
|
125 boolean isIs) { |
|
126 this(name, description, openType, isReadable, isWritable, isIs, |
|
127 (Descriptor) null); |
|
128 } |
|
129 |
|
130 /** |
|
131 * <p>Constructs an {@code OpenMBeanAttributeInfoSupport} instance, |
|
132 * which describes the attribute of an open MBean with the |
|
133 * specified {@code name}, {@code openType}, {@code |
|
134 * description}, read/write access properties, and {@code Descriptor}.</p> |
|
135 * |
|
136 * <p>The {@code descriptor} can contain entries that will define |
|
137 * the values returned by certain methods of this class, as |
|
138 * explained in the {@link <a href="package-summary.html#constraints"> |
|
139 * package description</a>}. |
|
140 * |
|
141 * @param name cannot be a null or empty string. |
|
142 * |
|
143 * @param description cannot be a null or empty string. |
|
144 * |
|
145 * @param openType cannot be null. |
|
146 * |
|
147 * @param isReadable {@code true} if the attribute has a getter |
|
148 * exposed for management. |
|
149 * |
|
150 * @param isWritable {@code true} if the attribute has a setter |
|
151 * exposed for management. |
|
152 * |
|
153 * @param isIs {@code true} if the attribute's getter is of the |
|
154 * form <tt>is<i>XXX</i></tt>. |
|
155 * |
|
156 * @param descriptor The descriptor for the attribute. This may be null |
|
157 * which is equivalent to an empty descriptor. |
|
158 * |
|
159 * @throws IllegalArgumentException if {@code name} or {@code |
|
160 * description} are null or empty string, or {@code openType} is |
|
161 * null, or the descriptor entries are invalid as described in the |
|
162 * {@link <a href="package-summary.html#constraints">package |
|
163 * description</a>}. |
|
164 * |
|
165 * @since 1.6 |
|
166 */ |
|
167 public OpenMBeanAttributeInfoSupport(String name, |
|
168 String description, |
|
169 OpenType<?> openType, |
|
170 boolean isReadable, |
|
171 boolean isWritable, |
|
172 boolean isIs, |
|
173 Descriptor descriptor) { |
|
174 // Construct parent's state |
|
175 // |
|
176 super(name, |
|
177 (openType==null) ? null : openType.getClassName(), |
|
178 description, |
|
179 isReadable, |
|
180 isWritable, |
|
181 isIs, |
|
182 ImmutableDescriptor.union(descriptor, (openType==null)?null: |
|
183 openType.getDescriptor())); |
|
184 |
|
185 // Initialize this instance's specific state |
|
186 // |
|
187 this.openType = openType; |
|
188 |
|
189 descriptor = getDescriptor(); // replace null by empty |
|
190 this.defaultValue = valueFrom(descriptor, "defaultValue", openType); |
|
191 this.legalValues = valuesFrom(descriptor, "legalValues", openType); |
|
192 this.minValue = comparableValueFrom(descriptor, "minValue", openType); |
|
193 this.maxValue = comparableValueFrom(descriptor, "maxValue", openType); |
|
194 |
|
195 try { |
|
196 check(this); |
|
197 } catch (OpenDataException e) { |
|
198 throw new IllegalArgumentException(e.getMessage(), e); |
|
199 } |
|
200 } |
|
201 |
|
202 /** |
|
203 * Constructs an {@code OpenMBeanAttributeInfoSupport} instance, |
|
204 * which describes the attribute of an open MBean with the |
|
205 * specified {@code name}, {@code openType}, {@code description} |
|
206 * and {@code defaultValue}, and the specified read/write access |
|
207 * properties. |
|
208 * |
|
209 * @param name cannot be a null or empty string. |
|
210 * |
|
211 * @param description cannot be a null or empty string. |
|
212 * |
|
213 * @param openType cannot be null. |
|
214 * |
|
215 * @param isReadable {@code true} if the attribute has a getter |
|
216 * exposed for management. |
|
217 * |
|
218 * @param isWritable {@code true} if the attribute has a setter |
|
219 * exposed for management. |
|
220 * |
|
221 * @param isIs {@code true} if the attribute's getter is of the |
|
222 * form <tt>is<i>XXX</i></tt>. |
|
223 * |
|
224 * @param defaultValue must be a valid value for the {@code |
|
225 * openType} specified for this attribute; default value not |
|
226 * supported for {@code ArrayType} and {@code TabularType}; can |
|
227 * be null, in which case it means that no default value is set. |
|
228 * |
|
229 * @param <T> allows the compiler to check that the {@code defaultValue}, |
|
230 * if non-null, has the correct Java type for the given {@code openType}. |
|
231 * |
|
232 * @throws IllegalArgumentException if {@code name} or {@code |
|
233 * description} are null or empty string, or {@code openType} is |
|
234 * null. |
|
235 * |
|
236 * @throws OpenDataException if {@code defaultValue} is not a |
|
237 * valid value for the specified {@code openType}, or {@code |
|
238 * defaultValue} is non null and {@code openType} is an {@code |
|
239 * ArrayType} or a {@code TabularType}. |
|
240 */ |
|
241 public <T> OpenMBeanAttributeInfoSupport(String name, |
|
242 String description, |
|
243 OpenType<T> openType, |
|
244 boolean isReadable, |
|
245 boolean isWritable, |
|
246 boolean isIs, |
|
247 T defaultValue) |
|
248 throws OpenDataException { |
|
249 this(name, description, openType, isReadable, isWritable, isIs, |
|
250 defaultValue, (T[]) null); |
|
251 } |
|
252 |
|
253 |
|
254 /** |
|
255 * <p>Constructs an {@code OpenMBeanAttributeInfoSupport} instance, |
|
256 * which describes the attribute of an open MBean with the |
|
257 * specified {@code name}, {@code openType}, {@code description}, |
|
258 * {@code defaultValue} and {@code legalValues}, and the specified |
|
259 * read/write access properties.</p> |
|
260 * |
|
261 * <p>The contents of {@code legalValues} are copied, so subsequent |
|
262 * modifications of the array referenced by {@code legalValues} |
|
263 * have no impact on this {@code OpenMBeanAttributeInfoSupport} |
|
264 * instance.</p> |
|
265 * |
|
266 * @param name cannot be a null or empty string. |
|
267 * |
|
268 * @param description cannot be a null or empty string. |
|
269 * |
|
270 * @param openType cannot be null. |
|
271 * |
|
272 * @param isReadable {@code true} if the attribute has a getter |
|
273 * exposed for management. |
|
274 * |
|
275 * @param isWritable {@code true} if the attribute has a setter |
|
276 * exposed for management. |
|
277 * |
|
278 * @param isIs {@code true} if the attribute's getter is of the |
|
279 * form <tt>is<i>XXX</i></tt>. |
|
280 * |
|
281 * @param defaultValue must be a valid value |
|
282 * for the {@code |
|
283 * openType} specified for this attribute; default value not |
|
284 * supported for {@code ArrayType} and {@code TabularType}; can |
|
285 * be null, in which case it means that no default value is set. |
|
286 * |
|
287 * @param legalValues each contained value must be valid for the |
|
288 * {@code openType} specified for this attribute; legal values |
|
289 * not supported for {@code ArrayType} and {@code TabularType}; |
|
290 * can be null or empty. |
|
291 * |
|
292 * @param <T> allows the compiler to check that the {@code |
|
293 * defaultValue} and {@code legalValues}, if non-null, have the |
|
294 * correct Java type for the given {@code openType}. |
|
295 * |
|
296 * @throws IllegalArgumentException if {@code name} or {@code |
|
297 * description} are null or empty string, or {@code openType} is |
|
298 * null. |
|
299 * |
|
300 * @throws OpenDataException if {@code defaultValue} is not a |
|
301 * valid value for the specified {@code openType}, or one value in |
|
302 * {@code legalValues} is not valid for the specified {@code |
|
303 * openType}, or {@code defaultValue} is non null and {@code |
|
304 * openType} is an {@code ArrayType} or a {@code TabularType}, or |
|
305 * {@code legalValues} is non null and non empty and {@code |
|
306 * openType} is an {@code ArrayType} or a {@code TabularType}, or |
|
307 * {@code legalValues} is non null and non empty and {@code |
|
308 * defaultValue} is not contained in {@code legalValues}. |
|
309 */ |
|
310 public <T> OpenMBeanAttributeInfoSupport(String name, |
|
311 String description, |
|
312 OpenType<T> openType, |
|
313 boolean isReadable, |
|
314 boolean isWritable, |
|
315 boolean isIs, |
|
316 T defaultValue, |
|
317 T[] legalValues) |
|
318 throws OpenDataException { |
|
319 this(name, description, openType, isReadable, isWritable, isIs, |
|
320 defaultValue, legalValues, null, null); |
|
321 } |
|
322 |
|
323 |
|
324 /** |
|
325 * Constructs an {@code OpenMBeanAttributeInfoSupport} instance, |
|
326 * which describes the attribute of an open MBean, with the |
|
327 * specified {@code name}, {@code openType}, {@code description}, |
|
328 * {@code defaultValue}, {@code minValue} and {@code maxValue}. |
|
329 * |
|
330 * It is possible to specify minimal and maximal values only for |
|
331 * an open type whose values are {@code Comparable}. |
|
332 * |
|
333 * @param name cannot be a null or empty string. |
|
334 * |
|
335 * @param description cannot be a null or empty string. |
|
336 * |
|
337 * @param openType cannot be null. |
|
338 * |
|
339 * @param isReadable {@code true} if the attribute has a getter |
|
340 * exposed for management. |
|
341 * |
|
342 * @param isWritable {@code true} if the attribute has a setter |
|
343 * exposed for management. |
|
344 * |
|
345 * @param isIs {@code true} if the attribute's getter is of the |
|
346 * form <tt>is<i>XXX</i></tt>. |
|
347 * |
|
348 * @param defaultValue must be a valid value for the {@code |
|
349 * openType} specified for this attribute; default value not |
|
350 * supported for {@code ArrayType} and {@code TabularType}; can be |
|
351 * null, in which case it means that no default value is set. |
|
352 * |
|
353 * @param minValue must be valid for the {@code openType} |
|
354 * specified for this attribute; can be null, in which case it |
|
355 * means that no minimal value is set. |
|
356 * |
|
357 * @param maxValue must be valid for the {@code openType} |
|
358 * specified for this attribute; can be null, in which case it |
|
359 * means that no maximal value is set. |
|
360 * |
|
361 * @param <T> allows the compiler to check that the {@code |
|
362 * defaultValue}, {@code minValue}, and {@code maxValue}, if |
|
363 * non-null, have the correct Java type for the given {@code |
|
364 * openType}. |
|
365 * |
|
366 * @throws IllegalArgumentException if {@code name} or {@code |
|
367 * description} are null or empty string, or {@code openType} is |
|
368 * null. |
|
369 * |
|
370 * @throws OpenDataException if {@code defaultValue}, {@code |
|
371 * minValue} or {@code maxValue} is not a valid value for the |
|
372 * specified {@code openType}, or {@code defaultValue} is non null |
|
373 * and {@code openType} is an {@code ArrayType} or a {@code |
|
374 * TabularType}, or both {@code minValue} and {@code maxValue} are |
|
375 * non-null and {@code minValue.compareTo(maxValue) > 0} is {@code |
|
376 * true}, or both {@code defaultValue} and {@code minValue} are |
|
377 * non-null and {@code minValue.compareTo(defaultValue) > 0} is |
|
378 * {@code true}, or both {@code defaultValue} and {@code maxValue} |
|
379 * are non-null and {@code defaultValue.compareTo(maxValue) > 0} |
|
380 * is {@code true}. |
|
381 */ |
|
382 public <T> OpenMBeanAttributeInfoSupport(String name, |
|
383 String description, |
|
384 OpenType<T> openType, |
|
385 boolean isReadable, |
|
386 boolean isWritable, |
|
387 boolean isIs, |
|
388 T defaultValue, |
|
389 Comparable<T> minValue, |
|
390 Comparable<T> maxValue) |
|
391 throws OpenDataException { |
|
392 this(name, description, openType, isReadable, isWritable, isIs, |
|
393 defaultValue, null, minValue, maxValue); |
|
394 } |
|
395 |
|
396 private <T> OpenMBeanAttributeInfoSupport(String name, |
|
397 String description, |
|
398 OpenType<T> openType, |
|
399 boolean isReadable, |
|
400 boolean isWritable, |
|
401 boolean isIs, |
|
402 T defaultValue, |
|
403 T[] legalValues, |
|
404 Comparable<T> minValue, |
|
405 Comparable<T> maxValue) |
|
406 throws OpenDataException { |
|
407 super(name, |
|
408 (openType==null) ? null : openType.getClassName(), |
|
409 description, |
|
410 isReadable, |
|
411 isWritable, |
|
412 isIs, |
|
413 makeDescriptor(openType, |
|
414 defaultValue, legalValues, minValue, maxValue)); |
|
415 |
|
416 this.openType = openType; |
|
417 |
|
418 Descriptor d = getDescriptor(); |
|
419 this.defaultValue = defaultValue; |
|
420 this.minValue = minValue; |
|
421 this.maxValue = maxValue; |
|
422 // We already converted the array into an unmodifiable Set |
|
423 // in the descriptor. |
|
424 this.legalValues = (Set<?>) d.getFieldValue("legalValues"); |
|
425 |
|
426 check(this); |
|
427 } |
|
428 |
|
429 /** |
|
430 * An object serialized in a version of the API before Descriptors were |
|
431 * added to this class will have an empty or null Descriptor. |
|
432 * For consistency with our |
|
433 * behavior in this version, we must replace the object with one |
|
434 * where the Descriptors reflect the same values of openType, defaultValue, |
|
435 * etc. |
|
436 **/ |
|
437 private Object readResolve() { |
|
438 if (getDescriptor().getFieldNames().length == 0) { |
|
439 OpenType<Object> xopenType = cast(openType); |
|
440 Set<Object> xlegalValues = cast(legalValues); |
|
441 Comparable<Object> xminValue = cast(minValue); |
|
442 Comparable<Object> xmaxValue = cast(maxValue); |
|
443 return new OpenMBeanAttributeInfoSupport( |
|
444 name, description, openType, |
|
445 isReadable(), isWritable(), isIs(), |
|
446 makeDescriptor(xopenType, defaultValue, xlegalValues, |
|
447 xminValue, xmaxValue)); |
|
448 } else |
|
449 return this; |
|
450 } |
|
451 |
|
452 static void check(OpenMBeanParameterInfo info) throws OpenDataException { |
|
453 OpenType openType = info.getOpenType(); |
|
454 if (openType == null) |
|
455 throw new IllegalArgumentException("OpenType cannot be null"); |
|
456 |
|
457 if (info.getName() == null || |
|
458 info.getName().trim().equals("")) |
|
459 throw new IllegalArgumentException("Name cannot be null or empty"); |
|
460 |
|
461 if (info.getDescription() == null || |
|
462 info.getDescription().trim().equals("")) |
|
463 throw new IllegalArgumentException("Description cannot be null or empty"); |
|
464 |
|
465 // Check and initialize defaultValue |
|
466 // |
|
467 if (info.hasDefaultValue()) { |
|
468 // Default value not supported for ArrayType and TabularType |
|
469 // Cast to Object because "OpenType<T> instanceof" is illegal |
|
470 if (openType.isArray() || (Object)openType instanceof TabularType) { |
|
471 throw new OpenDataException("Default value not supported " + |
|
472 "for ArrayType and TabularType"); |
|
473 } |
|
474 // Check defaultValue's class |
|
475 if (!openType.isValue(info.getDefaultValue())) { |
|
476 final String msg = |
|
477 "Argument defaultValue's class [\"" + |
|
478 info.getDefaultValue().getClass().getName() + |
|
479 "\"] does not match the one defined in openType[\"" + |
|
480 openType.getClassName() +"\"]"; |
|
481 throw new OpenDataException(msg); |
|
482 } |
|
483 } |
|
484 |
|
485 // Check that we don't have both legalValues and min or max |
|
486 // |
|
487 if (info.hasLegalValues() && |
|
488 (info.hasMinValue() || info.hasMaxValue())) { |
|
489 throw new OpenDataException("cannot have both legalValue and " + |
|
490 "minValue or maxValue"); |
|
491 } |
|
492 |
|
493 // Check minValue and maxValue |
|
494 if (info.hasMinValue() && !openType.isValue(info.getMinValue())) { |
|
495 final String msg = |
|
496 "Type of minValue [" + info.getMinValue().getClass().getName() + |
|
497 "] does not match OpenType [" + openType.getClassName() + "]"; |
|
498 throw new OpenDataException(msg); |
|
499 } |
|
500 if (info.hasMaxValue() && !openType.isValue(info.getMaxValue())) { |
|
501 final String msg = |
|
502 "Type of maxValue [" + info.getMaxValue().getClass().getName() + |
|
503 "] does not match OpenType [" + openType.getClassName() + "]"; |
|
504 throw new OpenDataException(msg); |
|
505 } |
|
506 |
|
507 // Check that defaultValue is a legal value |
|
508 // |
|
509 if (info.hasDefaultValue()) { |
|
510 Object defaultValue = info.getDefaultValue(); |
|
511 if (info.hasLegalValues() && |
|
512 !info.getLegalValues().contains(defaultValue)) { |
|
513 throw new OpenDataException("defaultValue is not contained " + |
|
514 "in legalValues"); |
|
515 } |
|
516 |
|
517 // Check that minValue <= defaultValue <= maxValue |
|
518 // |
|
519 if (info.hasMinValue()) { |
|
520 if (compare(info.getMinValue(), defaultValue) > 0) { |
|
521 throw new OpenDataException("minValue cannot be greater " + |
|
522 "than defaultValue"); |
|
523 } |
|
524 } |
|
525 if (info.hasMaxValue()) { |
|
526 if (compare(info.getMaxValue(), defaultValue) < 0) { |
|
527 throw new OpenDataException("maxValue cannot be less " + |
|
528 "than defaultValue"); |
|
529 } |
|
530 } |
|
531 } |
|
532 |
|
533 // Check legalValues |
|
534 // |
|
535 if (info.hasLegalValues()) { |
|
536 // legalValues not supported for TabularType and arrays |
|
537 if ((Object)openType instanceof TabularType || openType.isArray()) { |
|
538 throw new OpenDataException("Legal values not supported " + |
|
539 "for TabularType and arrays"); |
|
540 } |
|
541 // Check legalValues are valid with openType |
|
542 for (Object v : info.getLegalValues()) { |
|
543 if (!openType.isValue(v)) { |
|
544 final String msg = |
|
545 "Element of legalValues [" + v + |
|
546 "] is not a valid value for the specified openType [" + |
|
547 openType.toString() +"]"; |
|
548 throw new OpenDataException(msg); |
|
549 } |
|
550 } |
|
551 } |
|
552 |
|
553 |
|
554 // Check that, if both specified, minValue <= maxValue |
|
555 // |
|
556 if (info.hasMinValue() && info.hasMaxValue()) { |
|
557 if (compare(info.getMinValue(), info.getMaxValue()) > 0) { |
|
558 throw new OpenDataException("minValue cannot be greater " + |
|
559 "than maxValue"); |
|
560 } |
|
561 } |
|
562 |
|
563 } |
|
564 |
|
565 @SuppressWarnings("unchecked") |
|
566 static int compare(Object x, Object y) { |
|
567 return ((Comparable) x).compareTo(y); |
|
568 } |
|
569 |
|
570 static <T> Descriptor makeDescriptor(OpenType<T> openType, |
|
571 T defaultValue, |
|
572 T[] legalValues, |
|
573 Comparable<T> minValue, |
|
574 Comparable<T> maxValue) { |
|
575 Map<String, Object> map = new HashMap<String, Object>(); |
|
576 if (defaultValue != null) |
|
577 map.put("defaultValue", defaultValue); |
|
578 if (legalValues != null) { |
|
579 Set<T> set = new HashSet<T>(); |
|
580 for (T v : legalValues) |
|
581 set.add(v); |
|
582 set = Collections.unmodifiableSet(set); |
|
583 map.put("legalValues", set); |
|
584 } |
|
585 if (minValue != null) |
|
586 map.put("minValue", minValue); |
|
587 if (maxValue != null) |
|
588 map.put("maxValue", maxValue); |
|
589 if (map.isEmpty()) { |
|
590 return openType.getDescriptor(); |
|
591 } else { |
|
592 map.put("openType", openType); |
|
593 return new ImmutableDescriptor(map); |
|
594 } |
|
595 } |
|
596 |
|
597 static <T> Descriptor makeDescriptor(OpenType<T> openType, |
|
598 T defaultValue, |
|
599 Set<T> legalValues, |
|
600 Comparable<T> minValue, |
|
601 Comparable<T> maxValue) { |
|
602 T[] legals; |
|
603 if (legalValues == null) |
|
604 legals = null; |
|
605 else { |
|
606 legals = cast(new Object[legalValues.size()]); |
|
607 legalValues.toArray(legals); |
|
608 } |
|
609 return makeDescriptor(openType, defaultValue, legals, minValue, maxValue); |
|
610 } |
|
611 |
|
612 |
|
613 static <T> T valueFrom(Descriptor d, String name, OpenType<T> openType) { |
|
614 Object x = d.getFieldValue(name); |
|
615 if (x == null) |
|
616 return null; |
|
617 try { |
|
618 return convertFrom(x, openType); |
|
619 } catch (Exception e) { |
|
620 final String msg = |
|
621 "Cannot convert descriptor field " + name + " to " + |
|
622 openType.getTypeName(); |
|
623 throw EnvHelp.initCause(new IllegalArgumentException(msg), e); |
|
624 } |
|
625 } |
|
626 |
|
627 static <T> Set<T> valuesFrom(Descriptor d, String name, |
|
628 OpenType<T> openType) { |
|
629 Object x = d.getFieldValue(name); |
|
630 if (x == null) |
|
631 return null; |
|
632 Collection<?> coll; |
|
633 if (x instanceof Set<?>) { |
|
634 Set<?> set = (Set<?>) x; |
|
635 boolean asis = true; |
|
636 for (Object element : set) { |
|
637 if (!openType.isValue(element)) { |
|
638 asis = false; |
|
639 break; |
|
640 } |
|
641 } |
|
642 if (asis) |
|
643 return cast(set); |
|
644 coll = set; |
|
645 } else if (x instanceof Object[]) { |
|
646 coll = Arrays.asList((Object[]) x); |
|
647 } else { |
|
648 final String msg = |
|
649 "Descriptor value for " + name + " must be a Set or " + |
|
650 "an array: " + x.getClass().getName(); |
|
651 throw new IllegalArgumentException(msg); |
|
652 } |
|
653 |
|
654 Set<T> result = new HashSet<T>(); |
|
655 for (Object element : coll) |
|
656 result.add(convertFrom(element, openType)); |
|
657 return result; |
|
658 } |
|
659 |
|
660 static <T> Comparable comparableValueFrom(Descriptor d, String name, |
|
661 OpenType<T> openType) { |
|
662 T t = valueFrom(d, name, openType); |
|
663 if (t == null || t instanceof Comparable<?>) |
|
664 return (Comparable) t; |
|
665 final String msg = |
|
666 "Descriptor field " + name + " with value " + t + |
|
667 " is not Comparable"; |
|
668 throw new IllegalArgumentException(msg); |
|
669 } |
|
670 |
|
671 private static <T> T convertFrom(Object x, OpenType<T> openType) { |
|
672 if (openType.isValue(x)) { |
|
673 T t = OpenMBeanAttributeInfoSupport.<T>cast(x); |
|
674 return t; |
|
675 } |
|
676 return convertFromStrings(x, openType); |
|
677 } |
|
678 |
|
679 private static <T> T convertFromStrings(Object x, OpenType<T> openType) { |
|
680 if (openType instanceof ArrayType<?>) |
|
681 return convertFromStringArray(x, openType); |
|
682 else if (x instanceof String) |
|
683 return convertFromString((String) x, openType); |
|
684 final String msg = |
|
685 "Cannot convert value " + x + " of type " + |
|
686 x.getClass().getName() + " to type " + openType.getTypeName(); |
|
687 throw new IllegalArgumentException(msg); |
|
688 } |
|
689 |
|
690 private static <T> T convertFromString(String s, OpenType<T> openType) { |
|
691 Class<T> c; |
|
692 try { |
|
693 c = cast(Class.forName(openType.getClassName())); |
|
694 } catch (ClassNotFoundException e) { |
|
695 throw new NoClassDefFoundError(e.toString()); // can't happen |
|
696 } |
|
697 |
|
698 // Look for: public static T valueOf(String) |
|
699 Method valueOf; |
|
700 try { |
|
701 valueOf = c.getMethod("valueOf", String.class); |
|
702 if (!Modifier.isStatic(valueOf.getModifiers()) || |
|
703 valueOf.getReturnType() != c) |
|
704 valueOf = null; |
|
705 } catch (NoSuchMethodException e) { |
|
706 valueOf = null; |
|
707 } |
|
708 if (valueOf != null) { |
|
709 try { |
|
710 return c.cast(valueOf.invoke(null, s)); |
|
711 } catch (Exception e) { |
|
712 final String msg = |
|
713 "Could not convert \"" + s + "\" using method: " + valueOf; |
|
714 throw new IllegalArgumentException(msg); |
|
715 } |
|
716 } |
|
717 |
|
718 // Look for: public T(String) |
|
719 Constructor<T> con; |
|
720 try { |
|
721 con = c.getConstructor(String.class); |
|
722 } catch (NoSuchMethodException e) { |
|
723 con = null; |
|
724 } |
|
725 if (con != null) { |
|
726 try { |
|
727 return con.newInstance(s); |
|
728 } catch (Exception e) { |
|
729 final String msg = |
|
730 "Could not convert \"" + s + "\" using constructor: " + con; |
|
731 throw new IllegalArgumentException(msg); |
|
732 } |
|
733 } |
|
734 |
|
735 throw new IllegalArgumentException("Don't know how to convert " + |
|
736 "string to " + |
|
737 openType.getTypeName()); |
|
738 } |
|
739 |
|
740 |
|
741 /* A Descriptor contained an array value encoded as Strings. The |
|
742 Strings must be organized in an array corresponding to the desired |
|
743 array. If the desired array has n dimensions, so must the String |
|
744 array. We will convert element by element from String to desired |
|
745 component type. */ |
|
746 private static <T> T convertFromStringArray(Object x, |
|
747 OpenType<T> openType) { |
|
748 ArrayType<?> arrayType = (ArrayType<?>) openType; |
|
749 OpenType<?> baseType = arrayType.getElementOpenType(); |
|
750 int dim = arrayType.getDimension(); |
|
751 String squareBrackets = "["; |
|
752 for (int i = 1; i < dim; i++) |
|
753 squareBrackets += "["; |
|
754 Class<?> stringArrayClass; |
|
755 Class<?> targetArrayClass; |
|
756 try { |
|
757 stringArrayClass = |
|
758 Class.forName(squareBrackets + "Ljava.lang.String;"); |
|
759 targetArrayClass = |
|
760 Class.forName(squareBrackets + "L" + baseType.getClassName() + |
|
761 ";"); |
|
762 } catch (ClassNotFoundException e) { |
|
763 throw new NoClassDefFoundError(e.toString()); // can't happen |
|
764 } |
|
765 if (!stringArrayClass.isInstance(x)) { |
|
766 final String msg = |
|
767 "Value for " + dim + "-dimensional array of " + |
|
768 baseType.getTypeName() + " must be same type or a String " + |
|
769 "array with same dimensions"; |
|
770 throw new IllegalArgumentException(msg); |
|
771 } |
|
772 Class<?> targetComponentClass = targetArrayClass.getComponentType(); |
|
773 OpenType<?> componentOpenType; |
|
774 if (dim == 1) |
|
775 componentOpenType = baseType; |
|
776 else { |
|
777 try { |
|
778 componentOpenType = new ArrayType<T>(dim - 1, baseType); |
|
779 } catch (OpenDataException e) { |
|
780 throw new IllegalArgumentException(e.getMessage(), e); |
|
781 // can't happen |
|
782 } |
|
783 } |
|
784 int n = Array.getLength(x); |
|
785 Object[] targetArray = (Object[]) |
|
786 Array.newInstance(targetArrayClass.getComponentType(), n); |
|
787 for (int i = 0; i < n; i++) { |
|
788 Object stringish = Array.get(x, i); // String or String[] etc |
|
789 Object converted = |
|
790 convertFromStrings(stringish, componentOpenType); |
|
791 Array.set(targetArray, i, converted); |
|
792 } |
|
793 return OpenMBeanAttributeInfoSupport.<T>cast(targetArray); |
|
794 } |
|
795 |
|
796 @SuppressWarnings("unchecked") |
|
797 static <T> T cast(Object x) { |
|
798 return (T) x; |
|
799 } |
|
800 |
|
801 /** |
|
802 * Returns the open type for the values of the attribute described |
|
803 * by this {@code OpenMBeanAttributeInfoSupport} instance. |
|
804 */ |
|
805 public OpenType<?> getOpenType() { |
|
806 return openType; |
|
807 } |
|
808 |
|
809 /** |
|
810 * Returns the default value for the attribute described by this |
|
811 * {@code OpenMBeanAttributeInfoSupport} instance, if specified, |
|
812 * or {@code null} otherwise. |
|
813 */ |
|
814 public Object getDefaultValue() { |
|
815 |
|
816 // Special case for ArrayType and TabularType |
|
817 // [JF] TODO: clone it so that it cannot be altered, |
|
818 // [JF] TODO: if we decide to support defaultValue as an array itself. |
|
819 // [JF] As of today (oct 2000) it is not supported so |
|
820 // defaultValue is null for arrays. Nothing to do. |
|
821 |
|
822 return defaultValue; |
|
823 } |
|
824 |
|
825 /** |
|
826 * Returns an unmodifiable Set of legal values for the attribute |
|
827 * described by this {@code OpenMBeanAttributeInfoSupport} |
|
828 * instance, if specified, or {@code null} otherwise. |
|
829 */ |
|
830 public Set<?> getLegalValues() { |
|
831 |
|
832 // Special case for ArrayType and TabularType |
|
833 // [JF] TODO: clone values so that they cannot be altered, |
|
834 // [JF] TODO: if we decide to support LegalValues as an array itself. |
|
835 // [JF] As of today (oct 2000) it is not supported so |
|
836 // legalValues is null for arrays. Nothing to do. |
|
837 |
|
838 // Returns our legalValues Set (set was constructed unmodifiable) |
|
839 return (legalValues); |
|
840 } |
|
841 |
|
842 /** |
|
843 * Returns the minimal value for the attribute described by this |
|
844 * {@code OpenMBeanAttributeInfoSupport} instance, if specified, |
|
845 * or {@code null} otherwise. |
|
846 */ |
|
847 public Comparable<?> getMinValue() { |
|
848 |
|
849 // Note: only comparable values have a minValue, |
|
850 // so that's not the case of arrays and tabulars (always null). |
|
851 |
|
852 return minValue; |
|
853 } |
|
854 |
|
855 /** |
|
856 * Returns the maximal value for the attribute described by this |
|
857 * {@code OpenMBeanAttributeInfoSupport} instance, if specified, |
|
858 * or {@code null} otherwise. |
|
859 */ |
|
860 public Comparable<?> getMaxValue() { |
|
861 |
|
862 // Note: only comparable values have a maxValue, |
|
863 // so that's not the case of arrays and tabulars (always null). |
|
864 |
|
865 return maxValue; |
|
866 } |
|
867 |
|
868 /** |
|
869 * Returns {@code true} if this {@code |
|
870 * OpenMBeanAttributeInfoSupport} instance specifies a non-null |
|
871 * default value for the described attribute, {@code false} |
|
872 * otherwise. |
|
873 */ |
|
874 public boolean hasDefaultValue() { |
|
875 |
|
876 return (defaultValue != null); |
|
877 } |
|
878 |
|
879 /** |
|
880 * Returns {@code true} if this {@code |
|
881 * OpenMBeanAttributeInfoSupport} instance specifies a non-null |
|
882 * set of legal values for the described attribute, {@code false} |
|
883 * otherwise. |
|
884 */ |
|
885 public boolean hasLegalValues() { |
|
886 |
|
887 return (legalValues != null); |
|
888 } |
|
889 |
|
890 /** |
|
891 * Returns {@code true} if this {@code |
|
892 * OpenMBeanAttributeInfoSupport} instance specifies a non-null |
|
893 * minimal value for the described attribute, {@code false} |
|
894 * otherwise. |
|
895 */ |
|
896 public boolean hasMinValue() { |
|
897 |
|
898 return (minValue != null); |
|
899 } |
|
900 |
|
901 /** |
|
902 * Returns {@code true} if this {@code |
|
903 * OpenMBeanAttributeInfoSupport} instance specifies a non-null |
|
904 * maximal value for the described attribute, {@code false} |
|
905 * otherwise. |
|
906 */ |
|
907 public boolean hasMaxValue() { |
|
908 |
|
909 return (maxValue != null); |
|
910 } |
|
911 |
|
912 |
|
913 /** |
|
914 * Tests whether {@code obj} is a valid value for the attribute |
|
915 * described by this {@code OpenMBeanAttributeInfoSupport} |
|
916 * instance. |
|
917 * |
|
918 * @param obj the object to be tested. |
|
919 * |
|
920 * @return {@code true} if {@code obj} is a valid value for |
|
921 * the parameter described by this {@code |
|
922 * OpenMBeanAttributeInfoSupport} instance, {@code false} |
|
923 * otherwise. |
|
924 */ |
|
925 public boolean isValue(Object obj) { |
|
926 return isValue(this, obj); |
|
927 } |
|
928 |
|
929 @SuppressWarnings("unchecked") // cast to Comparable |
|
930 static boolean isValue(OpenMBeanParameterInfo info, Object obj) { |
|
931 if (info.hasDefaultValue() && obj == null) |
|
932 return true; |
|
933 return |
|
934 info.getOpenType().isValue(obj) && |
|
935 (!info.hasLegalValues() || info.getLegalValues().contains(obj)) && |
|
936 (!info.hasMinValue() || |
|
937 ((Comparable) info.getMinValue()).compareTo(obj) <= 0) && |
|
938 (!info.hasMaxValue() || |
|
939 ((Comparable) info.getMaxValue()).compareTo(obj) >= 0); |
|
940 } |
|
941 |
|
942 /* *** Commodity methods from java.lang.Object *** */ |
|
943 |
|
944 |
|
945 /** |
|
946 * Compares the specified {@code obj} parameter with this {@code |
|
947 * OpenMBeanAttributeInfoSupport} instance for equality. |
|
948 * <p> |
|
949 * Returns {@code true} if and only if all of the following statements are true: |
|
950 * <ul> |
|
951 * <li>{@code obj} is non null,</li> |
|
952 * <li>{@code obj} also implements the {@code OpenMBeanAttributeInfo} interface,</li> |
|
953 * <li>their names are equal</li> |
|
954 * <li>their open types are equal</li> |
|
955 * <li>their access properties (isReadable, isWritable and isIs) are equal</li> |
|
956 * <li>their default, min, max and legal values are equal.</li> |
|
957 * </ul> |
|
958 * This ensures that this {@code equals} method works properly for |
|
959 * {@code obj} parameters which are different implementations of |
|
960 * the {@code OpenMBeanAttributeInfo} interface. |
|
961 * |
|
962 * <p>If {@code obj} also implements {@link DescriptorRead}, then its |
|
963 * {@link DescriptorRead#getDescriptor() getDescriptor()} method must |
|
964 * also return the same value as for this object.</p> |
|
965 * |
|
966 * @param obj the object to be compared for equality with this |
|
967 * {@code OpenMBeanAttributeInfoSupport} instance. |
|
968 * |
|
969 * @return {@code true} if the specified object is equal to this |
|
970 * {@code OpenMBeanAttributeInfoSupport} instance. |
|
971 */ |
|
972 public boolean equals(Object obj) { |
|
973 if (!(obj instanceof OpenMBeanAttributeInfo)) |
|
974 return false; |
|
975 |
|
976 OpenMBeanAttributeInfo other = (OpenMBeanAttributeInfo) obj; |
|
977 |
|
978 return |
|
979 this.isReadable() == other.isReadable() && |
|
980 this.isWritable() == other.isWritable() && |
|
981 this.isIs() == other.isIs() && |
|
982 equal(this, other); |
|
983 } |
|
984 |
|
985 static boolean equal(OpenMBeanParameterInfo x1, OpenMBeanParameterInfo x2) { |
|
986 if (x1 instanceof DescriptorRead) { |
|
987 if (!(x2 instanceof DescriptorRead)) |
|
988 return false; |
|
989 Descriptor d1 = ((DescriptorRead) x1).getDescriptor(); |
|
990 Descriptor d2 = ((DescriptorRead) x2).getDescriptor(); |
|
991 if (!d1.equals(d2)) |
|
992 return false; |
|
993 } else if (x2 instanceof DescriptorRead) |
|
994 return false; |
|
995 |
|
996 return |
|
997 x1.getName().equals(x2.getName()) && |
|
998 x1.getOpenType().equals(x2.getOpenType()) && |
|
999 (x1.hasDefaultValue() ? |
|
1000 x1.getDefaultValue().equals(x2.getDefaultValue()) : |
|
1001 !x2.hasDefaultValue()) && |
|
1002 (x1.hasMinValue() ? |
|
1003 x1.getMinValue().equals(x2.getMinValue()) : |
|
1004 !x2.hasMinValue()) && |
|
1005 (x1.hasMaxValue() ? |
|
1006 x1.getMaxValue().equals(x2.getMaxValue()) : |
|
1007 !x2.hasMaxValue()) && |
|
1008 (x1.hasLegalValues() ? |
|
1009 x1.getLegalValues().equals(x2.getLegalValues()) : |
|
1010 !x2.hasLegalValues()); |
|
1011 } |
|
1012 |
|
1013 /** |
|
1014 * <p>Returns the hash code value for this {@code |
|
1015 * OpenMBeanAttributeInfoSupport} instance.</p> |
|
1016 * |
|
1017 * <p>The hash code of an {@code OpenMBeanAttributeInfoSupport} |
|
1018 * instance is the sum of the hash codes of all elements of |
|
1019 * information used in {@code equals} comparisons (ie: its name, |
|
1020 * its <i>open type</i>, its default, min, max and legal |
|
1021 * values, and its Descriptor). |
|
1022 * |
|
1023 * <p>This ensures that {@code t1.equals(t2)} implies that {@code |
|
1024 * t1.hashCode()==t2.hashCode()} for any two {@code |
|
1025 * OpenMBeanAttributeInfoSupport} instances {@code t1} and {@code |
|
1026 * t2}, as required by the general contract of the method {@link |
|
1027 * Object#hashCode() Object.hashCode()}. |
|
1028 * |
|
1029 * <p>However, note that another instance of a class implementing |
|
1030 * the {@code OpenMBeanAttributeInfo} interface may be equal to |
|
1031 * this {@code OpenMBeanAttributeInfoSupport} instance as defined |
|
1032 * by {@link #equals(java.lang.Object)}, but may have a different |
|
1033 * hash code if it is calculated differently. |
|
1034 * |
|
1035 * <p>As {@code OpenMBeanAttributeInfoSupport} instances are |
|
1036 * immutable, the hash code for this instance is calculated once, |
|
1037 * on the first call to {@code hashCode}, and then the same value |
|
1038 * is returned for subsequent calls. |
|
1039 * |
|
1040 * @return the hash code value for this {@code |
|
1041 * OpenMBeanAttributeInfoSupport} instance |
|
1042 */ |
|
1043 public int hashCode() { |
|
1044 |
|
1045 // Calculate the hash code value if it has not yet been done |
|
1046 // (ie 1st call to hashCode()) |
|
1047 // |
|
1048 if (myHashCode == null) |
|
1049 myHashCode = hashCode(this); |
|
1050 |
|
1051 // return always the same hash code for this instance (immutable) |
|
1052 // |
|
1053 return myHashCode.intValue(); |
|
1054 } |
|
1055 |
|
1056 static int hashCode(OpenMBeanParameterInfo info) { |
|
1057 int value = 0; |
|
1058 value += info.getName().hashCode(); |
|
1059 value += info.getOpenType().hashCode(); |
|
1060 if (info.hasDefaultValue()) |
|
1061 value += info.getDefaultValue().hashCode(); |
|
1062 if (info.hasMinValue()) |
|
1063 value += info.getMinValue().hashCode(); |
|
1064 if (info.hasMaxValue()) |
|
1065 value += info.getMaxValue().hashCode(); |
|
1066 if (info.hasLegalValues()) |
|
1067 value += info.getLegalValues().hashCode(); |
|
1068 if (info instanceof DescriptorRead) |
|
1069 value += ((DescriptorRead) info).getDescriptor().hashCode(); |
|
1070 return value; |
|
1071 } |
|
1072 |
|
1073 /** |
|
1074 * Returns a string representation of this |
|
1075 * {@code OpenMBeanAttributeInfoSupport} instance. |
|
1076 * <p> |
|
1077 * The string representation consists of the name of this class (i.e. |
|
1078 * {@code javax.management.openmbean.OpenMBeanAttributeInfoSupport}), |
|
1079 * the string representation of the name and open type of the |
|
1080 * described parameter, the string representation of its |
|
1081 * default, min, max and legal values and the string |
|
1082 * representation of its descriptor. |
|
1083 * |
|
1084 * <p>As {@code OpenMBeanAttributeInfoSupport} instances are |
|
1085 * immutable, the string representation for this instance is |
|
1086 * calculated once, on the first call to {@code toString}, and |
|
1087 * then the same value is returned for subsequent calls. |
|
1088 * |
|
1089 * @return a string representation of this |
|
1090 * {@code OpenMBeanAttributeInfoSupport} instance. |
|
1091 */ |
|
1092 public String toString() { |
|
1093 |
|
1094 // Calculate the string value if it has not yet been done |
|
1095 // (ie 1st call to toString()) |
|
1096 // |
|
1097 if (myToString == null) |
|
1098 myToString = toString(this); |
|
1099 |
|
1100 // return always the same string representation for this |
|
1101 // instance (immutable) |
|
1102 // |
|
1103 return myToString; |
|
1104 } |
|
1105 |
|
1106 static String toString(OpenMBeanParameterInfo info) { |
|
1107 Descriptor d = (info instanceof DescriptorRead) ? |
|
1108 ((DescriptorRead) info).getDescriptor() : null; |
|
1109 return |
|
1110 info.getClass().getName() + |
|
1111 "(name=" + info.getName() + |
|
1112 ",openType=" + info.getOpenType() + |
|
1113 ",default=" + info.getDefaultValue() + |
|
1114 ",minValue=" + info.getMinValue() + |
|
1115 ",maxValue=" + info.getMaxValue() + |
|
1116 ",legalValues=" + info.getLegalValues() + |
|
1117 ((d == null) ? "" : ",descriptor=" + d) + |
|
1118 ")"; |
|
1119 } |
|
1120 } |