1 /* 2 * @(#)CompositeDataView.java 1.5 06/03/29 3 * 4 * Copyright 2006 Sun Microsystems, Inc. All rights reserved. 5 * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms. 6 */ 7 8 package javax.management.openmbean; 9 10 /** 11 * <p>A Java class can implement this interface to indicate how it is 12 * to be converted into a {@code CompositeData} by the MXBean framework.</p> 13 * 14 * <p>A typical way to use this class is to add extra items to the 15 * {@code CompositeData} in addition to the ones that are declared in the 16 * {@code CompositeType} supplied by the MXBean framework. To do this, 17 * you must create another {@code CompositeType} that has all the same items, 18 * plus your extra items.</p> 19 * 20 * <p>For example, suppose you have a class {@code Measure} that consists of 21 * a String called {@code units} and a {@code value} that is either a 22 * {@code long} or a {@code double}. It might look like this:</p> 23 * 24 * <pre> 25 * public class Measure implements CompositeDataView { 26 * private String units; 27 * private Number value; // a Long or a Double 28 * 29 * public Measure(String units, Number value) { 30 * this.units = units; 31 * this.value = value; 32 * } 33 * 34 * public static Measure from(CompositeData cd) { 35 * return new Measure((String) cd.get("units"), 36 * (Number) cd.get("value")); 37 * } 38 * 39 * public String getUnits() { 40 * return units; 41 * } 42 * 43 * // Can't be called getValue(), because Number is not a valid type 44 * // in an MXBean, so the implied "value" property would be rejected. 45 * public Number _getValue() { 46 * return value; 47 * } 48 * 49 * public CompositeData toCompositeData(CompositeType ct) { 50 * try { 51 * {@code List<String> itemNames = new ArrayList<String>(ct.keySet());} 52 * {@code List<String> itemDescriptions = new ArrayList<String>();} 53 * {@code List<OpenType<?>> itemTypes = new ArrayList<OpenType<?>>();} 54 * for (String item : itemNames) { 55 * itemDescriptions.add(ct.getDescription(item)); 56 * itemTypes.add(ct.getType(item)); 57 * } 58 * itemNames.add("value"); 59 * itemDescriptions.add("long or double value of the measure"); 60 * itemTypes.add((value instanceof Long) ? SimpleType.LONG : 61 * SimpleType.DOUBLE); 62 * CompositeType xct = 63 * new CompositeType(ct.getTypeName(), 64 * ct.getDescription(), 65 * itemNames.toArray(new String[0]), 66 * itemDescriptions.toArray(new String[0]), 67 * itemTypes.toArray(new OpenType<?>[0])); 68 * CompositeData cd = 69 * new CompositeDataSupport(xct, 70 * new String[] {"units", "value"}, 71 * new Object[] {units, value}); 72 * assert ct.isValue(cd); // check we've done it right 73 * return cd; 74 * } catch (Exception e) { 75 * throw new RuntimeException(e); 76 * } 77 * } 78 * } 79 * </pre> 80 * 81 * <p>The {@code CompositeType} that will appear in the {@code openType} field 82 * of the {@link javax.management.Descriptor Descriptor} for an attribute or 83 * operation of this type will show only the {@code units} item, but the actual 84 * {@code CompositeData} that is generated will have both {@code units} and 85 * {@code value}.</p> 86 * 87 * @see javax.management.MXBean 88 * 89 * @since 1.6 90 */ 91 public interface CompositeDataView { 92 /** 93 * <p>Return a {@code CompositeData} corresponding to the values in 94 * this object. The returned value should usually be an instance of 95 * {@link CompositeDataSupport}, or a class that serializes as a 96 * {@code CompositeDataSupport} via a {@code writeReplace} method. 97 * Otherwise, a remote client that receives the object might not be 98 * able to reconstruct it. 99 * 100 * @param ct The expected {@code CompositeType} of the returned 101 * value. If the returned value is {@code cd}, then 102 * {@code cd.getCompositeType().equals(ct)} should be true. 103 * Typically this will be because {@code cd} is a 104 * {@link CompositeDataSupport} constructed with {@code ct} as its 105 * {@code CompositeType}. 106 * 107 * @return the {@code CompositeData}. 108 */ 109 public CompositeData toCompositeData(CompositeType ct); 110 } 111