2362N/A * Copyright (c) 2005, 2006, Oracle and/or its affiliates. All rights reserved. 0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 0N/A * This code is free software; you can redistribute it and/or modify it 0N/A * under the terms of the GNU General Public License version 2 only, as 2362N/A * published by the Free Software Foundation. Oracle designates this 0N/A * particular file as subject to the "Classpath" exception as provided 2362N/A * by Oracle in the LICENSE file that accompanied this code. 0N/A * This code is distributed in the hope that it will be useful, but WITHOUT 0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 0N/A * version 2 for more details (a copy is included in the LICENSE file that 0N/A * accompanied this code). 0N/A * You should have received a copy of the GNU General Public License version 0N/A * 2 along with this work; if not, write to the Free Software Foundation, 0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 2362N/A * or visit www.oracle.com if you need additional information or have any 0N/A * <p>A Java class can implement this interface to indicate how it is 0N/A * to be converted into a {@code CompositeData} by the MXBean framework.</p> 0N/A * <p>A typical way to use this class is to add extra items to the 0N/A * {@code CompositeData} in addition to the ones that are declared in the 0N/A * {@code CompositeType} supplied by the MXBean framework. To do this, 0N/A * you must create another {@code CompositeType} that has all the same items, 0N/A * plus your extra items.</p> 0N/A * <p>For example, suppose you have a class {@code Measure} that consists of 0N/A * a String called {@code units} and a {@code value} that is either a 0N/A * {@code long} or a {@code double}. It might look like this:</p> 0N/A * public class Measure implements CompositeDataView { 0N/A * private String units; 0N/A * private Number value; // a Long or a Double 0N/A * public Measure(String units, Number value) { 0N/A * this.units = units; 0N/A * this.value = value; 0N/A * public static Measure from(CompositeData cd) { 0N/A * return new Measure((String) cd.get("units"), 0N/A * (Number) cd.get("value")); 0N/A * public String getUnits() { 0N/A * // Can't be called getValue(), because Number is not a valid type 0N/A * // in an MXBean, so the implied "value" property would be rejected. 0N/A * public Number _getValue() { 0N/A * public CompositeData toCompositeData(CompositeType ct) { 0N/A * {@code List<String> itemNames = new ArrayList<String>(ct.keySet());} 0N/A * {@code List<String> itemDescriptions = new ArrayList<String>();} 0N/A * {@code List<OpenType<?>> itemTypes = new ArrayList<OpenType<?>>();} 0N/A * for (String item : itemNames) { 0N/A * itemDescriptions.add(ct.getDescription(item)); 0N/A * itemTypes.add(ct.getType(item)); 0N/A * itemNames.add("value"); 0N/A * itemDescriptions.add("long or double value of the measure"); 0N/A * itemTypes.add((value instanceof Long) ? SimpleType.LONG : 0N/A * SimpleType.DOUBLE); 0N/A * CompositeType xct = 0N/A * new CompositeType(ct.getTypeName(), 0N/A * ct.getDescription(), 0N/A * itemNames.toArray(new String[0]), 0N/A * itemDescriptions.toArray(new String[0]), 0N/A * itemTypes.toArray(new OpenType<?>[0])); 0N/A * CompositeData cd = 0N/A * new CompositeDataSupport(xct, 0N/A * new String[] {"units", "value"}, 0N/A * new Object[] {units, value}); 0N/A * assert ct.isValue(cd); // check we've done it right 0N/A * } catch (Exception e) { 0N/A * throw new RuntimeException(e); 0N/A * <p>The {@code CompositeType} that will appear in the {@code openType} field 0N/A * of the {@link javax.management.Descriptor Descriptor} for an attribute or 0N/A * operation of this type will show only the {@code units} item, but the actual 0N/A * {@code CompositeData} that is generated will have both {@code units} and 0N/A * {@code value}.</p> 0N/A * @see javax.management.MXBean 0N/A * <p>Return a {@code CompositeData} corresponding to the values in 0N/A * this object. The returned value should usually be an instance of 0N/A * {@link CompositeDataSupport}, or a class that serializes as a 0N/A * {@code CompositeDataSupport} via a {@code writeReplace} method. 0N/A * Otherwise, a remote client that receives the object might not be 0N/A * able to reconstruct it. 0N/A * @param ct The expected {@code CompositeType} of the returned 0N/A * value. If the returned value is {@code cd}, then 0N/A * {@code cd.getCompositeType().equals(ct)} should be true. 0N/A * Typically this will be because {@code cd} is a 0N/A * {@link CompositeDataSupport} constructed with {@code ct} as its 0N/A * {@code CompositeType}. 0N/A * @return the {@code CompositeData}.