下面列出了怎么用javax.management.openmbean.CompositeType的API类实例代码及写法,或者点击链接到github查看源代码。
private MXBeanMapping
makeTabularMapping(Type objType, boolean sortedMap,
Type keyType, Type valueType,
MXBeanMappingFactory factory)
throws OpenDataException {
final String objTypeName = typeName(objType);
final MXBeanMapping keyMapping = factory.mappingForType(keyType, factory);
final MXBeanMapping valueMapping = factory.mappingForType(valueType, factory);
final OpenType<?> keyOpenType = keyMapping.getOpenType();
final OpenType<?> valueOpenType = valueMapping.getOpenType();
final CompositeType rowType =
new CompositeType(objTypeName,
objTypeName,
keyValueArray,
keyValueArray,
new OpenType<?>[] {keyOpenType, valueOpenType});
final TabularType tabularType =
new TabularType(objTypeName, objTypeName, rowType, keyArray);
return new TabularMapping(objType, sortedMap, tabularType,
keyMapping, valueMapping);
}
private MXBeanMapping
makeTabularMapping(Type objType, boolean sortedMap,
Type keyType, Type valueType,
MXBeanMappingFactory factory)
throws OpenDataException {
final String objTypeName = typeName(objType);
final MXBeanMapping keyMapping = factory.mappingForType(keyType, factory);
final MXBeanMapping valueMapping = factory.mappingForType(valueType, factory);
final OpenType<?> keyOpenType = keyMapping.getOpenType();
final OpenType<?> valueOpenType = valueMapping.getOpenType();
final CompositeType rowType =
new CompositeType(objTypeName,
objTypeName,
keyValueArray,
keyValueArray,
new OpenType<?>[] {keyOpenType, valueOpenType});
final TabularType tabularType =
new TabularType(objTypeName, objTypeName, rowType, keyArray);
return new TabularMapping(objType, sortedMap, tabularType,
keyMapping, valueMapping);
}
private MXBeanMapping
makeTabularMapping(Type objType, boolean sortedMap,
Type keyType, Type valueType,
MXBeanMappingFactory factory)
throws OpenDataException {
final String objTypeName = typeName(objType);
final MXBeanMapping keyMapping = factory.mappingForType(keyType, factory);
final MXBeanMapping valueMapping = factory.mappingForType(valueType, factory);
final OpenType<?> keyOpenType = keyMapping.getOpenType();
final OpenType<?> valueOpenType = valueMapping.getOpenType();
final CompositeType rowType =
new CompositeType(objTypeName,
objTypeName,
keyValueArray,
keyValueArray,
new OpenType<?>[] {keyOpenType, valueOpenType});
final TabularType tabularType =
new TabularType(objTypeName, objTypeName, rowType, keyArray);
return new TabularMapping(objType, sortedMap, tabularType,
keyMapping, valueMapping);
}
@Override
final Object toNonNullOpenValue(Object value)
throws OpenDataException {
CompositeType ct = (CompositeType) getOpenType();
if (value instanceof CompositeDataView)
return ((CompositeDataView) value).toCompositeData(ct);
if (value == null)
return null;
Object[] values = new Object[getters.length];
for (int i = 0; i < getters.length; i++) {
try {
Object got = MethodUtil.invoke(getters[i], value, (Object[]) null);
values[i] = getterMappings[i].toOpenValue(got);
} catch (Exception e) {
throw openDataException("Error calling getter for " +
itemNames[i] + ": " + e, e);
}
}
return new CompositeDataSupport(ct, itemNames, values);
}
private CompositeDataSupport makeCompositeData_2_2_12() throws OpenDataException {
Map<String, Object> fields = Maps.newTreeMap();
fields.put("currentRxBytes", 0L);
fields.put("currentTxBytes", 59875L);
fields.put("description", "Repair");
fields.put("planId", "636b1490-7d39-11e8-a943-973315b0e477");
fields.put("rxPercentage", 100.0);
fields.put("sessions", new CompositeData[] {makeSessions_2_2_12()});
fields.put("totalRxBytes", 0L);
fields.put("totalTxBytes", 44670289L);
fields.put("txPercentage", 0.0);
// C* 2.2.12 uses the same stream notification format like 2.1.10
CompositeType compositeType = streamStateType_2_1_20();
return new CompositeDataSupport(compositeType, fields);
}
@Override
final Object toNonNullOpenValue(Object value)
throws OpenDataException {
CompositeType ct = (CompositeType) getOpenType();
if (value instanceof CompositeDataView)
return ((CompositeDataView) value).toCompositeData(ct);
if (value == null)
return null;
Object[] values = new Object[getters.length];
for (int i = 0; i < getters.length; i++) {
try {
Object got = MethodUtil.invoke(getters[i], value, (Object[]) null);
values[i] = getterMappings[i].toOpenValue(got);
} catch (Exception e) {
throw openDataException("Error calling getter for " +
itemNames[i] + ": " + e, e);
}
}
return new CompositeDataSupport(ct, itemNames, values);
}
private static boolean isTypeMatched(OpenType<?> ot1, OpenType<?> ot2) {
if (ot1 instanceof CompositeType) {
if (! (ot2 instanceof CompositeType))
return false;
if (!isTypeMatched((CompositeType) ot1, (CompositeType) ot2))
return false;
} else if (ot1 instanceof TabularType) {
if (! (ot2 instanceof TabularType))
return false;
if (!isTypeMatched((TabularType) ot1, (TabularType) ot2))
return false;
} else if (ot1 instanceof ArrayType) {
if (! (ot2 instanceof ArrayType))
return false;
if (!isTypeMatched((ArrayType<?>) ot1, (ArrayType<?>) ot2)) {
return false;
}
} else if (!ot1.equals(ot2)) {
return false;
}
return true;
}
@Override
final Object toNonNullOpenValue(Object value)
throws OpenDataException {
CompositeType ct = (CompositeType) getOpenType();
if (value instanceof CompositeDataView)
return ((CompositeDataView) value).toCompositeData(ct);
if (value == null)
return null;
Object[] values = new Object[getters.length];
for (int i = 0; i < getters.length; i++) {
try {
Object got = MethodUtil.invoke(getters[i], value, (Object[]) null);
values[i] = getterMappings[i].toOpenValue(got);
} catch (Exception e) {
throw openDataException("Error calling getter for " +
itemNames[i] + ": " + e, e);
}
}
return new CompositeDataSupport(ct, itemNames, values);
}
CompositeMapping(Class<?> targetClass,
CompositeType compositeType,
String[] itemNames,
Method[] getters,
MXBeanMappingFactory factory) throws OpenDataException {
super(targetClass, compositeType);
assert(itemNames.length == getters.length);
this.itemNames = itemNames;
this.getters = getters;
this.getterMappings = new MXBeanMapping[getters.length];
for (int i = 0; i < getters.length; i++) {
Type retType = getters[i].getGenericReturnType();
getterMappings[i] = factory.mappingForType(retType, factory);
}
}
protected CompositeData getCompositeData() {
// CONTENTS OF THIS ARRAY MUST BE SYNCHRONIZED WITH
// gcNotifInfoItemNames!
final Object[] gcNotifInfoItemValues;
gcNotifInfoItemValues = new Object[] {
gcNotifInfo.getGcName(),
gcNotifInfo.getGcAction(),
gcNotifInfo.getGcCause(),
GcInfoCompositeData.toCompositeData(gcNotifInfo.getGcInfo())
};
CompositeType gict = getCompositeTypeByBuilder();
try {
return new CompositeDataSupport(gict,
gcNotifInfoItemNames,
gcNotifInfoItemValues);
} catch (OpenDataException e) {
// Should never reach here
throw new AssertionError(e);
}
}
public static TabularData tabularData(final String typeName, final String typeDescription, final String[] names, final Object[] values) {
if (names.length == 0) {
return null;
}
final OpenType<?>[] types = new OpenType<?>[names.length];
for (int i = 0; i < types.length; i++) {
types[i] = SimpleType.STRING;
}
try {
final CompositeType ct = new CompositeType(typeName, typeDescription, names, names, types);
final TabularType type = new TabularType(typeName, typeDescription, ct, names);
final TabularDataSupport data = new TabularDataSupport(type);
final CompositeData line = new CompositeDataSupport(ct, names, values);
data.put(line);
return data;
} catch (final OpenDataException e) {
return null;
}
}
protected CompositeData getCompositeData() {
// CONTENTS OF THIS ARRAY MUST BE SYNCHRONIZED WITH
// gcNotifInfoItemNames!
final Object[] gcNotifInfoItemValues;
gcNotifInfoItemValues = new Object[] {
gcNotifInfo.getGcName(),
gcNotifInfo.getGcAction(),
gcNotifInfo.getGcCause(),
GcInfoCompositeData.toCompositeData(gcNotifInfo.getGcInfo())
};
CompositeType gict = getCompositeTypeByBuilder();
try {
return new CompositeDataSupport(gict,
gcNotifInfoItemNames,
gcNotifInfoItemValues);
} catch (OpenDataException e) {
// Should never reach here
throw new AssertionError(e);
}
}
@Override
public Object fromModelNode(ModelNode node) {
if (node == null || !node.isDefined()) {
return null;
}
final OpenType<?> openType = getOpenType();
if (openType instanceof CompositeType) {
final CompositeType compositeType = (CompositeType)openType;
//Create a composite
final Map<String, Object> items = new HashMap<String, Object>();
for (String attrName : compositeType.keySet()) {
TypeConverter converter = getConverter(typeNode.get(attrName, TYPE), typeNode.get(attrName, VALUE_TYPE));
items.put(attrName, converter.fromModelNode(node.get(attrName)));
}
try {
return new CompositeDataSupport(compositeType, items);
} catch (OpenDataException e) {
throw new RuntimeException(e);
}
} else {
return node.toJSONString(false);
}
}
@Test
public void testJsonObjectInComplexValue() throws Exception {
ModelNode description = createDescription(ModelType.OBJECT);
ModelNode complexValueType = new ModelNode();
complexValueType.get("value", DESCRIPTION).set("A value");
complexValueType.get("value", TYPE).set(ModelType.OBJECT);
description.get(VALUE_TYPE).set(complexValueType);
TypeConverter converter = getConverter(description);
CompositeType type = assertCast(CompositeType.class, converter.getOpenType());
Set<String> keys = type.keySet();
Assert.assertEquals(1, keys.size());
Assert.assertEquals(SimpleType.STRING, type.getType("value"));
ModelNode node = new ModelNode();
node.get("value", "long").set(1L);
node.get("value", "string").set("test");
CompositeData data = assertCast(CompositeData.class, converter.fromModelNode(node));
Assert.assertEquals(type, data.getCompositeType());
Assert.assertEquals(ModelNode.fromJSONString(node.toJSONString(false)), converter.toModelNode(data));
}
private static boolean isTypeMatched(OpenType<?> ot1, OpenType<?> ot2) {
if (ot1 instanceof CompositeType) {
if (! (ot2 instanceof CompositeType))
return false;
if (!isTypeMatched((CompositeType) ot1, (CompositeType) ot2))
return false;
} else if (ot1 instanceof TabularType) {
if (! (ot2 instanceof TabularType))
return false;
if (!isTypeMatched((TabularType) ot1, (TabularType) ot2))
return false;
} else if (ot1 instanceof ArrayType) {
if (! (ot2 instanceof ArrayType))
return false;
if (!isTypeMatched((ArrayType<?>) ot1, (ArrayType<?>) ot2)) {
return false;
}
} else if (!ot1.equals(ot2)) {
return false;
}
return true;
}
CompositeMapping(Class<?> targetClass,
CompositeType compositeType,
String[] itemNames,
Method[] getters,
MXBeanMappingFactory factory) throws OpenDataException {
super(targetClass, compositeType);
assert(itemNames.length == getters.length);
this.itemNames = itemNames;
this.getters = getters;
this.getterMappings = new MXBeanMapping[getters.length];
for (int i = 0; i < getters.length; i++) {
Type retType = getters[i].getGenericReturnType();
getterMappings[i] = factory.mappingForType(retType, factory);
}
}
/**
* Compares two CompositeTypes and returns true if
* all items in type1 exist in type2 and their item types
* are the same.
* @param type1 the base composite type
* @param type2 the checked composite type
* @return {@code true} if all items in type1 exist in type2 and their item
* types are the same.
*/
protected static boolean isTypeMatched(CompositeType type1, CompositeType type2) {
if (type1 == type2) return true;
// We can't use CompositeType.isValue() since it returns false
// if the type name doesn't match.
Set<String> allItems = type1.keySet();
// Check all items in the type1 exist in type2
if (!type2.keySet().containsAll(allItems))
return false;
return allItems.stream().allMatch(
item -> isTypeMatched(type1.getType(item), type2.getType(item))
);
}
protected CompositeData getCompositeData() {
// CONTENTS OF THIS ARRAY MUST BE SYNCHRONIZED WITH
// gcNotifInfoItemNames!
final Object[] gcNotifInfoItemValues;
gcNotifInfoItemValues = new Object[] {
gcNotifInfo.getGcName(),
gcNotifInfo.getGcAction(),
gcNotifInfo.getGcCause(),
GcInfoCompositeData.toCompositeData(gcNotifInfo.getGcInfo())
};
CompositeType gict = getCompositeTypeByBuilder();
try {
return new CompositeDataSupport(gict,
gcNotifInfoItemNames,
gcNotifInfoItemValues);
} catch (OpenDataException e) {
// Should never reach here
throw new AssertionError(e);
}
}
@Test
public void testJsonObjectInComplexValue() throws Exception {
ModelNode description = createDescription(ModelType.OBJECT);
ModelNode complexValueType = new ModelNode();
complexValueType.get("value", DESCRIPTION).set("A value");
complexValueType.get("value", TYPE).set(ModelType.OBJECT);
description.get(VALUE_TYPE).set(complexValueType);
TypeConverter converter = getConverter(description);
CompositeType type = assertCast(CompositeType.class, converter.getOpenType());
Set<String> keys = type.keySet();
Assert.assertEquals(1, keys.size());
Assert.assertEquals(SimpleType.STRING, type.getType("value"));
ModelNode node = new ModelNode();
node.get("value", "long").set(1L);
node.get("value", "string").set("test");
CompositeData data = assertCast(CompositeData.class, converter.fromModelNode(node));
Assert.assertEquals(type, data.getCompositeType());
Assert.assertEquals(ModelNode.fromJSONString(node.toJSONString(false)), converter.toModelNode(data));
}
@Override
final Object toNonNullOpenValue(Object value)
throws OpenDataException {
CompositeType ct = (CompositeType) getOpenType();
if (value instanceof CompositeDataView)
return ((CompositeDataView) value).toCompositeData(ct);
if (value == null)
return null;
Object[] values = new Object[getters.length];
for (int i = 0; i < getters.length; i++) {
try {
Object got = MethodUtil.invoke(getters[i], value, (Object[]) null);
values[i] = getterMappings[i].toOpenValue(got);
} catch (Exception e) {
throw openDataException("Error calling getter for " +
itemNames[i] + ": " + e, e);
}
}
return new CompositeDataSupport(ct, itemNames, values);
}
private MXBeanMapping
makeTabularMapping(Type objType, boolean sortedMap,
Type keyType, Type valueType,
MXBeanMappingFactory factory)
throws OpenDataException {
final String objTypeName = typeName(objType);
final MXBeanMapping keyMapping = factory.mappingForType(keyType, factory);
final MXBeanMapping valueMapping = factory.mappingForType(valueType, factory);
final OpenType<?> keyOpenType = keyMapping.getOpenType();
final OpenType<?> valueOpenType = valueMapping.getOpenType();
final CompositeType rowType =
new CompositeType(objTypeName,
objTypeName,
keyValueArray,
keyValueArray,
new OpenType<?>[] {keyOpenType, valueOpenType});
final TabularType tabularType =
new TabularType(objTypeName, objTypeName, rowType, keyArray);
return new TabularMapping(objType, sortedMap, tabularType,
keyMapping, valueMapping);
}
protected CompositeData getCompositeData() {
// CONTENTS OF THIS ARRAY MUST BE SYNCHRONIZED WITH
// gcNotifInfoItemNames!
final Object[] gcNotifInfoItemValues;
gcNotifInfoItemValues = new Object[] {
gcNotifInfo.getGcName(),
gcNotifInfo.getGcAction(),
gcNotifInfo.getGcCause(),
GcInfoCompositeData.toCompositeData(gcNotifInfo.getGcInfo())
};
CompositeType gict = getCompositeTypeByBuilder();
try {
return new CompositeDataSupport(gict,
gcNotifInfoItemNames,
gcNotifInfoItemValues);
} catch (OpenDataException e) {
// Should never reach here
throw new AssertionError(e);
}
}
static CompositeType make(String className,
String description,
String[] itemNames,
String[] itemDescriptions,
OpenType[] itemTypes) {
try {
return new CompositeType(className,
description,
itemNames,
itemDescriptions,
itemTypes);
} catch (OpenDataException e) {
throw new Error(e);
}
}
/**
* Compares two CompositeTypes and returns true if
* all items in type1 exist in type2 and their item types
* are the same.
*/
protected static boolean isTypeMatched(CompositeType type1, CompositeType type2) {
if (type1 == type2) return true;
// We can't use CompositeType.isValue() since it returns false
// if the type name doesn't match.
Set<String> allItems = type1.keySet();
// Check all items in the type1 exist in type2
if (!type2.keySet().containsAll(allItems))
return false;
for (String item: allItems) {
OpenType<?> ot1 = type1.getType(item);
OpenType<?> ot2 = type2.getType(item);
if (ot1 instanceof CompositeType) {
if (! (ot2 instanceof CompositeType))
return false;
if (!isTypeMatched((CompositeType) ot1, (CompositeType) ot2))
return false;
} else if (ot1 instanceof TabularType) {
if (! (ot2 instanceof TabularType))
return false;
if (!isTypeMatched((TabularType) ot1, (TabularType) ot2))
return false;
} else if (!ot1.equals(ot2)) {
return false;
}
}
return true;
}
static TabularType make(String typeName, String description,
CompositeType rowType, String[] indexNames) {
try {
return new TabularType(typeName, description, rowType,
indexNames);
} catch (OpenDataException e) {
throw new Error(e);
}
}
private OpenType<?> assertCompositeType(CompositeType composite, String name, String type, String description, boolean validateType){
Assert.assertTrue(composite.keySet().contains(name));
if (validateType) {
Assert.assertEquals(type, composite.getType(name).getTypeName());
}
Assert.assertEquals(description, composite.getDescription(name));
return composite.getType(name);
}
private CompositeType makeFilesType_2_0_17() throws OpenDataException {
String typeName = "org.apache.cassandra.streaming.ProgressInfo";
String description = "ProgressInfo";
String[] itemNames = {
"currentBytes",
"direction",
"fileName",
"peer",
"planId",
"totalBytes"
};
String[] itemDescriptions = {
"currentBytes",
"direction",
"fileName",
"peer",
"planId",
"totalBytes"
};
OpenType[] itemTypes = {
SimpleType.LONG,
SimpleType.STRING,
SimpleType.STRING,
SimpleType.STRING,
SimpleType.STRING,
SimpleType.LONG,
};
return new CompositeType(typeName, description, itemNames, itemDescriptions, itemTypes);
}
@Test
public void testInvokeOperationExpressionsStandalone() throws Exception {
MBeanServerConnection connection = setupAndGetConnection(new MBeanInfoAdditionalInitialization(ProcessType.STANDALONE_SERVER, new TestExtension(true)));
ObjectName name = createObjectName(EXPR_DOMAIN + ":subsystem=test");
ModelControllerResourceDefinition.VoidOperationNoParams.INSTANCE.invoked = false;
Assert.assertNull(connection.invoke(name, ModelControllerResourceDefinition.VoidOperationNoParams.OPERATION_JMX_NAME, null, null));
Assert.assertTrue(ModelControllerResourceDefinition.VoidOperationNoParams.INSTANCE.invoked);
String result = assertCast(String.class, connection.invoke(
name,
ModelControllerResourceDefinition.IntOperationWithParams.OPERATION_JMX_NAME,
new Object[]{"${should.not.exist!!!!!:100}", new String[]{"${should.not.exist!!!!!:A}"}, Collections.singletonMap("test", "${should.not.exist!!!!!:3}"), "${should.not.exist!!!!!:5}", "${should.not.exist!!!!!:5}"},
new String[]{Long.class.getName(), String[].class.getName(), Map.class.getName(), Integer.class.getName(), Integer.class.getName()}));
Assert.assertEquals("A105", result);
Assert.assertTrue(ModelControllerResourceDefinition.IntOperationWithParams.INSTANCE_EXPRESSIONS.invoked);
MBeanInfo info = connection.getMBeanInfo(name);
CompositeType complexType = assertCast(CompositeType.class, findAttribute(info.getAttributes(), "complex").getOpenType());
CompositeData complexData = createComplexData(connection, complexType, "${should.not.exist!!!!!:5}", "${should.not.exist!!!!!:10}");
Assert.assertEquals(complexData, assertCast(CompositeData.class, connection.invoke(
name,
ModelControllerResourceDefinition.ComplexOperation.OPERATION_NAME,
new Object[]{complexData},
new String[]{CompositeData.class.getName()})));
}
public CompositeData getCompositeDataAttribute()
throws OpenDataException {
CompositeType ct = new CompositeType("CompositeDataAttribute",
"Composite Data Attribute",
itemNames,
itemDescriptions,
itemTypes);
Object itemValues[] = { ia, da, sa };
return new CompositeDataSupport(ct, itemNames, itemValues);
}
protected <T> TabularType createTabularType(Class<T> type, OpenType openType) throws OpenDataException {
String typeName = "java.util.Map<java.lang.String, " + type.getName() + ">";
String[] keyValue = new String[]{"key", "value"};
OpenType[] openTypes = new OpenType[]{SimpleType.STRING, openType};
CompositeType rowType = new CompositeType(typeName, typeName, keyValue, keyValue, openTypes);
return new TabularType(typeName, typeName, rowType, new String[]{"key"});
}