|
JavaTM Platform Standard Ed. 6 |
|||||||||
| 上一个类 下一个类 | 框架 无框架 | |||||||||
| 摘要: 嵌套 | 字段 | 构造方法 | 方法 | 详细信息: 字段 | 构造方法 | 方法 | |||||||||
public interface CompositeDataView
Java 类可以实现此接口,以指示如何通过 MXBean 框架将其转换为 CompositeData。
使用此类的典型方式是,除了已经在 MXBean 框架提供的 CompositeType 中声明的项外,还要向 CompositeData 添加额外的项。要做到这一点,必须创建另一个 CompositeType,其中包含所有相同的项以及额外的项。
例如,假定您有一个由名为 units 的 String 和一个 value(可以是 long 或 double)组成的 Measure 类。其形式可能如下所示:
public class Measure implements CompositeDataView {
private String units;
private Number value; // a Long or a Double
public Measure(String units, Number value) {
this.units = units;
this.value = value;
}
public static Measure from(CompositeData cd) {
return new Measure((String) cd.get("units"),
(Number) cd.get("value"));
}
public String getUnits() {
return units;
}
// Can't be called getValue(), because Number is not a valid type
// in an MXBean, so the implied "value" property would be rejected.
public Number _getValue() {
return value;
}
public CompositeData toCompositeData(CompositeType ct) {
try {
List<String> itemNames = new ArrayList<String>(ct.keySet());
List<String> itemDescriptions = new ArrayList<String>();
List<OpenType<?>> itemTypes = new ArrayList<OpenType<?>>();
for (String item :itemNames) {
itemDescriptions.add(ct.getDescription(item));
itemTypes.add(ct.getType(item));
}
itemNames.add("value");
itemDescriptions.add("long or double value of the measure");
itemTypes.add((value instanceof Long) ?SimpleType.LONG :
SimpleType.DOUBLE);
CompositeType xct =
new CompositeType(ct.getTypeName(),
ct.getDescription(),
itemNames.toArray(new String[0]),
itemDescriptions.toArray(new String[0]),
itemTypes.toArray(new OpenType<?>[0]));
CompositeData cd =
new CompositeDataSupport(xct,
new String[] {"units", "value"},
new Object[] {units, value});
assert ct.isValue(cd); // check we've done it right
return cd;
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}
将出现在用于此类型的属性或操作的 Descriptor 的 openType 字段中的 CompositeType 将只显示 units 项,但生成的实际 CompositeData 将既包含 units 又包含 value。
MXBean| 方法摘要 | |
|---|---|
CompositeData |
toCompositeData(CompositeType ct)
返回对应于此对象中的值的 CompositeData。 |
| 方法详细信息 |
|---|
CompositeData toCompositeData(CompositeType ct)
返回对应于此对象中的值的 CompositeData。返回的值通常应该是一个 CompositeDataSupport 实例,或通过 writeReplace 方法序列化为一个 CompositeDataSupport 的类。否则,接收对象的远程客户端可能无法重新构造它。
ct - 返回值的期望 CompositeType。如果返回值是 cd,则 cd.getCompositeType().equals(ct) 应该为 true。通常这是因为 cd 是一个通过将 ct 作为其 CompositeType 构造而成的 CompositeDataSupport。
CompositeData。
|
JavaTM Platform Standard Ed. 6 |
|||||||||
| 上一个类 下一个类 | 框架 无框架 | |||||||||
| 摘要: 嵌套 | 字段 | 构造方法 | 方法 | 详细信息: 字段 | 构造方法 | 方法 | |||||||||
版权所有 2007 Sun Microsystems, Inc. 保留所有权利。 请遵守许可证条款。另请参阅文档重新分发政策。