下面列出了怎么用javax.management.openmbean.ArrayType的API类实例代码及写法,或者点击链接到github查看源代码。
@Before
public void before() throws OpenDataException {
exceptionDetailsItemNames = new String[]{
"lastException",
"lastExceptionCausedObject",
"lastExceptionStackTrace",
"lastExceptionTimestamp",
"totalExceptions"
};
exceptionDetailsItemTypes = new OpenType<?>[]{
SimpleType.STRING,
SimpleType.STRING,
new ArrayType<>(1, SimpleType.STRING),
SimpleType.LONG,
SimpleType.INTEGER
};
}
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;
}
CollectionMapping(Type targetType,
ArrayType<?> openArrayType,
Class<?> openArrayClass,
MXBeanMapping elementMapping) {
super(targetType, openArrayType);
this.elementMapping = elementMapping;
/* Determine the concrete class to be used when converting
back to this Java type. We convert all Lists to ArrayList
and all Sets to TreeSet. (TreeSet because it is a SortedSet,
so works for both Set and SortedSet.) */
Type raw = ((ParameterizedType) targetType).getRawType();
Class<?> c = (Class<?>) raw;
final Class<?> collC;
if (c == List.class)
collC = ArrayList.class;
else if (c == Set.class)
collC = HashSet.class;
else if (c == SortedSet.class)
collC = TreeSet.class;
else { // can't happen
assert(false);
collC = null;
}
collectionClass = Util.cast(collC);
}
CollectionMapping(Type targetType,
ArrayType<?> openArrayType,
Class<?> openArrayClass,
MXBeanMapping elementMapping) {
super(targetType, openArrayType);
this.elementMapping = elementMapping;
/* Determine the concrete class to be used when converting
back to this Java type. We convert all Lists to ArrayList
and all Sets to TreeSet. (TreeSet because it is a SortedSet,
so works for both Set and SortedSet.) */
Type raw = ((ParameterizedType) targetType).getRawType();
Class<?> c = (Class<?>) raw;
final Class<?> collC;
if (c == List.class)
collC = ArrayList.class;
else if (c == Set.class)
collC = HashSet.class;
else if (c == SortedSet.class)
collC = TreeSet.class;
else { // can't happen
assert(false);
collC = null;
}
collectionClass = Util.cast(collC);
}
@Test
public void testSimpleTypeExpressionList() throws Exception {
ModelNode description = createDescription(ModelType.LIST, ModelType.INT);
description.get(EXPRESSIONS_ALLOWED).set(true);
TypeConverter converter = getConverter(description);
ArrayType<?> arrayType = assertCast(ArrayType.class, converter.getOpenType());
Assert.assertEquals(SimpleType.INTEGER, arrayType.getElementOpenType());
ModelNode node = new ModelNode();
node.add().set(new ValueExpression("${this.should.not.exist.!!!!!:1}"));
node.add().set(new ValueExpression("${this.should.not.exist.!!!!!:2}"));
Integer[] data = assertCast(Integer[].class, converter.fromModelNode(node));
Assert.assertEquals(Integer.valueOf(1), data[0]);
Assert.assertEquals(Integer.valueOf(2), data[1]);
}
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;
}
CollectionMapping(Type targetType,
ArrayType<?> openArrayType,
Class<?> openArrayClass,
MXBeanMapping elementMapping) {
super(targetType, openArrayType);
this.elementMapping = elementMapping;
/* Determine the concrete class to be used when converting
back to this Java type. We convert all Lists to ArrayList
and all Sets to TreeSet. (TreeSet because it is a SortedSet,
so works for both Set and SortedSet.) */
Type raw = ((ParameterizedType) targetType).getRawType();
Class<?> c = (Class<?>) raw;
final Class<?> collC;
if (c == List.class)
collC = ArrayList.class;
else if (c == Set.class)
collC = HashSet.class;
else if (c == SortedSet.class)
collC = TreeSet.class;
else { // can't happen
assert(false);
collC = null;
}
collectionClass = Util.cast(collC);
}
@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 ArrayType<?> getVersionedArrayType(ArrayType<?> type, String version)
throws OpenDataException
{
if (type.isPrimitiveArray()) {
return type;
}
OpenType<?> ot = getVersionedType(
type.getElementOpenType(),
version
);
if (ot instanceof SimpleType) {
return new ArrayType<>((SimpleType<?>)ot, type.isPrimitiveArray());
} else {
return new ArrayType<>(type.getDimension(), ot);
}
}
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;
}
CollectionMapping(Type targetType,
ArrayType<?> openArrayType,
Class<?> openArrayClass,
MXBeanMapping elementMapping) {
super(targetType, openArrayType);
this.elementMapping = elementMapping;
/* Determine the concrete class to be used when converting
back to this Java type. We convert all Lists to ArrayList
and all Sets to TreeSet. (TreeSet because it is a SortedSet,
so works for both Set and SortedSet.) */
Type raw = ((ParameterizedType) targetType).getRawType();
Class<?> c = (Class<?>) raw;
final Class<?> collC;
if (c == List.class)
collC = ArrayList.class;
else if (c == Set.class)
collC = HashSet.class;
else if (c == SortedSet.class)
collC = TreeSet.class;
else { // can't happen
assert(false);
collC = null;
}
collectionClass = Util.cast(collC);
}
static ArrayType make(int dims, OpenType baseType) {
try {
return new ArrayType(dims, baseType);
} catch (OpenDataException e) {
throw new Error(e);
}
}
static ArrayType make(SimpleType baseType, boolean primitiveArray) {
try {
return new ArrayType(baseType, primitiveArray);
} catch (OpenDataException e) {
throw new Error(e);
}
}
static ArrayType make(int dims, OpenType baseType) {
try {
return new ArrayType(dims, baseType);
} catch (OpenDataException e) {
throw new Error(e);
}
}
static ArrayType make(SimpleType baseType, boolean primitiveArray) {
try {
return new ArrayType(baseType, primitiveArray);
} catch (OpenDataException e) {
throw new Error(e);
}
}
private MXBeanMapping
makeArrayOrCollectionMapping(Type collectionType, Type elementType,
MXBeanMappingFactory factory)
throws OpenDataException {
final MXBeanMapping elementMapping = factory.mappingForType(elementType, factory);
final OpenType<?> elementOpenType = elementMapping.getOpenType();
final ArrayType<?> openType = ArrayType.getArrayType(elementOpenType);
final Class<?> elementOpenClass = elementMapping.getOpenClass();
final Class<?> openArrayClass;
final String openArrayClassName;
if (elementOpenClass.isArray())
openArrayClassName = "[" + elementOpenClass.getName();
else
openArrayClassName = "[L" + elementOpenClass.getName() + ";";
try {
openArrayClass = Class.forName(openArrayClassName);
} catch (ClassNotFoundException e) {
throw openDataException("Cannot obtain array class", e);
}
if (collectionType instanceof ParameterizedType) {
return new CollectionMapping(collectionType,
openType, openArrayClass,
elementMapping);
} else {
if (isIdentity(elementMapping)) {
return new IdentityMapping(collectionType,
openType);
} else {
return new ArrayMapping(collectionType,
openType,
openArrayClass,
elementMapping);
}
}
}
private static void testMXBean(MBeanServer mbs, ObjectName on)
throws Exception {
MBeanInfo mbi = mbs.getMBeanInfo(on);
MBeanAttributeInfo[] attrs = mbi.getAttributes();
int nattrs = attrs.length;
if (mbi.getAttributes().length != 1)
failure("wrong number of attributes: " + attrs);
else {
MBeanAttributeInfo mbai = attrs[0];
if (mbai.getName().equals("Ints")
&& mbai.isReadable() && !mbai.isWritable()
&& mbai.getDescriptor().getFieldValue("openType")
.equals(new ArrayType<int[]>(SimpleType.INTEGER, true))
&& attrs[0].getType().equals("[I"))
success("MBeanAttributeInfo");
else
failure("MBeanAttributeInfo: " + mbai);
}
int[] ints = (int[]) mbs.getAttribute(on, "Ints");
if (equal(ints, new int[] {1, 2, 3}, null))
success("getAttribute");
else
failure("getAttribute: " + Arrays.toString(ints));
ExplicitMXBean proxy =
JMX.newMXBeanProxy(mbs, on, ExplicitMXBean.class);
int[] pints = proxy.getInts();
if (equal(pints, new int[] {1, 2, 3}, null))
success("getAttribute through proxy");
else
failure("getAttribute through proxy: " + Arrays.toString(pints));
}
private static void printArrayType(ArrayType a) {
System.out.println("\tArrayType.getClassName() = " + a.getClassName());
System.out.println("\tArrayType.getDescription() = " + a.getDescription());
System.out.println("\tArrayType.getTypeName() = " + a.getTypeName());
System.out.println("\tArrayType.isArray() = " + a.isArray());
System.out.println("\tArrayType.isPrimitiveArray() = " + a.isPrimitiveArray());
System.out.println("\tArrayType.getDimension() = " + a.getDimension());
}
private static void printArrayType(ArrayType a) {
System.out.println("\tArrayType.getClassName() = " + a.getClassName());
System.out.println("\tArrayType.getDescription() = " + a.getDescription());
System.out.println("\tArrayType.getTypeName() = " + a.getTypeName());
System.out.println("\tArrayType.isArray() = " + a.isArray());
System.out.println("\tArrayType.isPrimitiveArray() = " + a.isPrimitiveArray());
System.out.println("\tArrayType.getDimension() = " + a.getDimension());
}
static ArrayType make(int dims, OpenType baseType) {
try {
return new ArrayType(dims, baseType);
} catch (OpenDataException e) {
throw new Error(e);
}
}
private MXBeanMapping
makeArrayOrCollectionMapping(Type collectionType, Type elementType,
MXBeanMappingFactory factory)
throws OpenDataException {
final MXBeanMapping elementMapping = factory.mappingForType(elementType, factory);
final OpenType<?> elementOpenType = elementMapping.getOpenType();
final ArrayType<?> openType = ArrayType.getArrayType(elementOpenType);
final Class<?> elementOpenClass = elementMapping.getOpenClass();
final Class<?> openArrayClass;
final String openArrayClassName;
if (elementOpenClass.isArray())
openArrayClassName = "[" + elementOpenClass.getName();
else
openArrayClassName = "[L" + elementOpenClass.getName() + ";";
try {
openArrayClass = Class.forName(openArrayClassName);
} catch (ClassNotFoundException e) {
throw openDataException("Cannot obtain array class", e);
}
if (collectionType instanceof ParameterizedType) {
return new CollectionMapping(collectionType,
openType, openArrayClass,
elementMapping);
} else {
if (isIdentity(elementMapping)) {
return new IdentityMapping(collectionType,
openType);
} else {
return new ArrayMapping(collectionType,
openType,
openArrayClass,
elementMapping);
}
}
}
@Override
public OpenType<?> getOpenType() {
try {
return ArrayType.getArrayType(getConverter(valueTypeNode, null).getOpenType());
} catch (OpenDataException e) {
throw new RuntimeException(e);
}
}
private CompositeType streamStateType_4_0_0() throws OpenDataException {
String typeName = "org.apache.cassandra.streaming.StreamState";
String description = "StreamState";
String[] itemNames = {
"currentRxBytes",
"currentTxBytes",
"description",
"planId",
"rxPercentage",
"sessions",
"totalRxBytes",
"totalTxBytes",
"txPercentage"
};
String[] itemDescriptions = {
"currentRxBytes",
"currentTxBytes",
"description",
"planId",
"rxPercentage",
"sessions",
"totalRxBytes",
"totalTxBytes",
"txPercentage"
};
OpenType[] itemTypes = {
SimpleType.LONG,
SimpleType.LONG,
SimpleType.STRING,
SimpleType.STRING,
SimpleType.DOUBLE,
ArrayType.getArrayType(makeSessionsType4_0()),
SimpleType.LONG,
SimpleType.LONG,
SimpleType.DOUBLE
};
return new CompositeType(typeName, description, itemNames, itemDescriptions, itemTypes);
}
static ArrayType make(SimpleType baseType, boolean primitiveArray) {
try {
return new ArrayType(baseType, primitiveArray);
} catch (OpenDataException e) {
throw new Error(e);
}
}
private static void testMXBean(MBeanServer mbs, ObjectName on)
throws Exception {
MBeanInfo mbi = mbs.getMBeanInfo(on);
MBeanAttributeInfo[] attrs = mbi.getAttributes();
int nattrs = attrs.length;
if (mbi.getAttributes().length != 1)
failure("wrong number of attributes: " + attrs);
else {
MBeanAttributeInfo mbai = attrs[0];
if (mbai.getName().equals("Ints")
&& mbai.isReadable() && !mbai.isWritable()
&& mbai.getDescriptor().getFieldValue("openType")
.equals(new ArrayType<int[]>(SimpleType.INTEGER, true))
&& attrs[0].getType().equals("[I"))
success("MBeanAttributeInfo");
else
failure("MBeanAttributeInfo: " + mbai);
}
int[] ints = (int[]) mbs.getAttribute(on, "Ints");
if (equal(ints, new int[] {1, 2, 3}, null))
success("getAttribute");
else
failure("getAttribute: " + Arrays.toString(ints));
ExplicitMXBean proxy =
JMX.newMXBeanProxy(mbs, on, ExplicitMXBean.class);
int[] pints = proxy.getInts();
if (equal(pints, new int[] {1, 2, 3}, null))
success("getAttribute through proxy");
else
failure("getAttribute through proxy: " + Arrays.toString(pints));
}
private MXBeanMapping
makeArrayOrCollectionMapping(Type collectionType, Type elementType,
MXBeanMappingFactory factory)
throws OpenDataException {
final MXBeanMapping elementMapping = factory.mappingForType(elementType, factory);
final OpenType<?> elementOpenType = elementMapping.getOpenType();
final ArrayType<?> openType = ArrayType.getArrayType(elementOpenType);
final Class<?> elementOpenClass = elementMapping.getOpenClass();
final Class<?> openArrayClass;
final String openArrayClassName;
if (elementOpenClass.isArray())
openArrayClassName = "[" + elementOpenClass.getName();
else
openArrayClassName = "[L" + elementOpenClass.getName() + ";";
try {
openArrayClass = Class.forName(openArrayClassName);
} catch (ClassNotFoundException e) {
throw openDataException("Cannot obtain array class", e);
}
if (collectionType instanceof ParameterizedType) {
return new CollectionMapping(collectionType,
openType, openArrayClass,
elementMapping);
} else {
if (isIdentity(elementMapping)) {
return new IdentityMapping(collectionType,
openType);
} else {
return new ArrayMapping(collectionType,
openType,
openArrayClass,
elementMapping);
}
}
}
private static void testMXBean(MBeanServer mbs, ObjectName on)
throws Exception {
MBeanInfo mbi = mbs.getMBeanInfo(on);
MBeanAttributeInfo[] attrs = mbi.getAttributes();
int nattrs = attrs.length;
if (mbi.getAttributes().length != 1)
failure("wrong number of attributes: " + attrs);
else {
MBeanAttributeInfo mbai = attrs[0];
if (mbai.getName().equals("Ints")
&& mbai.isReadable() && !mbai.isWritable()
&& mbai.getDescriptor().getFieldValue("openType")
.equals(new ArrayType<int[]>(SimpleType.INTEGER, true))
&& attrs[0].getType().equals("[I"))
success("MBeanAttributeInfo");
else
failure("MBeanAttributeInfo: " + mbai);
}
int[] ints = (int[]) mbs.getAttribute(on, "Ints");
if (equal(ints, new int[] {1, 2, 3}, null))
success("getAttribute");
else
failure("getAttribute: " + Arrays.toString(ints));
ExplicitMXBean proxy =
JMX.newMXBeanProxy(mbs, on, ExplicitMXBean.class);
int[] pints = proxy.getInts();
if (equal(pints, new int[] {1, 2, 3}, null))
success("getAttribute through proxy");
else
failure("getAttribute through proxy: " + Arrays.toString(pints));
}
private MXBeanMapping
makeArrayOrCollectionMapping(Type collectionType, Type elementType,
MXBeanMappingFactory factory)
throws OpenDataException {
final MXBeanMapping elementMapping = factory.mappingForType(elementType, factory);
final OpenType<?> elementOpenType = elementMapping.getOpenType();
final ArrayType<?> openType = ArrayType.getArrayType(elementOpenType);
final Class<?> elementOpenClass = elementMapping.getOpenClass();
final Class<?> openArrayClass;
final String openArrayClassName;
if (elementOpenClass.isArray())
openArrayClassName = "[" + elementOpenClass.getName();
else
openArrayClassName = "[L" + elementOpenClass.getName() + ";";
try {
openArrayClass = Class.forName(openArrayClassName);
} catch (ClassNotFoundException e) {
throw openDataException("Cannot obtain array class", e);
}
if (collectionType instanceof ParameterizedType) {
return new CollectionMapping(collectionType,
openType, openArrayClass,
elementMapping);
} else {
if (isIdentity(elementMapping)) {
return new IdentityMapping(collectionType,
openType);
} else {
return new ArrayMapping(collectionType,
openType,
openArrayClass,
elementMapping);
}
}
}
@Test
public void testBytesConverter() {
ModelNode description = createDescription(ModelType.BYTES);
TypeConverter converter = getConverter(description);
Assert.assertEquals(ArrayType.getPrimitiveArrayType(byte[].class), converter.getOpenType());
Assert.assertTrue(Arrays.equals(new byte[] {1,2,3}, assertCast(byte[].class, converter.fromModelNode(new ModelNode().set(new byte[] {1,2,3})))));
Assert.assertTrue(Arrays.equals(new byte[] {1,2,3}, converter.toModelNode(new byte[] {1,2,3}).asBytes()));
byte[][] bytes = assertCast(byte[][].class, converter.toArray(createList(new byte[] {1,2,3}, new byte[] {4,5,6})));
Assert.assertEquals(2, bytes.length);
Assert.assertTrue(Arrays.equals(new byte[] {1,2,3}, bytes[0]));
Assert.assertTrue(Arrays.equals(new byte[] {4,5,6}, bytes[1]));
}