Class TabularDataAccessor
- All Implemented Interfaces:
ObjectAccessor
- Since:
- Apr 19, 2009
- Author:
- roland
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionbooleanWhether this accessor can be used to set a value inside (directly or nested) a value ofgiven class.extractObject(ObjectToJsonConverter pConverter, Object pTabularData, Deque<String> pPathParts, boolean pJsonify) Convert aTabularDatato aJSONObject.Class<?>getType()Gets a class of supported objects.If this accessor can be used toset inner values, this method sets the value inside the passed outer object.Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface org.jolokia.service.serializer.json.ObjectAccessor
extractString, supportsStringConversion
-
Constructor Details
-
TabularDataAccessor
-
-
Method Details
-
getType
Description copied from interface:ObjectAccessorGets a class of supported objects.- Specified by:
getTypein interfaceObjectAccessor- Returns:
- supported type of the objects handled by this accessor
-
extractObject
public Object extractObject(ObjectToJsonConverter pConverter, Object pTabularData, Deque<String> pPathParts, boolean pJsonify) throws AttributeNotFoundException Convert a
TabularDatato aJSONObject. There are 3 possible representations of the returned JSON object depending onTabularType. These are described also inTabularDataConverter.#1. If the
TabularTypedescribesMaprepresentation defined inMXBeanspecification, the resulting map is simpleJSONObjectwith key-value pairs, where each pair represents singlerowofTabularData.
The requirements are:TabularType.getRowType()defineskeyandvalueitems of typeSimpleType.STRING(or convertible to String)TabularType.getIndexNames()should containkeyonly.
#2. If any of the
TabularType.getIndexNames()point toCompositeTypefields of type different thanSimpleType, the convertedTabularDatais said to use complex keys. This means that the complex values can't be used as keys of aJSONObject.
In this case, eachrowof theTabularDatais converted to singleJSONObjectwith keys being String item names ofCompositeDataand values being complex item values ofCompositeData.
In order to recreate such representation, index keys are stored underindexNamesfield of the returnedJSONObjectand rows are returned inJSONArrayundervaluesfield of the returnedJSONObject.#3. If all of the index items are
SimpleType, we can convert the index values to Strings and use them as keys of the returnedJSONObject. We can avoid then the extraindexNamesandvaluestop level fields and serialize theTabularDataas multi-layeredJSONObjectwhere leafs are map representations ofrows- with keys being item names ofCompositeData. The moreindex namesthe more nested the leafs are stored in the returnedJSONObject.Let's consider a
CompositeTypewith 3 items:idofSimpleType.INTEGERtypenameofSimpleType.STRINGtypeattributesofCompositeTypetype - a nested map.
CompositeTypecould look like this:
We can define two different{ "id": 123, "name": "some value", "attributes": { "a1": "v1", "a2": "v2" } }tabular typesbased on thisCompositeType:- One with index keys:
idandname - One with index keys:
idandattributes- this would be a validTabularTypewith complex keys.
Now, when the
TabularType.getIndexNames()are allsimple typeswe can get this nested map representation #3 (notice the duplication of data):{ "123": { "some value": { "id": 123, "name": "some value", "attributes": { "a1": "v1", "a2": "v2" } } } }When the
TabularType.getIndexNames()use also complex types, we'll get this representation #2:{ "indexNames": [ "id", "attributes" ], "values": [ { "id": 123, "name": "some value", "attributes": { "a1": "v1", "a2": "v2" } } ] }Accessing
TabularDatawith apathis only supported for index keys ofSimpleType, i.e. each index name must point to a String-convertible value. As many path elements must be provided as index names for the tabular type exists (i.e.pPathParts.size() >= pTabularData.getTabularType().getIndexNames().size())For
TabularDataof type #1, conforming toMXBeanspecification, a path access with the singlekeyvalue will return the content of thevalueitem. For non-MXBeantabular types, w need proper path specification.- Specified by:
extractObjectin interfaceObjectAccessor- Parameters:
pConverter- the global converter in order to be able do dispatch for serializing inner data typespTabularData- the value to convertpPathParts- extra arguments which contain e.g. a pathpJsonify- whether to convert to a JSON object/list or whether the plain object should be returned. The later is required for writing an inner value- Returns:
- the extracted object
- Throws:
AttributeNotFoundException
-
canSetValue
public boolean canSetValue()Description copied from interface:ObjectAccessorWhether this accessor can be used to set a value inside (directly or nested) a value ofgiven class.- Specified by:
canSetValuein interfaceObjectAccessor- Returns:
trueif this extractor can be used to set inner values.
-
setObjectValue
public Object setObjectValue(Converter<String> pConverter, Object pObject, String pAttribute, Object pValue) throws IllegalAccessException, InvocationTargetException Description copied from interface:ObjectAccessorIf this accessor can be used toset inner values, this method sets the value inside the passed outer object.- Specified by:
setObjectValuein interfaceObjectAccessor- Parameters:
pConverter-Converterused to convert the value being set to a class of the accessed attributepObject- object on which to set the valuepAttribute- attribute of the object to set. (For arrays or lists it should be an index.)pValue- the new value to set afterconversion- Returns:
- the old value of the changed attribute
- Throws:
IllegalAccessException- if the attribute to set to is not accessibleInvocationTargetException- reflection error
-