下面列出了怎么用javax.management.openmbean.TabularType的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 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;
}
private static void compareTabularType(TabularType t1, TabularType t2) {
if (t1.equals(t2)) {
System.out.println("same tabular type");
return;
}
if (t1.getClassName().equals(t2.getClassName()))
System.out.println("same class name");
if (t1.getDescription().equals(t2.getDescription()))
System.out.println("same description");
else {
System.out.println("t1 description: " + t1.getDescription());
System.out.println("t2 description: " + t2.getDescription());
}
if (t1.getIndexNames().equals(t2.getIndexNames()))
System.out.println("same index names");
if (t1.getRowType().equals(t2.getRowType()))
System.out.println("same row type");
}
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;
}
@Test
public void testSimpleTypeObjectEmpty() throws Exception {
ModelNode description = createDescription(ModelType.OBJECT, ModelType.LONG);
TypeConverter converter = getConverter(description);
assertMapType(assertCast(TabularType.class, converter.getOpenType()), SimpleType.STRING, SimpleType.LONG);
ModelNode node = new ModelNode();
node.get("one").set("");
node.get("two").set("");
TabularData tabularData = assertCast(TabularData.class, converter.fromModelNode(node));
Assert.assertEquals(2, tabularData.size());
Assert.assertNull(tabularData.get(new Object[] {"one"}).get("value"));
Assert.assertNull(tabularData.get(new Object[] {"two"}).get("value"));
ModelNode expected = new ModelNode();
expected.get("one");
expected.get("two");
Assert.assertEquals(expected, converter.toModelNode(tabularData));
}
private static void compareTabularType(TabularType t1, TabularType t2) {
if (t1.equals(t2)) {
System.out.println("same tabular type");
return;
}
if (t1.getClassName().equals(t2.getClassName()))
System.out.println("same class name");
if (t1.getDescription().equals(t2.getDescription()))
System.out.println("same description");
else {
System.out.println("t1 description: " + t1.getDescription());
System.out.println("t2 description: " + t2.getDescription());
}
if (t1.getIndexNames().equals(t2.getIndexNames()))
System.out.println("same index names");
if (t1.getRowType().equals(t2.getRowType()))
System.out.println("same row type");
}
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 TabularData toTabularData(final String typeName, final String description, final Table table)
{
final OpenType<?>[] types = new OpenType<?>[table.getColumnNames().size()];
for (int i = 0; i < types.length; i++)
{
types[i] = SimpleType.STRING;
}
try
{
final String[] keys = table.getColumnNames().toArray(new String[table.getColumnNames().size()]);
final CompositeType ct = new CompositeType(
typeName, description, keys, keys, types);
final TabularType type = new TabularType(typeName, description, ct, keys);
final TabularDataSupport data = new TabularDataSupport(type);
for (final Collection<String> line : table.getLines())
{
data.put(new CompositeDataSupport(ct, keys, line.toArray(new Object[line.size()])));
}
return data;
}
catch (final OpenDataException e)
{
throw new IllegalArgumentException(e);
}
}
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 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;
}
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;
}
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 static void compareTabularType(TabularType t1, TabularType t2) {
if (t1.equals(t2)) {
System.out.println("same tabular type");
return;
}
if (t1.getClassName().equals(t2.getClassName()))
System.out.println("same class name");
if (t1.getDescription().equals(t2.getDescription()))
System.out.println("same description");
else {
System.out.println("t1 description: " + t1.getDescription());
System.out.println("t2 description: " + t2.getDescription());
}
if (t1.getIndexNames().equals(t2.getIndexNames()))
System.out.println("same index names");
if (t1.getRowType().equals(t2.getRowType()))
System.out.println("same row type");
}
@Test
public void testByteArrayObject() throws Exception {
ModelNode description = createDescription(ModelType.OBJECT, ModelType.BYTES);
TypeConverter converter = getConverter(description);
assertMapType(assertCast(TabularType.class, converter.getOpenType()), SimpleType.STRING, ArrayType.getPrimitiveArrayType(byte[].class));
ModelNode node = new ModelNode();
node.get("one").set(new byte[] {1,2});
node.get("two").set(new byte[] {3,4});
TabularData tabularData = assertCast(TabularData.class, converter.fromModelNode(node));
Assert.assertEquals(2, tabularData.size());
Assert.assertTrue(Arrays.equals(new byte[] {1,2}, (byte[])tabularData.get(new Object[] {"one"}).get("value")));
Assert.assertTrue(Arrays.equals(new byte[] {3,4}, (byte[])tabularData.get(new Object[] {"two"}).get("value")));
//Allow plain map as well? Yeah why not!
Map<String, byte[]> map = new HashMap<String, byte[]>();
map.put("one", new byte[] {1,2});
map.put("two", new byte[] {3,4});
Assert.assertEquals(node, converter.toModelNode(map));
}
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;
}
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);
}
@Test
public void testSimpleTypeObjectExpressions() throws Exception {
ModelNode description = createDescription(ModelType.OBJECT, ModelType.LONG);
description.get(EXPRESSIONS_ALLOWED).set(true);
TypeConverter converter = getConverter(description);
assertMapType(assertCast(TabularType.class, converter.getOpenType()), SimpleType.STRING, SimpleType.LONG);
ModelNode node = new ModelNode();
node.get("one").set(new ValueExpression("${this.should.not.exist.!!!!!:1}"));
node.get("two").set(new ValueExpression("${this.should.not.exist.!!!!!:2}"));
TabularData tabularData = assertCast(TabularData.class, converter.fromModelNode(node));
Assert.assertEquals(2, tabularData.size());
Assert.assertEquals((long) 1, tabularData.get(new Object[] {"one"}).get("value"));
Assert.assertEquals((long) 2, tabularData.get(new Object[] {"two"}).get("value"));
}
@Test
public void testSimpleTypeObjectExpressions() throws Exception {
ModelNode description = createDescription(ModelType.OBJECT, ModelType.LONG);
description.get(EXPRESSIONS_ALLOWED).set(true);
TypeConverter converter = getConverter(description);
assertMapType(assertCast(TabularType.class, converter.getOpenType()), SimpleType.STRING, SimpleType.STRING);
ModelNode node = new ModelNode();
node.get("one").set(new ValueExpression("${this.should.not.exist.!!!!!:1}"));
node.get("two").set(new ValueExpression("${this.should.not.exist.!!!!!:2}"));
TabularData tabularData = assertCast(TabularData.class, converter.fromModelNode(node));
Assert.assertEquals(2, tabularData.size());
Assert.assertEquals("${this.should.not.exist.!!!!!:1}", tabularData.get(new Object[] {"one"}).get("value"));
Assert.assertEquals("${this.should.not.exist.!!!!!:2}", tabularData.get(new Object[] {"two"}).get("value"));
Assert.assertEquals(node, converter.toModelNode(tabularData));
}
@Test
public void testSimpleTypeObject() throws Exception {
ModelNode description = createDescription(ModelType.OBJECT, ModelType.LONG);
TypeConverter converter = getConverter(description);
assertMapType(assertCast(TabularType.class, converter.getOpenType()), SimpleType.STRING, SimpleType.STRING);
ModelNode node = new ModelNode();
node.get("one").set(1L);
node.get("two").set(2L);
TabularData tabularData = assertCast(TabularData.class, converter.fromModelNode(node));
Assert.assertEquals(2, tabularData.size());
Assert.assertEquals("1", tabularData.get(new Object[] {"one"}).get("value"));
Assert.assertEquals("2", tabularData.get(new Object[] {"two"}).get("value"));
Assert.assertEquals(node, converter.toModelNode(tabularData));
//Allow plain map as well? Yeah why not!
Map<String, String> map = new HashMap<String, String>();
map.put("one", "1");
map.put("two", "2");
Assert.assertEquals(node, converter.toModelNode(map));
}
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;
}
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);
}
@Test
public void testSimpleTypeObjectEmpty() throws Exception {
ModelNode description = createDescription(ModelType.OBJECT, ModelType.LONG);
TypeConverter converter = getConverter(description);
assertMapType(assertCast(TabularType.class, converter.getOpenType()), SimpleType.STRING, SimpleType.STRING);
ModelNode node = new ModelNode();
node.get("one").set("");
node.get("two").set("");
TabularData tabularData = assertCast(TabularData.class, converter.fromModelNode(node));
Assert.assertEquals(2, tabularData.size());
Assert.assertNull(tabularData.get(new Object[] {"one"}).get("value"));
Assert.assertNull(tabularData.get(new Object[] {"two"}).get("value"));
ModelNode expected = new ModelNode();
expected.get("one");
expected.get("two");
Assert.assertEquals(expected, converter.toModelNode(tabularData));
}
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 static void compareTabularType(TabularType t1, TabularType t2) {
if (t1.equals(t2)) {
System.out.println("same tabular type");
return;
}
if (t1.getClassName().equals(t2.getClassName()))
System.out.println("same class name");
if (t1.getDescription().equals(t2.getDescription()))
System.out.println("same description");
else {
System.out.println("t1 description: " + t1.getDescription());
System.out.println("t2 description: " + t2.getDescription());
}
if (t1.getIndexNames().equals(t2.getIndexNames()))
System.out.println("same index names");
if (t1.getRowType().equals(t2.getRowType()))
System.out.println("same row type");
}
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);
}
TabularMapping(Type targetType,
boolean sortedMap,
TabularType tabularType,
MXBeanMapping keyConverter,
MXBeanMapping valueConverter) {
super(targetType, tabularType);
this.sortedMap = sortedMap;
this.keyMapping = keyConverter;
this.valueMapping = valueConverter;
}
private void assertMapType(OpenType<?> mapType, OpenType<?> keyType, OpenType<?> valueType) {
TabularType type = assertCast(TabularType.class, mapType);
Assert.assertEquals(1, type.getIndexNames().size());
Assert.assertEquals("key", type.getIndexNames().get(0));
Assert.assertEquals(2, type.getRowType().keySet().size());
Assert.assertTrue(type.getRowType().keySet().contains("key"));
Assert.assertTrue(type.getRowType().keySet().contains("value"));
Assert.assertEquals(keyType, type.getRowType().getType("key"));
Assert.assertEquals(valueType, type.getRowType().getType("value"));
}
private void assertMapType(OpenType<?> mapType, OpenType<?> keyType, OpenType<?> valueType) {
TabularType type = assertCast(TabularType.class, mapType);
Assert.assertEquals(1, type.getIndexNames().size());
Assert.assertEquals("key", type.getIndexNames().get(0));
Assert.assertEquals(2, type.getRowType().keySet().size());
Assert.assertTrue(type.getRowType().keySet().contains("key"));
Assert.assertTrue(type.getRowType().keySet().contains("value"));
Assert.assertEquals(keyType, type.getRowType().getType("key"));
Assert.assertEquals(valueType, type.getRowType().getType("value"));
}