@Test public void testProperPropertyValueTypeExpressionConverter() throws Exception { ModelNode description = createDescription(ModelType.PROPERTY, ModelType.INT); TypeConverter converter = TypeConverters.createLegacyTypeConverters(true).getConverter(description); CompositeType type = assertCast(CompositeType.class, converter.getOpenType()); Set<String> keys = type.keySet(); Assert.assertEquals(2, keys.size()); assertCompositeType(type, "name", String.class.getName(), "The property name"); assertCompositeType(type, "value", Integer.class.getName(), "The property value"); CompositeData data = assertCast( CompositeData.class, converter.fromModelNode( new ModelNode().setExpression("one", "${this.should.not.exist.!!!!!:1}"))); Assert.assertEquals(type, data.getCompositeType()); Assert.assertEquals("one", data.get("name")); Assert.assertEquals(1, data.get("value")); data = new CompositeDataSupport( type, new String[] {"name", "value"}, new Object[] {"two", Integer.valueOf(2)}); ModelNode newNode = converter.toModelNode(data); Assert.assertEquals(ModelType.PROPERTY, newNode.getType()); Assert.assertEquals(new ModelNode().set("two", 2), newNode); assertToArray(converter, data); }
@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)); }
@Test public void testProperPropertyTypeConverter() throws Exception { ModelNode description = createDescription(ModelType.PROPERTY); TypeConverter converter = TypeConverters.createLegacyTypeConverters(true).getConverter(description); CompositeType type = assertCast(CompositeType.class, converter.getOpenType()); Set<String> keys = type.keySet(); Assert.assertEquals(2, keys.size()); assertCompositeType(type, "name", String.class.getName(), "The property name"); assertCompositeType(type, "value", String.class.getName(), "The property value"); CompositeData data = assertCast(CompositeData.class, converter.fromModelNode(new ModelNode().set("one", "uno"))); Assert.assertEquals(type, data.getCompositeType()); Assert.assertEquals("one", data.get("name")); Assert.assertEquals("uno", data.get("value")); data = new CompositeDataSupport(type, new String[] {"name", "value"}, new String[] {"two", "dos"}); ModelNode newNode = converter.toModelNode(data); Assert.assertEquals(ModelType.PROPERTY, newNode.getType()); Assert.assertEquals(new ModelNode().set("two", "dos"), newNode); assertToArray(converter, data); }
/** * Adds the attribute to the given list. * * @param mBeanAttributes The attributes * @param root The root attribute * @param value The value */ private void addAttributeRoots( List<AttributeNode> mBeanAttributes, AttributeNode root, Object value) { if (value instanceof CompositeData) { mBeanAttributes.add(root); CompositeData compositeData = (CompositeData) value; CompositeType type = compositeData.getCompositeType(); for (String key : type.keySet()) { AttributeNode attribute = new AttributeNode(key, root); root.addChild(attribute); addAttributeItems(attribute, compositeData.get(key)); } } else if (value instanceof TabularData) { mBeanAttributes.add(root); TabularData tabularData = (TabularData) value; for (Object keyList : tabularData.keySet()) { @SuppressWarnings("unchecked") Object[] keys = ((List<Object>) keyList).toArray(new Object[0]); AttributeNode attribute = new AttributeNode(String.valueOf(keys[0]), root); root.addChild(attribute); addAttributeItems(attribute, tabularData.get(keys)); } } else if (value instanceof Long || value instanceof Integer || value instanceof Double) { root.setValidLeaf(true); root.setRgb(getRGB(root.getQualifiedName())); mBeanAttributes.add(root); } }
/* */ public static void validateCompositeData(CompositeData paramCompositeData) /* */ { /* 122 */ if (paramCompositeData == null) { /* 123 */ throw new NullPointerException("Null CompositeData"); /* */ } /* */ /* 126 */ if (!isTypeMatched(vmOptionCompositeType, paramCompositeData.getCompositeType())) /* 127 */ throw new IllegalArgumentException("Unexpected composite type for VMOption"); /* */ }
/** * Validate if the input CompositeData has the expected CompositeType (i.e. contain all attributes * with expected names and types). */ public static void validateCompositeData(CompositeData cd) { if (cd == null) { throw new NullPointerException("Null CompositeData"); } if (!isTypeMatched(stackTraceElementCompositeType, cd.getCompositeType())) { throw new IllegalArgumentException("Unexpected composite type for StackTraceElement"); } }
/** * Recursively extracts simple numeric values from composite data objects. The map {@code values} * will be populated with a path to the value as the key and the simple object as the value. */ private void extractValues(String path, Map<String, Object> values, CompositeData obj) { for (String key : obj.getCompositeType().keySet()) { String newPath = (path == null) ? key : path + "." + key; Object value = obj.get(key); if (value instanceof CompositeData) { extractValues(newPath, values, (CompositeData) value); } else if (value != null) { values.put(newPath, value); } } }
private Object unmarshall(Object value) { if (value instanceof ObjectName) { ObjectName name = (ObjectName) value; return new MBean(_server, name); } else if (value instanceof ObjectName[]) { ObjectName[] names = (ObjectName[]) value; MBean[] mbeans = new MBean[names.length]; for (int i = 0; i < names.length; i++) mbeans[i] = new MBean(_server, names[i]); return mbeans; } else if (value instanceof CompositeData) { CompositeData compositeValue = (CompositeData) value; CompositeType type = compositeValue.getCompositeType(); if (type != null) { String typeName = type.getTypeName(); try { ClassLoader loader = Thread.currentThread().getContextClassLoader(); Class typeClass = Class.forName(typeName, false, loader); Method from = typeClass.getMethod("from", new Class[] {CompositeData.class}); if (from != null) return from.invoke(null, compositeValue); } catch (Exception e) { log.log(Level.FINER, e.toString(), e); } } return new CompositeDataBean(compositeValue); } else return value; }
public String toString() { return _data.getCompositeType().toString(); }
public Set getKeys() { return _data.getCompositeType().keySet(); }
public Builder add(String name, CompositeData data) throws OpenDataException { if (data != null) return add(name, data.getCompositeType(), data); return add(name, SimpleType.VOID, null); }
@Test public void testComplexValue() throws Exception { ModelNode description = createDescription(ModelType.OBJECT); ModelNode complexValueType = new ModelNode(); complexValueType.get("int-value", DESCRIPTION).set("An int value"); complexValueType.get("int-value", TYPE).set(ModelType.INT); complexValueType.get("bigint-value", DESCRIPTION).set("A biginteger value"); complexValueType.get("bigint-value", TYPE).set(ModelType.BIG_INTEGER); complexValueType.get("bigdecimal-value", DESCRIPTION).set("A bigdecimal value"); complexValueType.get("bigdecimal-value", TYPE).set(ModelType.BIG_DECIMAL); complexValueType.get("boolean-value", DESCRIPTION).set("A boolean value"); complexValueType.get("boolean-value", TYPE).set(ModelType.BOOLEAN); complexValueType.get("bytes-value", DESCRIPTION).set("A bytes value"); complexValueType.get("bytes-value", TYPE).set(ModelType.BYTES); complexValueType.get("double-value", DESCRIPTION).set("A double value"); complexValueType.get("double-value", TYPE).set(ModelType.DOUBLE); complexValueType.get("string-value", DESCRIPTION).set("A string value"); complexValueType.get("string-value", TYPE).set(ModelType.STRING); complexValueType.get("long-value", DESCRIPTION).set("A long value"); complexValueType.get("long-value", TYPE).set(ModelType.LONG); complexValueType.get("type-value", DESCRIPTION).set("A type value"); complexValueType.get("type-value", TYPE).set(ModelType.TYPE); complexValueType.get("list-int-value", DESCRIPTION).set("An int list value"); complexValueType.get("list-int-value", TYPE).set(ModelType.LIST); complexValueType.get("list-int-value", VALUE_TYPE).set(ModelType.INT); complexValueType.get("map-int-value", DESCRIPTION).set("An int map value"); complexValueType.get("map-int-value", TYPE).set(ModelType.OBJECT); complexValueType.get("map-int-value", VALUE_TYPE).set(ModelType.INT); description.get(VALUE_TYPE).set(complexValueType); TypeConverter converter = getConverter(description); CompositeType type = assertCast(CompositeType.class, converter.getOpenType()); Set<String> keys = type.keySet(); Assert.assertEquals(11, keys.size()); assertCompositeType(type, "int-value", Integer.class.getName(), "An int value"); assertCompositeType(type, "bigint-value", BigInteger.class.getName(), "A biginteger value"); assertCompositeType(type, "bigdecimal-value", BigDecimal.class.getName(), "A bigdecimal value"); assertCompositeType(type, "boolean-value", Boolean.class.getName(), "A boolean value"); assertCompositeType(type, "bytes-value", byte[].class.getName(), "A bytes value"); assertCompositeType(type, "double-value", Double.class.getName(), "A double value"); assertCompositeType(type, "string-value", String.class.getName(), "A string value"); assertCompositeType(type, "long-value", Long.class.getName(), "A long value"); assertCompositeType(type, "type-value", String.class.getName(), "A type value"); assertCompositeType(type, "list-int-value", Integer[].class.getName(), "An int list value"); assertMapType( assertCast( TabularType.class, assertCompositeType( type, "map-int-value", TabularType.class.getName(), "An int map value", false)), SimpleType.STRING, SimpleType.INTEGER); ModelNode node = new ModelNode(); node.get("int-value").set(1); node.get("bigint-value").set(BigInteger.valueOf(2)); node.get("bigdecimal-value").set(BigDecimal.valueOf(3)); node.get("boolean-value").set(Boolean.TRUE); node.get("bytes-value").set(new byte[] {4, 5}); node.get("double-value").set(Double.valueOf(6)); node.get("string-value").set("Seven"); node.get("long-value").set(Long.valueOf(8)); node.get("type-value").set(ModelType.INT); node.get("list-int-value").add(9); node.get("list-int-value").add(10); node.get("map-int-value", "one").set(11); node.get("map-int-value", "two").set(12); CompositeData data = assertCast(CompositeData.class, converter.fromModelNode(node)); Assert.assertEquals(type, data.getCompositeType()); Assert.assertEquals(node, converter.toModelNode(data)); // Another test testing missing data in fromModelNode(); node = new ModelNode(); node.get("int-value").set(1); data = assertCast(CompositeData.class, converter.fromModelNode(node)); Assert.assertEquals(node, converter.toModelNode(data)); // And another test testing missing data in fromModelNode(); node = new ModelNode(); node.get("boolean-value").set(true); data = assertCast(CompositeData.class, converter.fromModelNode(node)); Assert.assertEquals(node, converter.toModelNode(data)); }
/** * create result as property with name from property prefix When result is an array and * isSeparateArrayResults is true, resultproperty used as prefix (<code> * resultproperty.0-array.length</code> and store the result array length at <code> * resultproperty.length</code>. Other option is that you delimit your result with a delimiter * (java.util.StringTokenizer is used). * * @param propertyPrefix * @param result */ protected void createProperty(String propertyPrefix, Object result) { if (propertyPrefix == null) propertyPrefix = ""; if (result instanceof CompositeDataSupport) { CompositeDataSupport data = (CompositeDataSupport) result; CompositeType compositeType = data.getCompositeType(); Set<String> keys = compositeType.keySet(); for (Iterator<String> iter = keys.iterator(); iter.hasNext(); ) { String key = iter.next(); Object value = data.get(key); OpenType<?> type = compositeType.getType(key); if (type instanceof SimpleType<?>) { setProperty(propertyPrefix + "." + key, value); } else { createProperty(propertyPrefix + "." + key, value); } } } else if (result instanceof TabularDataSupport) { TabularDataSupport data = (TabularDataSupport) result; for (Iterator<Object> iter = data.keySet().iterator(); iter.hasNext(); ) { Object key = iter.next(); for (Iterator<?> iter1 = ((List<?>) key).iterator(); iter1.hasNext(); ) { Object key1 = iter1.next(); CompositeData valuedata = data.get(new Object[] {key1}); Object value = valuedata.get("value"); OpenType<?> type = valuedata.getCompositeType().getType("value"); if (type instanceof SimpleType<?>) { setProperty(propertyPrefix + "." + key1, value); } else { createProperty(propertyPrefix + "." + key1, value); } } } } else if (result.getClass().isArray()) { if (isSeparatearrayresults()) { int size = 0; for (int i = 0; i < Array.getLength(result); i++) { if (setProperty(propertyPrefix + "." + size, Array.get(result, i))) { size++; } } if (size > 0) { setProperty(propertyPrefix + ".Length", Integer.toString(size)); } } } else { String delim = getDelimiter(); if (delim != null) { StringTokenizer tokenizer = new StringTokenizer(result.toString(), delim); int size = 0; for (; tokenizer.hasMoreTokens(); ) { String token = tokenizer.nextToken(); if (setProperty(propertyPrefix + "." + size, token)) { size++; } } if (size > 0) setProperty(propertyPrefix + ".Length", Integer.toString(size)); } else { setProperty(propertyPrefix, result.toString()); } } }