public CompositeDataSupport getCompositeData(final CompositeType type) throws OpenDataException{ Object[] values = new Object[] { query, Integer.valueOf(nrOfInvocations), Long.valueOf(maxInvocationTime), Long.valueOf(maxInvocationDate), Long.valueOf(minInvocationTime), Long.valueOf(minInvocationDate), Long.valueOf(totalInvocationTime), Long.valueOf(failures), Integer.valueOf(prepareCount), Long.valueOf(prepareTime), Long.valueOf(lastInvocation) }; return new CompositeDataSupport(type,FIELD_NAMES,values); }
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); } }
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); } }
@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); }
@Override protected Map<String, Object> preprocessValueMap(final Map<?, ?> valueMap) { CompositeType openType = getOpenType(); Preconditions.checkArgument( valueMap.size() == 1 && valueMap.containsKey(JavaAttribute.DESCRIPTION_OF_VALUE_ATTRIBUTE_FOR_UNION), "Unexpected structure of incoming map, expecting one element under %s, but was %s", JavaAttribute.DESCRIPTION_OF_VALUE_ATTRIBUTE_FOR_UNION, valueMap); Map<String, Object> newMap = Maps.newHashMap(); for (String key : openType.keySet()) { if (openType.getDescription(key).equals(JavaAttribute.DESCRIPTION_OF_VALUE_ATTRIBUTE_FOR_UNION)) { newMap.put(key, valueMap.get(JavaAttribute.DESCRIPTION_OF_VALUE_ATTRIBUTE_FOR_UNION)); } else { newMap.put(key, null); } } return newMap; }
/** * Converts to open value */ 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 = getters[i].invoke(value, (Object[]) null); values[i] = getterConverters[i].toOpenValue(got); } catch (Exception e) { throw openDataException("Error calling getter for " + itemNames[i] + ": " + e, e); } } return new CompositeDataSupport(ct, itemNames, values); }
protected void addOpenAttribute(String csDescription, Class cls, String csMethodName, CompositeType compositeType) { Method methodGet = MethodFinder.getMethod(cls, "get"+csMethodName); boolean bCanGet = true; if(methodGet == null) bCanGet = false; Method methodSet = MethodFinder.getMethod(cls, "set"+csMethodName, CompositeData.class); boolean bCanSet = true; if(methodSet == null) bCanSet = false; OpenMBeanAttributeInfoSupport attrOpen = new OpenMBeanAttributeInfoSupport(csMethodName, csDescription, compositeType, bCanGet, bCanSet, false); OpenMBeanAttributeInfoWrapper attr = new OpenMBeanAttributeInfoWrapper(csMethodName, csDescription, attrOpen, methodGet, methodSet); if(m_arrOpenMBeanAttributeInfosWrapper == null) m_arrOpenMBeanAttributeInfosWrapper = new ArrayList<OpenMBeanAttributeInfoWrapper>(); m_arrOpenMBeanAttributeInfosWrapper.add(attr); }
/** * 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)) ); }
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 OpenType<?> getCompositeType(final Type baseType, final TypeDefinition<?> baseTypeDefinition) { final SimpleType<?> innerItemType = SimpleTypeResolver.getSimpleType(baseType); final String innerItemName = this.typeProviderWrapper.getJMXParamForBaseType(baseTypeDefinition); final String[] itemNames = new String[]{innerItemName}; final String description = getNullableDescription() == null ? getAttributeYangName() : getNullableDescription(); final OpenType<?>[] itemTypes = new OpenType[]{innerItemType}; try { return new CompositeType(getUpperCaseCammelCase(), description, itemNames, itemNames, itemTypes); } catch (final OpenDataException e) { throw new RuntimeException("Unable to create " + CompositeType.class + " with inner element of type " + itemTypes, e); } }
@Override public CompositeType getOpenType() { final String description = getNullableDescription() == null ? getAttributeYangName() : getNullableDescription(); final FunctionImpl functionImpl = new FunctionImpl(); final Map<String, AttributeIfc> jmxPropertiesToTypesMap = getJmxPropertiesToTypesMap(); final OpenType<?>[] itemTypes = Collections2.transform( jmxPropertiesToTypesMap.entrySet(), functionImpl).toArray( new OpenType<?>[] {}); final String[] itemNames = functionImpl.getItemNames(); try { // TODO add package name to create fully qualified name for this // type final CompositeType compositeType = new CompositeType( getUpperCaseCammelCase(), description, itemNames, itemNames, itemTypes); return compositeType; } catch (final OpenDataException e) { throw new RuntimeException("Unable to create CompositeType for " + this, e); } }
private void writeObject(JsonGenerator jg, Object value) throws IOException { if(value == null) { jg.writeNull(); } else { Class<?> c = value.getClass(); if (c.isArray()) { jg.writeStartArray(); int len = Array.getLength(value); for (int j = 0; j < len; j++) { Object item = Array.get(value, j); writeObject(jg, item); } jg.writeEndArray(); } else if(value instanceof Number) { Number n = (Number)value; jg.writeNumber(n.toString()); } else if(value instanceof Boolean) { Boolean b = (Boolean)value; jg.writeBoolean(b); } else if(value instanceof CompositeData) { CompositeData cds = (CompositeData)value; CompositeType comp = cds.getCompositeType(); Set<String> keys = comp.keySet(); jg.writeStartObject(); for(String key: keys) { writeAttribute(jg, key, cds.get(key)); } jg.writeEndObject(); } else if(value instanceof TabularData) { TabularData tds = (TabularData)value; jg.writeStartArray(); for(Object entry : tds.values()) { writeObject(jg, entry); } jg.writeEndArray(); } else { jg.writeString(value.toString()); } } }
/** * Will call all getter methods on payload that are defined in the given interfaces */ public static Map makeCallerChain ( Object payload, Class... ifaces ) throws OpenDataException, NoSuchMethodException, InstantiationException, IllegalAccessException, InvocationTargetException, Exception, ClassNotFoundException { CompositeType rt = new CompositeType("a", "b", new String[] { "a" }, new String[] { "a" }, new OpenType[] { javax.management.openmbean.SimpleType.INTEGER }); TabularType tt = new TabularType("a", "b", rt, new String[] { "a" }); TabularDataSupport t1 = new TabularDataSupport(tt); TabularDataSupport t2 = new TabularDataSupport(tt); // we need to make payload implement composite data // it's very likely that there are other proxy impls that could be used AdvisedSupport as = new AdvisedSupport(); as.setTarget(payload); InvocationHandler delegateInvocationHandler = (InvocationHandler) Reflections .getFirstCtor("org.springframework.aop.framework.JdkDynamicAopProxy").newInstance(as); InvocationHandler cdsInvocationHandler = Gadgets.createMemoizedInvocationHandler(Gadgets.createMap("getCompositeType", rt)); CompositeInvocationHandlerImpl invocationHandler = new CompositeInvocationHandlerImpl(); invocationHandler.addInvocationHandler(CompositeData.class, cdsInvocationHandler); invocationHandler.setDefaultHandler(delegateInvocationHandler); final CompositeData cdsProxy = Gadgets.createProxy(invocationHandler, CompositeData.class, ifaces); JSONObject jo = new JSONObject(); Map m = new HashMap(); m.put("t", cdsProxy); Reflections.setFieldValue(jo, "properties", m); Reflections.setFieldValue(jo, "properties", m); Reflections.setFieldValue(t1, "dataMap", jo); Reflections.setFieldValue(t2, "dataMap", jo); return Gadgets.makeMap(t1, t2); }
@Override protected AttributeResolvingStrategy<?, ? extends OpenType<?>> caseJavaUnionAttribute(final OpenType<?> openType) { Preconditions.checkState(openType instanceof CompositeType, "Unexpected open type, expected %s but was %s"); CompositeType compositeType = (CompositeType) openType; Map<String, AttributeResolvingStrategy<?, ? extends OpenType<?>>> innerMap = Maps.newHashMap(); Map<String, String> yangToJmxMapping = Maps.newHashMap(); fillMappingForComposite(compositeType, innerMap, yangToJmxMapping); return new UnionCompositeAttributeResolvingStrategy(innerMap, compositeType, yangToJmxMapping); }
CompositeAttributeResolvingStrategy( final Map<String, AttributeResolvingStrategy<?, ? extends OpenType<?>>> innerTypes, final CompositeType openType, final Map<String, String> yangToJavaAttrMapping) { super(openType); this.innerTypes = innerTypes; this.yangToJavaAttrMapping = yangToJavaAttrMapping; }
private static OpenTypeConverter makeTabularConverter(Type objType, boolean sortedMap, Type keyType, Type valueType) throws OpenDataException { final String objTypeName = objType.toString(); final OpenTypeConverter keyConverter = toConverter(keyType); final OpenTypeConverter valueConverter = toConverter(valueType); final OpenType keyOpenType = keyConverter.getOpenType(); final OpenType valueOpenType = valueConverter.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 TableConverter(objType, sortedMap, tabularType, keyConverter, valueConverter); }
CompositeConverter(Class targetClass, CompositeType compositeType, String[] itemNames, Method[] getters) throws OpenDataException { super(targetClass, compositeType, CompositeData.class); assert (itemNames.length == getters.length); this.itemNames = itemNames; this.getters = getters; this.getterConverters = new OpenTypeConverter[getters.length]; for (int i = 0; i < getters.length; i++) { Type retType = getters[i].getGenericReturnType(); getterConverters[i] = OpenTypeConverter.toConverter(retType); } }
public CompositeType getStateType() { CompositeTypeDesc compositeTypeDesc = new CompositeTypeDesc("LogCenterCompositeType", "LogCenterCompositeType Desc"); compositeTypeDesc.addItem("Enable", "EnableDesc", SimpleType.BOOLEAN); compositeTypeDesc.addItem("Level", "LevelDesc", SimpleType.STRING); return compositeTypeDesc.generateCompositeType(); }
public CompositeData getState() { CompositeType compositeType = getStateType(); CompositeDataDesc compositeDataDesc = new CompositeDataDesc(compositeType); compositeDataDesc.setItemValue("Enable", getEnable()); compositeDataDesc.setItemValue("Level", getLevel()); return compositeDataDesc.generateCompositeData(); }
public CompositeType generateCompositeType() { try { int nNbItems = m_arrItemDesc.size(); OpenType [] openTypes = new OpenType [nNbItems]; String [] itemTypeNames = new String [nNbItems]; String [] itemTypeDescriptions = new String [nNbItems]; for(int n=0; n<nNbItems; n++) { CompositeTypeDescItem itemDesc = m_arrItemDesc.get(n); openTypes[n] = itemDesc.m_openType; itemTypeNames[n] = itemDesc.m_csName; itemTypeDescriptions[n] = itemDesc.m_csDescription; } CompositeType compositeType = new CompositeType( m_csName, m_csDescription, itemTypeNames, itemTypeDescriptions, openTypes); return compositeType; } catch (OpenDataException e) { e.printStackTrace(); } return null; }
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); } }
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); } }
public CompositeData toCompositeData(CompositeType ct) { try { return new CompositeDataSupport(ct, new String[] {"whatsit"}, new String[] {"!" + whatsit}); } catch (Exception e) { throw new RuntimeException(e); } }
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); }
private void fillMappingForComposite(final CompositeType openType, final Map<String, AttributeResolvingStrategy<?, ? extends OpenType<?>>> innerMap, final Map<String, String> yangToJmxMapping) { for (String innerAttributeKey : openType.keySet()) { innerMap.put(innerAttributeKey, caseJavaAttribute(openType.getType(innerAttributeKey))); yangToJmxMapping.put(innerAttributeKey, innerAttributeKey); } }
public final T caseJavaAttribute(final OpenType<?> openType) { if (openType instanceof SimpleType<?>) { return caseJavaSimpleAttribute((SimpleType<?>) openType); } else if (openType instanceof ArrayType<?>) { return caseJavaArrayAttribute((ArrayType<?>) openType); } else if (openType instanceof CompositeType) { return caseJavaCompositeAttribute((CompositeType) openType); } throw new UnknownOpenTypeException("Unknown attribute open type " + openType); }
private OpenType<?> getVersionedType(OpenType<?> type, String version) throws OpenDataException { if (type instanceof ArrayType) { return getVersionedArrayType((ArrayType)type, version); } if (type instanceof CompositeType) { return getVersionedCompositeType((CompositeType)type, version); } if (type instanceof TabularType) { return getVersionedTabularType((TabularType)type, version); } return type; }
@Override protected AttributeResolvingStrategy<?, ? extends OpenType<?>> caseTOAttribute(final CompositeType openType) { Preconditions.checkState(getLastAttribute() instanceof TOAttribute); TOAttribute toAttribute = (TOAttribute) getLastAttribute(); Map<String, AttributeResolvingStrategy<?, ? extends OpenType<?>>> innerMap = Maps.newHashMap(); for (String innerName : openType.keySet()) { AttributeIfc innerAttributeIfc = toAttribute.getJmxPropertiesToTypesMap().get(innerName); innerMap.put(innerAttributeIfc.getAttributeYangName(), prepareStrategy(innerAttributeIfc)); } return new CompositeAttributeResolvingStrategy(innerMap, openType, createYangToJmxMapping(toAttribute)); }