|
Java example source code file (OpenMBeanAttributeInfoSupport.java)
The OpenMBeanAttributeInfoSupport.java Java example source code/* * Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. Oracle designates this * particular file as subject to the "Classpath" exception as provided * by Oracle in the LICENSE file that accompanied this code. * * This code is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License * version 2 for more details (a copy is included in the LICENSE file that * accompanied this code). * * You should have received a copy of the GNU General Public License version * 2 along with this work; if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA * or visit www.oracle.com if you need additional information or have any * questions. */ package javax.management.openmbean; // java import // import java.lang.reflect.Array; import java.lang.reflect.Constructor; import java.lang.reflect.Method; import java.lang.reflect.Modifier; import java.util.Arrays; import java.util.Collection; import java.util.Collections; import java.util.HashMap; import java.util.HashSet; import java.util.Map; import java.util.Set; import javax.management.Descriptor; import javax.management.DescriptorRead; import javax.management.ImmutableDescriptor; import javax.management.MBeanAttributeInfo; import com.sun.jmx.remote.util.EnvHelp; import sun.reflect.misc.ConstructorUtil; import sun.reflect.misc.MethodUtil; import sun.reflect.misc.ReflectUtil; /** * Describes an attribute of an open MBean. * * * @since 1.5 */ public class OpenMBeanAttributeInfoSupport extends MBeanAttributeInfo implements OpenMBeanAttributeInfo { /* Serial version */ static final long serialVersionUID = -4867215622149721849L; /** * @serial The open mbean attribute's <i>open type */ private OpenType<?> openType; /** * @serial The open mbean attribute's default value */ private final Object defaultValue; /** * @serial The open mbean attribute's legal values. This {@link * Set} is unmodifiable */ private final Set<?> legalValues; // to be constructed unmodifiable /** * @serial The open mbean attribute's min value */ private final Comparable<?> minValue; /** * @serial The open mbean attribute's max value */ private final Comparable<?> maxValue; // As this instance is immutable, these two values need only // be calculated once. private transient Integer myHashCode = null; private transient String myToString = null; /** * Constructs an {@code OpenMBeanAttributeInfoSupport} instance, * which describes the attribute of an open MBean with the * specified {@code name}, {@code openType} and {@code * description}, and the specified read/write access properties. * * @param name cannot be a null or empty string. * * @param description cannot be a null or empty string. * * @param openType cannot be null. * * @param isReadable {@code true} if the attribute has a getter * exposed for management. * * @param isWritable {@code true} if the attribute has a setter * exposed for management. * * @param isIs {@code true} if the attribute's getter is of the * form <tt>isXXX. * * @throws IllegalArgumentException if {@code name} or {@code * description} are null or empty string, or {@code openType} is * null. */ public OpenMBeanAttributeInfoSupport(String name, String description, OpenType<?> openType, boolean isReadable, boolean isWritable, boolean isIs) { this(name, description, openType, isReadable, isWritable, isIs, (Descriptor) null); } /** * <p>Constructs an {@code OpenMBeanAttributeInfoSupport} instance, * which describes the attribute of an open MBean with the * specified {@code name}, {@code openType}, {@code * description}, read/write access properties, and {@code Descriptor}.</p> * * <p>The {@code descriptor} can contain entries that will define * the values returned by certain methods of this class, as * explained in the <a href="package-summary.html#constraints"> * package description</a>. * * @param name cannot be a null or empty string. * * @param description cannot be a null or empty string. * * @param openType cannot be null. * * @param isReadable {@code true} if the attribute has a getter * exposed for management. * * @param isWritable {@code true} if the attribute has a setter * exposed for management. * * @param isIs {@code true} if the attribute's getter is of the * form <tt>isXXX. * * @param descriptor The descriptor for the attribute. This may be null * which is equivalent to an empty descriptor. * * @throws IllegalArgumentException if {@code name} or {@code * description} are null or empty string, or {@code openType} is * null, or the descriptor entries are invalid as described in the * <a href="package-summary.html#constraints">package description. * * @since 1.6 */ public OpenMBeanAttributeInfoSupport(String name, String description, OpenType<?> openType, boolean isReadable, boolean isWritable, boolean isIs, Descriptor descriptor) { // Construct parent's state // super(name, (openType==null) ? null : openType.getClassName(), description, isReadable, isWritable, isIs, ImmutableDescriptor.union(descriptor, (openType==null)?null: openType.getDescriptor())); // Initialize this instance's specific state // this.openType = openType; descriptor = getDescriptor(); // replace null by empty this.defaultValue = valueFrom(descriptor, "defaultValue", openType); this.legalValues = valuesFrom(descriptor, "legalValues", openType); this.minValue = comparableValueFrom(descriptor, "minValue", openType); this.maxValue = comparableValueFrom(descriptor, "maxValue", openType); try { check(this); } catch (OpenDataException e) { throw new IllegalArgumentException(e.getMessage(), e); } } /** * Constructs an {@code OpenMBeanAttributeInfoSupport} instance, * which describes the attribute of an open MBean with the * specified {@code name}, {@code openType}, {@code description} * and {@code defaultValue}, and the specified read/write access * properties. * * @param name cannot be a null or empty string. * * @param description cannot be a null or empty string. * * @param openType cannot be null. * * @param isReadable {@code true} if the attribute has a getter * exposed for management. * * @param isWritable {@code true} if the attribute has a setter * exposed for management. * * @param isIs {@code true} if the attribute's getter is of the * form <tt>isXXX. * * @param defaultValue must be a valid value for the {@code * openType} specified for this attribute; default value not * supported for {@code ArrayType} and {@code TabularType}; can * be null, in which case it means that no default value is set. * * @param <T> allows the compiler to check that the {@code defaultValue}, * if non-null, has the correct Java type for the given {@code openType}. * * @throws IllegalArgumentException if {@code name} or {@code * description} are null or empty string, or {@code openType} is * null. * * @throws OpenDataException if {@code defaultValue} is not a * valid value for the specified {@code openType}, or {@code * defaultValue} is non null and {@code openType} is an {@code * ArrayType} or a {@code TabularType}. */ public <T> OpenMBeanAttributeInfoSupport(String name, String description, OpenType<T> openType, boolean isReadable, boolean isWritable, boolean isIs, T defaultValue) throws OpenDataException { this(name, description, openType, isReadable, isWritable, isIs, defaultValue, (T[]) null); } /** * <p>Constructs an {@code OpenMBeanAttributeInfoSupport} instance, * which describes the attribute of an open MBean with the * specified {@code name}, {@code openType}, {@code description}, * {@code defaultValue} and {@code legalValues}, and the specified * read/write access properties.</p> * * <p>The contents of {@code legalValues} are copied, so subsequent * modifications of the array referenced by {@code legalValues} * have no impact on this {@code OpenMBeanAttributeInfoSupport} * instance.</p> * * @param name cannot be a null or empty string. * * @param description cannot be a null or empty string. * * @param openType cannot be null. * * @param isReadable {@code true} if the attribute has a getter * exposed for management. * * @param isWritable {@code true} if the attribute has a setter * exposed for management. * * @param isIs {@code true} if the attribute's getter is of the * form <tt>isXXX. * * @param defaultValue must be a valid value * for the {@code * openType} specified for this attribute; default value not * supported for {@code ArrayType} and {@code TabularType}; can * be null, in which case it means that no default value is set. * * @param legalValues each contained value must be valid for the * {@code openType} specified for this attribute; legal values * not supported for {@code ArrayType} and {@code TabularType}; * can be null or empty. * * @param <T> allows the compiler to check that the {@code * defaultValue} and {@code legalValues}, if non-null, have the * correct Java type for the given {@code openType}. * * @throws IllegalArgumentException if {@code name} or {@code * description} are null or empty string, or {@code openType} is * null. * * @throws OpenDataException if {@code defaultValue} is not a * valid value for the specified {@code openType}, or one value in * {@code legalValues} is not valid for the specified {@code * openType}, or {@code defaultValue} is non null and {@code * openType} is an {@code ArrayType} or a {@code TabularType}, or * {@code legalValues} is non null and non empty and {@code * openType} is an {@code ArrayType} or a {@code TabularType}, or * {@code legalValues} is non null and non empty and {@code * defaultValue} is not contained in {@code legalValues}. */ public <T> OpenMBeanAttributeInfoSupport(String name, String description, OpenType<T> openType, boolean isReadable, boolean isWritable, boolean isIs, T defaultValue, T[] legalValues) throws OpenDataException { this(name, description, openType, isReadable, isWritable, isIs, defaultValue, legalValues, null, null); } /** * Constructs an {@code OpenMBeanAttributeInfoSupport} instance, * which describes the attribute of an open MBean, with the * specified {@code name}, {@code openType}, {@code description}, * {@code defaultValue}, {@code minValue} and {@code maxValue}. * * It is possible to specify minimal and maximal values only for * an open type whose values are {@code Comparable}. * * @param name cannot be a null or empty string. * * @param description cannot be a null or empty string. * * @param openType cannot be null. * * @param isReadable {@code true} if the attribute has a getter * exposed for management. * * @param isWritable {@code true} if the attribute has a setter * exposed for management. * * @param isIs {@code true} if the attribute's getter is of the * form <tt>isXXX. * * @param defaultValue must be a valid value for the {@code * openType} specified for this attribute; default value not * supported for {@code ArrayType} and {@code TabularType}; can be * null, in which case it means that no default value is set. * * @param minValue must be valid for the {@code openType} * specified for this attribute; can be null, in which case it * means that no minimal value is set. * * @param maxValue must be valid for the {@code openType} * specified for this attribute; can be null, in which case it * means that no maximal value is set. * * @param <T> allows the compiler to check that the {@code * defaultValue}, {@code minValue}, and {@code maxValue}, if * non-null, have the correct Java type for the given {@code * openType}. * * @throws IllegalArgumentException if {@code name} or {@code * description} are null or empty string, or {@code openType} is * null. * * @throws OpenDataException if {@code defaultValue}, {@code * minValue} or {@code maxValue} is not a valid value for the * specified {@code openType}, or {@code defaultValue} is non null * and {@code openType} is an {@code ArrayType} or a {@code * TabularType}, or both {@code minValue} and {@code maxValue} are * non-null and {@code minValue.compareTo(maxValue) > 0} is {@code * true}, or both {@code defaultValue} and {@code minValue} are * non-null and {@code minValue.compareTo(defaultValue) > 0} is * {@code true}, or both {@code defaultValue} and {@code maxValue} * are non-null and {@code defaultValue.compareTo(maxValue) > 0} * is {@code true}. */ public <T> OpenMBeanAttributeInfoSupport(String name, String description, OpenType<T> openType, boolean isReadable, boolean isWritable, boolean isIs, T defaultValue, Comparable<T> minValue, Comparable<T> maxValue) throws OpenDataException { this(name, description, openType, isReadable, isWritable, isIs, defaultValue, null, minValue, maxValue); } private <T> OpenMBeanAttributeInfoSupport(String name, String description, OpenType<T> openType, boolean isReadable, boolean isWritable, boolean isIs, T defaultValue, T[] legalValues, Comparable<T> minValue, Comparable<T> maxValue) throws OpenDataException { super(name, (openType==null) ? null : openType.getClassName(), description, isReadable, isWritable, isIs, makeDescriptor(openType, defaultValue, legalValues, minValue, maxValue)); this.openType = openType; Descriptor d = getDescriptor(); this.defaultValue = defaultValue; this.minValue = minValue; this.maxValue = maxValue; // We already converted the array into an unmodifiable Set // in the descriptor. this.legalValues = (Set<?>) d.getFieldValue("legalValues"); check(this); } /** * An object serialized in a version of the API before Descriptors were * added to this class will have an empty or null Descriptor. * For consistency with our * behavior in this version, we must replace the object with one * where the Descriptors reflect the same values of openType, defaultValue, * etc. **/ private Object readResolve() { if (getDescriptor().getFieldNames().length == 0) { OpenType<Object> xopenType = cast(openType); Set<Object> xlegalValues = cast(legalValues); Comparable<Object> xminValue = cast(minValue); Comparable<Object> xmaxValue = cast(maxValue); return new OpenMBeanAttributeInfoSupport( name, description, openType, isReadable(), isWritable(), isIs(), makeDescriptor(xopenType, defaultValue, xlegalValues, xminValue, xmaxValue)); } else return this; } static void check(OpenMBeanParameterInfo info) throws OpenDataException { OpenType<?> openType = info.getOpenType(); if (openType == null) throw new IllegalArgumentException("OpenType cannot be null"); if (info.getName() == null || info.getName().trim().equals("")) throw new IllegalArgumentException("Name cannot be null or empty"); if (info.getDescription() == null || info.getDescription().trim().equals("")) throw new IllegalArgumentException("Description cannot be null or empty"); // Check and initialize defaultValue // if (info.hasDefaultValue()) { // Default value not supported for ArrayType and TabularType // Cast to Object because "OpenType<T> instanceof" is illegal if (openType.isArray() || (Object)openType instanceof TabularType) { throw new OpenDataException("Default value not supported " + "for ArrayType and TabularType"); } // Check defaultValue's class if (!openType.isValue(info.getDefaultValue())) { final String msg = "Argument defaultValue's class [\"" + info.getDefaultValue().getClass().getName() + "\"] does not match the one defined in openType[\"" + openType.getClassName() +"\"]"; throw new OpenDataException(msg); } } // Check that we don't have both legalValues and min or max // if (info.hasLegalValues() && (info.hasMinValue() || info.hasMaxValue())) { throw new OpenDataException("cannot have both legalValue and " + "minValue or maxValue"); } // Check minValue and maxValue if (info.hasMinValue() && !openType.isValue(info.getMinValue())) { final String msg = "Type of minValue [" + info.getMinValue().getClass().getName() + "] does not match OpenType [" + openType.getClassName() + "]"; throw new OpenDataException(msg); } if (info.hasMaxValue() && !openType.isValue(info.getMaxValue())) { final String msg = "Type of maxValue [" + info.getMaxValue().getClass().getName() + "] does not match OpenType [" + openType.getClassName() + "]"; throw new OpenDataException(msg); } // Check that defaultValue is a legal value // if (info.hasDefaultValue()) { Object defaultValue = info.getDefaultValue(); if (info.hasLegalValues() && !info.getLegalValues().contains(defaultValue)) { throw new OpenDataException("defaultValue is not contained " + "in legalValues"); } // Check that minValue <= defaultValue <= maxValue // if (info.hasMinValue()) { if (compare(info.getMinValue(), defaultValue) > 0) { throw new OpenDataException("minValue cannot be greater " + "than defaultValue"); } } if (info.hasMaxValue()) { if (compare(info.getMaxValue(), defaultValue) < 0) { throw new OpenDataException("maxValue cannot be less " + "than defaultValue"); } } } // Check legalValues // if (info.hasLegalValues()) { // legalValues not supported for TabularType and arrays if ((Object)openType instanceof TabularType || openType.isArray()) { throw new OpenDataException("Legal values not supported " + "for TabularType and arrays"); } // Check legalValues are valid with openType for (Object v : info.getLegalValues()) { if (!openType.isValue(v)) { final String msg = "Element of legalValues [" + v + "] is not a valid value for the specified openType [" + openType.toString() +"]"; throw new OpenDataException(msg); } } } // Check that, if both specified, minValue <= maxValue // if (info.hasMinValue() && info.hasMaxValue()) { if (compare(info.getMinValue(), info.getMaxValue()) > 0) { throw new OpenDataException("minValue cannot be greater " + "than maxValue"); } } } @SuppressWarnings({"unchecked", "rawtypes"}) static int compare(Object x, Object y) { return ((Comparable) x).compareTo(y); } static <T> Descriptor makeDescriptor(OpenType Other Java examples (source code examples)Here is a short list of links related to this Java OpenMBeanAttributeInfoSupport.java source code file: |
... this post is sponsored by my books ... | |
#1 New Release! |
FP Best Seller |
Copyright 1998-2024 Alvin Alexander, alvinalexander.com
All Rights Reserved.
A percentage of advertising revenue from
pages under the /java/jwarehouse
URI on this website is
paid back to open source projects.